4 Commits

Author SHA1 Message Date
gelbeinhalb
e09ccf9356 add comments to all tests
Some checks failed
Pull Request Pipeline / test (pull_request) Failing after 2m2s
Pull Request Pipeline / lint (pull_request) Successful in 2m9s
2026-01-20 15:58:16 +01:00
gelbeinhalb
b0b21bcba6 add new group tests 2026-01-20 15:48:40 +01:00
gelbeinhalb
dec74e9b62 add game tests 2026-01-20 15:41:05 +01:00
gelbeinhalb
4e73babb71 rename game_test to match_test 2026-01-20 15:28:26 +01:00
8 changed files with 1101 additions and 308 deletions

View File

@@ -1,370 +1,548 @@
import 'package:clock/clock.dart';
import 'package:drift/drift.dart';
import 'package:drift/drift.dart' hide isNull;
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/group.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 Player testPlayer4;
late Player testPlayer5;
late Group testGroup1;
late Group testGroup2;
late Game testGame;
late Match testMatch1;
late Match testMatch2;
late Match testMatchOnlyPlayers;
late Match testMatchOnlyGroup;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
late Game testGame1;
late Game testGame2;
late Game testGame3;
final fixedDate = DateTime(2025, 11, 19, 00, 11, 23);
final fakeClock = Clock(() => fixedDate);
setUp(() async {
setUp(() {
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');
testPlayer5 = Player(name: 'Eve');
testGroup1 = Group(
name: 'Test Group 2',
members: [testPlayer1, testPlayer2, testPlayer3],
testGame1 = Game(
name: 'Chess',
ruleset: 'winner.single',
description: 'A classic strategy game',
color: 0xFF0000FF,
icon: 'chess_icon',
);
testGroup2 = Group(
name: 'Test Group 2',
members: [testPlayer4, testPlayer5],
testGame2 = Game(
id: 'game2',
name: 'Poker',
ruleset: 'Texas Hold\'em rules',
description: 'winner.multiple',
color: 0xFFFF0000,
icon: 'poker_icon',
);
testGame = Game(name: 'Test Game');
testMatch1 = Match(
name: 'First Test Match',
game: testGame,
group: testGroup1,
players: [testPlayer4, testPlayer5],
winner: testPlayer4,
);
testMatch2 = Match(
name: 'Second Test Match',
game: testGame,
group: testGroup2,
players: [testPlayer1, testPlayer2, testPlayer3],
winner: testPlayer2,
);
testMatchOnlyPlayers = Match(
name: 'Test Match with Players',
game: testGame,
players: [testPlayer1, testPlayer2, testPlayer3],
winner: testPlayer3,
);
testMatchOnlyGroup = Match(
name: 'Test Match with Group',
game: testGame,
group: testGroup2,
testGame3 = Game(
id: 'game3',
name: 'Monopoly',
description: 'A board game about real estate',
);
});
await database.playerDao.addPlayersAsList(
players: [
testPlayer1,
testPlayer2,
testPlayer3,
testPlayer4,
testPlayer5,
],
);
await database.groupDao.addGroupsAsList(groups: [testGroup1, testGroup2]);
await database.gameDao.addGame(game: testGame);
});
tearDown(() async {
await database.close();
});
group('Match Tests', () {
test('Adding and fetching single match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
group('Game Tests', () {
final result = await database.matchDao.getMatchById(
matchId: testMatch1.id,
);
expect(result.id, testMatch1.id);
expect(result.name, testMatch1.name);
expect(result.createdAt, testMatch1.createdAt);
if (result.winner != null && testMatch1.winner != null) {
expect(result.winner!.id, testMatch1.winner!.id);
expect(result.winner!.name, testMatch1.winner!.name);
expect(result.winner!.createdAt, testMatch1.winner!.createdAt);
} else {
expect(result.winner, testMatch1.winner);
}
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');
}
if (result.players != 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,
);
}
} else {
fail('Players is null');
}
// Verifies that getAllGames returns an empty list when the database has no games.
test('getAllGames returns empty list when no games exist', () async {
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
test('Adding and fetching multiple matches works correctly', () async {
await database.matchDao.addMatchAsList(
matches: [
testMatch1,
testMatch2,
testMatchOnlyGroup,
testMatchOnlyPlayers,
],
);
// Verifies that a single game can be added and retrieved with all fields intact.
test('Adding and fetching a single game works correctly', () async {
await database.gameDao.addGame(game: testGame1);
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);
if (match.winner != null && testMatch.winner != null) {
expect(match.winner!.id, testMatch.winner!.id);
expect(match.winner!.name, testMatch.winner!.name);
expect(match.winner!.createdAt, testMatch.winner!.createdAt);
} else {
expect(match.winner, testMatch.winner);
}
// 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
if (testMatch.players != null) {
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,
);
}
} else {
expect(match.players, null);
}
}
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 1);
expect(allGames.first.id, testGame1.id);
expect(allGames.first.name, testGame1.name);
expect(allGames.first.ruleset, testGame1.ruleset);
expect(allGames.first.description, testGame1.description);
expect(allGames.first.color, testGame1.color);
expect(allGames.first.icon, testGame1.icon);
expect(allGames.first.createdAt, testGame1.createdAt);
});
test('Adding the same match twice does not create duplicates', () async {
await database.matchDao.addMatch(match: testMatch1);
await database.matchDao.addMatch(match: testMatch1);
// Verifies that multiple games can be added and retrieved correctly.
test('Adding and fetching multiple games works correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.addGame(game: testGame2);
await database.gameDao.addGame(game: testGame3);
final matchCount = await database.matchDao.getMatchCount();
expect(matchCount, 1);
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 3);
final names = allGames.map((g) => g.name).toList();
expect(names, containsAll(['Chess', 'Poker', 'Monopoly']));
});
test('Match existence check works correctly', () async {
var matchExists = await database.matchDao.matchExists(
matchId: testMatch1.id,
);
expect(matchExists, false);
// Verifies that getGameById returns the correct game with all properties.
test('getGameById returns correct game', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.addGame(game: testGame2);
await database.matchDao.addMatch(match: testMatch1);
matchExists = await database.matchDao.matchExists(matchId: testMatch1.id);
expect(matchExists, true);
final game = await database.gameDao.getGameById(gameId: testGame2.id);
expect(game.id, testGame2.id);
expect(game.name, testGame2.name);
expect(game.ruleset, testGame2.ruleset);
expect(game.description, testGame2.description);
expect(game.color, testGame2.color);
expect(game.icon, testGame2.icon);
});
test('Deleting a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
final matchDeleted = await database.matchDao.deleteMatch(
matchId: testMatch1.id,
// Verifies that getGameById throws a StateError when the game doesn't exist.
test('getGameById throws exception for non-existent game', () async {
expect(
() => database.gameDao.getGameById(gameId: 'non-existent-id'),
throwsA(isA<StateError>()),
);
expect(matchDeleted, true);
final matchExists = await database.matchDao.matchExists(
matchId: testMatch1.id,
);
expect(matchExists, false);
});
test('Getting the match count works correctly', () async {
var matchCount = await database.matchDao.getMatchCount();
expect(matchCount, 0);
// Verifies that addGame returns true when a game is successfully added.
test('addGame returns true when game is added successfully', () async {
final result = await database.gameDao.addGame(game: testGame1);
expect(result, true);
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);
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 1);
});
test('Checking if match has winner works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
await database.matchDao.addMatch(match: testMatchOnlyGroup);
// Verifies that addGame returns false when trying to add a duplicate game.
test('addGame returns false when game already exists', () async {
final firstAdd = await database.gameDao.addGame(game: testGame1);
expect(firstAdd, true);
var hasWinner = await database.matchDao.hasWinner(matchId: testMatch1.id);
expect(hasWinner, true);
final secondAdd = await database.gameDao.addGame(game: testGame1);
expect(secondAdd, false);
hasWinner = await database.matchDao.hasWinner(
matchId: testMatchOnlyGroup.id,
);
expect(hasWinner, false);
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 1);
});
test('Fetching the winner of a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
// Verifies that a game with null optional fields can be added and retrieved.
test('addGame handles game with null optional fields', () async {
final gameWithNulls = Game(name: 'Simple Game');
final result = await database.gameDao.addGame(game: gameWithNulls);
expect(result, true);
final winner = await database.matchDao.getWinner(matchId: testMatch1.id);
if (winner == null) {
fail('Winner is null');
} else {
expect(winner.id, testMatch1.winner!.id);
expect(winner.name, testMatch1.winner!.name);
expect(winner.createdAt, testMatch1.winner!.createdAt);
}
final fetchedGame = await database.gameDao.getGameById(
gameId: gameWithNulls.id,
);
expect(fetchedGame.name, 'Simple Game');
expect(fetchedGame.description, isNull);
expect(fetchedGame.color, isNull);
expect(fetchedGame.icon, isNull);
});
test('Updating the winner of a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
final winner = await database.matchDao.getWinner(matchId: testMatch1.id);
if (winner == null) {
fail('Winner is null');
} else {
expect(winner.id, testMatch1.winner!.id);
expect(winner.name, testMatch1.winner!.name);
expect(winner.createdAt, testMatch1.winner!.createdAt);
expect(winner.id, testPlayer4.id);
expect(winner.id != testPlayer5.id, true);
}
await database.matchDao.setWinner(
matchId: testMatch1.id,
winnerId: testPlayer5.id,
// Verifies that multiple games can be added at once using addGamesAsList.
test('addGamesAsList adds multiple games correctly', () async {
final result = await database.gameDao.addGamesAsList(
games: [testGame1, testGame2, testGame3],
);
expect(result, true);
final newWinner = await database.matchDao.getWinner(
matchId: testMatch1.id,
);
if (newWinner == null) {
fail('New winner is null');
} else {
expect(newWinner.id, testPlayer5.id);
expect(newWinner.name, testPlayer5.name);
expect(newWinner.createdAt, testPlayer5.createdAt);
}
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 3);
});
test('Removing a winner works correctly', () async {
await database.matchDao.addMatch(match: testMatch2);
// Verifies that addGamesAsList returns false when given an empty list.
test('addGamesAsList returns false for empty list', () async {
final result = await database.gameDao.addGamesAsList(games: []);
expect(result, false);
var hasWinner = await database.matchDao.hasWinner(matchId: testMatch2.id);
expect(hasWinner, true);
await database.matchDao.removeWinner(matchId: testMatch2.id);
hasWinner = await database.matchDao.hasWinner(matchId: testMatch2.id);
expect(hasWinner, false);
final removedWinner = await database.matchDao.getWinner(
matchId: testMatch2.id,
);
expect(removedWinner, null);
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 0);
});
test('Renaming a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
// Verifies that addGamesAsList ignores duplicate games when adding.
test('addGamesAsList ignores duplicate games', () async {
await database.gameDao.addGame(game: testGame1);
var fetchedMatch = await database.matchDao.getMatchById(
matchId: testMatch1.id,
final result = await database.gameDao.addGamesAsList(
games: [testGame1, testGame2],
);
expect(fetchedMatch.name, testMatch1.name);
expect(result, true);
const newName = 'Updated Match Name';
await database.matchDao.updateMatchName(
matchId: testMatch1.id,
newName: newName,
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 2);
});
// Verifies that deleteGame returns true and removes the game from database.
test('deleteGame returns true when game is deleted', () async {
await database.gameDao.addGame(game: testGame1);
final result = await database.gameDao.deleteGame(gameId: testGame1.id);
expect(result, true);
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
// Verifies that deleteGame returns false for a non-existent game ID.
test('deleteGame returns false for non-existent game', () async {
final result = await database.gameDao.deleteGame(
gameId: 'non-existent-id',
);
expect(result, false);
});
// Verifies that deleteGame only removes the specified game, leaving others intact.
test('deleteGame only deletes the specified game', () async {
await database.gameDao.addGamesAsList(
games: [testGame1, testGame2, testGame3],
);
fetchedMatch = await database.matchDao.getMatchById(
matchId: testMatch1.id,
await database.gameDao.deleteGame(gameId: testGame2.id);
final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 2);
expect(allGames.any((g) => g.id == testGame2.id), false);
expect(allGames.any((g) => g.id == testGame1.id), true);
expect(allGames.any((g) => g.id == testGame3.id), true);
});
// Verifies that gameExists returns true when the game exists in database.
test('gameExists returns true for existing game', () async {
await database.gameDao.addGame(game: testGame1);
final exists = await database.gameDao.gameExists(gameId: testGame1.id);
expect(exists, true);
});
// Verifies that gameExists returns false for a non-existent game ID.
test('gameExists returns false for non-existent game', () async {
final exists = await database.gameDao.gameExists(
gameId: 'non-existent-id',
);
expect(fetchedMatch.name, newName);
expect(exists, false);
});
// Verifies that gameExists returns false after a game has been deleted.
test('gameExists returns false after game is deleted', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.deleteGame(gameId: testGame1.id);
final exists = await database.gameDao.gameExists(gameId: testGame1.id);
expect(exists, false);
});
// Verifies that updateGameName correctly updates only the name field.
test('updateGameName updates the name correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameName(
gameId: testGame1.id,
newName: 'Updated Chess',
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.name, 'Updated Chess');
expect(updatedGame.ruleset, testGame1.ruleset);
});
// Verifies that updateGameName does nothing when game doesn't exist.
test('updateGameName does nothing for non-existent game', () async {
await database.gameDao.updateGameName(
gameId: 'non-existent-id',
newName: 'New Name',
);
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
// Verifies that updateGameRuleset correctly updates only the ruleset field.
test('updateGameRuleset updates the ruleset correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameRuleset(
gameId: testGame1.id,
newRuleset: 'New ruleset for chess',
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.ruleset, 'New ruleset for chess');
expect(updatedGame.name, testGame1.name);
});
// Verifies that updateGameRuleset does nothing when game doesn't exist.
test('updateGameRuleset does nothing for non-existent game', () async {
await database.gameDao.updateGameRuleset(
gameId: 'non-existent-id',
newRuleset: 'New Ruleset',
);
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
// Verifies that updateGameDescription correctly updates the description.
test('updateGameDescription updates the description correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameDescription(
gameId: testGame1.id,
newDescription: 'An updated description',
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.description, 'An updated description');
});
// Verifies that updateGameDescription can set the description to null.
test('updateGameDescription can set description to null', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameDescription(
gameId: testGame1.id,
newDescription: null,
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.description, isNull);
});
// Verifies that updateGameDescription does nothing when game doesn't exist.
test('updateGameDescription does nothing for non-existent game', () async {
await database.gameDao.updateGameDescription(
gameId: 'non-existent-id',
newDescription: 'New Description',
);
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
// Verifies that updateGameColor correctly updates the color value.
test('updateGameColor updates the color correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameColor(
gameId: testGame1.id,
newColor: 0xFF00FF00,
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.color, 0xFF00FF00);
});
// Verifies that updateGameColor can set the color to null.
test('updateGameColor can set color to null', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameColor(
gameId: testGame1.id,
newColor: null,
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.color, isNull);
});
// Verifies that updateGameColor does nothing when game doesn't exist.
test('updateGameColor does nothing for non-existent game', () async {
await database.gameDao.updateGameColor(
gameId: 'non-existent-id',
newColor: 0xFF00FF00,
);
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
// Verifies that updateGameIcon correctly updates the icon value.
test('updateGameIcon updates the icon correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameIcon(
gameId: testGame1.id,
newIcon: 'new_chess_icon',
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.icon, 'new_chess_icon');
});
// Verifies that updateGameIcon can set the icon to null.
test('updateGameIcon can set icon to null', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameIcon(
gameId: testGame1.id,
newIcon: null,
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.icon, isNull);
});
// Verifies that updateGameIcon does nothing when game doesn't exist.
test('updateGameIcon does nothing for non-existent game', () async {
await database.gameDao.updateGameIcon(
gameId: 'non-existent-id',
newIcon: 'some_icon',
);
final allGames = await database.gameDao.getAllGames();
expect(allGames, isEmpty);
});
// Verifies that getGameCount returns 0 when no games exist.
test('getGameCount returns 0 when no games exist', () async {
final count = await database.gameDao.getGameCount();
expect(count, 0);
});
// Verifies that getGameCount returns the correct count after adding games.
test('getGameCount returns correct count after adding games', () async {
await database.gameDao.addGamesAsList(
games: [testGame1, testGame2, testGame3],
);
final count = await database.gameDao.getGameCount();
expect(count, 3);
});
// Verifies that getGameCount updates correctly after deleting a game.
test('getGameCount updates correctly after deletion', () async {
await database.gameDao.addGamesAsList(
games: [testGame1, testGame2],
);
final countBefore = await database.gameDao.getGameCount();
expect(countBefore, 2);
await database.gameDao.deleteGame(gameId: testGame1.id);
final countAfter = await database.gameDao.getGameCount();
expect(countAfter, 1);
});
// Verifies that deleteAllGames removes all games from the database.
test('deleteAllGames removes all games', () async {
await database.gameDao.addGamesAsList(
games: [testGame1, testGame2, testGame3],
);
final countBefore = await database.gameDao.getGameCount();
expect(countBefore, 3);
final result = await database.gameDao.deleteAllGames();
expect(result, true);
final countAfter = await database.gameDao.getGameCount();
expect(countAfter, 0);
});
// Verifies that deleteAllGames returns false when no games exist.
test('deleteAllGames returns false when no games exist', () async {
final result = await database.gameDao.deleteAllGames();
expect(result, false);
});
// Verifies that games with special characters (quotes, emojis) are stored correctly.
test('Game with special characters in name is stored correctly', () async {
final specialGame = Game(
name: 'Game\'s & "Special" <Name>',
description: 'Description with émojis 🎮🎲',
);
await database.gameDao.addGame(game: specialGame);
final fetchedGame = await database.gameDao.getGameById(
gameId: specialGame.id,
);
expect(fetchedGame.name, 'Game\'s & "Special" <Name>');
expect(fetchedGame.description, 'Description with émojis 🎮🎲');
});
// Verifies that games with empty string fields are stored and retrieved correctly.
test('Game with empty string fields is stored correctly', () async {
final emptyGame = Game(
name: '',
ruleset: '',
description: '',
icon: '',
);
await database.gameDao.addGame(game: emptyGame);
final fetchedGame = await database.gameDao.getGameById(
gameId: emptyGame.id,
);
expect(fetchedGame.name, '');
expect(fetchedGame.ruleset, '');
expect(fetchedGame.description, '');
expect(fetchedGame.icon, '');
});
// Verifies that games with very long strings (10000 chars) are handled correctly.
test('Game with very long strings is stored correctly', () async {
final longString = 'A' * 10000;
final longGame = Game(
name: longString,
description: longString,
ruleset: longString,
);
await database.gameDao.addGame(game: longGame);
final fetchedGame = await database.gameDao.getGameById(
gameId: longGame.id,
);
expect(fetchedGame.name.length, 10000);
expect(fetchedGame.description?.length, 10000);
expect(fetchedGame.ruleset?.length, 10000);
});
// Verifies that multiple sequential updates to the same game work correctly.
test('Multiple updates to the same game work correctly', () async {
await database.gameDao.addGame(game: testGame1);
await database.gameDao.updateGameName(
gameId: testGame1.id,
newName: 'Updated Name',
);
await database.gameDao.updateGameColor(
gameId: testGame1.id,
newColor: 0xFF123456,
);
await database.gameDao.updateGameDescription(
gameId: testGame1.id,
newDescription: 'Updated Description',
);
final updatedGame = await database.gameDao.getGameById(
gameId: testGame1.id,
);
expect(updatedGame.name, 'Updated Name');
expect(updatedGame.color, 0xFF123456);
expect(updatedGame.description, 'Updated Description');
expect(updatedGame.ruleset, testGame1.ruleset);
expect(updatedGame.icon, testGame1.icon);
});
});
}

View File

@@ -1,5 +1,5 @@
import 'package:clock/clock.dart';
import 'package:drift/drift.dart';
import 'package:drift/drift.dart' hide isNull;
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:game_tracker/data/db/database.dart';
@@ -58,6 +58,8 @@ void main() {
await database.close();
});
group('Group Tests', () {
// Verifies that a single group can be added and retrieved with all fields and members intact.
test('Adding and fetching a single group works correctly', () async {
await database.groupDao.addGroup(group: testGroup1);
@@ -80,6 +82,7 @@ void main() {
}
});
// Verifies that multiple groups can be added and retrieved with correct members.
test('Adding and fetching multiple groups works correctly', () async {
await database.groupDao.addGroupsAsList(
groups: [testGroup1, testGroup2, testGroup3, testGroup4],
@@ -106,6 +109,7 @@ void main() {
}
});
// Verifies that adding the same group twice does not create duplicates.
test('Adding the same group twice does not create duplicates', () async {
await database.groupDao.addGroup(group: testGroup1);
await database.groupDao.addGroup(group: testGroup1);
@@ -114,6 +118,7 @@ void main() {
expect(allGroups.length, 1);
});
// Verifies that groupExists returns correct boolean based on group presence.
test('Group existence check works correctly', () async {
var groupExists = await database.groupDao.groupExists(
groupId: testGroup1.id,
@@ -126,6 +131,7 @@ void main() {
expect(groupExists, true);
});
// Verifies that deleteGroup removes the group and returns true.
test('Deleting a group works correctly', () async {
await database.groupDao.addGroup(group: testGroup1);
@@ -140,6 +146,7 @@ void main() {
expect(groupExists, false);
});
// Verifies that updateGroupName correctly updates only the name field.
test('Updating a group name works correcly', () async {
await database.groupDao.addGroup(group: testGroup1);
@@ -156,6 +163,7 @@ void main() {
expect(result.name, newGroupName);
});
// Verifies that getGroupCount returns correct count through add/delete operations.
test('Getting the group count works correctly', () async {
final initialCount = await database.groupDao.getGroupCount();
expect(initialCount, 0);
@@ -173,5 +181,189 @@ void main() {
final finalCount = await database.groupDao.getGroupCount();
expect(finalCount, 0);
});
// Verifies that getAllGroups returns an empty list when no groups exist.
test('getAllGroups returns empty list when no groups exist', () async {
final allGroups = await database.groupDao.getAllGroups();
expect(allGroups, isEmpty);
});
// Verifies that getGroupById throws StateError for non-existent group ID.
test('getGroupById throws exception for non-existent group', () async {
expect(
() => database.groupDao.getGroupById(groupId: 'non-existent-id'),
throwsA(isA<StateError>()),
);
});
// Verifies that addGroup returns false when trying to add a duplicate group.
test('addGroup returns false when group already exists', () async {
final firstAdd = await database.groupDao.addGroup(group: testGroup1);
expect(firstAdd, true);
final secondAdd = await database.groupDao.addGroup(group: testGroup1);
expect(secondAdd, false);
final allGroups = await database.groupDao.getAllGroups();
expect(allGroups.length, 1);
});
// Verifies that addGroupsAsList handles an empty list without errors.
test('addGroupsAsList handles empty list correctly', () async {
await database.groupDao.addGroupsAsList(groups: []);
final allGroups = await database.groupDao.getAllGroups();
expect(allGroups.length, 0);
});
// Verifies that deleteGroup returns false for a non-existent group ID.
test('deleteGroup returns false for non-existent group', () async {
final deleted = await database.groupDao.deleteGroup(
groupId: 'non-existent-id',
);
expect(deleted, false);
});
// Verifies that updateGroupName returns false for a non-existent group ID.
test('updateGroupName returns false for non-existent group', () async {
final updated = await database.groupDao.updateGroupName(
groupId: 'non-existent-id',
newName: 'New Name',
);
expect(updated, false);
});
// Verifies that updateGroupDescription correctly updates the description field.
test('Updating a group description works correctly', () async {
await database.groupDao.addGroup(group: testGroup1);
const newDescription = 'This is a new description';
final updated = await database.groupDao.updateGroupDescription(
groupId: testGroup1.id,
newDescription: newDescription,
);
expect(updated, true);
final result = await database.groupDao.getGroupById(
groupId: testGroup1.id,
);
expect(result.description, newDescription);
});
// Verifies that updateGroupDescription can set the description to null.
test('updateGroupDescription can set description to null', () async {
final groupWithDescription = Group(
name: 'Group with description',
description: 'Initial description',
members: [testPlayer1],
);
await database.groupDao.addGroup(group: groupWithDescription);
final updated = await database.groupDao.updateGroupDescription(
groupId: groupWithDescription.id,
newDescription: null,
);
expect(updated, true);
final result = await database.groupDao.getGroupById(
groupId: groupWithDescription.id,
);
expect(result.description, isNull);
});
// Verifies that updateGroupDescription returns false for a non-existent group.
test('updateGroupDescription returns false for non-existent group',
() async {
final updated = await database.groupDao.updateGroupDescription(
groupId: 'non-existent-id',
newDescription: 'New Description',
);
expect(updated, false);
});
// Verifies that deleteAllGroups removes all groups from the database.
test('deleteAllGroups removes all groups', () async {
await database.groupDao.addGroupsAsList(
groups: [testGroup1, testGroup2],
);
final countBefore = await database.groupDao.getGroupCount();
expect(countBefore, 2);
final deleted = await database.groupDao.deleteAllGroups();
expect(deleted, true);
final countAfter = await database.groupDao.getGroupCount();
expect(countAfter, 0);
});
// Verifies that deleteAllGroups returns false when no groups exist.
test('deleteAllGroups returns false when no groups exist', () async {
final deleted = await database.groupDao.deleteAllGroups();
expect(deleted, false);
});
// Verifies that groups with special characters (quotes, emojis) are stored correctly.
test('Group with special characters in name is stored correctly', () async {
final specialGroup = Group(
name: 'Group\'s & "Special" <Name>',
description: 'Description with émojis 🎮🎲',
members: [testPlayer1],
);
await database.groupDao.addGroup(group: specialGroup);
final fetchedGroup = await database.groupDao.getGroupById(
groupId: specialGroup.id,
);
expect(fetchedGroup.name, 'Group\'s & "Special" <Name>');
expect(fetchedGroup.description, 'Description with émojis 🎮🎲');
});
// Verifies that a group with an empty members list can be stored and retrieved.
test('Group with empty members list is stored correctly', () async {
final emptyGroup = Group(
name: 'Empty Group',
members: [],
);
await database.groupDao.addGroup(group: emptyGroup);
final fetchedGroup = await database.groupDao.getGroupById(
groupId: emptyGroup.id,
);
expect(fetchedGroup.name, 'Empty Group');
expect(fetchedGroup.members, isEmpty);
});
// Verifies that multiple sequential updates to the same group work correctly.
test('Multiple updates to the same group work correctly', () async {
await database.groupDao.addGroup(group: testGroup1);
await database.groupDao.updateGroupName(
groupId: testGroup1.id,
newName: 'Updated Name',
);
await database.groupDao.updateGroupDescription(
groupId: testGroup1.id,
newDescription: 'Updated Description',
);
final updatedGroup = await database.groupDao.getGroupById(
groupId: testGroup1.id,
);
expect(updatedGroup.name, 'Updated Name');
expect(updatedGroup.description, 'Updated Description');
expect(updatedGroup.members.length, testGroup1.members.length);
});
// Verifies that addGroupsAsList with duplicate groups only adds unique ones.
test('addGroupsAsList with duplicate groups only adds once', () async {
await database.groupDao.addGroupsAsList(
groups: [testGroup1, testGroup1, testGroup1],
);
final allGroups = await database.groupDao.getAllGroups();
expect(allGroups.length, 1);
});
});
}

View File

@@ -0,0 +1,382 @@
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/game.dart';
import 'package:game_tracker/data/dto/group.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 Player testPlayer4;
late Player testPlayer5;
late Group testGroup1;
late Group testGroup2;
late Game testGame;
late Match testMatch1;
late Match testMatch2;
late Match testMatchOnlyPlayers;
late Match testMatchOnlyGroup;
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');
testPlayer5 = Player(name: 'Eve');
testGroup1 = Group(
name: 'Test Group 2',
members: [testPlayer1, testPlayer2, testPlayer3],
);
testGroup2 = Group(
name: 'Test Group 2',
members: [testPlayer4, testPlayer5],
);
testGame = Game(name: 'Test Game');
testMatch1 = Match(
name: 'First Test Match',
game: testGame,
group: testGroup1,
players: [testPlayer4, testPlayer5],
winner: testPlayer4,
);
testMatch2 = Match(
name: 'Second Test Match',
game: testGame,
group: testGroup2,
players: [testPlayer1, testPlayer2, testPlayer3],
winner: testPlayer2,
);
testMatchOnlyPlayers = Match(
name: 'Test Match with Players',
game: testGame,
players: [testPlayer1, testPlayer2, testPlayer3],
winner: testPlayer3,
);
testMatchOnlyGroup = Match(
name: 'Test Match with Group',
game: testGame,
group: testGroup2,
);
});
await database.playerDao.addPlayersAsList(
players: [
testPlayer1,
testPlayer2,
testPlayer3,
testPlayer4,
testPlayer5,
],
);
await database.groupDao.addGroupsAsList(groups: [testGroup1, testGroup2]);
await database.gameDao.addGame(game: testGame);
});
tearDown(() async {
await database.close();
});
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);
final result = await database.matchDao.getMatchById(
matchId: testMatch1.id,
);
expect(result.id, testMatch1.id);
expect(result.name, testMatch1.name);
expect(result.createdAt, testMatch1.createdAt);
if (result.winner != null && testMatch1.winner != null) {
expect(result.winner!.id, testMatch1.winner!.id);
expect(result.winner!.name, testMatch1.winner!.name);
expect(result.winner!.createdAt, testMatch1.winner!.createdAt);
} else {
expect(result.winner, testMatch1.winner);
}
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');
}
if (result.players != 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,
);
}
} else {
fail('Players is null');
}
});
// 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);
if (match.winner != null && testMatch.winner != null) {
expect(match.winner!.id, testMatch.winner!.id);
expect(match.winner!.name, testMatch.winner!.name);
expect(match.winner!.createdAt, testMatch.winner!.createdAt);
} else {
expect(match.winner, testMatch.winner);
}
// 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
if (testMatch.players != null) {
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,
);
}
} else {
expect(match.players, null);
}
}
});
// 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);
final matchCount = await database.matchDao.getMatchCount();
expect(matchCount, 1);
});
// 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);
await database.matchDao.addMatch(match: testMatch1);
matchExists = await database.matchDao.matchExists(matchId: testMatch1.id);
expect(matchExists, true);
});
// Verifies that deleteMatch removes the match and returns true.
test('Deleting a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
final matchDeleted = await database.matchDao.deleteMatch(
matchId: testMatch1.id,
);
expect(matchDeleted, true);
final matchExists = await database.matchDao.matchExists(
matchId: testMatch1.id,
);
expect(matchExists, false);
});
// 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.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);
});
// Verifies that hasWinner correctly identifies matches with and without winners.
test('Checking if match has winner works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
await database.matchDao.addMatch(match: testMatchOnlyGroup);
var hasWinner = await database.matchDao.hasWinner(matchId: testMatch1.id);
expect(hasWinner, true);
hasWinner = await database.matchDao.hasWinner(
matchId: testMatchOnlyGroup.id,
);
expect(hasWinner, false);
});
// Verifies that getWinner returns the correct winner player for a match.
test('Fetching the winner of a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
final winner = await database.matchDao.getWinner(matchId: testMatch1.id);
if (winner == null) {
fail('Winner is null');
} else {
expect(winner.id, testMatch1.winner!.id);
expect(winner.name, testMatch1.winner!.name);
expect(winner.createdAt, testMatch1.winner!.createdAt);
}
});
// Verifies that setWinner correctly updates the winner of a match.
test('Updating the winner of a match works correctly', () async {
await database.matchDao.addMatch(match: testMatch1);
final winner = await database.matchDao.getWinner(matchId: testMatch1.id);
if (winner == null) {
fail('Winner is null');
} else {
expect(winner.id, testMatch1.winner!.id);
expect(winner.name, testMatch1.winner!.name);
expect(winner.createdAt, testMatch1.winner!.createdAt);
expect(winner.id, testPlayer4.id);
expect(winner.id != testPlayer5.id, true);
}
await database.matchDao.setWinner(
matchId: testMatch1.id,
winnerId: testPlayer5.id,
);
final newWinner = await database.matchDao.getWinner(
matchId: testMatch1.id,
);
if (newWinner == null) {
fail('New winner is null');
} else {
expect(newWinner.id, testPlayer5.id);
expect(newWinner.name, testPlayer5.name);
expect(newWinner.createdAt, testPlayer5.createdAt);
}
});
// Verifies that removeWinner clears the winner and hasWinner returns false.
test('Removing a winner works correctly', () async {
await database.matchDao.addMatch(match: testMatch2);
var hasWinner = await database.matchDao.hasWinner(matchId: testMatch2.id);
expect(hasWinner, true);
await database.matchDao.removeWinner(matchId: testMatch2.id);
hasWinner = await database.matchDao.hasWinner(matchId: testMatch2.id);
expect(hasWinner, false);
final removedWinner = await database.matchDao.getWinner(
matchId: testMatch2.id,
);
expect(removedWinner, null);
});
// 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);
});
});
}

