From 397c5c1550f328924c373b74165c01807ae574bc Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Wed, 26 Nov 2025 14:17:11 +0100 Subject: [PATCH] Added updateGroupOfGame(), added docc & tests --- lib/data/dao/game_dao.dart | 10 +++- lib/data/dao/group_game_dao.dart | 21 +++++++- test/db_tests/group_game_test.dart | 80 +++++++++++++++++++++++------- 3 files changed, 91 insertions(+), 20 deletions(-) diff --git a/lib/data/dao/game_dao.dart b/lib/data/dao/game_dao.dart index ad6b090..6d9f316 100644 --- a/lib/data/dao/game_dao.dart +++ b/lib/data/dao/game_dao.dart @@ -65,6 +65,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { /// Adds a new [Game] to the database. /// Also adds associated players and group if they exist. + /// If a game, player, or group already exists, it will be replaced. Future addGame({required Game game}) async { await db.transaction(() async { await into(gameTable).insert( @@ -89,11 +90,18 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { if (game.group != null) { await db.groupDao.addGroup(group: game.group!); - await db.groupGameDao.addGroupToGame(game.id, game.group!.id); + await db.groupGameDao.addGroupToGame( + gameId: game.id, + groupId: game.group!.id, + ); } }); } + /// Adds multiple [Game]s to the database in a batch operation. + /// Also adds associated players and groups if they exist. + /// If the [games] list is empty, the method returns immediately. + /// If a game, player, or group already exists, it will be replaced. Future addGames({required List games}) async { if (games.isEmpty) return; await db.transaction(() async { diff --git a/lib/data/dao/group_game_dao.dart b/lib/data/dao/group_game_dao.dart index f3ddcc7..3f7a146 100644 --- a/lib/data/dao/group_game_dao.dart +++ b/lib/data/dao/group_game_dao.dart @@ -11,8 +11,12 @@ class GroupGameDao extends DatabaseAccessor GroupGameDao(super.db); /// Associates a group with a game by inserting a record into the - /// [GroupGameTable]. - Future addGroupToGame(String gameId, String groupId) async { + /// [GroupGameTable]. If there is already group associated to the game, + /// it will be replaced. + Future addGroupToGame({ + required String gameId, + required String groupId, + }) async { await into(groupGameTable).insert( GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId), mode: InsertMode.insertOrReplace, @@ -76,4 +80,17 @@ class GroupGameDao extends DatabaseAccessor final rowsAffected = await query.go(); return rowsAffected > 0; } + + /// Updates the group associated with a game to [newGroupId] based on + /// [gameId]. + /// Returns `true` if more than 0 rows were affected, otherwise `false`. + Future updateGroupOfGame({ + required String gameId, + required String newGroupId, + }) async { + final updatedRows = + await (update(groupGameTable)..where((g) => g.gameId.equals(gameId))) + .write(GroupGameTableCompanion(groupId: Value(newGroupId))); + return updatedRows > 0; + } } diff --git a/test/db_tests/group_game_test.dart b/test/db_tests/group_game_test.dart index 1733243..1e9b8fc 100644 --- a/test/db_tests/group_game_test.dart +++ b/test/db_tests/group_game_test.dart @@ -14,7 +14,8 @@ void main() { late Player testPlayer3; late Player testPlayer4; late Player testPlayer5; - late Group testgroup; + late Group testGroup1; + late Group testGroup2; late Game testgameWithGroup; late Game testgameWithPlayers; final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); @@ -35,15 +36,19 @@ void main() { testPlayer3 = Player(name: 'Charlie'); testPlayer4 = Player(name: 'Diana'); testPlayer5 = Player(name: 'Eve'); - testgroup = Group( + testGroup1 = Group( name: 'Test Group', members: [testPlayer1, testPlayer2, testPlayer3], ); + testGroup2 = Group( + name: 'Test Group', + members: [testPlayer3, testPlayer2], + ); testgameWithPlayers = Game( name: 'Test Game with Players', players: [testPlayer4, testPlayer5], ); - testgameWithGroup = Game(name: 'Test Game with Group', group: testgroup); + testgameWithGroup = Game(name: 'Test Game with Group', group: testGroup1); }); }); tearDown(() async { @@ -52,7 +57,7 @@ void main() { group('Group-Game Tests', () { test('Game has group works correctly', () async { await database.gameDao.addGame(game: testgameWithPlayers); - await database.groupDao.addGroup(group: testgroup); + await database.groupDao.addGroup(group: testGroup1); var gameHasGroup = await database.groupGameDao.gameHasGroup( gameId: testgameWithPlayers.id, @@ -61,8 +66,8 @@ void main() { expect(gameHasGroup, false); await database.groupGameDao.addGroupToGame( - testgameWithPlayers.id, - testgroup.id, + gameId: testgameWithPlayers.id, + groupId: testGroup1.id, ); gameHasGroup = await database.groupGameDao.gameHasGroup( @@ -74,15 +79,15 @@ void main() { test('Adding a group to a game works correctly', () async { await database.gameDao.addGame(game: testgameWithPlayers); - await database.groupDao.addGroup(group: testgroup); + await database.groupDao.addGroup(group: testGroup1); await database.groupGameDao.addGroupToGame( - testgameWithPlayers.id, - testgroup.id, + gameId: testgameWithPlayers.id, + groupId: testGroup1.id, ); var groupAdded = await database.groupGameDao.isGroupInGame( gameId: testgameWithPlayers.id, - groupId: testgroup.id, + groupId: testGroup1.id, ); expect(groupAdded, true); @@ -120,14 +125,55 @@ void main() { fail('Group should not be null'); } - expect(group.id, testgroup.id); - expect(group.name, testgroup.name); - expect(group.createdAt, testgroup.createdAt); - expect(group.members.length, testgroup.members.length); + expect(group.id, testGroup1.id); + expect(group.name, testGroup1.name); + expect(group.createdAt, testGroup1.createdAt); + expect(group.members.length, testGroup1.members.length); for (int i = 0; i < group.members.length; i++) { - expect(group.members[i].id, testgroup.members[i].id); - expect(group.members[i].name, testgroup.members[i].name); - expect(group.members[i].createdAt, testgroup.members[i].createdAt); + expect(group.members[i].id, testGroup1.members[i].id); + expect(group.members[i].name, testGroup1.members[i].name); + expect(group.members[i].createdAt, testGroup1.members[i].createdAt); + } + }); + + test('Updating the group of a game works correctly', () async { + await database.gameDao.addGame(game: testgameWithGroup); + + var group = await database.groupGameDao.getGroupOfGame( + gameId: testgameWithGroup.id, + ); + + if (group == null) { + fail('Initial group should not be null'); + } else { + expect(group.id, testGroup1.id); + expect(group.name, testGroup1.name); + expect(group.createdAt, testGroup1.createdAt); + expect(group.members.length, testGroup1.members.length); + } + + await database.groupDao.addGroup(group: testGroup2); + await database.groupGameDao.updateGroupOfGame( + gameId: testgameWithGroup.id, + newGroupId: testGroup2.id, + ); + + group = await database.groupGameDao.getGroupOfGame( + gameId: testgameWithGroup.id, + ); + + if (group == null) { + fail('Updated group should not be null'); + } else { + expect(group.id, testGroup2.id); + expect(group.name, testGroup2.name); + expect(group.createdAt, testGroup2.createdAt); + expect(group.members.length, testGroup2.members.length); + for (int i = 0; i < group.members.length; i++) { + expect(group.members[i].id, testGroup2.members[i].id); + expect(group.members[i].name, testGroup2.members[i].name); + expect(group.members[i].createdAt, testGroup2.members[i].createdAt); + } } }); });