diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 93df7d7..3df8b9d 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -15,74 +15,13 @@ part 'match_dao.g.dart'; class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { MatchDao(super.db); - /// Retrieves all matches from the database. - Future> getAllMatches() async { - final query = select(matchTable); - final result = await query.get(); - - return Future.wait( - result.map((row) async { - final game = await db.gameDao.getGameById(gameId: row.gameId); - Group? group; - if (row.groupId != null) { - group = await db.groupDao.getGroupById(groupId: row.groupId!); - } - final players = - await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? []; - - final scores = await db.scoreEntryDao.getAllMatchScores( - matchId: row.id, - ); - - return Match( - id: row.id, - name: row.name, - game: game, - group: group, - players: players, - notes: row.notes ?? '', - createdAt: row.createdAt, - endedAt: row.endedAt, - scores: scores, - ); - }), - ); - } - - /// Retrieves a [Match] by its [matchId]. - Future getMatchById({required String matchId}) async { - final query = select(matchTable)..where((g) => g.id.equals(matchId)); - final result = await query.getSingle(); - - final game = await db.gameDao.getGameById(gameId: result.gameId); - - Group? group; - if (result.groupId != null) { - group = await db.groupDao.getGroupById(groupId: result.groupId!); - } - - final players = - await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; - - final scores = await db.scoreEntryDao.getAllMatchScores(matchId: matchId); - - return Match( - id: result.id, - name: result.name, - game: game, - group: group, - players: players, - notes: result.notes ?? '', - createdAt: result.createdAt, - endedAt: result.endedAt, - scores: scores, - ); - } + /* Create */ /// Adds a new [Match] to the database. Also adds players associations. /// This method assumes that the game and group (if any) are already present /// in the database. - Future addMatch({required Match match}) async { + Future addMatch({required Match match}) async { + if (await matchExists(matchId: match.id)) return false; await db.transaction(() async { await into(matchTable).insert( MatchTableCompanion.insert( @@ -114,15 +53,17 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { ); } } + print('Return true'); }); + return true; } /// Adds multiple [Match]es 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; + Future addMatchesAsList({required List matches}) async { + if (matches.isEmpty) return false; await db.transaction(() async { // Add all games first (deduplicated) final uniqueGames = {}; @@ -280,14 +221,17 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { } }); }); + return true; } - /// Deletes the match with the given [matchId] from the database. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future deleteMatch({required String matchId}) async { - final query = delete(matchTable)..where((g) => g.id.equals(matchId)); - final rowsAffected = await query.go(); - return rowsAffected > 0; + /* Read */ + + /// Checks if a match with the given [matchId] exists in the database. + /// Returns `true` if the match exists, otherwise `false`. + Future matchExists({required String matchId}) async { + final query = select(matchTable)..where((g) => g.id.equals(matchId)); + final result = await query.getSingleOrNull(); + return result != null; } /// Retrieves the number of matches in the database. @@ -299,6 +243,70 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return count ?? 0; } + /// Retrieves all matches from the database. + Future> getAllMatches() async { + final query = select(matchTable); + final result = await query.get(); + + return Future.wait( + result.map((row) async { + final game = await db.gameDao.getGameById(gameId: row.gameId); + Group? group; + if (row.groupId != null) { + group = await db.groupDao.getGroupById(groupId: row.groupId!); + } + final players = + await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? []; + + final scores = await db.scoreEntryDao.getAllMatchScores( + matchId: row.id, + ); + + return Match( + id: row.id, + name: row.name, + game: game, + group: group, + players: players, + notes: row.notes ?? '', + createdAt: row.createdAt, + endedAt: row.endedAt, + scores: scores, + ); + }), + ); + } + + /// Retrieves a [Match] by its [matchId]. + Future getMatchById({required String matchId}) async { + final query = select(matchTable)..where((g) => g.id.equals(matchId)); + final result = await query.getSingle(); + + final game = await db.gameDao.getGameById(gameId: result.gameId); + + Group? group; + if (result.groupId != null) { + group = await db.groupDao.getGroupById(groupId: result.groupId!); + } + + final players = + await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; + + final scores = await db.scoreEntryDao.getAllMatchScores(matchId: matchId); + + return Match( + id: result.id, + name: result.name, + game: game, + group: group, + players: players, + notes: result.notes ?? '', + createdAt: result.createdAt, + endedAt: result.endedAt, + scores: scores, + ); + } + /// Retrieves all matches associated with the given [groupId]. /// Queries the database directly, filtering by [groupId]. Future> getGroupMatches({required String groupId}) async { @@ -325,34 +333,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { ); } - /// Checks if a match with the given [matchId] exists in the database. - /// Returns `true` if the match exists, otherwise `false`. - Future matchExists({required String matchId}) async { - final query = select(matchTable)..where((g) => g.id.equals(matchId)); - final result = await query.getSingleOrNull(); - return result != null; - } - - /// Deletes all matches from the database. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future deleteAllMatches() async { - final query = delete(matchTable); - final rowsAffected = await query.go(); - return rowsAffected > 0; - } - - /// Updates the notes of the match with the given [matchId]. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future updateMatchNotes({ - required String matchId, - required String? notes, - }) async { - final query = update(matchTable)..where((g) => g.id.equals(matchId)); - final rowsAffected = await query.write( - MatchTableCompanion(notes: Value(notes)), - ); - return rowsAffected > 0; - } + /* Update */ /// Changes the name of the match with the given [matchId] to [newName]. /// Returns `true` if more than 0 rows were affected, otherwise `false`. @@ -367,19 +348,6 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return rowsAffected > 0; } - /// Updates the game of the match with the given [matchId]. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future updateMatchGame({ - required String matchId, - required String gameId, - }) async { - final query = update(matchTable)..where((g) => g.id.equals(matchId)); - final rowsAffected = await query.write( - MatchTableCompanion(gameId: Value(gameId)), - ); - return rowsAffected > 0; - } - /// Updates the group of the match with the given [matchId]. /// Replaces the existing group association with the new group specified by [newGroupId]. /// Pass null to remove the group association. @@ -395,6 +363,19 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return rowsAffected > 0; } + /// Updates the notes of the match with the given [matchId]. + /// Returns `true` if more than 0 rows were affected, otherwise `false`. + Future updateMatchNotes({ + required String matchId, + required String notes, + }) async { + final query = update(matchTable)..where((g) => g.id.equals(matchId)); + final rowsAffected = await query.write( + MatchTableCompanion(notes: Value(notes)), + ); + return rowsAffected > 0; + } + /// Removes the group association of the match with the given [matchId]. /// Sets the groupId to null. /// Returns `true` if more than 0 rows were affected, otherwise `false`. @@ -406,25 +387,12 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return rowsAffected > 0; } - /// Updates the createdAt timestamp of the match with the given [matchId]. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future updateMatchCreatedAt({ - required String matchId, - required DateTime createdAt, - }) async { - final query = update(matchTable)..where((g) => g.id.equals(matchId)); - final rowsAffected = await query.write( - MatchTableCompanion(createdAt: Value(createdAt)), - ); - return rowsAffected > 0; - } - /// Updates the endedAt timestamp of the match with the given [matchId]. /// Pass null to remove the ended time (mark match as ongoing). /// Returns `true` if more than 0 rows were affected, otherwise `false`. Future updateMatchEndedAt({ required String matchId, - required DateTime? endedAt, + required DateTime endedAt, }) async { final query = update(matchTable)..where((g) => g.id.equals(matchId)); final rowsAffected = await query.write( @@ -436,7 +404,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { /// Replaces all players in a match with the provided list of players. /// Removes all existing players from the match and adds the new players. /// Also adds any new players to the player table if they don't exist. - Future replaceMatchPlayers({ + Future updateMatchPlayers({ required String matchId, required List newPlayers, }) async { @@ -466,4 +434,22 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { ); }); } + + /* Delete */ + + /// Deletes the match with the given [matchId] from the database. + /// Returns `true` if more than 0 rows were affected, otherwise `false`. + Future deleteMatch({required String matchId}) async { + final query = delete(matchTable)..where((g) => g.id.equals(matchId)); + final rowsAffected = await query.go(); + return rowsAffected > 0; + } + + /// Deletes all matches from the database. + /// Returns `true` if more than 0 rows were affected, otherwise `false`. + Future deleteAllMatches() async { + final query = delete(matchTable); + final rowsAffected = await query.go(); + return rowsAffected > 0; + } } diff --git a/lib/services/data_transfer_service.dart b/lib/services/data_transfer_service.dart index daf4768..e47b220 100644 --- a/lib/services/data_transfer_service.dart +++ b/lib/services/data_transfer_service.dart @@ -143,7 +143,7 @@ class DataTransferService { await db.gameDao.addGamesAsList(games: importedGames); await db.groupDao.addGroupsAsList(groups: importedGroups); await db.teamDao.addTeamsAsList(teams: importedTeams); - await db.matchDao.addMatchAsList(matches: importedMatches); + await db.matchDao.addMatchesAsList(matches: importedMatches); } /* Parsing Methods */ diff --git a/test/db_tests/aggregates/match_test.dart b/test/db_tests/aggregates/match_test.dart index 3305b9a..a056b2c 100644 --- a/test/db_tests/aggregates/match_test.dart +++ b/test/db_tests/aggregates/match_test.dart @@ -101,212 +101,221 @@ void main() { }); group('Match Tests', () { - // Verifies that a single match can be added and retrieved with all fields, group, and players intact. - test('Adding and fetching single match works correctly', () async { - await database.matchDao.addMatch(match: testMatch1); + group('CREATE', () { + test('Adding and fetching single match works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); - final result = await database.matchDao.getMatchById( - matchId: testMatch1.id, - ); + final result = await database.matchDao.getMatchById( + matchId: testMatch1.id, + ); - expect(result.id, testMatch1.id); - expect(result.name, testMatch1.name); - expect(result.createdAt, testMatch1.createdAt); + expect(result.id, testMatch1.id); + expect(result.name, testMatch1.name); + expect(result.createdAt, testMatch1.createdAt); - if (result.group != null) { - expect(result.group!.members.length, testGroup1.members.length); + if (result.group != null) { + expect(result.group!.members.length, testGroup1.members.length); - for (int i = 0; i < testGroup1.members.length; i++) { - expect(result.group!.members[i].id, testGroup1.members[i].id); - expect(result.group!.members[i].name, testGroup1.members[i].name); - } - } else { - fail('Group is null'); - } - expect(result.players.length, testMatch1.players.length); - - for (int i = 0; i < testMatch1.players.length; i++) { - expect(result.players[i].id, testMatch1.players[i].id); - expect(result.players[i].name, testMatch1.players[i].name); - expect(result.players[i].createdAt, testMatch1.players[i].createdAt); - } - }); - - // Verifies that multiple matches can be added and retrieved with correct groups and players. - test('Adding and fetching multiple matches works correctly', () async { - await database.matchDao.addMatchAsList( - matches: [ - testMatch1, - testMatch2, - testMatchOnlyGroup, - testMatchOnlyPlayers, - ], - ); - - final allMatches = await database.matchDao.getAllMatches(); - expect(allMatches.length, 4); - - final testMatches = { - testMatch1.id: testMatch1, - testMatch2.id: testMatch2, - testMatchOnlyGroup.id: testMatchOnlyGroup, - testMatchOnlyPlayers.id: testMatchOnlyPlayers, - }; - - for (final match in allMatches) { - final testMatch = testMatches[match.id]!; - - // Match-Checks - expect(match.id, testMatch.id); - expect(match.name, testMatch.name); - expect(match.createdAt, testMatch.createdAt); - - // Group-Checks - if (testMatch.group != null) { - expect(match.group!.id, testMatch.group!.id); - expect(match.group!.name, testMatch.group!.name); - expect(match.group!.createdAt, testMatch.group!.createdAt); - - // Group Members-Checks - expect(match.group!.members.length, testMatch.group!.members.length); - for (int i = 0; i < testMatch.group!.members.length; i++) { - expect(match.group!.members[i].id, testMatch.group!.members[i].id); - expect( - match.group!.members[i].name, - testMatch.group!.members[i].name, - ); - expect( - match.group!.members[i].createdAt, - testMatch.group!.members[i].createdAt, - ); + for (int i = 0; i < testGroup1.members.length; i++) { + expect(result.group!.members[i].id, testGroup1.members[i].id); + expect(result.group!.members[i].name, testGroup1.members[i].name); } } else { - expect(match.group, null); + fail('Group is null'); } + expect(result.players.length, testMatch1.players.length); - // Players-Checks - expect(match.players.length, testMatch.players.length); - for (int i = 0; i < testMatch.players.length; i++) { - expect(match.players[i].id, testMatch.players[i].id); - expect(match.players[i].name, testMatch.players[i].name); - expect(match.players[i].createdAt, testMatch.players[i].createdAt); + for (int i = 0; i < testMatch1.players.length; i++) { + expect(result.players[i].id, testMatch1.players[i].id); + expect(result.players[i].name, testMatch1.players[i].name); + expect(result.players[i].createdAt, testMatch1.players[i].createdAt); } - } + }); + + test('Adding and fetching multiple matches works correctly', () async { + await database.matchDao.addMatchesAsList( + matches: [ + testMatch1, + testMatch2, + testMatchOnlyGroup, + testMatchOnlyPlayers, + ], + ); + + final allMatches = await database.matchDao.getAllMatches(); + expect(allMatches.length, 4); + + final testMatches = { + testMatch1.id: testMatch1, + testMatch2.id: testMatch2, + testMatchOnlyGroup.id: testMatchOnlyGroup, + testMatchOnlyPlayers.id: testMatchOnlyPlayers, + }; + + for (final match in allMatches) { + final testMatch = testMatches[match.id]!; + + // Match-Checks + expect(match.id, testMatch.id); + expect(match.name, testMatch.name); + expect(match.createdAt, testMatch.createdAt); + + // Group-Checks + if (testMatch.group != null) { + expect(match.group!.id, testMatch.group!.id); + expect(match.group!.name, testMatch.group!.name); + expect(match.group!.createdAt, testMatch.group!.createdAt); + + // Group Members-Checks + expect( + match.group!.members.length, + testMatch.group!.members.length, + ); + for (int i = 0; i < testMatch.group!.members.length; i++) { + expect( + match.group!.members[i].id, + testMatch.group!.members[i].id, + ); + expect( + match.group!.members[i].name, + testMatch.group!.members[i].name, + ); + expect( + match.group!.members[i].createdAt, + testMatch.group!.members[i].createdAt, + ); + } + } else { + expect(match.group, null); + } + + // Players-Checks + expect(match.players.length, testMatch.players.length); + for (int i = 0; i < testMatch.players.length; i++) { + expect(match.players[i].id, testMatch.players[i].id); + expect(match.players[i].name, testMatch.players[i].name); + expect(match.players[i].createdAt, testMatch.players[i].createdAt); + } + } + }); + + test('addMatch() ignores duplicate games', () async { + var added = await database.matchDao.addMatch(match: testMatch1); + expect(added, isTrue); + + added = await database.matchDao.addMatch(match: testMatch1); + expect(added, isFalse); + + final matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 1); + }); + + test('addMatchesAsList() returns isFalse for empty list', () async { + var added = await database.matchDao.addMatchesAsList(matches: []); + expect(added, isFalse); + }); + + test('addMatchesAsList() ignores duplicate games', () async { + final added = await database.matchDao.addMatchesAsList( + matches: [testMatch1, testMatch2, testMatch1], + ); + expect(added, isTrue); + + final matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 2); + }); }); - // Verifies that adding the same match twice does not create duplicates. - test('Adding the same match twice does not create duplicates', () async { - await database.matchDao.addMatch(match: testMatch1); - await database.matchDao.addMatch(match: testMatch1); + group('READ', () { + test('matchExists() works correctly', () async { + var matchExists = await database.matchDao.matchExists( + matchId: testMatch1.id, + ); + expect(matchExists, isFalse); - final matchCount = await database.matchDao.getMatchCount(); - expect(matchCount, 1); + await database.matchDao.addMatch(match: testMatch1); + + matchExists = await database.matchDao.matchExists( + matchId: testMatch1.id, + ); + expect(matchExists, isTrue); + }); + + test('getGroupMatches() works correctly', () async { + var matches = await database.matchDao.getGroupMatches( + groupId: 'non-existing-id', + ); + + expect(matches, isEmpty); + + await database.matchDao.addMatch(match: testMatch1); + matches = await database.matchDao.getGroupMatches( + groupId: testGroup1.id, + ); + expect(matches, isNotEmpty); + + final match = matches.first; + expect(match.id, testMatch1.id); + expect(match.group, isNotNull); + expect(match.group!.id, testGroup1.id); + }); }); - // Verifies that matchExists returns correct boolean based on match presence. - test('Match existence check works correctly', () async { - var matchExists = await database.matchDao.matchExists( - matchId: testMatch1.id, - ); - expect(matchExists, false); + group('UPDATE', () { + test('updateMatchName() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); - await database.matchDao.addMatch(match: testMatch1); + const newName = 'New name'; + await database.matchDao.updateMatchName( + matchId: testMatch1.id, + newName: newName, + ); - matchExists = await database.matchDao.matchExists(matchId: testMatch1.id); - expect(matchExists, true); - }); + final fetchedMatch = await database.matchDao.getMatchById( + matchId: testMatch1.id, + ); + expect(fetchedMatch.name, newName); + }); - // Verifies that deleteMatch removes the match and returns true. - test('Deleting a match works correctly', () async { - await database.matchDao.addMatch(match: testMatch1); + test('updateMatchName() does nothing for non-existent match', () async { + final updated = await database.matchDao.updateMatchName( + matchId: 'non-existing-id', + newName: 'New Name', + ); + expect(updated, isFalse); - final matchDeleted = await database.matchDao.deleteMatch( - matchId: testMatch1.id, - ); - expect(matchDeleted, true); + final allMatches = await database.matchDao.getAllMatches(); + expect(allMatches, isEmpty); + }); - final matchExists = await database.matchDao.matchExists( - matchId: testMatch1.id, - ); - expect(matchExists, false); - }); + test('updateMatchGroup() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + await database.groupDao.addGroup(group: testGroup2); - // Verifies that getMatchCount returns correct count through add/delete operations. - test('Getting the match count works correctly', () async { - var matchCount = await database.matchDao.getMatchCount(); - expect(matchCount, 0); + await database.matchDao.updateMatchGroup( + matchId: testMatch1.id, + newGroupId: testGroup2.id, + ); - await database.matchDao.addMatch(match: testMatch1); + final fetchedMatch = await database.matchDao.getMatchById( + matchId: testMatch1.id, + ); + expect(fetchedMatch.group?.id, testGroup2.id); + }); - matchCount = await database.matchDao.getMatchCount(); - expect(matchCount, 1); + test('updateMatchGroup() does nothing for non-existent match', () async { + final updated = await database.matchDao.updateMatchGroup( + matchId: 'non-existing-id', + newGroupId: 'group-id', + ); + expect(updated, isFalse); - await database.matchDao.addMatch(match: testMatch2); + final allMatches = await database.matchDao.getAllMatches(); + expect(allMatches, isEmpty); + }); - matchCount = await database.matchDao.getMatchCount(); - expect(matchCount, 2); - - await database.matchDao.deleteMatch(matchId: testMatch1.id); - - matchCount = await database.matchDao.getMatchCount(); - expect(matchCount, 1); - - await database.matchDao.deleteMatch(matchId: testMatch2.id); - - matchCount = await database.matchDao.getMatchCount(); - expect(matchCount, 0); - }); - - // Verifies that updateMatchName correctly updates only the name field. - test('Renaming a match works correctly', () async { - await database.matchDao.addMatch(match: testMatch1); - - var fetchedMatch = await database.matchDao.getMatchById( - matchId: testMatch1.id, - ); - expect(fetchedMatch.name, testMatch1.name); - - const newName = 'Updated Match Name'; - await database.matchDao.updateMatchName( - matchId: testMatch1.id, - newName: newName, - ); - - fetchedMatch = await database.matchDao.getMatchById( - matchId: testMatch1.id, - ); - expect(fetchedMatch.name, newName); - }); - - test('Fetching a winner works correctly', () async { - await database.matchDao.addMatch(match: testMatch1); - - var fetchedMatch = await database.matchDao.getMatchById( - matchId: testMatch1.id, - ); - - expect(fetchedMatch.mvp, isNotNull); - expect(fetchedMatch.mvp.first.id, testPlayer4.id); - }); - - test('Setting a winner works correctly', () async { - await database.matchDao.addMatch(match: testMatch1); - - await database.scoreEntryDao.setWinner( - matchId: testMatch1.id, - playerId: testPlayer5.id, - ); - - final fetchedMatch = await database.matchDao.getMatchById( - matchId: testMatch1.id, - ); - expect(fetchedMatch.mvp, isNotNull); - expect(fetchedMatch.mvp.first.id, testPlayer5.id); - }); - - test( - 'removeMatchGroup removes group from match with existing group', - () async { + test('removeMatchGroup() works correctly', () async { + expect(testMatch1.group, isNotNull); await database.matchDao.addMatch(match: testMatch1); final removed = await database.matchDao.removeMatchGroup( @@ -318,53 +327,149 @@ void main() { matchId: testMatch1.id, ); expect(updatedMatch.group, null); - expect(updatedMatch.game.id, testMatch1.game.id); - expect(updatedMatch.name, testMatch1.name); - expect(updatedMatch.notes, testMatch1.notes); - }, - ); + }); - test( - 'removeMatchGroup on match that already has no group still succeeds', - () async { - await database.matchDao.addMatch(match: testMatchOnlyPlayers); + test( + 'removeMatchGroup() on match that already has no group still succeeds', + () async { + await database.matchDao.addMatch(match: testMatchOnlyPlayers); - final removed = await database.matchDao.removeMatchGroup( - matchId: testMatchOnlyPlayers.id, - ); - expect(removed, isTrue); + final removed = await database.matchDao.removeMatchGroup( + matchId: testMatchOnlyPlayers.id, + ); + expect(removed, isTrue); - final updatedMatch = await database.matchDao.getMatchById( - matchId: testMatchOnlyPlayers.id, - ); - expect(updatedMatch.group, null); - }, - ); - - test('removeMatchGroup on non-existing match returns false', () async { - final removed = await database.matchDao.removeMatchGroup( - matchId: 'non-existing-id', + final updatedMatch = await database.matchDao.getMatchById( + matchId: testMatchOnlyPlayers.id, + ); + expect(updatedMatch.group, null); + }, ); - expect(removed, isFalse); + + test( + 'removeMatchGroup() on non-existing match returns isFalse', + () async { + final removed = await database.matchDao.removeMatchGroup( + matchId: 'non-existing-id', + ); + expect(removed, isFalse); + }, + ); + + test('updateMatchNotes() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + const newName = 'New name'; + await database.matchDao.updateMatchName( + matchId: testMatch1.id, + newName: newName, + ); + + final fetchedMatch = await database.matchDao.getMatchById( + matchId: testMatch1.id, + ); + expect(fetchedMatch.name, newName); + }); + + test('updateMatchNotes() does nothing for non-existent game', () async { + final updated = await database.matchDao.updateMatchNotes( + matchId: 'non-existing-id', + notes: 'New notes', + ); + expect(updated, isFalse); + + final allMatches = await database.matchDao.getAllMatches(); + expect(allMatches, isEmpty); + }); + + test('updateMatchEndedAt() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + DateTime newEndedAt = DateTime(2030, 1, 1, 12, 0, 0); + print(newEndedAt); + await database.matchDao.updateMatchEndedAt( + matchId: testMatch1.id, + endedAt: newEndedAt, + ); + + final fetchedMatch = await database.matchDao.getMatchById( + matchId: testMatch1.id, + ); + expect(fetchedMatch.endedAt, newEndedAt); + }); + + test('updateMatchEndedAt() does nothing for non-existent game', () async { + final updated = await database.matchDao.updateMatchEndedAt( + matchId: 'non-existing-id', + endedAt: DateTime.now(), + ); + expect(updated, isFalse); + + final allMatches = await database.matchDao.getAllMatches(); + expect(allMatches, isEmpty); + }); + + test('Getting the match count works correctly', () async { + var matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 0); + + await database.matchDao.addMatch(match: testMatch1); + + matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 1); + + await database.matchDao.addMatch(match: testMatch2); + + matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 2); + + await database.matchDao.deleteMatch(matchId: testMatch1.id); + + matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 1); + + await database.matchDao.deleteMatch(matchId: testMatch2.id); + + matchCount = await database.matchDao.getMatchCount(); + expect(matchCount, 0); + }); }); - test('Fetching all matches related to a group', () async { - var matches = await database.matchDao.getGroupMatches( - groupId: 'non-existing-id', - ); + group('DELETE', () { + test('deleteMatch() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); - expect(matches, isEmpty); + var deleted = await database.matchDao.deleteMatch( + matchId: testMatch1.id, + ); + expect(deleted, isTrue); - await database.matchDao.addMatch(match: testMatch1); + final matchExists = await database.matchDao.matchExists( + matchId: testMatch1.id, + ); + expect(matchExists, isFalse); - matches = await database.matchDao.getGroupMatches(groupId: testGroup1.id); + deleted = await database.matchDao.deleteMatch(matchId: testMatch1.id); + expect(deleted, isFalse); + }); - expect(matches, isNotEmpty); + test('deleteAllMatches() works correctly', () async { + await database.matchDao.addMatchesAsList( + matches: [testMatch1, testMatch2, testMatchOnlyPlayers], + ); - final match = matches.first; - expect(match.id, testMatch1.id); - expect(match.group, isNotNull); - expect(match.group!.id, testGroup1.id); + var count = await database.matchDao.getMatchCount(); + expect(count, 3); + + var deleted = await database.matchDao.deleteAllMatches(); + expect(deleted, isTrue); + + count = await database.matchDao.getMatchCount(); + expect(count, 0); + + deleted = await database.matchDao.deleteAllMatches(); + expect(deleted, isFalse); + }); }); }); }