From 6fb4a8996cdb9a9d12752f8032ca5c26cd36777d Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Fri, 22 May 2026 00:48:21 +0200 Subject: [PATCH] updated team tests --- lib/data/dao/score_entry_dao.dart | 6 +- lib/data/dao/team_dao.dart | 43 +++- .../match_view/match_result_view.dart | 5 +- pubspec.yaml | 2 +- test/db_tests/aggregates/match_test.dart | 46 ++-- test/db_tests/aggregates/team_test.dart | 197 +++++++++++++++++- 6 files changed, 258 insertions(+), 41 deletions(-) diff --git a/lib/data/dao/score_entry_dao.dart b/lib/data/dao/score_entry_dao.dart index 830135d..9e41524 100644 --- a/lib/data/dao/score_entry_dao.dart +++ b/lib/data/dao/score_entry_dao.dart @@ -16,12 +16,12 @@ class ScoreEntryDao extends DatabaseAccessor /* Create */ /// Adds a score entry to the database. - Future addScore({ + Future addScore({ required String playerId, required String matchId, required ScoreEntry entry, }) async { - await into(scoreEntryTable).insert( + final rowsAffected = await into(scoreEntryTable).insert( ScoreEntryTableCompanion.insert( playerId: playerId, matchId: matchId, @@ -31,6 +31,8 @@ class ScoreEntryDao extends DatabaseAccessor ), mode: InsertMode.insertOrReplace, ); + + return rowsAffected > 0; } Future addScoresAsList({ diff --git a/lib/data/dao/team_dao.dart b/lib/data/dao/team_dao.dart index 793cce8..32e5f07 100644 --- a/lib/data/dao/team_dao.dart +++ b/lib/data/dao/team_dao.dart @@ -212,7 +212,7 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { await (update(teamTable)..where((t) => t.id.equals(teamId))).write( const TeamTableCompanion(score: Value(null)), ); - await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId); + await _deleteAllScoresForMembersOfTeam(teamId: teamId, matchId: matchId); final rowsAffected = await (update(teamTable)..where((t) => t.id.equals(teamId))).write( @@ -238,7 +238,7 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { await (update(teamTable)..where((t) => t.id.equals(teamId))).write( const TeamTableCompanion(score: Value(null)), ); - await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId); + await _deleteAllScoresForMembersOfTeam(teamId: teamId, matchId: matchId); return true; } @@ -300,16 +300,17 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { required List winners, required String matchId, }) async { - List success = List.generate(winners.length, (index) => null); - - for (int i = 0; i < winners.length; i++) { - success[i] = await updateTeamScore( - teamId: winners[i].id, - matchId: matchId, - score: 1, - ); + // Reset all team scores . + await removeAllTeamScores(matchId: matchId); + // Reset all score entries + for (final team in winners) { + await _deleteAllScoresForMembersOfTeam(teamId: team.id, matchId: matchId); } - return success.every((result) => result == true); + + for (final team in winners) { + await updateTeamScore(teamId: team.id, matchId: matchId, score: 1); + } + return true; } /// Removes the winner status from all Teams with the given [matchId] by setting its score to null. @@ -349,4 +350,24 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { } return success.every((result) => result == true); } + + /// Helper method to delete all scores for members of a team in a specific match. + Future _deleteAllScoresForMembersOfTeam({ + required String teamId, + required String matchId, + }) async { + final playerMatchQuery = select(db.playerMatchTable) + ..where((pm) => pm.teamId.equals(teamId) & pm.matchId.equals(matchId)); + final playerMatches = await playerMatchQuery.get(); + + if (playerMatches.isEmpty) return false; + + for (final pm in playerMatches) { + await db.scoreEntryDao.deleteAllScoresForPlayerInMatch( + playerId: pm.playerId, + matchId: matchId, + ); + } + return true; + } } diff --git a/lib/presentation/views/main_menu/match_view/match_result_view.dart b/lib/presentation/views/main_menu/match_view/match_result_view.dart index 64dd8ad..8708bfe 100644 --- a/lib/presentation/views/main_menu/match_view/match_result_view.dart +++ b/lib/presentation/views/main_menu/match_view/match_result_view.dart @@ -362,10 +362,7 @@ class _MatchResultViewState extends State { Future _handleLoser() async { if (isTeamMatch) { if (_selectedTeam == null) { - return await db.teamDao.removeLoserTeam( - matchId: widget.match.id, - teamId: _selectedTeam!.id, - ); + return await db.teamDao.removeLoserTeam(matchId: widget.match.id); } else { return await db.teamDao.setLoserTeam( matchId: widget.match.id, diff --git a/pubspec.yaml b/pubspec.yaml index 9b94141..5e57448 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: tallee description: "Tracking App for Card Games" publish_to: 'none' -version: 0.0.30+331 +version: 0.0.30+332 environment: sdk: ^3.8.1 diff --git a/test/db_tests/aggregates/match_test.dart b/test/db_tests/aggregates/match_test.dart index 916d5d4..3f76a23 100644 --- a/test/db_tests/aggregates/match_test.dart +++ b/test/db_tests/aggregates/match_test.dart @@ -507,34 +507,36 @@ void main() { deleted = await database.matchDao.deleteAllMatches(); expect(deleted, isFalse); }); - }); - test('deleteMatchesByGame() deletes all matches for a game', () async { - await database.matchDao.addMatch(match: testMatch1); - await database.matchDao.addMatch(match: testMatch2); + test('deleteMatchesByGame() deletes all matches for a game', () async { + await database.matchDao.addMatch(match: testMatch1); + await database.matchDao.addMatch(match: testMatch2); - var count = await database.matchDao.getMatchCountByGame( - gameId: testGame.id, - ); - expect(count, 2); + var count = await database.matchDao.getMatchCountByGame( + gameId: testGame.id, + ); + expect(count, 2); - final deletedCount = await database.matchDao.deleteMatchesByGame( - gameId: testGame.id, - ); - expect(deletedCount, 2); + final deletedCount = await database.matchDao.deleteMatchesByGame( + gameId: testGame.id, + ); + expect(deletedCount, 2); - count = await database.matchDao.getMatchCountByGame(gameId: testGame.id); - expect(count, 0); + count = await database.matchDao.getMatchCountByGame( + gameId: testGame.id, + ); + expect(count, 0); - final allMatches = await database.matchDao.getAllMatches(); - expect(allMatches, isEmpty); - }); + final allMatches = await database.matchDao.getAllMatches(); + expect(allMatches, isEmpty); + }); - test('deleteMatchesByGame() returns 0 for non-existent game', () async { - final deletedCount = await database.matchDao.deleteMatchesByGame( - gameId: 'non-existent-game-id', - ); - expect(deletedCount, 0); + test('deleteMatchesByGame() returns 0 for non-existent game', () async { + final deletedCount = await database.matchDao.deleteMatchesByGame( + gameId: 'non-existent-game-id', + ); + expect(deletedCount, 0); + }); }); }); } diff --git a/test/db_tests/aggregates/team_test.dart b/test/db_tests/aggregates/team_test.dart index 381d22b..832dcb8 100644 --- a/test/db_tests/aggregates/team_test.dart +++ b/test/db_tests/aggregates/team_test.dart @@ -1,7 +1,7 @@ import 'dart:core' hide Match; import 'package:clock/clock.dart'; -import 'package:drift/drift.dart'; +import 'package:drift/drift.dart' hide isNotNull, isNull; import 'package:drift/native.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tallee/core/enums.dart'; @@ -327,5 +327,200 @@ void main() { expect(deleted, isFalse); }); }); + + group('SCORE', () { + test('updateTeamScore() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + final updated = await database.teamDao.updateTeamScore( + teamId: testTeam1.id, + matchId: testMatch1.id, + score: 5, + ); + expect(updated, isTrue); + final team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, 5); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 5); + } + }); + + test('set-/removeWinnerTeam() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + final set = await database.teamDao.setWinnerTeam( + teamId: testTeam1.id, + matchId: testMatch1.id, + ); + expect(set, isTrue); + + var team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, 1); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 1); + } + + final removed = await database.teamDao.removeWinnerTeam( + matchId: testMatch1.id, + ); + expect(removed, isTrue); + + team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, isNull); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNull); + } + }); + + test('set-/removeLoserTeam() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + final set = await database.teamDao.setLoserTeam( + teamId: testTeam1.id, + matchId: testMatch1.id, + ); + expect(set, isTrue); + + var team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, 0); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 0); + } + + final removed = await database.teamDao.removeLoserTeam( + matchId: testMatch1.id, + ); + expect(removed, isTrue); + + team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, isNull); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNull); + } + }); + + test('set-/removeWinnerTeams() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + final set = await database.teamDao.setWinnerTeams( + winners: [testTeam1, testTeam2], + matchId: testMatch1.id, + ); + expect(set, isTrue); + + // check both teams got the winner score + var team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, 1); + team = await database.teamDao.getTeamById(teamId: testTeam2.id); + expect(team.score, 1); + + // check all members of both teams got the winner score + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 1); + } + + for (final member in testTeam2.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 1); + } + + final removed = await database.teamDao.removeWinnerTeam( + matchId: testMatch1.id, + ); + expect(removed, isTrue); + + team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, isNull); + + team = await database.teamDao.getTeamById(teamId: testTeam2.id); + expect(team.score, isNull); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNull); + } + + for (final member in testTeam2.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNull); + } + }); + + test('setTeamPlacements() works correctly', () async { + await database.matchDao.addMatch(match: testMatch1); + + final set = await database.teamDao.setTeamPlacements( + teams: [testTeam1, testTeam2], + matchId: testMatch1.id, + ); + expect(set, isTrue); + + var team = await database.teamDao.getTeamById(teamId: testTeam1.id); + expect(team.score, 2); + team = await database.teamDao.getTeamById(teamId: testTeam2.id); + expect(team.score, 1); + + for (final member in testTeam1.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 2); + } + + for (final member in testTeam2.members) { + final entry = await database.scoreEntryDao.getScore( + playerId: member.id, + matchId: testMatch1.id, + ); + expect(entry, isNotNull); + expect(entry!.score, 1); + } + }); + }); }); }