-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
70 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dist-app | |
dist-auth | ||
dist-worker | ||
.dev.vars | ||
test/support/test.sqlite |
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,5 +1,3 @@ | ||
-- Migration number: 0000 2024-02-13T03:45:01.307Z | ||
|
||
create table users | ||
( | ||
id integer primary key, | ||
|
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
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,24 @@ | ||
import {membershipsGateway} from "../../src/accounts/membershipsGateway"; | ||
import {clear, createDb, migrate} from "../support/databaseSupport"; | ||
import {beforeEach} from "vitest"; | ||
|
||
describe("membershipsGateway", async () => { | ||
const db = createDb() | ||
await migrate(db) | ||
|
||
beforeEach(async () => { | ||
await clear(db) | ||
}); | ||
|
||
test("create", async () => { | ||
const gateway = membershipsGateway(db) | ||
|
||
await db.exec("insert into users (id, email) values (11, '[email protected]');") | ||
await db.exec("insert into accounts (id, name) values (22, 'Some account');") | ||
|
||
const result = await gateway.create(11, 22) | ||
|
||
expect(result["userId"]).toEqual(11) | ||
expect(result["accountId"]).toEqual(22) | ||
}); | ||
}); |
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 fs from "fs"; | ||
import Database from "better-sqlite3"; | ||
import {D1Database, D1DatabaseAPI} from "@miniflare/d1"; | ||
import {D1Database as D1DatabaseType} from "@cloudflare/workers-types" | ||
|
||
export const createDb = (): D1DatabaseType => { | ||
fs.rmSync('test/support/test.sqlite', {force: true}) | ||
const sqlite = new Database('test/support/test.sqlite'); | ||
const api = new D1DatabaseAPI(sqlite) | ||
return new D1Database(api) as D1DatabaseType | ||
} | ||
|
||
export const migrate = async (db: D1DatabaseType): Promise<void> => { | ||
const sql = fs.readFileSync('migrations/0000_create_user_tables.sql', {encoding: "utf8"}); | ||
const statements = sql.replaceAll('\n', '').split(';').filter(statement => statement !== ''); | ||
for (const statement of statements) { | ||
await db.exec(`${statement};`) | ||
} | ||
} | ||
|
||
export const clear = async (db: D1DatabaseType): Promise<void> => { | ||
await db.exec('delete from memberships') | ||
await db.exec('delete from accounts') | ||
await db.exec('delete from users') | ||
} |