You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blogs/series-5-devops-data/5.1-database-seeding.md
+43-18Lines changed: 43 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -424,7 +424,7 @@ This is useful for:
424
424
* Seeding specific row counts in different environments
425
425
* Integration test setup — seed exactly the records you need via an API call
426
426
427
-
The command is dispatched via an API endpoint that sends a POST to `api/v1/positions/seed`. This can be called from Playwright tests, CI scripts, or Swagger UI.
427
+
The command is dispatched via `POST /api/v1/positions/AddMock`. It requires a valid Bearer token (`[Authorize]`) and accepts `{ "RowCount": N }` as a JSON body. It can be called from Playwright tests, CI scripts, or Swagger UI.
428
428
429
429
---
430
430
@@ -469,7 +469,7 @@ Playwright provides a `request` fixture — an `APIRequestContext` that makes HT
469
469
470
470
### Calling the Seed Endpoint
471
471
472
-
The `InsertMockPositionCommand` is exposed as `POST /api/v1/positions/seed`. Here is a complete implementation of `seed.spec.ts`:
472
+
The `InsertMockPositionCommand` is exposed as `POST /api/v1/positions/AddMock`. It requires a Bearer token (`[Authorize]`) and accepts a JSON body. Here is a complete implementation of `seed.spec.ts`:
**`params: { rowCount: 25 }`** — Playwright serializes this as a query string: `?rowCount=25`. The command handler uses this value to control how many positions Bogus generates.
506
+
**`POST /api/v1/positions/AddMock`** — the actual route registered by the `[Route("AddMock")]` attribute on `PositionsController`. The endpoint is protected with `[Authorize]`, so a valid Bearer token is required.
500
507
501
-
**`request.post()`** — uses the `request` fixture from the `test` parameters. No browser is opened. The call completes before the test body continues.
508
+
**`data: { RowCount: 25 }`** — Playwright serializes this as a JSON body. `InsertMockPositionCommand` binds from the request body (POST default). Property name matches the C# property: `RowCount` (Pascal case).
502
509
503
-
**`await response.json()`** — the endpoint returns the count of rows inserted as a plain integer. The assertion confirms at least one record was created.
510
+
**`result.data`** — the endpoint returns a `Result<int>` wrapper with `{ succeeded: true, data: 25, ... }`, not a plain integer. Access the row count via `result.data`.
504
511
505
512
### Seeding in `beforeAll` for a Test Suite
506
513
507
514
When multiple tests in the same file need the seeded data, call the seed endpoint in `beforeAll` so it runs once before the suite rather than repeating it in every test:
@@ -570,7 +595,7 @@ async function globalSetup() {
570
595
exportdefaultglobalSetup;
571
596
```
572
597
573
-
`request.newContext()` in `globalSetup` creates a standalone API context outside of any test. `await apiContext.dispose()` releases it when done. Throwing an error from `globalSetup` fails the entire run immediately with a clear message rather than silently running tests against an unseeded database.
598
+
`request.newContext()` in `globalSetup` creates a standalone API context outside of any test. The auth call acquires a short-lived JWT using the same test credentials used in browser-based tests. `await apiContext.dispose()` releases it when done. Throwing on either failure (auth or seed) fails the entire run immediately with a clear message rather than silently running tests against an unseeded database.
574
599
575
600
### When to Use Each Approach
576
601
@@ -592,7 +617,7 @@ export default globalSetup;
592
617
593
618
**Development-only seeding with an idempotency check.** The entire seed block is inside `if (app.Environment.IsDevelopment())` — production never auto-seeds. The `needsSeed` check (`!dbContext.Departments.Any() || !dbContext.Employees.Any()`) ensures the seeder only runs on an empty database, so restarting the API doesn't duplicate records.
594
619
595
-
**`InsertMockPositionCommand` for runtime seeding.** The CQRS `POST /api/v1/positions/seed` endpoint lets you add more positions without restarting the API. It fetches existing departments and salary ranges from the database and passes them to `PositionInsertBogusConfig` — so every generated position has valid FK references to real rows. This pattern extends to any entity that has FK dependencies.
620
+
**`InsertMockPositionCommand` for runtime seeding.** The CQRS `POST /api/v1/positions/AddMock` endpoint lets you add more positions without restarting the API. It fetches existing departments and salary ranges from the database and passes them to `PositionInsertBogusConfig` — so every generated position has valid FK references to real rows. This pattern extends to any entity that has FK dependencies.
0 commit comments