11import { InsertSeedSource } from "@db/CustomSeedSource" ;
22import { init_tables , init_views } from "@db/init_tables" ;
33import { knexDb } from "@db/knexfile" ;
4+ import type { GuildSeasonRecord } from "@v1/guild/models" ;
5+ import { guildSeasonRecordFactory } from "@v1/guild/test/models.factories" ;
46import type {
57 MatchCharacterRecord ,
68 MatchPlayerRecord ,
@@ -21,10 +23,11 @@ import {
2123} from "@v1/match/service" ;
2224import { matchReportFactory } from "@v1/match/test/schemas.factories" ;
2325import { MatchReportDerivedRow } from "@v1/match/views" ;
26+ import { currentSeasonRecordFactory } from "@v1/season/test/models.factories" ;
2427import { afterEach , beforeEach , describe , expect , test } from "vitest" ;
2528import { matchPlayerRecordFactory , matchRecordFactory } from "./models.factories" ;
2629
27- // Nominal fake data that is not inserted should be defined here
30+ // Nominal fake schema data should be defined here
2831const nominal_match_report = matchReportFactory ( {
2932 guild_id : "123456789" ,
3033 players : [
@@ -41,9 +44,33 @@ const nominal_match_report = matchReportFactory({
4144 ] ,
4245} ) ;
4346
47+ // Seed a GuildSeason record for each test
48+ const guild_id = nominal_match_report . guild_id ;
49+ let season_id : number ;
4450beforeEach ( async ( ) => {
4551 await init_tables ( knexDb ) ;
4652 await init_views ( knexDb ) ;
53+
54+ const season_record = currentSeasonRecordFactory ( ) ;
55+ await knexDb . seed . run ( {
56+ seedSource : new InsertSeedSource ( {
57+ Season : [ season_record ] ,
58+ } ) ,
59+ } ) ;
60+ season_id = await knexDb ( "Season" )
61+ . first ( )
62+ . where ( season_record )
63+ . then ( ( res ) => res . season_id ) ;
64+
65+ const guild_season_record = guildSeasonRecordFactory ( {
66+ guild_id,
67+ season_id,
68+ } ) ;
69+ await knexDb . seed . run ( {
70+ seedSource : new InsertSeedSource ( {
71+ GuildSeason : [ guild_season_record ] ,
72+ } ) ,
73+ } ) ;
4774} ) ;
4875afterEach ( async ( ) => {
4976 await knexDb . destroy ( ) ;
@@ -54,15 +81,19 @@ describe("Match table operations", () => {
5481 describe ( "Use `createMatch`" , ( ) => {
5582 describe ( "Nominal" , ( ) => {
5683 test ( "Insert a Match record with `createMatch`" , async ( ) => {
57- const match_id = await createMatch (
58- nominal_match_report . guild_id ,
59- knexDb ,
60- ) ;
84+ const match_id = await createMatch ( guild_id , knexDb ) ;
6185 const created_match = await knexDb < MatchRecord > ( "Match" )
6286 . first ( )
6387 . where ( { match_id } ) ;
6488 expect ( created_match ?. guild_id ) . toEqual ( nominal_match_report . guild_id ) ;
65- console . log ( created_match ) ;
89+ expect ( created_match ?. season_id ) . toEqual ( season_id ) ;
90+ } ) ;
91+ } ) ;
92+ describe ( "Negative" , ( ) => {
93+ test ( "A Match record cannot be created when no matching GuildSeason is provided" , async ( ) => {
94+ await expect ( createMatch ( "seasonoflove" , knexDb ) ) . rejects . toThrowError (
95+ "The guild is not registered in a season." ,
96+ ) ;
6697 } ) ;
6798 } ) ;
6899 } ) ;
@@ -73,7 +104,7 @@ describe("MatchPlayer table operations", () => {
73104 let match_record : Omit < MatchRecord , "match_id" | "created_at" > ;
74105 let match_id : number ;
75106 beforeEach ( async ( ) => {
76- match_record = matchRecordFactory ( { guild_id : "123456789" } ) ;
107+ match_record = matchRecordFactory ( { guild_id } ) ;
77108 await knexDb . seed . run ( {
78109 seedSource : new InsertSeedSource ( { Match : [ match_record ] } ) ,
79110 } ) ;
@@ -140,7 +171,7 @@ describe("MatchCharacter table operations", () => {
140171 let match_player_characters_2 : Array < SSBUCharFighterNumber > ;
141172 beforeEach ( async ( ) => {
142173 match_record = matchRecordFactory ( {
143- guild_id : nominal_match_report . guild_id ,
174+ guild_id,
144175 } ) ;
145176 await knexDb . seed . run ( {
146177 seedSource : new InsertSeedSource ( {
@@ -238,10 +269,8 @@ describe("MatchCharacter table operations", () => {
238269
239270describe ( "Operations across Match, MatchPlayer, and MatchCharacter tables" , ( ) => {
240271 describe ( "Use `reportMatch`" , async ( ) => {
241- describe ( "Random " , ( ) => {
272+ describe ( "Nominal " , ( ) => {
242273 test ( "Report a match" , async ( ) => {
243- const { guild_id } = nominal_match_report ;
244-
245274 const match_id = await reportMatch ( nominal_match_report , knexDb ) ;
246275
247276 const match_player_records = await knexDb ( "MatchPlayer" )
@@ -280,10 +309,9 @@ describe("Operations across Match, MatchPlayer, and MatchCharacter tables", () =
280309 } ) ;
281310
282311 describe ( "Use `getMatches`" , async ( ) => {
283- describe ( "Random " , ( ) => {
312+ describe ( "Nominal " , ( ) => {
284313 test ( "Retrieve a derived row from MatchReportView by match_id" , async ( ) => {
285314 const match_id = await reportMatch ( nominal_match_report , knexDb ) ;
286- const { guild_id } = nominal_match_report ;
287315
288316 const match_query : MatchQuery = { match_id } ;
289317 const result = await getMatches ( match_query , knexDb ) ;
@@ -298,6 +326,7 @@ describe("Operations across Match, MatchPlayer, and MatchCharacter tables", () =
298326 expected . push (
299327 MatchReportDerivedRow . parse ( {
300328 match_id,
329+ season_id,
301330 guild_id,
302331 user_id,
303332 win_count,
0 commit comments