From ab9a8d01939875dffe44f4740b91a6eeaafb898d Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Fri, 1 May 2026 18:01:07 +0200 Subject: [PATCH] Fixed test issues --- lib/data/dao/match_dao.dart | 21 ++++- .../relationships/player_match_test.dart | 6 +- test/services/data_transfer_service_test.dart | 79 +++++++++++-------- 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 11cd5a2..f43909c 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -42,11 +42,24 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { await db.teamDao.addTeamsAsList(teams: match.teams!, matchId: match.id); } + // Collect all player IDs that are already in teams + final playersInTeams = {}; + if (match.teams != null) { + for (final team in match.teams!) { + for (final member in team.members) { + playersInTeams.add(member.id); + } + } + } + + // Add players that are not in teams for (final p in match.players) { - await db.playerMatchDao.addPlayerToMatch( - matchId: match.id, - playerId: p.id, - ); + if (!playersInTeams.contains(p.id)) { + await db.playerMatchDao.addPlayerToMatch( + matchId: match.id, + playerId: p.id, + ); + } } for (final pid in match.scores.keys) { diff --git a/test/db_tests/relationships/player_match_test.dart b/test/db_tests/relationships/player_match_test.dart index 2cc5185..85ccab9 100644 --- a/test/db_tests/relationships/player_match_test.dart +++ b/test/db_tests/relationships/player_match_test.dart @@ -103,6 +103,7 @@ void main() { test('addPlayerToMatch() ignores duplicates', () async { await database.matchDao.addMatch(match: testMatch1); + await database.playerDao.addPlayer(player: testPlayer5); final isInMatch = await database.playerMatchDao.isPlayerInMatch( matchId: testMatch1.id, @@ -117,11 +118,11 @@ void main() { await database.playerMatchDao.addPlayerToMatch( matchId: testMatch1.id, - playerId: testPlayer1.id, + playerId: testPlayer5.id, ); await database.playerMatchDao.addPlayerToMatch( matchId: testMatch1.id, - playerId: testPlayer1.id, + playerId: testPlayer5.id, ); players = await database.playerMatchDao.getPlayersOfMatch( @@ -149,6 +150,7 @@ void main() { }); test('isPlayerInMatch() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); final isInMatch = await database.playerMatchDao.isPlayerInMatch( matchId: testMatch1.id, playerId: testPlayer1.id, diff --git a/test/services/data_transfer_service_test.dart b/test/services/data_transfer_service_test.dart index 6aec390..781ee67 100644 --- a/test/services/data_transfer_service_test.dart +++ b/test/services/data_transfer_service_test.dart @@ -99,10 +99,8 @@ void main() { await database.playerDao.addPlayer(player: testPlayer1); await database.gameDao.addGame(game: testGame); await database.groupDao.addGroup(group: testGroup); - /* - await database.teamDao.addTeam(team: testTeam); -*/ await database.matchDao.addMatch(match: testMatch); + await database.teamDao.addTeam(team: testTeam, matchId: testMatch.id); var playerCount = await database.playerDao.getPlayerCount(); var gameCount = await database.gameDao.getGameCount(); @@ -154,19 +152,16 @@ void main() { expect(decoded.containsKey('players'), true); expect(decoded.containsKey('games'), true); expect(decoded.containsKey('groups'), true); - expect(decoded.containsKey('teams'), true); expect(decoded.containsKey('matches'), true); final players = decoded['players'] as List; final games = decoded['games'] as List; final groups = decoded['groups'] as List; - final teams = decoded['teams'] as List; final matches = decoded['matches'] as List; expect(players.length, 2); expect(games.length, 1); expect(groups.length, 1); - expect(teams.length, 1); expect(matches.length, 1); }); @@ -179,13 +174,11 @@ void main() { final players = decoded['players'] as List; final games = decoded['games'] as List; final groups = decoded['groups'] as List; - final teams = decoded['teams'] as List; final matches = decoded['matches'] as List; expect(players, isEmpty); expect(games, isEmpty); expect(groups, isEmpty); - expect(teams, isEmpty); expect(matches, isEmpty); }); }); @@ -247,31 +240,6 @@ void main() { expect(memberIds, containsAll([testPlayer1.id, testPlayer2.id])); }); - testWidgets('Team data is correct', (tester) async { - /* - await database.teamDao.addTeam(team: testTeam); -*/ - - final ctx = await getContext(tester); - final jsonString = await DataTransferService.getAppDataAsJson(ctx); - final decoded = json.decode(jsonString) as Map; - final teams = decoded['teams'] as List; - - expect(teams.length, 1); - - final teamData = teams[0] as Map; - - expect(teamData['id'], testTeam.id); - expect(teamData['name'], testTeam.name); - expect(teamData['memberIds'], isA()); - - // Note: In this system, teams don't have independent members. - // Team members are only tracked through matches via PlayerMatchTable. - // Therefore, memberIds will be empty for standalone teams. - final memberIds = teamData['memberIds'] as List; - expect(memberIds, isEmpty); - }); - testWidgets('Match data is correct', (tester) async { await database.playerDao.addPlayersAsList( players: [testPlayer1, testPlayer2], @@ -323,6 +291,51 @@ void main() { expect(player2Score.change, 15); }); + testWidgets('Match with teams is handled correctly', (tester) async { + final matchWithTeams = Match( + name: 'Match with Teams', + game: testGame, + players: [testPlayer1, testPlayer2], + teams: [testTeam], + notes: 'Team match', + ); + + await database.playerDao.addPlayersAsList( + players: [testPlayer1, testPlayer2], + ); + await database.gameDao.addGame(game: testGame); + await database.matchDao.addMatch(match: matchWithTeams); + + final ctx = await getContext(tester); + final jsonString = await DataTransferService.getAppDataAsJson(ctx); + final decoded = json.decode(jsonString) as Map; + final matches = decoded['matches'] as List; + + expect(matches.length, 1); + + final matchData = matches[0] as Map; + expect(matchData['id'], matchWithTeams.id); + expect(matchData['name'], matchWithTeams.name); + expect( + matchData['teams'], + isNotNull, + reason: 'teams should not be null', + ); + expect(matchData['teams'], isA()); + + final teamsInMatch = matchData['teams'] as List; + expect(teamsInMatch.length, 1); + + final teamData = teamsInMatch[0] as Map; + expect(teamData['id'], testTeam.id); + expect(teamData['name'], testTeam.name); + expect(teamData['memberIds'], isA()); + + final memberIds = teamData['memberIds'] as List; + expect(memberIds.length, 2); + expect(memberIds, containsAll([testPlayer1.id, testPlayer2.id])); + }); + testWidgets('Match without group is handled correctly', (tester) async { final matchWithoutGroup = Match( name: 'No Group Match',