test: move resetDb to own testing lib function
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import { eq, sql } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { DateTime } from "luxon";
|
import { DateTime } from "luxon";
|
||||||
|
|
||||||
import { beforeEach, describe, expect, it } from "bun:test";
|
import { beforeEach, describe, expect, it } from "bun:test";
|
||||||
|
|
||||||
import app from "~/index";
|
import app from "~/index";
|
||||||
import { getTestDb } from "~/libs/test/getTestDb";
|
import { getTestDb } from "~/libs/test/getTestDb";
|
||||||
|
import { resetTestDb } from "~/libs/test/resetTestDb";
|
||||||
import { server } from "~/mocks";
|
import { server } from "~/mocks";
|
||||||
import { resetDb } from "~/models/db";
|
|
||||||
import { deviceTokensTable } from "~/models/schema";
|
import { deviceTokensTable } from "~/models/schema";
|
||||||
|
|
||||||
server.listen();
|
server.listen();
|
||||||
@@ -15,7 +15,7 @@ describe("requests the /token route", () => {
|
|||||||
const db = getTestDb();
|
const db = getTestDb();
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await resetDb();
|
await resetTestDb();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should succeed", async () => {
|
it("should succeed", async () => {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { beforeEach, describe, expect, it } from "bun:test";
|
|||||||
import app from "~/index";
|
import app from "~/index";
|
||||||
import { getTestDb } from "~/libs/test/getTestDb";
|
import { getTestDb } from "~/libs/test/getTestDb";
|
||||||
import { getTestEnv } from "~/libs/test/getTestEnv";
|
import { getTestEnv } from "~/libs/test/getTestEnv";
|
||||||
|
import { resetTestDb } from "~/libs/test/resetTestDb";
|
||||||
import { server } from "~/mocks";
|
import { server } from "~/mocks";
|
||||||
import { resetDb } from "~/models/db";
|
|
||||||
import { deviceTokensTable } from "~/models/schema";
|
import { deviceTokensTable } from "~/models/schema";
|
||||||
|
|
||||||
server.listen();
|
server.listen();
|
||||||
@@ -13,7 +13,7 @@ describe("requests the /watch-status route", () => {
|
|||||||
const db = getTestDb();
|
const db = getTestDb();
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await resetDb();
|
await resetTestDb();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("saving title, deviceId in db, should succeed", async () => {
|
it("saving title, deviceId in db, should succeed", async () => {
|
||||||
|
|||||||
11
src/libs/test/resetTestDb.ts
Normal file
11
src/libs/test/resetTestDb.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { tables } from "~/models/schema";
|
||||||
|
|
||||||
|
import { getTestDb } from "./getTestDb";
|
||||||
|
|
||||||
|
export async function resetTestDb() {
|
||||||
|
const db = getTestDb();
|
||||||
|
|
||||||
|
for (const table of tables) {
|
||||||
|
await db.delete(table);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,6 @@ import { drizzle } from "drizzle-orm/libsql";
|
|||||||
|
|
||||||
import type { Env } from "~/types/env";
|
import type { Env } from "~/types/env";
|
||||||
|
|
||||||
import { tables } from "./schema";
|
|
||||||
|
|
||||||
type Db = ReturnType<typeof createDb>;
|
type Db = ReturnType<typeof createDb>;
|
||||||
let db: Db | null = null;
|
let db: Db | null = null;
|
||||||
|
|
||||||
@@ -17,14 +15,6 @@ export function getDb(env: Env): Db {
|
|||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resetDb() {
|
|
||||||
if (!db) return;
|
|
||||||
|
|
||||||
for (const table of tables) {
|
|
||||||
await db.delete(table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createDb(env: Env) {
|
function createDb(env: Env) {
|
||||||
const client = createClient({
|
const client = createClient({
|
||||||
url: env.TURSO_URL,
|
url: env.TURSO_URL,
|
||||||
|
|||||||
Reference in New Issue
Block a user