2 Commits

Author SHA1 Message Date
gelbeinhalb
25e0c75dc6 add team_test.dart.dart
Some checks failed
Pull Request Pipeline / lint (pull_request) Successful in 2m8s
Pull Request Pipeline / test (pull_request) Failing after 1m56s
2026-01-19 19:52:00 +01:00
gelbeinhalb
fb59372c97 add score_test.dart 2026-01-19 19:51:48 +01:00
2 changed files with 643 additions and 0 deletions

View File

@@ -0,0 +1,417 @@
import 'package:clock/clock.dart';
import 'package:drift/drift.dart' hide isNull, isNotNull;
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:game_tracker/data/db/database.dart';
import 'package:game_tracker/data/dto/game.dart';
import 'package:game_tracker/data/dto/match.dart';
import 'package:game_tracker/data/dto/player.dart';
void main() {
late AppDatabase database;
late Player testPlayer1;
late Player testPlayer2;
late Player testPlayer3;
late Game testGame;
late Match testMatch1;
late Match testMatch2;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
final fakeClock = Clock(() => fixedDate);
setUp(() async {
database = AppDatabase(
DatabaseConnection(
NativeDatabase.memory(),
// Recommended for widget tests to avoid test errors.
closeStreamsSynchronously: true,
),
);
withClock(fakeClock, () {
testPlayer1 = Player(name: 'Alice');
testPlayer2 = Player(name: 'Bob');
testPlayer3 = Player(name: 'Charlie');
testGame = Game(name: 'Test Game');
testMatch1 = Match(
name: 'Test Match 1',
game: testGame,
players: [testPlayer1, testPlayer2],
);
testMatch2 = Match(
name: 'Test Match 2',
game: testGame,
players: [testPlayer2, testPlayer3],
);
});
await database.playerDao.addPlayersAsList(
players: [testPlayer1, testPlayer2, testPlayer3],
);
await database.gameDao.addGame(game: testGame);
await database.matchDao.addMatch(match: testMatch1);
await database.matchDao.addMatch(match: testMatch2);
});
tearDown(() async {
await database.close();
});
group('Score Tests', () {
test('Adding and fetching a score works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
final score = await database.scoreDao.getScoreForRound(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
);
expect(score, isNotNull);
expect(score!.playerId, testPlayer1.id);
expect(score.matchId, testMatch1.id);
expect(score.roundNumber, 1);
expect(score.score, 10);
expect(score.change, 10);
});
test('Getting scores for a match works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
await database.scoreDao.addScore(
playerId: testPlayer2.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 20,
change: 20,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 2,
score: 25,
change: 15,
);
final scores = await database.scoreDao.getScoresForMatch(
matchId: testMatch1.id,
);
expect(scores.length, 3);
});
test('Getting player scores in a match works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 2,
score: 25,
change: 15,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 3,
score: 30,
change: 5,
);
final playerScores = await database.scoreDao.getPlayerScoresInMatch(
playerId: testPlayer1.id,
matchId: testMatch1.id,
);
expect(playerScores.length, 3);
expect(playerScores[0].roundNumber, 1);
expect(playerScores[1].roundNumber, 2);
expect(playerScores[2].roundNumber, 3);
expect(playerScores[0].score, 10);
expect(playerScores[1].score, 25);
expect(playerScores[2].score, 30);
});
test('Getting score for a non-existent round returns null', () async {
final score = await database.scoreDao.getScoreForRound(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 999,
);
expect(score, isNull);
});
test('Updating a score works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
final updated = await database.scoreDao.updateScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
newScore: 50,
newChange: 40,
);
expect(updated, true);
final score = await database.scoreDao.getScoreForRound(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
);
expect(score, isNotNull);
expect(score!.score, 50);
expect(score.change, 40);
});
test('Updating a non-existent score returns false', () async {
final updated = await database.scoreDao.updateScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 999,
newScore: 50,
newChange: 40,
);
expect(updated, false);
});
test('Deleting a score works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
final deleted = await database.scoreDao.deleteScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
);
expect(deleted, true);
final score = await database.scoreDao.getScoreForRound(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
);
expect(score, isNull);
});
test('Deleting a non-existent score returns false', () async {
final deleted = await database.scoreDao.deleteScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 999,
);
expect(deleted, false);
});
test('Deleting scores for a match works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
await database.scoreDao.addScore(
playerId: testPlayer2.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 20,
change: 20,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch2.id,
roundNumber: 1,
score: 15,
change: 15,
);
final deleted = await database.scoreDao.deleteScoresForMatch(
matchId: testMatch1.id,
);
expect(deleted, true);
final match1Scores = await database.scoreDao.getScoresForMatch(
matchId: testMatch1.id,
);
expect(match1Scores.length, 0);
final match2Scores = await database.scoreDao.getScoresForMatch(
matchId: testMatch2.id,
);
expect(match2Scores.length, 1);
});
test('Deleting scores for a player works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch2.id,
roundNumber: 1,
score: 15,
change: 15,
);
await database.scoreDao.addScore(
playerId: testPlayer2.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 20,
change: 20,
);
final deleted = await database.scoreDao.deleteScoresForPlayer(
playerId: testPlayer1.id,
);
expect(deleted, true);
final player1Scores = await database.scoreDao.getPlayerScoresInMatch(
playerId: testPlayer1.id,
matchId: testMatch1.id,
);
expect(player1Scores.length, 0);
final player2Scores = await database.scoreDao.getPlayerScoresInMatch(
playerId: testPlayer2.id,
matchId: testMatch1.id,
);
expect(player2Scores.length, 1);
});
test('Getting latest round number works correctly', () async {
var latestRound = await database.scoreDao.getLatestRoundNumber(
matchId: testMatch1.id,
);
expect(latestRound, 0);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
latestRound = await database.scoreDao.getLatestRoundNumber(
matchId: testMatch1.id,
);
expect(latestRound, 1);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 5,
score: 50,
change: 40,
);
latestRound = await database.scoreDao.getLatestRoundNumber(
matchId: testMatch1.id,
);
expect(latestRound, 5);
});
test('Getting total score for a player works correctly', () async {
var totalScore = await database.scoreDao.getTotalScoreForPlayer(
playerId: testPlayer1.id,
matchId: testMatch1.id,
);
expect(totalScore, 0);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 2,
score: 25,
change: 15,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 3,
score: 40,
change: 15,
);
totalScore = await database.scoreDao.getTotalScoreForPlayer(
playerId: testPlayer1.id,
matchId: testMatch1.id,
);
expect(totalScore, 40);
});
test('Adding the same score twice replaces the existing one', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 10,
change: 10,
);
await database.scoreDao.addScore(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
score: 99,
change: 99,
);
final score = await database.scoreDao.getScoreForRound(
playerId: testPlayer1.id,
matchId: testMatch1.id,
roundNumber: 1,
);
expect(score, isNotNull);
expect(score!.score, 99);
expect(score.change, 99);
});
});
}

