Files
game-tracker/test/db_tests/player_game_test.dart
Felix Kirchner e15f5d163d
All checks were successful
Pull Request Pipeline / test (pull_request) Successful in 2m6s
Pull Request Pipeline / lint (pull_request) Successful in 2m7s
Added missing methods
2025-11-21 13:12:36 +01:00

141 lines
4.1 KiB
Dart

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/player.dart';
void main() {
late AppDatabase database;
late Player player1;
late Player player2;
late Player player3;
late Player player4;
late Player player5;
late Player player6;
late Group testgroup;
late Game testgameWithGroup;
late Game testgameWithPlayers;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
final fakeClock = Clock(() => fixedDate);
setUp(() {
database = AppDatabase(
DatabaseConnection(
NativeDatabase.memory(),
// Recommended for widget tests to avoid test errors.
closeStreamsSynchronously: true,
),
);
withClock(fakeClock, () {
player1 = Player(name: 'Alice');
player2 = Player(name: 'Bob');
player3 = Player(name: 'Charlie');
player4 = Player(name: 'Diana');
player5 = Player(name: 'Eve');
player6 = Player(name: 'Frank');
testgroup = Group(
name: 'Test Group',
members: [player1, player2, player3],
);
testgameWithGroup = Game(name: 'Test Game', group: testgroup);
testgameWithPlayers = Game(
name: 'Test Game with Players',
players: [player4, player5, player6],
);
});
});
tearDown(() async {
await database.close();
});
group('Player-Game Tests', () {
test('Game has player works correctly', () async {
database.gameDao.addGame(game: testgameWithGroup);
database.playerDao.addPlayer(player: player1);
var gameHasPlayers = await database.playerGameDao.gameHasPlayers(
gameId: testgameWithGroup.id,
);
expect(gameHasPlayers, false);
database.playerGameDao.addPlayerToGame(
gameId: testgameWithGroup.id,
playerId: player1.id,
);
gameHasPlayers = await database.playerGameDao.gameHasPlayers(
gameId: testgameWithGroup.id,
);
expect(gameHasPlayers, true);
});
test('Adding a player to a game works correctly', () async {
database.gameDao.addGame(game: testgameWithGroup);
database.playerDao.addPlayer(player: player5);
database.playerGameDao.addPlayerToGame(
gameId: testgameWithGroup.id,
playerId: player5.id,
);
var playerAdded = await database.playerGameDao.isPlayerInGame(
gameId: testgameWithGroup.id,
playerId: player5.id,
);
expect(playerAdded, true);
playerAdded = await database.playerGameDao.isPlayerInGame(
gameId: testgameWithGroup.id,
playerId: '',
);
expect(playerAdded, false);
});
test('Removing player from game works correctly', () async {
await database.gameDao.addGame(game: testgameWithPlayers);
final playerToRemove = testgameWithPlayers.players![0];
final removed = await database.playerGameDao.removePlayerFromGame(
playerId: playerToRemove.id,
gameId: testgameWithPlayers.id,
);
expect(removed, true);
final result = await database.gameDao.getGameById(
gameId: testgameWithPlayers.id,
);
expect(result.players!.length, testgameWithPlayers.players!.length - 1);
final playerExists = result.players!.any(
(p) => p.id == playerToRemove.id,
);
expect(playerExists, false);
});
test('Retrieving players of a game works correctly', () async {
await database.gameDao.addGame(game: testgameWithPlayers);
final players = await database.playerGameDao.getPlayersOfGame(
gameId: testgameWithPlayers.id,
);
if (players == null) {
fail('Players should not be null');
}
for (int i = 0; i < players.length; i++) {
expect(players[i].id, testgameWithPlayers.players![i].id);
expect(players[i].name, testgameWithPlayers.players![i].name);
expect(players[i].createdAt, testgameWithPlayers.players![i].createdAt);
}
});
});
}