Skip to content

Commit 28e1ba4

Browse files
CopilotGeekTrainer
andcommitted
Replace mock API with real Flask server using seeded test database for e2e tests
Co-authored-by: GeekTrainer <6109729+GeekTrainer@users.noreply.github.com>
1 parent 9a5fed4 commit 28e1ba4

10 files changed

Lines changed: 145 additions & 155 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ htmlcov/
4141

4242
# playwright
4343
client/test-results/
44-
client/playwright-report/
44+
client/playwright-report/
45+
46+
# e2e test database
47+
server/e2e_test_dogshelter.db

client/e2e-tests/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Make sure you have installed dependencies:
1818
npm install
1919
```
2020

21+
You also need Python 3 with Flask dependencies installed:
22+
```bash
23+
pip install -r ../server/requirements.txt
24+
```
25+
2126
### Running Tests
2227

2328
```bash
@@ -34,33 +39,44 @@ npm run test:e2e:headed
3439
npm run test:e2e:debug
3540
```
3641

42+
## Test Architecture
43+
44+
Tests run against the real Flask server with a separate test database seeded with deterministic data. When Playwright starts, it:
45+
46+
1. Seeds a test database (`server/e2e_test_dogshelter.db`) with known dogs and breeds
47+
2. Starts the Flask server using the test database
48+
3. Starts the Astro dev server pointing at the Flask server
49+
4. Runs all e2e tests against the live application
50+
51+
The test data is defined in `server/utils/seed_test_database.py`.
52+
3753
## Test Coverage
3854

3955
The tests cover the following core functionality:
4056

4157
### Homepage Tests
4258
- Page loads with correct title and content
4359
- Dog list displays properly
44-
- Loading states work correctly
45-
- Error handling for API failures
4660

4761
### About Page Tests
4862
- About page content displays correctly
4963
- Navigation back to homepage works
5064

5165
### Dog Details Tests
5266
- Navigation from homepage to dog details
67+
- Full dog details display correctly
5368
- Navigation back from dog details to homepage
5469
- Handling of invalid dog IDs
5570

5671
### API Integration Tests
57-
- Successful API responses
58-
- Empty dog list handling
59-
- Network error handling
72+
- Dogs render correctly on the homepage
73+
- Dog details render correctly
74+
- 404 handling for non-existent dogs
75+
- Navigation from card to detail page
6076

6177
## Configuration
6278

63-
Tests are configured in `../playwright.config.ts` and automatically start the application servers using the existing `scripts/start-app.sh` script before running tests.
79+
Tests are configured in `../playwright.config.ts` and automatically start the Flask and Astro servers before running tests.
6480

6581
The tests run against:
6682
- Client (Astro): http://localhost:4321

client/e2e-tests/api-integration.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test, expect } from '@playwright/test';
22

33
test.describe('API Integration', () => {
4-
test('should render dogs from the mock API on the homepage', async ({ page }) => {
4+
test('should render dogs from the API on the homepage', async ({ page }) => {
55
await page.goto('/');
66

77
const dogCards = page.getByTestId('dog-card');
@@ -17,7 +17,7 @@ test.describe('API Integration', () => {
1717
await expect(page.getByTestId('dog-breed').nth(2)).toHaveText('German Shepherd');
1818
});
1919

20-
test('should render dog details from the mock API', async ({ page }) => {
20+
test('should render dog details from the API', async ({ page }) => {
2121
await page.goto('/dog/1');
2222

2323
await expect(page.getByTestId('dog-details')).toBeVisible();

client/e2e-tests/homepage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test.describe('Tailspin Shelter Homepage', () => {
1111
await expect(page.getByText('Find your perfect companion from our wonderful selection')).toBeVisible();
1212
});
1313

14-
test('should display dog list with mock data', async ({ page }) => {
14+
test('should display dog list', async ({ page }) => {
1515
await page.goto('/');
1616

1717
await expect(page.getByRole('heading', { name: 'Available Dogs' })).toBeVisible();
@@ -23,7 +23,7 @@ test.describe('Tailspin Shelter Homepage', () => {
2323
await expect(dogCards).toHaveCount(3);
2424
});
2525

26-
test('should display dog names and breeds from mock API', async ({ page }) => {
26+
test('should display dog names and breeds', async ({ page }) => {
2727
await page.goto('/');
2828

2929
await expect(page.getByTestId('dog-name').nth(0)).toHaveText('Buddy');

client/e2e-tests/mock-api.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

client/package-lock.json

Lines changed: 1 addition & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"devDependencies": {
2323
"@playwright/test": "^1.58.1",
2424
"@types/node": "^24.3.1",
25-
"tailwindcss": "^4.1.17",
26-
"tsx": "^4.21.0"
25+
"tailwindcss": "^4.1.17"
2726
}
2827
}

client/playwright.config.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { defineConfig, devices } from '@playwright/test';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
24

3-
// Port configuration for mock API and Astro dev server
4-
const mockApiPort = 5199;
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
6+
const serverDir = path.resolve(__dirname, '..', 'server');
7+
const testDbPath = path.join(serverDir, 'e2e_test_dogshelter.db');
8+
const flaskPort = 5100;
59
const astroDevPort = 4321;
610

711
export default defineConfig({
@@ -23,13 +27,13 @@ export default defineConfig({
2327
],
2428
webServer: [
2529
{
26-
command: 'npx tsx e2e-tests/mock-api.ts',
27-
url: `http://localhost:${mockApiPort}/api/dogs`,
30+
command: `cd ${serverDir} && python3 utils/seed_test_database.py && DATABASE_PATH=${testDbPath} python3 app.py`,
31+
url: `http://localhost:${flaskPort}/api/dogs`,
2832
reuseExistingServer: !process.env.CI,
29-
timeout: 10_000,
33+
timeout: 30_000,
3034
},
3135
{
32-
command: `API_SERVER_URL=http://localhost:${mockApiPort} npm run dev -- --no-clearScreen`,
36+
command: `API_SERVER_URL=http://localhost:${flaskPort} npm run dev -- --no-clearScreen`,
3337
url: `http://localhost:${astroDevPort}`,
3438
reuseExistingServer: !process.env.CI,
3539
timeout: 30_000,

server/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
base_dir: str = os.path.abspath(os.path.dirname(__file__))
88

99
app: Flask = Flask(__name__)
10-
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(base_dir, "dogshelter.db")}'
10+
db_path: str = os.environ.get('DATABASE_PATH', os.path.join(base_dir, 'dogshelter.db'))
11+
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
1112
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1213

1314
# Initialize the database with the app

0 commit comments

Comments
 (0)