1+ import { InsertSeedSource } from "@db/CustomSeedSource" ;
12import { init_tables , init_views } from "@db/init_tables" ;
23import { knexDb } from "@db/knexfile" ;
3- import type { MatchCharacterRecord } from "@v1/match/models" ;
4- import type { MatchQuery , MatchReport } from "@v1/match/schemas" ;
4+ import type {
5+ MatchCharacterRecord ,
6+ MatchPlayerRecord ,
7+ MatchRecord ,
8+ } from "@v1/match/models" ;
9+ import {
10+ MatchPlayer ,
11+ type MatchQuery ,
12+ type MatchReport ,
13+ type SSBUCharFighterNumber ,
14+ } from "@v1/match/schemas" ;
515import {
616 createMatch ,
717 createMatchCharacter ,
@@ -11,27 +21,25 @@ import {
1121} from "@v1/match/service" ;
1222import { matchReportFactory } from "@v1/match/test/schemas.factories" ;
1323import { MatchReportDerivedRow } from "@v1/match/views" ;
14- import type { Knex } from "knex" ;
15- import { InsertSeedSource } from "@db/CustomSeedSource" ;
1624import { afterEach , beforeEach , describe , expect , test } from "vitest" ;
25+ import { matchPlayerRecordFactory , matchRecordFactory } from "./models.factories" ;
1726
18- // const _fakeMatchReport = MatchReport.parse({
19- // guild_id: "19283746",
20- // players: [
21- // {
22- // user_id: "12345678",
23- // win_count: 5,
24- // character: ["Mario"],
25- // },
26- // {
27- // user_id: "87654321",
28- // win_count: 2,
29- // character: ["Kazuya", "Cloud"],
30- // },
31- // ],
32- // });
33- const fakeMatchReport = matchReportFactory ( ) ;
34-
27+ // Nominal fake data that is not inserted should be defined here
28+ const nominal_match_report = matchReportFactory ( {
29+ guild_id : "123456789" ,
30+ players : [
31+ MatchPlayer . parse ( {
32+ user_id : "12345678" ,
33+ win_count : 5 ,
34+ character : [ "Mario" ] ,
35+ } ) ,
36+ MatchPlayer . parse ( {
37+ user_id : "87654321" ,
38+ win_count : 2 ,
39+ character : [ "Kazuya" , "Cloud" ] ,
40+ } ) ,
41+ ] ,
42+ } ) ;
3543
3644beforeEach ( async ( ) => {
3745 await init_tables ( knexDb ) ;
@@ -44,11 +52,16 @@ afterEach(async () => {
4452
4553describe ( "Match table operations" , ( ) => {
4654 describe ( "Use `createMatch`" , ( ) => {
47- describe ( "Random " , ( ) => {
55+ describe ( "Nominal " , ( ) => {
4856 test ( "Insert a Match record with `createMatch`" , async ( ) => {
49- const match_id = await createMatch ( fakeMatchReport . guild_id , knexDb ) ;
50- const created_match = await knexDb ( "Match" ) . first ( ) . where ( { match_id } ) ;
51- expect ( created_match . guild_id ) . toEqual ( fakeMatchReport . guild_id ) ;
57+ const match_id = await createMatch (
58+ nominal_match_report . guild_id ,
59+ knexDb ,
60+ ) ;
61+ const created_match = await knexDb < MatchRecord > ( "Match" )
62+ . first ( )
63+ . where ( { match_id } ) ;
64+ expect ( created_match ?. guild_id ) . toEqual ( nominal_match_report . guild_id ) ;
5265 console . log ( created_match ) ;
5366 } ) ;
5467 } ) ;
@@ -57,48 +70,55 @@ describe("Match table operations", () => {
5770
5871describe ( "MatchPlayer table operations" , ( ) => {
5972 describe ( "Use `createMatchPlayer`" , ( ) => {
60- describe ( "Random" , ( ) => {
61- test ( "Insert a pair of MatchPlayer records" , async ( ) => {
62- const { guild_id } = fakeMatchReport ;
63- const match_id = ( await knexDb ( "Match" ) . insert ( { guild_id } ) ) [ 0 ] ;
73+ let match_record : Omit < MatchRecord , "match_id" | "created_at" > ;
74+ let match_id : number ;
75+ beforeEach ( async ( ) => {
76+ match_record = matchRecordFactory ( { guild_id : "123456789" } ) ;
77+ await knexDb . seed . run ( {
78+ seedSource : new InsertSeedSource ( { Match : [ match_record ] } ) ,
79+ } ) ;
6480
81+ match_id = await knexDb ( "Match" )
82+ . first ( )
83+ . where ( match_record )
84+ . then ( ( res ) => res . match_id ) ;
85+ } ) ;
86+ describe ( "Nominal" , ( ) => {
87+ test ( "Insert a pair of MatchPlayer records" , async ( ) => {
6588 await createMatchPlayer (
6689 match_id ,
67- fakeMatchReport . players [ 0 ] . user_id ,
68- fakeMatchReport . players [ 0 ] . win_count ,
90+ nominal_match_report . players [ 0 ] . user_id ,
91+ nominal_match_report . players [ 0 ] . win_count ,
6992 knexDb ,
7093 ) ;
7194 const created_match_player = await knexDb ( "MatchPlayer" )
7295 . first ( )
7396 . where ( { match_id } ) ;
7497
75- expect ( created_match_player ) . toEqual ( {
98+ expect ( created_match_player ) . toMatchObject ( {
7699 match_id,
77- user_id : fakeMatchReport . players [ 0 ] . user_id ,
78- win_count : fakeMatchReport . players [ 0 ] . win_count ,
100+ user_id : nominal_match_report . players [ 0 ] . user_id ,
101+ win_count : nominal_match_report . players [ 0 ] . win_count ,
79102 } ) ;
80103 } ) ;
81104 } ) ;
82105 describe ( "Negative" , ( ) => {
83106 describe ( "Illegal insertions of MatchPlayer" , ( ) => {
84107 test . fails ( "Cannot insert MatchPlayer where [match_id, user_id] is not unique" , async ( ) => {
85- const { guild_id } = fakeMatchReport ;
86- const match_id = ( await knexDb ( "Match" ) . insert ( { guild_id } ) ) [ 0 ] ;
87-
88108 for ( let p_i = 0 ; p_i < 2 ; p_i ++ ) {
89109 await createMatchPlayer (
90110 match_id ,
91- fakeMatchReport . players [ 0 ] . user_id ,
92- fakeMatchReport . players [ 0 ] . win_count ,
111+ nominal_match_report . players [ 0 ] . user_id ,
112+ nominal_match_report . players [ 0 ] . win_count ,
93113 knexDb ,
94114 ) ;
95115 }
96116 } ) ;
97117 test . fails ( "Cannot insert MatchPlayer where there is no Match record with matching match_id" , async ( ) => {
98118 await createMatchPlayer (
99119 3000 ,
100- fakeMatchReport . players [ 0 ] . user_id ,
101- fakeMatchReport . players [ 0 ] . win_count ,
120+ nominal_match_report . players [ 0 ] . user_id ,
121+ nominal_match_report . players [ 0 ] . win_count ,
102122 knexDb ,
103123 ) ;
104124 } ) ;
@@ -109,32 +129,78 @@ describe("MatchPlayer table operations", () => {
109129
110130describe ( "MatchCharacter table operations" , ( ) => {
111131 describe ( "Use `createMatchCharacter`" , ( ) => {
112- describe ( "Random" , ( ) => {
113- test ( "Insert a pair of MatchCharacter records" , async ( ) => {
114- const { guild_id } = fakeMatchReport ;
115- const match_id = ( await knexDb ( "Match" ) . insert ( { guild_id } ) ) [ 0 ] ;
132+ let match_record : Omit < MatchRecord , "match_id" | "created_at" > ;
133+ let match_player_record_1 : MatchPlayerRecord ;
134+ let match_player_record_2 : MatchPlayerRecord ;
135+ let match_id : number ;
136+ let match_player_id_1 : string ;
137+ let match_player_id_2 : string ;
116138
117- for ( let p_i = 0 ; p_i < fakeMatchReport . players . length ; p_i ++ ) {
118- const { user_id, win_count, character } =
119- fakeMatchReport . players [ p_i ] ;
120- await createMatchPlayer ( match_id , user_id , win_count , knexDb ) ;
121- for ( let c_i = 0 ; c_i < character . length ; c_i ++ ) {
122- await createMatchCharacter (
123- match_id ,
124- user_id ,
125- character [ c_i ] ,
126- knexDb ,
127- ) ;
128- }
139+ let match_player_characters_1 : Array < SSBUCharFighterNumber > ;
140+ let match_player_characters_2 : Array < SSBUCharFighterNumber > ;
141+ beforeEach ( async ( ) => {
142+ match_record = matchRecordFactory ( {
143+ guild_id : nominal_match_report . guild_id ,
144+ } ) ;
145+ await knexDb . seed . run ( {
146+ seedSource : new InsertSeedSource ( {
147+ Match : [ match_record ] ,
148+ } ) ,
149+ } ) ;
150+
151+ match_id = await knexDb ( "Match" )
152+ . first ( )
153+ . where ( match_record )
154+ . then ( ( res ) => res . match_id ) ;
155+ match_player_record_1 = matchPlayerRecordFactory ( {
156+ match_id,
157+ user_id : nominal_match_report . players [ 0 ] . user_id ,
158+ win_count : nominal_match_report . players [ 0 ] . win_count ,
159+ } ) ;
160+ match_player_record_2 = matchPlayerRecordFactory ( {
161+ match_id,
162+ user_id : nominal_match_report . players [ 1 ] . user_id ,
163+ win_count : nominal_match_report . players [ 1 ] . win_count ,
164+ } ) ;
165+ await knexDb . seed . run ( {
166+ seedSource : new InsertSeedSource ( {
167+ MatchPlayer : [ match_player_record_1 , match_player_record_2 ] ,
168+ } ) ,
169+ } ) ;
170+
171+ match_player_id_1 = match_player_record_1 . user_id ;
172+ match_player_id_2 = match_player_record_2 . user_id ;
173+
174+ match_player_characters_1 = nominal_match_report . players [ 0 ] . character ;
175+ match_player_characters_2 = nominal_match_report . players [ 1 ] . character ;
176+ } ) ;
177+ describe ( "Nominal" , ( ) => {
178+ test ( "Insert a pair of MatchCharacter records" , async ( ) => {
179+ for ( let c_i = 0 ; c_i < match_player_characters_1 . length ; c_i ++ ) {
180+ await createMatchCharacter (
181+ match_id ,
182+ match_player_id_1 ,
183+ match_player_characters_1 [ c_i ] ,
184+ knexDb ,
185+ ) ;
186+ }
187+ for ( let c_i = 0 ; c_i < match_player_characters_2 . length ; c_i ++ ) {
188+ await createMatchCharacter (
189+ match_id ,
190+ match_player_id_2 ,
191+ match_player_characters_2 [ c_i ] ,
192+ knexDb ,
193+ ) ;
129194 }
130195
131196 const created = await knexDb < MatchCharacterRecord > ( "MatchCharacter" )
132197 . select ( )
133198 . where ( { match_id } ) ;
134199 const expected = ( ) : Array < MatchCharacterRecord > => {
135200 const result = [ ] ;
136- for ( let p_i = 0 ; p_i < fakeMatchReport . players . length ; p_i ++ ) {
137- const { user_id, character } = fakeMatchReport . players [ p_i ] ;
201+ for ( let p_i = 0 ; p_i < 2 ; p_i ++ ) {
202+ const { user_id, character } =
203+ nominal_match_report . players [ p_i ] ;
138204 for ( let c_i = 0 ; c_i < character . length ; c_i ++ ) {
139205 const fighter_number = character [ c_i ] ;
140206 result . push ( {
@@ -152,25 +218,19 @@ describe("MatchCharacter table operations", () => {
152218
153219 describe ( "Negative" , ( ) => {
154220 test . fails ( "Cannot insert MatchCharacter where [match_id, user_id, fighter_number] is not unique, specifically, `fighter_number`" , async ( ) => {
155- const { guild_id } = fakeMatchReport ;
156- const match_id = ( await knexDb ( "Match" ) . insert ( { guild_id } ) ) [ 0 ] ;
157-
158221 for ( let i = 0 ; i < 2 ; i ++ ) {
159222 await createMatchCharacter (
160223 match_id ,
161- fakeMatchReport . players [ i ] . user_id ,
162- fakeMatchReport . players [ 1 ] . character [ 0 ] ,
224+ nominal_match_report . players [ 0 ] . user_id ,
225+ nominal_match_report . players [ 1 ] . character [ 0 ] ,
163226 knexDb ,
164227 ) ;
165228 }
166229 } ) ;
167230 test . fails ( "Cannot insert MatchCharacter where a fighter_number is not in the SSBUCharTable" , async ( ) => {
168- const { guild_id } = fakeMatchReport ;
169- const { user_id } = fakeMatchReport . players [ 1 ] ;
231+ const { user_id } = nominal_match_report . players [ 1 ] ;
170232
171- const match_id = ( await knexDb ( "Match" ) . insert ( { guild_id } ) ) [ 0 ] ;
172- await knexDb ( "MatchPlayer" ) . insert ( { match_id, user_id } ) ;
173- await createMatchCharacter ( match_id , user_id , 104 , knexDb ) ;
233+ await createMatchCharacter ( match_id , user_id , 404 , knexDb ) ;
174234 } ) ;
175235 } ) ;
176236 } ) ;
@@ -180,9 +240,9 @@ describe("Operations across Match, MatchPlayer, and MatchCharacter tables", () =
180240 describe ( "Use `reportMatch`" , async ( ) => {
181241 describe ( "Random" , ( ) => {
182242 test ( "Report a match" , async ( ) => {
183- const { guild_id } = fakeMatchReport ;
243+ const { guild_id } = nominal_match_report ;
184244
185- const match_id = await reportMatch ( fakeMatchReport , knexDb ) ;
245+ const match_id = await reportMatch ( nominal_match_report , knexDb ) ;
186246
187247 const match_player_records = await knexDb ( "MatchPlayer" )
188248 . select ( )
@@ -206,7 +266,7 @@ describe("Operations across Match, MatchPlayer, and MatchCharacter tables", () =
206266 ) ,
207267 } ) ) ,
208268 } ;
209- expect ( fakeMatchReport ) . toEqual ( {
269+ expect ( nominal_match_report ) . toEqual ( {
210270 ...expected ,
211271 players : expect . arrayContaining (
212272 expected . players . map ( ( player ) => ( {
@@ -222,17 +282,17 @@ describe("Operations across Match, MatchPlayer, and MatchCharacter tables", () =
222282 describe ( "Use `getMatches`" , async ( ) => {
223283 describe ( "Random" , ( ) => {
224284 test ( "Retrieve a derived row from MatchReportView by match_id" , async ( ) => {
225- const match_id = await reportMatch ( fakeMatchReport , knexDb ) ;
226- const { guild_id } = fakeMatchReport ;
285+ const match_id = await reportMatch ( nominal_match_report , knexDb ) ;
286+ const { guild_id } = nominal_match_report ;
227287
228288 const match_query : MatchQuery = { match_id } ;
229289 const result = await getMatches ( match_query , knexDb ) ;
230290 const created_at = result [ 0 ] . created_at ;
231291
232292 const expected = [ ] ;
233- for ( let p_i = 0 ; p_i < fakeMatchReport . players . length ; p_i ++ ) {
293+ for ( let p_i = 0 ; p_i < nominal_match_report . players . length ; p_i ++ ) {
234294 const { user_id, win_count, character } =
235- fakeMatchReport . players [ p_i ] ;
295+ nominal_match_report . players [ p_i ] ;
236296 for ( let c_i = 0 ; c_i < character . length ; c_i ++ ) {
237297 const fighter_number = character [ c_i ] ;
238298 expected . push (
0 commit comments