Skip to content

Commit ba9e364

Browse files
feat: Season table and season query function w/ tests. Refactor test seeding and use the word 'factory' over 'mock' when referring to overridable test object generators.
1 parent 9be40ec commit ba9e364

28 files changed

Lines changed: 268 additions & 285 deletions

apps/backend/.swcrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
},
1010
"baseUrl": "src",
1111
"paths": {
12-
"@seeds/*": ["../seeds/*"],
1312
"@test/*": ["test/*"],
1413
"@db/*": ["db/*"],
1514
"@v1/*": ["v1/*"]

apps/backend/seeds/SSBUCharacters.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Knex } from "knex";
55
const log = getLogger(["grindcord", "db"]);
66

77
// Seek more customizable API where list of seeds can be provided to the seedsource
8-
export class SeedSource {
8+
export class BaseSeedSource {
99
getSeeds() {
1010
return Promise.resolve(["SSBUCharacters"]);
1111
}

apps/backend/src/db/init_tables.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ import {
77
SSBUCharTable,
88
} from "@v1/match/models";
99
import { MatchReportView, MatchWinnerView } from "@v1/match/views";
10+
import { SeasonTable } from "@v1/season/models";
11+
import { Season } from "@v1/season/schemas";
1012
import type { Knex } from "knex";
1113

1214
const log = getLogger(["grindcord", "db"]);
1315

14-
const tables = [MatchTable, MatchPlayerTable, MatchCharacterTable, SSBUCharTable];
16+
const tables = [
17+
MatchTable,
18+
MatchPlayerTable,
19+
MatchCharacterTable,
20+
SSBUCharTable,
21+
SeasonTable,
22+
];
1523
const views = [MatchWinnerView, MatchReportView];
1624

1725
async function create_table_if_notexists(
@@ -31,18 +39,19 @@ async function create_table_if_notexists(
3139
}
3240
}
3341

34-
export async function init_tables(db: Knex = knexDb) {
42+
export async function init_tables(db: Knex = knexDb): Promise<void> {
3543
const trx = await db.transaction();
3644
for (const table of tables) {
3745
await create_table_if_notexists(trx, table.table_name, table.initialize);
3846
}
47+
await trx.seed.run();
3948
await trx.commit();
4049
}
4150

42-
export async function run_seed(
51+
export async function seed_db(
4352
seedSource: Knex.SeedSource<unknown>,
4453
db: Knex = knexDb,
45-
) {
54+
): Promise<void> {
4655
await db.seed.run({ seedSource });
4756
}
4857

apps/backend/src/db/knexfile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import knex, { type Knex } from "knex";
2+
import { BaseSeedSource } from "./BaseSeedSource";
23

34
// TODO: https://knexjs.org/guide/#log
45
export const config: Knex.Config<Knex.Sqlite3ConnectionConfig> = {
56
client: "better-sqlite3",
67
connection: {
78
filename: "./bot_data.db",
89
},
10+
seeds: {
11+
seedSource: new BaseSeedSource(),
12+
},
913
useNullAsDefault: true,
1014
};
1115

apps/backend/src/db/schemas.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

apps/backend/src/db/test/unit.test.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

apps/backend/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { SeedSource as SSBUCharacterSeedSource } from "@db/BaseSeedSource";
12
import { init_tables, init_views, run_seed, teardown } from "@db/init_tables";
2-
import { SeedSource as SSBUCharacterSeedSource } from "@db/SeedSource";
33
import { serve } from "@hono/node-server";
44
import { honoLogger } from "@logtape/hono";
55
import { configure, getConsoleSink } from "@logtape/logtape";
@@ -34,6 +34,7 @@ app.get("/", (c) => {
3434
});
3535

3636
app.route("/match", match_router);
37+
app.route("/user", user_router);
3738

3839
app.get(
3940
"/openapi.json",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// General utilities for generating mock data
1+
// General utilities for generating test data
22

33
import { ssbu_character_names } from "@db/seeds/SSBUCharacters";
44
import { faker } from "@faker-js/faker";

apps/backend/src/test/test_knexfile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { BaseSeedSource } from "@db/BaseSeedSource";
12
import knex, { type Knex } from "knex";
23

34
export const test_config: Knex.Config = {
45
client: "better-sqlite3",
56
connection: {
67
filename: ":memory:",
78
},
9+
seeds: {
10+
seedSource: new BaseSeedSource(),
11+
},
812
useNullAsDefault: true,
913
};
1014

0 commit comments

Comments
 (0)