View File

@@ -41,9 +41,8 @@ void main() {
});
group('Player-Group Tests', () {
/// No need to test if group has players since the members attribute is
/// not nullable
// Verifies that a player can be added to an existing group and isPlayerInGroup returns true.
test('Adding a player to a group works correctly', () async {
await database.groupDao.addGroup(group: testGroup);
await database.playerDao.addPlayer(player: testPlayer4);
@@ -67,6 +66,7 @@ void main() {
expect(playerAdded, false);
});
// Verifies that a player can be removed from a group and the group's member count decreases.
test('Removing player from group works correctly', () async {
await database.groupDao.addGroup(group: testGroup);
@@ -87,6 +87,7 @@ void main() {
expect(playerExists, false);
});
// Verifies that getPlayersOfGroup returns all members of a group with correct data.
test('Retrieving players of a group works correctly', () async {
await database.groupDao.addGroup(group: testGroup);
final players = await database.playerGroupDao.getPlayersOfGroup(

View File

@@ -73,6 +73,8 @@ void main() {
});
group('Player-Match Tests', () {
// Verifies that matchHasPlayers returns false initially and true after adding a player.
test('Match has player works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.playerDao.addPlayer(player: testPlayer1);
@@ -95,6 +97,7 @@ void main() {
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 {
await database.matchDao.addMatch(match: testMatchOnlyGroup);
await database.playerDao.addPlayer(player: testPlayer5);
@@ -118,6 +121,7 @@ void main() {
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 {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
@@ -140,6 +144,7 @@ void main() {
expect(playerExists, false);
});
// Verifies that getPlayersOfMatch returns all players of a match with correct data.
test('Retrieving players of a match works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
final players = await database.playerMatchDao.getPlayersOfMatch(
@@ -160,6 +165,7 @@ void main() {
}
});
// Verifies that updatePlayersFromMatch replaces all existing players with new ones.
test('Updating the match players works correctly', () async {
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
@@ -203,6 +209,7 @@ void main() {
}
});
// Verifies that the same player can be added to multiple different matches.
test(
'Adding the same player to separate matches works correctly',
() async {

View File

@@ -35,6 +35,8 @@ void main() {
});
group('Player Tests', () {
// Verifies that players can be added and retrieved with all fields intact.
test('Adding and fetching single player works correctly', () async {
await database.playerDao.addPlayer(player: testPlayer1);
await database.playerDao.addPlayer(player: testPlayer2);
@@ -55,6 +57,7 @@ void main() {
expect(fetchedPlayer2.createdAt, testPlayer2.createdAt);
});
// Verifies that multiple players can be added at once and retrieved correctly.
test('Adding and fetching multiple players works correctly', () async {
await database.playerDao.addPlayersAsList(
players: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
@@ -80,6 +83,7 @@ void main() {
}
});
// Verifies that adding the same player twice does not create duplicates.
test('Adding the same player twice does not create duplicates', () async {
await database.playerDao.addPlayer(player: testPlayer1);
await database.playerDao.addPlayer(player: testPlayer1);
@@ -88,6 +92,7 @@ void main() {
expect(allPlayers.length, 1);
});
// Verifies that playerExists returns correct boolean based on player presence.
test('Player existence check works correctly', () async {
var playerExists = await database.playerDao.playerExists(
playerId: testPlayer1.id,
@@ -102,6 +107,7 @@ void main() {
expect(playerExists, true);
});
// Verifies that deletePlayer removes the player and returns true.
test('Deleting a player works correctly', () async {
await database.playerDao.addPlayer(player: testPlayer1);
final playerDeleted = await database.playerDao.deletePlayer(
@@ -115,6 +121,7 @@ void main() {
expect(playerExists, false);
});
// Verifies that updatePlayerName correctly updates only the name field.
test('Updating a player name works correctly', () async {
await database.playerDao.addPlayer(player: testPlayer1);
@@ -131,6 +138,7 @@ void main() {
expect(result.name, newPlayerName);
});
// Verifies that getPlayerCount returns correct count through add/delete operations.
test('Getting the player count works correctly', () async {
var playerCount = await database.playerDao.getPlayerCount();
expect(playerCount, 0);

View File

@@ -57,6 +57,8 @@ void main() {
});
group('Score Tests', () {
// Verifies that a score can be added and retrieved with all fields intact.
test('Adding and fetching a score works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -80,6 +82,7 @@ void main() {
expect(score.change, 10);
});
// Verifies that getScoresForMatch returns all scores for a given match.
test('Getting scores for a match works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -110,6 +113,7 @@ void main() {
expect(scores.length, 3);
});
// Verifies that getPlayerScoresInMatch returns all scores for a player in a match, ordered by round.
test('Getting player scores in a match works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -147,6 +151,7 @@ void main() {
expect(playerScores[2].score, 30);
});
// Verifies that getScoreForRound returns null for a non-existent round number.
test('Getting score for a non-existent round returns null', () async {
final score = await database.scoreDao.getScoreForRound(
playerId: testPlayer1.id,
@@ -157,6 +162,7 @@ void main() {
expect(score, isNull);
});
// Verifies that updateScore correctly updates the score and change values.
test('Updating a score works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -187,6 +193,7 @@ void main() {
expect(score.change, 40);
});
// Verifies that updateScore returns false for a non-existent score entry.
test('Updating a non-existent score returns false', () async {
final updated = await database.scoreDao.updateScore(
playerId: testPlayer1.id,
@@ -199,6 +206,7 @@ void main() {
expect(updated, false);
});
// Verifies that deleteScore removes the score entry and returns true.
test('Deleting a score works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -225,6 +233,7 @@ void main() {
expect(score, isNull);
});
// Verifies that deleteScore returns false for a non-existent score entry.
test('Deleting a non-existent score returns false', () async {
final deleted = await database.scoreDao.deleteScore(
playerId: testPlayer1.id,
@@ -235,6 +244,7 @@ void main() {
expect(deleted, false);
});
// Verifies that deleteScoresForMatch removes all scores for a match but keeps other match scores.
test('Deleting scores for a match works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -275,6 +285,7 @@ void main() {
expect(match2Scores.length, 1);
});
// Verifies that deleteScoresForPlayer removes all scores for a player across all matches.
test('Deleting scores for a player works correctly', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -317,6 +328,7 @@ void main() {
expect(player2Scores.length, 1);
});
// Verifies that getLatestRoundNumber returns the highest round number for a match.
test('Getting latest round number works correctly', () async {
var latestRound = await database.scoreDao.getLatestRoundNumber(
matchId: testMatch1.id,
@@ -350,6 +362,7 @@ void main() {
expect(latestRound, 5);
});
// Verifies that getTotalScoreForPlayer returns the latest score (cumulative) for a player.
test('Getting total score for a player works correctly', () async {
var totalScore = await database.scoreDao.getTotalScoreForPlayer(
playerId: testPlayer1.id,
@@ -386,6 +399,7 @@ void main() {
expect(totalScore, 40);
});
// Verifies that adding a score with the same player/match/round replaces the existing one.
test('Adding the same score twice replaces the existing one', () async {
await database.scoreDao.addScore(
playerId: testPlayer1.id,
@@ -414,4 +428,3 @@ void main() {
});
});
}

View File

@@ -56,6 +56,8 @@ void main() {
});
group('Team Tests', () {
// Verifies that a single team can be added and retrieved with all fields intact.
test('Adding and fetching a single team works correctly', () async {
final added = await database.teamDao.addTeam(team: testTeam1);
expect(added, true);
@@ -69,6 +71,7 @@ void main() {
expect(fetchedTeam.createdAt, testTeam1.createdAt);
});
// Verifies that multiple teams can be added at once and retrieved correctly.
test('Adding and fetching multiple teams works correctly', () async {
await database.teamDao.addTeamsAsList(
teams: [testTeam1, testTeam2, testTeam3],
@@ -92,6 +95,7 @@ void main() {
}
});
// Verifies that adding the same team twice does not create duplicates and returns false.
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);
@@ -102,6 +106,7 @@ void main() {
expect(teamCount, 1);
});
// Verifies that teamExists returns correct boolean based on team presence.
test('Team existence check works correctly', () async {
var teamExists = await database.teamDao.teamExists(teamId: testTeam1.id);
expect(teamExists, false);
@@ -112,6 +117,7 @@ void main() {
expect(teamExists, true);
});
// Verifies that deleteTeam removes the team and returns true.
test('Deleting a team works correctly', () async {
await database.teamDao.addTeam(team: testTeam1);
@@ -126,6 +132,7 @@ void main() {
expect(teamExists, false);
});
// Verifies that deleteTeam returns false for a non-existent team ID.
test('Deleting a non-existent team returns false', () async {
final teamDeleted = await database.teamDao.deleteTeam(
teamId: 'non-existent-id',
@@ -133,6 +140,7 @@ void main() {
expect(teamDeleted, false);
});
// Verifies that getTeamCount returns correct count through add/delete operations.
test('Getting the team count works correctly', () async {
var teamCount = await database.teamDao.getTeamCount();
expect(teamCount, 0);
@@ -158,6 +166,7 @@ void main() {
expect(teamCount, 0);
});
// Verifies that updateTeamName correctly updates only the name field.
test('Updating team name works correctly', () async {
await database.teamDao.addTeam(team: testTeam1);
@@ -176,6 +185,7 @@ void main() {
expect(fetchedTeam.name, newName);
});
// Verifies that deleteAllTeams removes all teams from the database.
test('Deleting all teams works correctly', () async {
await database.teamDao.addTeamsAsList(
teams: [testTeam1, testTeam2, testTeam3],
@@ -191,16 +201,19 @@ void main() {
expect(teamCount, 0);
});
// Verifies that deleteAllTeams returns false when no teams exist.
test('Deleting all teams when empty returns false', () async {
final deleted = await database.teamDao.deleteAllTeams();
expect(deleted, false);
});
// Verifies that addTeamsAsList returns false when given an empty list.
test('Adding teams as list with empty list returns false', () async {
final added = await database.teamDao.addTeamsAsList(teams: []);
expect(added, false);
});
// Verifies that addTeamsAsList with duplicate IDs ignores duplicates and keeps the first.
test('Adding teams with duplicate IDs ignores duplicates', () async {
final duplicateTeam = Team(
id: testTeam1.id,
@@ -222,5 +235,4 @@ void main() {
expect(fetchedTeam.name, testTeam1.name);
});
});
}
}