Skip to content

Commit 99f4058

Browse files
test: Rename some test variables, add a test for after being ahead of before
1 parent 20d49e6 commit 99f4058

4 files changed

Lines changed: 60 additions & 21 deletions

File tree

apps/backend/src/v1/season/schemas.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const SeasonQuery = z
1414
season_id: z.int(),
1515
guild_id: z.string(),
1616
season_name: z.string(),
17-
during: z.iso.datetime(),
17+
after: z.iso.datetime(),
18+
before: z.iso.datetime(),
1819
})
1920
.partial();
2021
export type SeasonQuery = z.infer<typeof SeasonQuery>;

apps/backend/src/v1/season/service.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import type { SeasonRecord } from "@v1/season/models";
22
import type { Knex } from "knex";
33
import type { SeasonQuery } from "./schemas";
44

5-
/** Find the `season_id` of the current Seasons of a Guild with a `guild_id` */
5+
/** Find the `season_id` of the current Seasons of a Guild with a `guild_id`
6+
* The arguments `after` and `before` do not refer to the entire span of a Season,
7+
* instead, it refers to a Season that is active for the full timespan between
8+
* `after` and `before`.
9+
*/
610
export async function getSeasons(
711
season_query: SeasonQuery,
812
db: Knex,
913
): Promise<Array<SeasonRecord>> {
10-
const { season_id, guild_id, season_name, during } = season_query;
14+
const { season_id, guild_id, season_name, after, before } = season_query;
1115
const query = db<SeasonRecord>("Season").select();
1216
if (guild_id) {
1317
query.where({ guild_id });
@@ -18,8 +22,11 @@ export async function getSeasons(
1822
if (season_name) {
1923
query.whereILike(season_name, `%{season_name}%`);
2024
}
21-
if (during) {
22-
query.where("end_at", ">=", during).where("start_at", "<=", during);
25+
if (after) {
26+
query.where("end_at", ">=", after);
27+
}
28+
if (before) {
29+
query.where("start_at", "<=", before);
2330
}
2431
return await query;
2532
}

apps/backend/src/v1/season/test/integration.test.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
currentSeasonRecordFactory,
88
futureSeasonRecordFactory,
99
pastSeasonRecordFactory,
10-
seasonRecordFactory,
1110
} from "@v1/season/test/models.factories";
1211
import type { Knex } from "knex";
1312
import { afterEach, beforeEach, describe, expect, test } from "vitest";
@@ -43,7 +42,7 @@ describe("Season table operations", () => {
4342
await teardown(test_knexDb);
4443
});
4544
describe("Use `getSeasons`", () => {
46-
test("Typical", async () => {
45+
test("Based on `after` and `before` arguments", async () => {
4746
const past_season = pastSeasonRecordFactory();
4847
const current_season = currentSeasonRecordFactory();
4948
const future_season = futureSeasonRecordFactory();
@@ -54,19 +53,46 @@ describe("Season table operations", () => {
5453
test_knexDb,
5554
);
5655

57-
const result = await getSeasons({ during: (new Date()).toISOString() }, test_knexDb);
56+
const current_result = await getSeasons(
57+
{ before: new Date().toISOString(), after: new Date().toISOString() },
58+
test_knexDb,
59+
);
60+
expect(current_result, "Returns only the ongoing Season").toHaveLength(1);
61+
expect(current_result[0]).toMatchObject(current_season);
5862

59-
expect(result).toHaveLength(1);
60-
expect(result[0]).toMatchObject(current_season);
63+
const reverse_result = await getSeasons(
64+
{
65+
before: new Date(Date.now() - 1000).toISOString(),
66+
after: new Date(Date.now() + 1000).toISOString(),
67+
},
68+
test_knexDb,
69+
);
70+
expect(reverse_result, "`after` > `before`").toHaveLength(1);
71+
expect(reverse_result[0]).toMatchObject(current_season);
6172

62-
// const match_id = await createMatch(mockMatchReport.guild_id, test_knexDb);
63-
// const created_match = await test_knexDb("Match")
64-
// .first()
65-
// .where({ match_id });
66-
// expect(
67-
// created_match.guild_id,
68-
// "When retrieved, the Match record has the same data as is provided",
69-
// ).toEqual(mockMatchReport.guild_id);
73+
const current_future_result = await getSeasons(
74+
{ after: new Date().toISOString() },
75+
test_knexDb,
76+
);
77+
expect(current_future_result, "Returns current and future").toHaveLength(2);
78+
expect(current_future_result).toEqual(
79+
expect.arrayContaining([
80+
expect.objectContaining(current_season),
81+
expect.objectContaining(future_season),
82+
]),
83+
);
84+
85+
const current_past_result = await getSeasons(
86+
{ before: new Date().toISOString() },
87+
test_knexDb,
88+
);
89+
expect(current_past_result, "Returns current and past").toHaveLength(2);
90+
expect(current_past_result).toEqual(
91+
expect.arrayContaining([
92+
expect.objectContaining(current_season),
93+
expect.objectContaining(past_season),
94+
]),
95+
);
7096
});
7197
});
7298
});

apps/backend/src/v1/season/test/models.factories.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,32 @@ export const currentSeasonRecordFactory = (
2020
| Partial<Omit<SeasonRecord, "start_at" | "end_at">>
2121
| undefined = undefined,
2222
): Omit<SeasonRecord, "season_id"> => {
23-
return seasonRecordFactory(season_record);
23+
const season_name = `Current Season - ${faker.animal.fish()}`;
24+
return seasonRecordFactory({ season_name, ...season_record });
2425
};
2526

2627
export const pastSeasonRecordFactory = (
2728
season_record:
2829
| Partial<Omit<SeasonRecord, "start_at" | "end_at">>
2930
| undefined = undefined,
3031
): Omit<SeasonRecord, "season_id"> => {
32+
const season_name = `Past Season - ${faker.animal.fish()}`;
33+
3134
const end_at = faker.date.recent().toISOString();
3235
const start_at = faker.date.past({ refDate: end_at }).toISOString();
33-
return seasonRecordFactory({ start_at, end_at, ...season_record });
36+
return seasonRecordFactory({ season_name, start_at, end_at, ...season_record });
3437
};
3538

3639
export const futureSeasonRecordFactory = (
3740
season_record:
3841
| Partial<Omit<SeasonRecord, "start_at" | "end_at">>
3942
| undefined = undefined,
4043
): Omit<SeasonRecord, "season_id"> => {
44+
const season_name = `Future Season - ${faker.animal.fish()}`;
45+
4146
const start_at = faker.date.soon().toISOString();
4247
const end_at = faker.date.future({ refDate: start_at }).toISOString();
43-
return seasonRecordFactory({ start_at, end_at, ...season_record });
48+
return seasonRecordFactory({ season_name, start_at, end_at, ...season_record });
4449
};
4550

4651
export async function seed(

0 commit comments

Comments
 (0)