Skip to content

Commit

Permalink
Add smoke test for DB ops
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Jul 23, 2024
1 parent d1090ac commit ced869a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/storage/db/database-test-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { connectToDb, disconnectFromDb } from "./db.js";

export default async function createDatabase() {
await connectToDb(":memory:");

return async () => {
await disconnectFromDb();
};
}
4 changes: 4 additions & 0 deletions src/storage/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ let kysely: Kysely<Database>;
export default () => kysely;

export async function connectToDb(databasePath: string) {
if (kysely) {
return;
}

const nativeDb = new SqliteDatabase(databasePath);
const db = new Kysely<Database>({
dialect: new BunSqliteDialect({
Expand Down
31 changes: 30 additions & 1 deletion src/utils/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
import { describe, expect, test } from "bun:test";
import { describe, expect, test, beforeEach } from "bun:test";

import createDatabase from "../storage/db/database-test-init.js";

import db from "@db";

describe("smoke", () => {
test("does math still hold?", () => {
expect(1 + 1).toBe(2);
});
});

describe("database smoke", () => {
beforeEach(createDatabase);

test("does database work?", async () => {
const res = await db()
.insertInto("birthdays")
.values({
day: 1,
month: 2,
userId: "12345",
})
.returningAll()
.executeTakeFirstOrThrow();

expect(res).toMatchObject({
day: 1,
month: 2,
userId: "12345",
});

const res2 = await db().selectFrom("birthdays").selectAll().executeTakeFirstOrThrow();
expect(res).toStrictEqual(res2);
});
});

0 comments on commit ced869a

Please sign in to comment.