Refactoring + fixed tests

This commit is contained in:
2026-04-08 23:53:27 +02:00
parent 6a49b92310
commit be58c9ce01
3 changed files with 256 additions and 326 deletions

View File

@@ -30,8 +30,34 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
); );
} }
/// Retrieves the score for a specific round.
Future<ScoreEntry?> getScore({
required String playerId,
required String matchId,
int roundNumber = 0,
}) async {
final query = select(scoreTable)
..where(
(s) =>
s.playerId.equals(playerId) &
s.matchId.equals(matchId) &
s.roundNumber.equals(roundNumber),
);
final result = await query.getSingleOrNull();
if (result == null) return null;
return ScoreEntry(
playerId: result.playerId,
matchId: result.matchId,
roundNumber: result.roundNumber,
score: result.score,
change: result.change,
);
}
/// Retrieves all scores for a specific match. /// Retrieves all scores for a specific match.
Future<List<ScoreEntry>> getScoresForMatch({required String matchId}) async { Future<List<ScoreEntry>> getAllMatchScores({required String matchId}) async {
final query = select(scoreTable)..where((s) => s.matchId.equals(matchId)); final query = select(scoreTable)..where((s) => s.matchId.equals(matchId));
final result = await query.get(); final result = await query.get();
return result return result
@@ -48,7 +74,7 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
} }
/// Retrieves all scores for a specific player in a match. /// Retrieves all scores for a specific player in a match.
Future<List<ScoreEntry>> getPlayerScoresInMatch({ Future<List<ScoreEntry>> getAllPlayerScoresInMatch({
required String playerId, required String playerId,
required String matchId, required String matchId,
}) async { }) async {
@@ -69,30 +95,6 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
.toList(); .toList();
} }
/// Retrieves the score for a specific round.
Future<ScoreEntry?> getScoreForRound({
required String playerId,
required String matchId,
required int roundNumber,
}) async {
final query = select(scoreTable)
..where(
(s) =>
s.playerId.equals(playerId) &
s.matchId.equals(matchId) &
s.roundNumber.equals(roundNumber),
);
final result = await query.getSingleOrNull();
if (result == null) return null;
return ScoreEntry(
playerId: result.playerId,
matchId: result.matchId,
roundNumber: result.roundNumber,
score: result.score,
change: result.change,
);
}
/// Updates a score entry. /// Updates a score entry.
Future<bool> updateScore({ Future<bool> updateScore({
required String playerId, required String playerId,
@@ -134,16 +136,18 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
return rowsAffected > 0; return rowsAffected > 0;
} }
/// Deletes all scores for a specific match. Future<bool> deleteAllScoresForMatch({required String matchId}) async {
Future<bool> deleteScoresForMatch({required String matchId}) async {
final query = delete(scoreTable)..where((s) => s.matchId.equals(matchId)); final query = delete(scoreTable)..where((s) => s.matchId.equals(matchId));
final rowsAffected = await query.go(); final rowsAffected = await query.go();
return rowsAffected > 0; return rowsAffected > 0;
} }
/// Deletes all scores for a specific player. Future<bool> deleteAllScoresForPlayerInMatch({
Future<bool> deleteScoresForPlayer({required String playerId}) async { required String matchId,
final query = delete(scoreTable)..where((s) => s.playerId.equals(playerId)); required String playerId,
}) async {
final query = delete(scoreTable)
..where((s) => s.playerId.equals(playerId) & s.matchId.equals(matchId));
final rowsAffected = await query.go(); final rowsAffected = await query.go();
return rowsAffected > 0; return rowsAffected > 0;
} }
@@ -162,7 +166,7 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
required String playerId, required String playerId,
required String matchId, required String matchId,
}) async { }) async {
final scores = await getPlayerScoresInMatch( final scores = await getAllPlayerScoresInMatch(
playerId: playerId, playerId: playerId,
matchId: matchId, matchId: matchId,
); );
@@ -181,7 +185,7 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
required String playerId, required String playerId,
}) async { }) async {
// Clear previous winner if exists // Clear previous winner if exists
deleteScoresForMatch(matchId: matchId); deleteAllScoresForMatch(matchId: matchId);
// Set the winner's score to 1 // Set the winner's score to 1
final rowsAffected = await into(scoreTable).insert( final rowsAffected = await into(scoreTable).insert(
@@ -222,12 +226,12 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
/// Returns `true` if the winner was removed, `false` if there are multiple /// Returns `true` if the winner was removed, `false` if there are multiple
/// scores or if the winner cannot be removed. /// scores or if the winner cannot be removed.
Future<bool> removeWinner({required String matchId}) async { Future<bool> removeWinner({required String matchId}) async {
final scores = await getScoresForMatch(matchId: matchId); final scores = await getAllMatchScores(matchId: matchId);
if (scores.length > 1) { if (scores.length > 1) {
return false; return false;
} else { } else {
return await deleteScoresForMatch(matchId: matchId); return await deleteAllScoresForMatch(matchId: matchId);
} }
} }
@@ -241,7 +245,7 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
required String playerId, required String playerId,
}) async { }) async {
// Clear previous loosers if exists // Clear previous loosers if exists
deleteScoresForMatch(matchId: matchId); deleteAllScoresForMatch(matchId: matchId);
// Set the loosers score to 0 // Set the loosers score to 0
final rowsAffected = await into(scoreTable).insert( final rowsAffected = await into(scoreTable).insert(
@@ -280,12 +284,12 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
/// Returns `true` if the looser was removed, `false` if there are multiple /// Returns `true` if the looser was removed, `false` if there are multiple
/// scores or if the looser cannot be removed. /// scores or if the looser cannot be removed.
Future<bool> removeLooser({required String matchId}) async { Future<bool> removeLooser({required String matchId}) async {
final scores = await getScoresForMatch(matchId: matchId); final scores = await getAllMatchScores(matchId: matchId);
if (scores.length > 1) { if (scores.length > 1) {
return false; return false;
} else { } else {
return await deleteScoresForMatch(matchId: matchId); return await deleteAllScoresForMatch(matchId: matchId);
} }
} }
} }

View File

@@ -88,7 +88,6 @@ void main() {
}); });
group('Player-Match Tests', () { group('Player-Match Tests', () {
// Verifies that matchHasPlayers returns false initially and true after adding a player.
test('Match has player works correctly', () async { test('Match has player works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.playerDao.addPlayer(player: testPlayer1); await database.playerDao.addPlayer(player: testPlayer1);
@@ -111,7 +110,6 @@ void main() {
expect(matchHasPlayers, true); expect(matchHasPlayers, true);
}); });
// Verifies that a player can be added to a match and isPlayerInMatch returns true.
test('Adding a player to a match works correctly', () async { test('Adding a player to a match works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.playerDao.addPlayer(player: testPlayer5); await database.playerDao.addPlayer(player: testPlayer5);
@@ -135,7 +133,6 @@ void main() {
expect(playerAdded, false); expect(playerAdded, false);
}); });
// Verifies that a player can be removed from a match and the player count decreases.
test('Removing player from match works correctly', () async { test('Removing player from match works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers); await database.matchDao.addMatch(match: testMatchOnlyPlayers);
@@ -156,7 +153,6 @@ void main() {
expect(playerExists, false); expect(playerExists, false);
}); });
// Verifies that getPlayersOfMatch returns all players of a match with correct data.
test('Retrieving players of a match works correctly', () async { test('Retrieving players of a match works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers); await database.matchDao.addMatch(match: testMatchOnlyPlayers);
final players = final players =
@@ -172,7 +168,6 @@ void main() {
} }
}); });
// Verifies that updatePlayersFromMatch replaces all existing players with new ones.
test('Updating the match players works correctly', () async { test('Updating the match players works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers); await database.matchDao.addMatch(match: testMatchOnlyPlayers);
@@ -216,7 +211,6 @@ void main() {
} }
}); });
// Verifies that the same player can be added to multiple different matches.
test( test(
'Adding the same player to separate matches works correctly', 'Adding the same player to separate matches works correctly',
() async { () async {
@@ -273,77 +267,6 @@ void main() {
expect(players, isNull); expect(players, isNull);
}); });
// TODO: FIX
/*// Verifies that adding a player with initial score works correctly.
test('Adding player with initial score works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.playerMatchDao.addPlayerToMatch(
matchId: testMatchOnlyGroup.id,
playerId: testPlayer1.id,
score: 100,
);
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyGroup.id,
playerId: testPlayer1.id,
);
expect(score, 100);
});*/
// TODO: FIX
/*// Verifies that getPlayerScore returns the correct score.
test('getPlayerScore returns correct score', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
// Default score should be 0 when added through match
final score = await database.scoreDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 0);
});*/
// TODO: Fix
/*// Verifies that getPlayerScore returns null for non-existent player-match combination.
test(
'getPlayerScore returns null for non-existent player in match',
() async {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyGroup.id,
playerId: 'non-existent-player-id',
);
expect(score, isNull);
},
);*/
// TODO: Fix
// Verifies that updatePlayerScore updates the score correctly.
/*test('updatePlayerScore updates score correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
final updated = await database.playerMatchDao.updatePlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
newScore: 50,
);
expect(updated, true);
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 50);
});*/
// Verifies that updatePlayerScore returns false for non-existent player-match.
test( test(
'updatePlayerScore returns false for non-existent player-match', 'updatePlayerScore returns false for non-existent player-match',
() async { () async {
@@ -359,7 +282,6 @@ void main() {
}, },
); );
// Verifies that adding a player with teamId works correctly.
test('Adding player with teamId works correctly', () async { test('Adding player with teamId works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.teamDao.addTeam(team: testTeam1); await database.teamDao.addTeam(team: testTeam1);
@@ -379,7 +301,6 @@ void main() {
expect(playersInTeam[0].id, testPlayer1.id); expect(playersInTeam[0].id, testPlayer1.id);
}); });
// Verifies that updatePlayerTeam updates the team correctly.
test('updatePlayerTeam updates team correctly', () async { test('updatePlayerTeam updates team correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.teamDao.addTeam(team: testTeam1); await database.teamDao.addTeam(team: testTeam1);
@@ -418,7 +339,6 @@ void main() {
expect(playersInTeam1.isEmpty, true); expect(playersInTeam1.isEmpty, true);
}); });
// Verifies that updatePlayerTeam can set team to null.
test('updatePlayerTeam can remove player from team', () async { test('updatePlayerTeam can remove player from team', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.teamDao.addTeam(team: testTeam1); await database.teamDao.addTeam(team: testTeam1);
@@ -446,7 +366,6 @@ void main() {
expect(playersInTeam.isEmpty, true); expect(playersInTeam.isEmpty, true);
}); });
// Verifies that updatePlayerTeam returns false for non-existent player-match.
test( test(
'updatePlayerTeam returns false for non-existent player-match', 'updatePlayerTeam returns false for non-existent player-match',
() async { () async {
@@ -474,7 +393,6 @@ void main() {
expect(players.isEmpty, true); expect(players.isEmpty, true);
}); });
// Verifies that getPlayersInTeam returns all players of a team.
test('getPlayersInTeam returns all players of a team', () async { test('getPlayersInTeam returns all players of a team', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.teamDao.addTeam(team: testTeam1); await database.teamDao.addTeam(team: testTeam1);
@@ -501,7 +419,6 @@ void main() {
expect(playerIds.contains(testPlayer2.id), true); expect(playerIds.contains(testPlayer2.id), true);
}); });
// Verifies that removePlayerFromMatch returns false for non-existent player.
test( test(
'removePlayerFromMatch returns false for non-existent player', 'removePlayerFromMatch returns false for non-existent player',
() async { () async {
@@ -516,7 +433,6 @@ void main() {
}, },
); );
// Verifies that adding the same player twice to the same match is ignored.
test('Adding same player twice to same match is ignored', () async { test('Adding same player twice to same match is ignored', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup); await database.matchDao.addMatch(match: testMatchOnlyGroup);
@@ -539,7 +455,6 @@ void main() {
expect(players?.length, 1); expect(players?.length, 1);
}); });
// Verifies that updatePlayersFromMatch with empty list removes all players.
test( test(
'updatePlayersFromMatch with empty list removes all players', 'updatePlayersFromMatch with empty list removes all players',
() async { () async {
@@ -565,7 +480,6 @@ void main() {
}, },
); );
// Verifies that updatePlayersFromMatch with same players makes no changes.
test('updatePlayersFromMatch with same players makes no changes', () async { test('updatePlayersFromMatch with same players makes no changes', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers); await database.matchDao.addMatch(match: testMatchOnlyPlayers);
@@ -587,7 +501,6 @@ void main() {
} }
}); });
// Verifies that matchHasPlayers returns false for non-existent match.
test('matchHasPlayers returns false for non-existent match', () async { test('matchHasPlayers returns false for non-existent match', () async {
final hasPlayers = await database.playerMatchDao.matchHasPlayers( final hasPlayers = await database.playerMatchDao.matchHasPlayers(
matchId: 'non-existent-match-id', matchId: 'non-existent-match-id',
@@ -596,7 +509,6 @@ void main() {
expect(hasPlayers, false); expect(hasPlayers, false);
}); });
// Verifies that isPlayerInMatch returns false for non-existent match.
test('isPlayerInMatch returns false for non-existent match', () async { test('isPlayerInMatch returns false for non-existent match', () async {
final isInMatch = await database.playerMatchDao.isPlayerInMatch( final isInMatch = await database.playerMatchDao.isPlayerInMatch(
matchId: 'non-existent-match-id', matchId: 'non-existent-match-id',
@@ -606,122 +518,6 @@ void main() {
expect(isInMatch, false); expect(isInMatch, false);
}); });
// Verifies that updatePlayersFromMatch preserves scores for existing players.
test('updatePlayersFromMatch only modifies player associations', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
// Update score for existing player
await database.scoreDao.updateScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
newScore: 75,
);
// Update players, keeping testPlayer4 and adding testPlayer1
await database.playerMatchDao.updatePlayersFromMatch(
matchId: testMatchOnlyPlayers.id,
newPlayer: [testPlayer4, testPlayer1],
);
// TODO: Fix
/*// Verify testPlayer4's score is preserved
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 75);
// Verify testPlayer1 was added with default score
final newPlayerScore = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer1.id,
);
expect(newPlayerScore, 0);*/
});
// Verifies that adding a player with both score and teamId works correctly.
test('Adding player with score and teamId works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.teamDao.addTeam(team: testTeam1);
await database.playerMatchDao.addPlayerToMatch(
matchId: testMatchOnlyGroup.id,
playerId: testPlayer1.id,
teamId: testTeam1.id,
);
// TODO: fix
/*// Verify score
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyGroup.id,
playerId: testPlayer1.id,
);
expect(score, 150);*/
// Verify team assignment
final playersInTeam = await database.playerMatchDao.getPlayersInTeam(
matchId: testMatchOnlyGroup.id,
teamId: testTeam1.id,
);
expect(playersInTeam.length, 1);
expect(playersInTeam[0].id, testPlayer1.id);
});
// Verifies that updating score with negative value works.
test('updatePlayerScore with negative score works', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
await database.scoreDao.addScore(
playerId: testPlayer4.id,
matchId: testMatchOnlyPlayers.id,
score: 0,
);
final updated = await database.scoreDao.updateScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
newScore: -10,
);
expect(updated, true);
// TODO: fix
/* final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, -10);*/
});
// Verifies that updating score with zero value works.
test('updatePlayerScore with zero score works', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
await database.scoreDao.addScore(
playerId: testPlayer4.id,
matchId: testMatchOnlyPlayers.id,
score: 100,
);
// Then update to zero
final updated = await database.scoreDao.updateScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
newScore: 0,
);
expect(updated, true);
// TODO: Fix
/*final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 0);*/
});
// Verifies that getPlayersInTeam returns empty list for non-existent match. // Verifies that getPlayersInTeam returns empty list for non-existent match.
test( test(
'getPlayersInTeam returns empty list for non-existent match', 'getPlayersInTeam returns empty list for non-existent match',
@@ -823,56 +619,6 @@ void main() {
expect(isInMatch2, true); expect(isInMatch2, true);
}); });
// Verifies that updating scores for players in different matches are independent.
test('Player scores are independent across matches', () async {
final playersList = [testPlayer1];
final match1 = Match(
name: 'Match 1',
game: testGame,
players: playersList,
notes: '',
);
final match2 = Match(
name: 'Match 2',
game: testGame,
players: playersList,
notes: '',
);
await Future.wait([
database.matchDao.addMatch(match: match1),
database.matchDao.addMatch(match: match2),
]);
// Update score in match1
await database.scoreDao.updateScore(
matchId: match1.id,
playerId: testPlayer1.id,
newScore: 100,
);
// Update score in match2
await database.scoreDao.updateScore(
matchId: match2.id,
playerId: testPlayer1.id,
newScore: 50,
);
// TODO: fix
/* // Verify scores are independent
final scoreInMatch1 = await database.playerMatchDao.getPlayerScore(
matchId: match1.id,
playerId: testPlayer1.id,
);
final scoreInMatch2 = await database.playerMatchDao.getPlayerScore(
matchId: match2.id,
playerId: testPlayer1.id,
);
expect(scoreInMatch1, 100);
expect(scoreInMatch2, 50);*/
});
// Verifies that updatePlayersFromMatch on non-existent match fails with constraint error. // Verifies that updatePlayersFromMatch on non-existent match fails with constraint error.
test( test(
'updatePlayersFromMatch on non-existent match fails with foreign key constraint', 'updatePlayersFromMatch on non-existent match fails with foreign key constraint',

View File

@@ -76,7 +76,7 @@ void main() {
change: 10, change: 10,
); );
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -114,7 +114,7 @@ void main() {
change: 15, change: 15,
); );
final scores = await database.scoreDao.getScoresForMatch( final scores = await database.scoreDao.getAllMatchScores(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -145,7 +145,7 @@ void main() {
change: 5, change: 5,
); );
final playerScores = await database.scoreDao.getPlayerScoresInMatch( final playerScores = await database.scoreDao.getAllPlayerScoresInMatch(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -161,7 +161,7 @@ void main() {
// Verifies that getScoreForRound returns null for a non-existent round number. // Verifies that getScoreForRound returns null for a non-existent round number.
test('Getting score for a non-existent round returns null', () async { test('Getting score for a non-existent round returns null', () async {
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 999, roundNumber: 999,
@@ -190,7 +190,7 @@ void main() {
expect(updated, true); expect(updated, true);
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -232,7 +232,7 @@ void main() {
expect(deleted, true); expect(deleted, true);
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -276,18 +276,18 @@ void main() {
change: 15, change: 15,
); );
final deleted = await database.scoreDao.deleteScoresForMatch( final deleted = await database.scoreDao.deleteAllScoresForMatch(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
expect(deleted, true); expect(deleted, true);
final match1Scores = await database.scoreDao.getScoresForMatch( final match1Scores = await database.scoreDao.getAllMatchScores(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
expect(match1Scores.length, 0); expect(match1Scores.length, 0);
final match2Scores = await database.scoreDao.getScoresForMatch( final match2Scores = await database.scoreDao.getAllMatchScores(
matchId: testMatch2.id, matchId: testMatch2.id,
); );
expect(match2Scores.length, 1); expect(match2Scores.length, 1);
@@ -302,34 +302,37 @@ void main() {
score: 10, score: 10,
change: 10, change: 10,
); );
await database.scoreDao.addScore( await database.scoreDao.addScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch2.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 2,
score: 15, score: 10,
change: 15, change: 10,
); );
await database.scoreDao.addScore( await database.scoreDao.addScore(
playerId: testPlayer2.id, playerId: testPlayer2.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
score: 20, score: 10,
change: 20, change: 10,
); );
final deleted = await database.scoreDao.deleteScoresForPlayer( final deleted = await database.scoreDao.deleteAllScoresForPlayerInMatch(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id,
); );
expect(deleted, true); expect(deleted, true);
final player1Scores = await database.scoreDao.getPlayerScoresInMatch( final player1Scores = await database.scoreDao.getAllPlayerScoresInMatch(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
); );
expect(player1Scores.length, 0); expect(player1Scores.length, 0);
final player2Scores = await database.scoreDao.getPlayerScoresInMatch( final player2Scores = await database.scoreDao.getAllPlayerScoresInMatch(
playerId: testPlayer2.id, playerId: testPlayer2.id,
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -424,7 +427,7 @@ void main() {
change: 99, change: 99,
); );
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -439,7 +442,7 @@ void main() {
test( test(
'Getting scores for match with no scores returns empty list', 'Getting scores for match with no scores returns empty list',
() async { () async {
final scores = await database.scoreDao.getScoresForMatch( final scores = await database.scoreDao.getAllMatchScores(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -449,7 +452,7 @@ void main() {
// Verifies that getPlayerScoresInMatch returns empty list when player has no scores. // Verifies that getPlayerScoresInMatch returns empty list when player has no scores.
test('Getting player scores with no scores returns empty list', () async { test('Getting player scores with no scores returns empty list', () async {
final playerScores = await database.scoreDao.getPlayerScoresInMatch( final playerScores = await database.scoreDao.getAllPlayerScoresInMatch(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -467,7 +470,7 @@ void main() {
change: -10, change: -10,
); );
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -488,7 +491,7 @@ void main() {
change: 0, change: 0,
); );
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -509,7 +512,7 @@ void main() {
change: 100, change: 100,
); );
final score = await database.scoreDao.getScoreForRound( final score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 999999, roundNumber: 999999,
@@ -552,7 +555,7 @@ void main() {
// Verifies that deleteScoresForMatch returns false when no scores exist. // Verifies that deleteScoresForMatch returns false when no scores exist.
test('Deleting scores for empty match returns false', () async { test('Deleting scores for empty match returns false', () async {
final deleted = await database.scoreDao.deleteScoresForMatch( final deleted = await database.scoreDao.deleteAllScoresForMatch(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -561,8 +564,9 @@ void main() {
// Verifies that deleteScoresForPlayer returns false when player has no scores. // Verifies that deleteScoresForPlayer returns false when player has no scores.
test('Deleting scores for player with no scores returns false', () async { test('Deleting scores for player with no scores returns false', () async {
final deleted = await database.scoreDao.deleteScoresForPlayer( final deleted = await database.scoreDao.deleteAllScoresForPlayerInMatch(
playerId: testPlayer1.id, playerId: 'non-existing-player-id',
matchId: 'non-existing-match-id',
); );
expect(deleted, false); expect(deleted, false);
@@ -593,12 +597,12 @@ void main() {
newChange: 90, newChange: 90,
); );
final player1Score = await database.scoreDao.getScoreForRound( final player1Score = await database.scoreDao.getScore(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
); );
final player2Score = await database.scoreDao.getScoreForRound( final player2Score = await database.scoreDao.getScore(
playerId: testPlayer2.id, playerId: testPlayer2.id,
matchId: testMatch1.id, matchId: testMatch1.id,
roundNumber: 1, roundNumber: 1,
@@ -625,11 +629,11 @@ void main() {
change: 50, change: 50,
); );
final match1Scores = await database.scoreDao.getPlayerScoresInMatch( final match1Scores = await database.scoreDao.getAllPlayerScoresInMatch(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch1.id, matchId: testMatch1.id,
); );
final match2Scores = await database.scoreDao.getPlayerScoresInMatch( final match2Scores = await database.scoreDao.getAllPlayerScoresInMatch(
playerId: testPlayer1.id, playerId: testPlayer1.id,
matchId: testMatch2.id, matchId: testMatch2.id,
); );
@@ -707,7 +711,7 @@ void main() {
newChange: 89, newChange: 89,
); );
final scores = await database.scoreDao.getScoresForMatch( final scores = await database.scoreDao.getAllMatchScores(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -744,9 +748,12 @@ void main() {
change: 20, change: 20,
); );
await database.scoreDao.deleteScoresForPlayer(playerId: testPlayer1.id); await database.scoreDao.deleteAllScoresForPlayerInMatch(
playerId: testPlayer1.id,
matchId: testMatch1.id,
);
final match1Scores = await database.scoreDao.getScoresForMatch( final match1Scores = await database.scoreDao.getAllMatchScores(
matchId: testMatch1.id, matchId: testMatch1.id,
); );
@@ -754,4 +761,177 @@ void main() {
expect(match1Scores[0].playerId, testPlayer2.id); expect(match1Scores[0].playerId, testPlayer2.id);
}); });
}); });
// Verifies that updating scores for players in different matches are independent.
test('Player scores are independent across matches', () async {
final playersList = [testPlayer1];
final match1 = Match(
name: 'Match 1',
game: testGame,
players: playersList,
notes: '',
);
final match2 = Match(
name: 'Match 2',
game: testGame,
players: playersList,
notes: '',
);
await Future.wait([
database.matchDao.addMatch(match: match1),
database.matchDao.addMatch(match: match2),
]);
// Update score in match1
await database.scoreDao.updateScore(
matchId: match1.id,
playerId: testPlayer1.id,
newScore: 100,
);
// Update score in match2
await database.scoreDao.updateScore(
matchId: match2.id,
playerId: testPlayer1.id,
newScore: 50,
);
// TODO: fix
/* // Verify scores are independent
final scoreInMatch1 = await database.playerMatchDao.getPlayerScore(
matchId: match1.id,
playerId: testPlayer1.id,
);
final scoreInMatch2 = await database.playerMatchDao.getPlayerScore(
matchId: match2.id,
playerId: testPlayer1.id,
);
expect(scoreInMatch1, 100);
expect(scoreInMatch2, 50);*/
});
// Verifies that updating score with zero value works.
test('updatePlayerScore with zero score works', () async {
await database.matchDao.addMatch(match: testMatch1);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
score: 100,
);
final updated = await database.scoreDao.updateScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
newScore: 0,
);
expect(updated, true);
// TODO: Fix
/*final score = await database.scoreDao.getAllMatchScores(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 0);*/
});
// Verifies that updating score with negative value works.
/* test('updatePlayerScore with negative score works', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
await database.scoreDao.addScore(
playerId: testPlayer4.id,
matchId: testMatchOnlyPlayers.id,
score: 0,
);
final updated = await database.scoreDao.updateScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
newScore: -10,
);
expect(updated, true);
// TODO: fix
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, -10);
});*/
// TODO: FIX
/*// Verifies that adding a player with initial score works correctly.
test('Adding player with initial score works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.playerMatchDao.addPlayerToMatch(
matchId: testMatchOnlyGroup.id,
playerId: testPlayer1.id,
score: 100,
);
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyGroup.id,
playerId: testPlayer1.id,
);
expect(score, 100);
});*/
// TODO: FIX
/*// Verifies that getPlayerScore returns the correct score.
test('getPlayerScore returns correct score', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
// Default score should be 0 when added through match
final score = await database.scoreDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 0);
});*/
// TODO: Fix
/*// Verifies that getPlayerScore returns null for non-existent player-match combination.
test(
'getPlayerScore returns null for non-existent player in match',
() async {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyGroup.id,
playerId: 'non-existent-player-id',
);
expect(score, isNull);
},
);*/
// TODO: Fix
// Verifies that updatePlayerScore updates the score correctly.
/*test('updatePlayerScore updates score correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
final updated = await database.playerMatchDao.updatePlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
newScore: 50,
);
expect(updated, true);
final score = await database.playerMatchDao.getPlayerScore(
matchId: testMatchOnlyPlayers.id,
playerId: testPlayer4.id,
);
expect(score, 50);
});*/
} }