Skip to content

Commit

Permalink
Add test for memberships gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed Feb 13, 2024
1 parent b5b4c4d commit 97b92f4
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist-app
dist-auth
dist-worker
.dev.vars
test/support/test.sqlite
2 changes: 0 additions & 2 deletions migrations/0000_create_user_tables.sql
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,
Expand Down
10 changes: 5 additions & 5 deletions src/accounts/accountsGateway.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export type AccountRecord = {
id: string
id: number
name: string
}

export type AccountsGateway = {
findForUser: (userId: string) => Promise<AccountRecord | null>
findForUser: (userId: number) => Promise<AccountRecord | null>
create: (name: string) => Promise<AccountRecord>
}

export const accountsGateway = (db: D1Database): AccountsGateway => ({
findForUser: async (userId: string): Promise<AccountRecord | null> => {
findForUser: async (userId: number): Promise<AccountRecord | null> => {
const record = await db.prepare(`
select a.id, a.name from accounts a
join main.memberships m on a.id = m.account_id
Expand All @@ -18,7 +18,7 @@ export const accountsGateway = (db: D1Database): AccountsGateway => ({
if (record === null) return record

return {
id: record["id"] as string,
id: record["id"] as number,
name: record["name"] as string,
}
},
Expand All @@ -29,7 +29,7 @@ export const accountsGateway = (db: D1Database): AccountsGateway => ({
}

return {
id: record["id"] as string,
id: record["id"] as number,
name: record["name"] as string,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/accounts/accountsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {UsersGateway} from "./usersGateway";
import {AccountRecord, AccountsGateway} from "./accountsGateway";

export type UserAccount = {
id: string
id: number
email: string
accountId: string
accountId: number
accountName: string
}

Expand All @@ -18,7 +18,7 @@ export const accountsService = (
usersGateway: UsersGateway,
membershipsGateway: MembershipsGateway,
): AccountsService => {
const createAccountFor = async (userId: string, email: string): Promise<AccountRecord> => {
const createAccountFor = async (userId: number, email: string): Promise<AccountRecord> => {
const account = await accountsGateway.create(`${email} account`)
await membershipsGateway.create(userId, account.id)
return account
Expand Down
16 changes: 8 additions & 8 deletions src/accounts/membershipsGateway.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
type MembershipRecord = {
id: string
userId: string
accountId: string
id: number
userId: number
accountId: number
}

export type MembershipsGateway = {
create: (userId: string, accountId: string) => Promise<MembershipRecord>
create: (userId: number, accountId: number) => Promise<MembershipRecord>
}

export const membershipsGateway = (db: D1Database): MembershipsGateway => ({
create: async (userId: string, accountId: string): Promise<MembershipRecord> => {
create: async (userId: number, accountId: number): Promise<MembershipRecord> => {
const record = await db.prepare(`
insert into memberships (user_id, account_id, owner)
values (?, ?, true)
Expand All @@ -21,9 +21,9 @@ export const membershipsGateway = (db: D1Database): MembershipsGateway => ({
}

return {
id: record["id"] as string,
userId: record["user_id"] as string,
accountId: record["account_id"] as string,
id: record["id"] as number,
userId: record["user_id"] as number,
accountId: record["account_id"] as number,
}
}
})
6 changes: 3 additions & 3 deletions src/accounts/usersGateway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type UserRecord = {
id: string
id: number
email: string
}

Expand All @@ -14,7 +14,7 @@ export const usersGateway = (db: D1Database): UsersGateway => ({
if (record === null) return record

return {
id: record["id"] as string,
id: record["id"] as number,
email: record["email"] as string,
}
},
Expand All @@ -25,7 +25,7 @@ export const usersGateway = (db: D1Database): UsersGateway => ({
}

return {
id: record["id"] as string,
id: record["id"] as number,
email: record["email"] as string,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/auth/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {UserAccount} from "../accounts/accountsService";

type UserSession = { userId: string, email: string, accountId: string, accountName: string };
type UserSession = { userId: number, email: string, accountId: number, accountName: string };
type EmptySession = { userId: null, email: null, accountId: null, accountName: null };

export type Session = UserSession | EmptySession
Expand Down
24 changes: 24 additions & 0 deletions test/accounts/membershipsGateway.test.ts
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)
});
});
25 changes: 25 additions & 0 deletions test/support/databaseSupport.ts
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')
}

0 comments on commit 97b92f4

Please sign in to comment.