-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up integration testing with Testcontainers (#648)
- Loading branch information
Showing
41 changed files
with
542 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/core/src/modules/user/__test__/user-service.e2e-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import crypto from "crypto" | ||
import { beforeEach, describe, expect, it } from "vitest" | ||
import { ulid } from "ulid" | ||
import { createEnvironment } from "@dotkomonline/env" | ||
import { createKysely } from "@dotkomonline/db" | ||
import { createServiceLayer, type ServiceLayer } from "../../core" | ||
|
||
describe("users", () => { | ||
let core: ServiceLayer | ||
|
||
beforeEach(async () => { | ||
const env = createEnvironment() | ||
const db = createKysely(env) | ||
core = await createServiceLayer({ db }) | ||
}) | ||
|
||
it("can create new users", async () => { | ||
const none = await core.userService.getAllUsers(100) | ||
expect(none).toHaveLength(0) | ||
|
||
const user = await core.userService.createUser({ | ||
cognitoSub: crypto.randomUUID(), | ||
studyYear: 0, | ||
}) | ||
|
||
const users = await core.userService.getAllUsers(100) | ||
expect(users).toContainEqual(user) | ||
}) | ||
|
||
it("will not allow two users the same subject", async () => { | ||
const subject = crypto.randomUUID() | ||
const first = await core.userService.createUser({ | ||
cognitoSub: subject, | ||
studyYear: 0, | ||
}) | ||
expect(first).toBeDefined() | ||
await expect( | ||
core.userService.createUser({ | ||
cognitoSub: subject, | ||
studyYear: 0, | ||
}) | ||
).rejects.toThrow() | ||
}) | ||
|
||
it("will find users by their user id", async () => { | ||
const user = await core.userService.createUser({ | ||
cognitoSub: crypto.randomUUID(), | ||
studyYear: 0, | ||
}) | ||
|
||
const match = await core.userService.getUserById(user.id) | ||
expect(match).toEqual(user) | ||
const fail = await core.userService.getUserById(ulid()) | ||
expect(fail).toBeUndefined() | ||
}) | ||
|
||
it("can update users given their id", async () => { | ||
await expect( | ||
core.userService.updateUser(ulid(), { | ||
cognitoSub: crypto.randomUUID(), | ||
}) | ||
).rejects.toThrow() | ||
const user = await core.userService.createUser({ | ||
cognitoSub: crypto.randomUUID(), | ||
studyYear: 0, | ||
}) | ||
const updated = await core.userService.updateUser(user.id, { | ||
cognitoSub: crypto.randomUUID(), | ||
}) | ||
expect(updated.cognitoSub).not.toEqual(user.cognitoSub) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { defineConfig } from "vitest/config" | ||
|
||
const defaultExclude = [ | ||
"**/node_modules/**", | ||
"**/dist/**", | ||
"**/cypress/**", | ||
"**/.{idea,git,cache,output,temp}/**", | ||
"**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*", | ||
] | ||
|
||
export default defineConfig({ | ||
test: { | ||
exclude: defaultExclude.concat("**/*.spec.ts"), | ||
include: ["**/*.e2e-spec.ts"], | ||
threads: false, | ||
mockReset: true, | ||
setupFiles: ["./vitest-integration.setup.ts"], | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { beforeEach } from "vitest" | ||
import { PostgreSqlContainer, type StartedPostgreSqlContainer } from "@testcontainers/postgresql" | ||
import { createMigrator, createKysely } from "@dotkomonline/db" | ||
import { createEnvironment } from "@dotkomonline/env" | ||
|
||
const containers: StartedPostgreSqlContainer[] = [] | ||
|
||
beforeEach(async () => { | ||
const container = await new PostgreSqlContainer("public.ecr.aws/z5h0l8j6/dotkom/pgx-ulid:0.1.3") | ||
.withExposedPorts(5432) | ||
.withUsername("local") | ||
.withPassword("local") | ||
.withDatabase("main") | ||
.start() | ||
process.env.DATABASE_URL = container.getConnectionUri() | ||
const env = createEnvironment() | ||
const kysely = createKysely(env) | ||
const migrator = createMigrator(kysely, new URL("node_modules/@dotkomonline/db/src/migrations", import.meta.url)) | ||
await migrator.migrateToLatest() | ||
containers.push(container) | ||
}) | ||
|
||
process.on("beforeExit", async () => { | ||
await Promise.all(containers.map(async (container) => container.stop())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# db | ||
|
||
Migrations have to be written in JS with JSDoc annotations in order for Vite to be able to dynamically import them | ||
properly during Vitest runs |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { sql } from "kysely" | ||
import { createTableWithDefaults } from "../utils.js" | ||
|
||
/** @param db {import('kysely').Kysely} */ | ||
export async function up(db) { | ||
const query = sql`CREATE EXTENSION IF NOT EXISTS ulid;`.compile(db) | ||
await db.executeQuery(query) | ||
await createTableWithDefaults("ow_user", { id: true, createdAt: true }, db.schema).execute() | ||
} | ||
|
||
/** @param db {import('kysely').Kysely} */ | ||
export async function down(db) { | ||
await db.schema.dropTable("ow_user").execute() | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
...ges/db/src/migrations/0005_event_types.ts → ...ges/db/src/migrations/0005_event_types.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { type Kysely, sql } from "kysely" | ||
import { sql } from "kysely" | ||
|
||
export async function up(db: Kysely<any>) { | ||
/** @param db {import('kysely').Kysely} */ | ||
export async function up(db) { | ||
await db.schema.createType("event_type").asEnum(["SOCIAL", "ACADEMIC", "COMPANY", "BEDPRES"]).execute() | ||
await db.schema | ||
.alterTable("event") | ||
.addColumn("type", sql`event_type`) | ||
.execute() | ||
} | ||
|
||
export async function down(db: Kysely<any>) { | ||
/** @param db {import('kysely').Kysely} */ | ||
export async function down(db) { | ||
await db.schema.alterTable("event").dropColumn("type").execute() | ||
await db.schema.dropType("event_type").ifExists().execute() | ||
} |
Oops, something went wrong.
d0b5874
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rif – ./apps/rif
rif-two.vercel.app
rif-dotkom.vercel.app
dev.interesse.online.ntnu.no
rif-git-main-dotkom.vercel.app