View File

@@ -0,0 +1,226 @@
import 'package:clock/clock.dart';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:game_tracker/data/db/database.dart';
import 'package:game_tracker/data/dto/player.dart';
import 'package:game_tracker/data/dto/team.dart';
void main() {
late AppDatabase database;
late Player testPlayer1;
late Player testPlayer2;
late Player testPlayer3;
late Player testPlayer4;
late Team testTeam1;
late Team testTeam2;
late Team testTeam3;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
final fakeClock = Clock(() => fixedDate);
setUp(() async {
database = AppDatabase(
DatabaseConnection(
NativeDatabase.memory(),
// Recommended for widget tests to avoid test errors.
closeStreamsSynchronously: true,
),
);
withClock(fakeClock, () {
testPlayer1 = Player(name: 'Alice');
testPlayer2 = Player(name: 'Bob');
testPlayer3 = Player(name: 'Charlie');
testPlayer4 = Player(name: 'Diana');
testTeam1 = Team(
name: 'Team Alpha',
members: [testPlayer1, testPlayer2],
);
testTeam2 = Team(
name: 'Team Beta',
members: [testPlayer3, testPlayer4],
);
testTeam3 = Team(
name: 'Team Gamma',
members: [testPlayer1, testPlayer3],
);
});
await database.playerDao.addPlayersAsList(
players: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
);
});
tearDown(() async {
await database.close();
});
group('Team Tests', () {
test('Adding and fetching a single team works correctly', () async {
final added = await database.teamDao.addTeam(team: testTeam1);
expect(added, true);
final fetchedTeam = await database.teamDao.getTeamById(
teamId: testTeam1.id,
);
expect(fetchedTeam.id, testTeam1.id);
expect(fetchedTeam.name, testTeam1.name);
expect(fetchedTeam.createdAt, testTeam1.createdAt);
});
test('Adding and fetching multiple teams works correctly', () async {
await database.teamDao.addTeamsAsList(
teams: [testTeam1, testTeam2, testTeam3],
);
final allTeams = await database.teamDao.getAllTeams();
expect(allTeams.length, 3);
final testTeams = {
testTeam1.id: testTeam1,
testTeam2.id: testTeam2,
testTeam3.id: testTeam3,
};
for (final team in allTeams) {
final testTeam = testTeams[team.id]!;
expect(team.id, testTeam.id);
expect(team.name, testTeam.name);
expect(team.createdAt, testTeam.createdAt);
}
});
test('Adding the same team twice does not create duplicates', () async {
await database.teamDao.addTeam(team: testTeam1);
final addedAgain = await database.teamDao.addTeam(team: testTeam1);
expect(addedAgain, false);
final teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 1);
});
test('Team existence check works correctly', () async {
var teamExists = await database.teamDao.teamExists(teamId: testTeam1.id);
expect(teamExists, false);
await database.teamDao.addTeam(team: testTeam1);
teamExists = await database.teamDao.teamExists(teamId: testTeam1.id);
expect(teamExists, true);
});
test('Deleting a team works correctly', () async {
await database.teamDao.addTeam(team: testTeam1);
final teamDeleted = await database.teamDao.deleteTeam(
teamId: testTeam1.id,
);
expect(teamDeleted, true);
final teamExists = await database.teamDao.teamExists(
teamId: testTeam1.id,
);
expect(teamExists, false);
});
test('Deleting a non-existent team returns false', () async {
final teamDeleted = await database.teamDao.deleteTeam(
teamId: 'non-existent-id',
);
expect(teamDeleted, false);
});
test('Getting the team count works correctly', () async {
var teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 0);
await database.teamDao.addTeam(team: testTeam1);
teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 1);
await database.teamDao.addTeam(team: testTeam2);
teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 2);
await database.teamDao.deleteTeam(teamId: testTeam1.id);
teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 1);
await database.teamDao.deleteTeam(teamId: testTeam2.id);
teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 0);
});
test('Updating team name works correctly', () async {
await database.teamDao.addTeam(team: testTeam1);
var fetchedTeam = await database.teamDao.getTeamById(
teamId: testTeam1.id,
);
expect(fetchedTeam.name, testTeam1.name);
const newName = 'Updated Team Name';
await database.teamDao.updateTeamName(
teamId: testTeam1.id,
newName: newName,
);
fetchedTeam = await database.teamDao.getTeamById(teamId: testTeam1.id);
expect(fetchedTeam.name, newName);
});
test('Deleting all teams works correctly', () async {
await database.teamDao.addTeamsAsList(
teams: [testTeam1, testTeam2, testTeam3],
);
var teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 3);
final deleted = await database.teamDao.deleteAllTeams();
expect(deleted, true);
teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 0);
});
test('Deleting all teams when empty returns false', () async {
final deleted = await database.teamDao.deleteAllTeams();
expect(deleted, false);
});
test('Adding teams as list with empty list returns false', () async {
final added = await database.teamDao.addTeamsAsList(teams: []);
expect(added, false);
});
test('Adding teams with duplicate IDs ignores duplicates', () async {
final duplicateTeam = Team(
id: testTeam1.id,
name: 'Duplicate Team',
members: [testPlayer4],
);
await database.teamDao.addTeamsAsList(
teams: [testTeam1, duplicateTeam, testTeam2],
);
final teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 2);
// The first one should be kept (insertOrIgnore)
final fetchedTeam = await database.teamDao.getTeamById(
teamId: testTeam1.id,
);
expect(fetchedTeam.name, testTeam1.name);
});
});
}