43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import type { Db } from '../db';
|
|
import { openDb } from '../db';
|
|
import { runMigrations } from '../db/migrate';
|
|
|
|
/** A fresh in-memory database with every migration applied. */
|
|
export function makeDb(): Db {
|
|
const db = openDb(':memory:');
|
|
runMigrations(db);
|
|
return db;
|
|
}
|
|
|
|
export interface Fixture {
|
|
shops: { a: number; b: number; c: number };
|
|
admin: { id: number; phone: string };
|
|
admin2: { id: number; phone: string };
|
|
shopUser: { id: number; phone: string };
|
|
disabledUser: { id: number; phone: string };
|
|
}
|
|
|
|
export function seedFixture(db: Db): Fixture {
|
|
const shop = (code: string, name: string) =>
|
|
Number(db.prepare('INSERT INTO shops (name, code) VALUES (?, ?)').run(name, code).lastInsertRowid);
|
|
const shops = { a: shop('A', 'Shop A'), b: shop('B', 'Shop B'), c: shop('C', 'Shop C') };
|
|
|
|
const user = (username: string, phone: string, isAdmin: number, active: number) => ({
|
|
id: Number(
|
|
db
|
|
.prepare('INSERT INTO users (username, password_hash, phone, is_admin, active) VALUES (?, ?, ?, ?, ?)')
|
|
.run(username, 'x', phone, isAdmin, active).lastInsertRowid,
|
|
),
|
|
phone,
|
|
});
|
|
|
|
const admin = user('admin', '919000000001', 1, 1);
|
|
const admin2 = user('admin2', '919000000004', 1, 1);
|
|
const shopUser = user('shop-a', '919000000002', 0, 1);
|
|
const disabledUser = user('gone', '919000000003', 0, 0);
|
|
|
|
db.prepare('INSERT INTO user_shops (user_id, shop_id) VALUES (?, ?)').run(shopUser.id, shops.a);
|
|
|
|
return { shops, admin, admin2, shopUser, disabledUser };
|
|
}
|