Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds a test that runs the db seed, making sure it's compatible with latest schema #402

Merged
merged 6 commits into from
Sep 4, 2024

Conversation

thomhickey
Copy link
Contributor

@thomhickey thomhickey commented Sep 2, 2024

  • creates a test db
  • runs 'seedfile' from src/backend/db/lib/seed.ts
  • queries the db for both students and users
  • asserts that there are >0 students and users
  • closes Test for db: seed #387

Copy link
Contributor

@mrabbitt mrabbitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me except for a minor comment on test naming.

I tested that this actually fails when backwards incompatible schema changes are made by replacing all instances of last_name with surname in src/backend/db/migrations/1_initial-migrations.sql and then running the test. It failed exactly as I'd expect with the error message below. ✅

Thank you, @thomhickey !

  Uncaught exception in src/backend/db/lib/seed.test.ts

  error: terminating connection due to administrator command

  › Parser.parseErrorMessage (node_modules/pg-protocol/dist/parser.js:287:98)
  › Parser.handlePacket (node_modules/pg-protocol/dist/parser.js:126:29)
  › Parser.parse (node_modules/pg-protocol/dist/parser.js:39:38)
  › Socket.<anonymous> (node_modules/pg-protocol/dist/index.js:11:42)

  ✘ [fail]: seedFile works after schema changes Rejected promise returned by test
  ✘ src/backend/db/lib/seed.test.ts exited with a non-zero exit code: 1
  ─

  seedFile works after schema changes

  Rejected promise returned by test. Reason:

  Error {
    message: 'column "last_name" of relation "user" does not exist',
  }

  › Parser.parseErrorMessage (node_modules/pg-protocol/dist/parser.js:287:98)
  › Parser.handlePacket (node_modules/pg-protocol/dist/parser.js:126:29)
  › Parser.parse (node_modules/pg-protocol/dist/parser.js:39:38)
  › Socket.<anonymous> (node_modules/pg-protocol/dist/index.js:11:42)
  › PostgresConnection.executeQuery (node_modules/kysely/dist/cjs/dialect/postgres/postgres-driver.js:91:69)
  › <anonymous> (node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28)
  › DefaultConnectionProvider.provideConnection (node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
  › DefaultQueryExecutor.executeQuery (node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
  › InsertQueryBuilder.execute (node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:517:24)
  › InsertQueryBuilder.executeTakeFirst (node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:532:26)
  › InsertQueryBuilder.executeTakeFirstOrThrow (node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:544:24)
  › seed (src/backend/tests/seed.ts:6:17)
  › Object.beforeTemplateIsBaked (src/backend/tests/fixtures/get-test-database.ts:21:24)

  ─

  1 test failed
  1 uncaught exception

import { getDb } from "@/backend/db/lib/get-db";
import { seedfile } from "@/backend/db/lib/seed";

test("seedFile works after schema changes", async (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: I think "seedFile works with current schema" would be a slightly better title. The "after schema changes" part could imply that the test itself is applying schema changes before running seedFile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great callout @mrabbitt ! ... changed in 2c0415a to 'seedfile works with current schema'

@thomhickey
Copy link
Contributor Author

thanks for the negative test to make sure it fails @mrabbitt .. awesome

Copy link
Contributor

@mrabbitt mrabbitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

Copy link
Contributor

@KCCPMG KCCPMG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems pretty straightforward, and I can confirm in the debugger that students and users are what they're supposed to be. Looks good!

@thomhickey
Copy link
Contributor Author

@mrabbitt I added a catch block, now the test fails more elegantly, if it fails:

image

Copy link
Contributor

@mrabbitt mrabbitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@thomhickey thomhickey merged commit 8aae5f4 into main Sep 4, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test for db: seed
3 participants