From 356cb1fb4316c2c6e64ea696c48c19482a89dd71 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Tue, 30 Dec 2025 18:21:39 +0100 Subject: [PATCH 1/5] Updated insert mode to fix problem with replacing existing player match connections --- lib/data/dao/group_match_dao.dart | 2 +- lib/data/dao/player_match_dao.dart | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/data/dao/group_match_dao.dart b/lib/data/dao/group_match_dao.dart index 3c16c83..d428fb5 100644 --- a/lib/data/dao/group_match_dao.dart +++ b/lib/data/dao/group_match_dao.dart @@ -21,7 +21,7 @@ class GroupMatchDao extends DatabaseAccessor } await into(groupMatchTable).insert( GroupMatchTableCompanion.insert(groupId: groupId, matchId: matchId), - mode: InsertMode.insertOrReplace, + mode: InsertMode.insertOrIgnore, ); } diff --git a/lib/data/dao/player_match_dao.dart b/lib/data/dao/player_match_dao.dart index 9e242b7..7ebaee6 100644 --- a/lib/data/dao/player_match_dao.dart +++ b/lib/data/dao/player_match_dao.dart @@ -18,7 +18,7 @@ class PlayerMatchDao extends DatabaseAccessor }) async { await into(playerMatchTable).insert( PlayerMatchTableCompanion.insert(playerId: playerId, matchId: matchId), - mode: InsertMode.insertOrReplace, + mode: InsertMode.insertOrIgnore, ); } @@ -121,7 +121,7 @@ class PlayerMatchDao extends DatabaseAccessor inserts.map( (c) => into( playerMatchTable, - ).insert(c, mode: InsertMode.insertOrReplace), + ).insert(c, mode: InsertMode.insertOrIgnore), ), ); } From b8b65c4ca18cd4bcfccc3b040c51a06910287909 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Tue, 30 Dec 2025 18:23:15 +0100 Subject: [PATCH 2/5] Removed lines that added the group and players to the db again in addMatch() --- lib/data/dao/match_dao.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 32bd323..76c3709 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -80,7 +80,6 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { ); if (match.players != null) { - await db.playerDao.addPlayersAsList(players: match.players!); for (final p in match.players ?? []) { await db.playerMatchDao.addPlayerToMatch( matchId: match.id, @@ -90,7 +89,6 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { } if (match.group != null) { - await db.groupDao.addGroup(group: match.group!); await db.groupMatchDao.addGroupToMatch( matchId: match.id, groupId: match.group!.id, @@ -186,7 +184,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { matchId: match.id, playerId: p.id, ), - mode: InsertMode.insertOrReplace, + mode: InsertMode.insertOrIgnore, ); } } @@ -204,7 +202,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { playerId: m.id, groupId: match.group!.id, ), - mode: InsertMode.insertOrReplace, + mode: InsertMode.insertOrIgnore, ); } } @@ -221,7 +219,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { matchId: match.id, groupId: match.group!.id, ), - mode: InsertMode.insertOrReplace, + mode: InsertMode.insertOrIgnore, ); } } From 9cb35dc4a1fd5676073641f80364d5728f896238 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Tue, 30 Dec 2025 18:45:28 +0100 Subject: [PATCH 3/5] Updated comments --- lib/data/dao/match_dao.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 76c3709..b56a38b 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -65,8 +65,9 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { ); } - /// Adds a new [Match] to the database. - /// Also adds associated players and group if they exist. + /// Adds a new [Match] to the database. Also adds players and group + /// associations. This method assumes that the players and groups added to + /// this match are already present in the database. Future addMatch({required Match match}) async { await db.transaction(() async { await into(matchTable).insert( @@ -100,6 +101,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { /// Adds multiple [Match]s to the database in a batch operation. /// Also adds associated players and groups if they exist. /// If the [matches] list is empty, the method returns immediately. + /// This Method should only be used to import matches from a different device. Future addMatchAsList({required List matches}) async { if (matches.isEmpty) return; await db.transaction(() async { From e19f6967141a3ee586a696f681f6c7b7271a686d Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Tue, 30 Dec 2025 18:51:57 +0100 Subject: [PATCH 4/5] Fixed tests with adding players and groups to database on setup --- test/db_tests/game_test.dart | 12 +++++++++++- test/db_tests/group_game_test.dart | 12 +++++++++++- test/db_tests/player_game_test.dart | 13 ++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/test/db_tests/game_test.dart b/test/db_tests/game_test.dart index ca86a60..d750156 100644 --- a/test/db_tests/game_test.dart +++ b/test/db_tests/game_test.dart @@ -23,7 +23,7 @@ void main() { final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); final fakeClock = Clock(() => fixedDate); - setUp(() { + setUp(() async { database = AppDatabase( DatabaseConnection( NativeDatabase.memory(), @@ -68,6 +68,16 @@ void main() { group: testGroup2, ); }); + await database.playerDao.addPlayersAsList( + players: [ + testPlayer1, + testPlayer2, + testPlayer3, + testPlayer4, + testPlayer5, + ], + ); + await database.groupDao.addGroupsAsList(groups: [testGroup1, testGroup2]); }); tearDown(() async { await database.close(); diff --git a/test/db_tests/group_game_test.dart b/test/db_tests/group_game_test.dart index a4fa146..06d201b 100644 --- a/test/db_tests/group_game_test.dart +++ b/test/db_tests/group_game_test.dart @@ -21,7 +21,7 @@ void main() { final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); final fakeClock = Clock(() => fixedDate); - setUp(() { + setUp(() async { database = AppDatabase( DatabaseConnection( NativeDatabase.memory(), @@ -53,6 +53,16 @@ void main() { group: testGroup1, ); }); + await database.playerDao.addPlayersAsList( + players: [ + testPlayer1, + testPlayer2, + testPlayer3, + testPlayer4, + testPlayer5, + ], + ); + await database.groupDao.addGroupsAsList(groups: [testGroup1, testGroup2]); }); tearDown(() async { await database.close(); diff --git a/test/db_tests/player_game_test.dart b/test/db_tests/player_game_test.dart index e2501eb..71e4088 100644 --- a/test/db_tests/player_game_test.dart +++ b/test/db_tests/player_game_test.dart @@ -21,7 +21,7 @@ void main() { final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); final fakeClock = Clock(() => fixedDate); - setUp(() { + setUp(() async { database = AppDatabase( DatabaseConnection( NativeDatabase.memory(), @@ -50,6 +50,17 @@ void main() { players: [testPlayer4, testPlayer5, testPlayer6], ); }); + await database.playerDao.addPlayersAsList( + players: [ + testPlayer1, + testPlayer2, + testPlayer3, + testPlayer4, + testPlayer5, + testPlayer6, + ], + ); + await database.groupDao.addGroup(group: testgroup); }); tearDown(() async { await database.close(); From c1789458e0a7193d093380b058375ddd7e62d5de Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Tue, 30 Dec 2025 20:56:39 +0100 Subject: [PATCH 5/5] Added tests for bug causing behaviour --- test/db_tests/game_test.dart | 2 +- test/db_tests/group_game_test.dart | 30 +++++++++++++++++++++- test/db_tests/player_game_test.dart | 39 ++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/test/db_tests/game_test.dart b/test/db_tests/game_test.dart index d750156..0ec2cfc 100644 --- a/test/db_tests/game_test.dart +++ b/test/db_tests/game_test.dart @@ -263,7 +263,7 @@ void main() { expect(matchCount, 0); }); - test('Checking if match has winner works correclty', () async { + test('Checking if match has winner works correctly', () async { await database.matchDao.addMatch(match: testMatch1); await database.matchDao.addMatch(match: testMatchOnlyGroup); diff --git a/test/db_tests/group_game_test.dart b/test/db_tests/group_game_test.dart index 06d201b..7d812bd 100644 --- a/test/db_tests/group_game_test.dart +++ b/test/db_tests/group_game_test.dart @@ -1,5 +1,5 @@ import 'package:clock/clock.dart'; -import 'package:drift/drift.dart'; +import 'package:drift/drift.dart' hide isNotNull; import 'package:drift/native.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:game_tracker/data/db/database.dart'; @@ -189,5 +189,33 @@ void main() { } } }); + + test('Adding the same group to seperate matches works correctly', () async { + final match1 = Match(name: 'Match 1', group: testGroup1); + final match2 = Match(name: 'Match 2', group: testGroup1); + + await Future.wait([ + database.matchDao.addMatch(match: match1), + database.matchDao.addMatch(match: match2), + ]); + + final group1 = await database.groupMatchDao.getGroupOfMatch( + matchId: match1.id, + ); + final group2 = await database.groupMatchDao.getGroupOfMatch( + matchId: match2.id, + ); + + expect(group1, isNotNull); + expect(group2, isNotNull); + + final groups = [group1!, group2!]; + for (final group in groups) { + expect(group.members.length, testGroup1.members.length); + expect(group.id, testGroup1.id); + expect(group.name, testGroup1.name); + expect(group.createdAt, testGroup1.createdAt); + } + }); }); } diff --git a/test/db_tests/player_game_test.dart b/test/db_tests/player_game_test.dart index 71e4088..8a4f569 100644 --- a/test/db_tests/player_game_test.dart +++ b/test/db_tests/player_game_test.dart @@ -1,5 +1,5 @@ import 'package:clock/clock.dart'; -import 'package:drift/drift.dart'; +import 'package:drift/drift.dart' hide isNotNull; import 'package:drift/native.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:game_tracker/data/db/database.dart'; @@ -196,5 +196,42 @@ void main() { expect(player.createdAt, testPlayer.createdAt); } }); + + test( + 'Adding the same player to seperate matches works correctly', + () async { + final playersList = [testPlayer1, testPlayer2, testPlayer3]; + final match1 = Match(name: 'Match 1', players: playersList); + final match2 = Match(name: 'Match 2', players: playersList); + + await Future.wait([ + database.matchDao.addMatch(match: match1), + database.matchDao.addMatch(match: match2), + ]); + + final players1 = await database.playerMatchDao.getPlayersOfMatch( + matchId: match1.id, + ); + final players2 = await database.playerMatchDao.getPlayersOfMatch( + matchId: match2.id, + ); + + expect(players1, isNotNull); + expect(players2, isNotNull); + + expect( + players1!.map((p) => p.id).toList(), + equals(players2!.map((p) => p.id).toList()), + ); + expect( + players1.map((p) => p.name).toList(), + equals(players2.map((p) => p.name).toList()), + ); + expect( + players1.map((p) => p.createdAt).toList(), + equals(players2.map((p) => p.createdAt).toList()), + ); + }, + ); }); }