4 Commits

Author SHA1 Message Date
961c6bb679 Refactored tests in own files
Some checks failed
Pull Request Pipeline / test (pull_request) Failing after 2m5s
Pull Request Pipeline / lint (pull_request) Successful in 2m5s
2025-11-21 01:05:35 +01:00
b21ca54672 Refactoring 2025-11-21 00:10:29 +01:00
6055eb63a8 Refactoring 2025-11-21 00:07:34 +01:00
31589855f2 Added methods of todos 2025-11-21 00:07:29 +01:00
11 changed files with 513 additions and 174 deletions

View File

@@ -18,10 +18,8 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
return Future.wait( return Future.wait(
result.map((row) async { result.map((row) async {
final group = await db.groupGameDao.getGroupByGameId(gameId: row.id); final group = await db.groupGameDao.getGroupOfGame(gameId: row.id);
final player = await db.playerGameDao.getPlayersByGameId( final player = await db.playerGameDao.getPlayersOfGame(gameId: row.id);
gameId: row.id,
);
return Game( return Game(
id: row.id, id: row.id,
name: row.name, name: row.name,
@@ -41,11 +39,11 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
List<Player>? players; List<Player>? players;
if (await db.playerGameDao.gameHasPlayers(gameId: gameId)) { if (await db.playerGameDao.gameHasPlayers(gameId: gameId)) {
players = await db.playerGameDao.getPlayersByGameId(gameId: gameId); players = await db.playerGameDao.getPlayersOfGame(gameId: gameId);
} }
Group? group; Group? group;
if (await db.groupGameDao.hasGameGroup(gameId: gameId)) { if (await db.groupGameDao.gameHasGroup(gameId: gameId)) {
group = await db.groupGameDao.getGroupByGameId(gameId: gameId); group = await db.groupGameDao.getGroupOfGame(gameId: gameId);
} }
return Game( return Game(

View File

@@ -16,7 +16,7 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
final result = await query.get(); final result = await query.get();
return Future.wait( return Future.wait(
result.map((groupData) async { result.map((groupData) async {
final members = await db.playerGroupDao.getPlayersOfGroupById( final members = await db.playerGroupDao.getPlayersOfGroup(
groupId: groupData.id, groupId: groupData.id,
); );
return Group( return Group(
@@ -34,7 +34,7 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
final query = select(groupTable)..where((g) => g.id.equals(groupId)); final query = select(groupTable)..where((g) => g.id.equals(groupId));
final result = await query.getSingle(); final result = await query.getSingle();
List<Player> members = await db.playerGroupDao.getPlayersOfGroupById( List<Player> members = await db.playerGroupDao.getPlayersOfGroup(
groupId: groupId, groupId: groupId,
); );

View File

@@ -10,21 +10,18 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
with _$GroupGameDaoMixin { with _$GroupGameDaoMixin {
GroupGameDao(super.db); GroupGameDao(super.db);
/// Checks if there is a group associated with the given [gameId]. /// Associates a group with a game by inserting a record into the
/// Returns `true` if there is a group, otherwise `false`. /// [GroupGameTable].
Future<bool> hasGameGroup({required String gameId}) async { Future<void> addGroupToGame(String gameId, String groupId) async {
final count = await into(groupGameTable).insert(
await (selectOnly(groupGameTable) GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId),
..where(groupGameTable.gameId.equals(gameId)) mode: InsertMode.insertOrReplace,
..addColumns([groupGameTable.groupId.count()])) );
.map((row) => row.read(groupGameTable.groupId.count()))
.getSingle();
return (count ?? 0) > 0;
} }
/// Retrieves the [Group] associated with the given [gameId]. /// Retrieves the [Group] associated with the given [gameId].
/// Returns `null` if no group is found. /// Returns `null` if no group is found.
Future<Group?> getGroupByGameId({required String gameId}) async { Future<Group?> getGroupOfGame({required String gameId}) async {
final result = await (select( final result = await (select(
groupGameTable, groupGameTable,
)..where((g) => g.gameId.equals(gameId))).getSingleOrNull(); )..where((g) => g.gameId.equals(gameId))).getSingleOrNull();
@@ -37,11 +34,46 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
return group; return group;
} }
/// Associates a group with a game by inserting a record into the /// Checks if there is a group associated with the given [gameId].
/// [GroupGameTable]. /// Returns `true` if there is a group, otherwise `false`.
Future<void> addGroupToGame(String gameId, String groupId) async { Future<bool> gameHasGroup({required String gameId}) async {
await into( final count =
groupGameTable, await (selectOnly(groupGameTable)
).insert(GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId), mode: InsertMode.insertOrReplace); ..where(groupGameTable.gameId.equals(gameId))
..addColumns([groupGameTable.groupId.count()]))
.map((row) => row.read(groupGameTable.groupId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Checks if a specific group is associated with a specific game.
/// Returns `true` if the group is in the game, otherwise `false`.
Future<bool> isGroupInGame({
required String gameId,
required String groupId,
}) async {
final count =
await (selectOnly(groupGameTable)
..where(
groupGameTable.gameId.equals(gameId) &
groupGameTable.groupId.equals(groupId),
)
..addColumns([groupGameTable.groupId.count()]))
.map((row) => row.read(groupGameTable.groupId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Removes the association of a group from a game based on [groupId] and
/// [gameId].
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
Future<bool> removeGroupFromGame({
required String gameId,
required String groupId,
}) async {
final query = delete(groupGameTable)
..where((g) => g.gameId.equals(gameId) & g.groupId.equals(groupId));
final rowsAffected = await query.go();
return rowsAffected > 0;
} }
} }

View File

@@ -10,21 +10,21 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
with _$PlayerGameDaoMixin { with _$PlayerGameDaoMixin {
PlayerGameDao(super.db); PlayerGameDao(super.db);
/// Checks if there are any players associated with the given [gameId]. /// Associates a player with a game by inserting a record into the
/// Returns `true` if there are players, otherwise `false`. /// [PlayerGameTable].
Future<bool> gameHasPlayers({required String gameId}) async { Future<void> addPlayerToGame({
final count = required String gameId,
await (selectOnly(playerGameTable) required String playerId,
..where(playerGameTable.gameId.equals(gameId)) }) async {
..addColumns([playerGameTable.playerId.count()])) await into(playerGameTable).insert(
.map((row) => row.read(playerGameTable.playerId.count())) PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId),
.getSingle(); mode: InsertMode.insertOrReplace,
return (count ?? 0) > 0; );
} }
/// Retrieves a list of [Player]s associated with the given [gameId]. /// Retrieves a list of [Player]s associated with the given [gameId].
/// Returns null if no players are found. /// Returns null if no players are found.
Future<List<Player>?> getPlayersByGameId({required String gameId}) async { Future<List<Player>?> getPlayersOfGame({required String gameId}) async {
final result = await (select( final result = await (select(
playerGameTable, playerGameTable,
)..where((p) => p.gameId.equals(gameId))).get(); )..where((p) => p.gameId.equals(gameId))).get();
@@ -38,15 +38,45 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
return players; return players;
} }
/// Associates a player with a game by inserting a record into the /// Checks if there are any players associated with the given [gameId].
/// [PlayerGameTable]. /// Returns `true` if there are players, otherwise `false`.
Future<void> addPlayerToGame({ Future<bool> gameHasPlayers({required String gameId}) async {
final count =
await (selectOnly(playerGameTable)
..where(playerGameTable.gameId.equals(gameId))
..addColumns([playerGameTable.playerId.count()]))
.map((row) => row.read(playerGameTable.playerId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Checks if a specific player is associated with a specific game.
/// Returns `true` if the player is in the game, otherwise `false`.
Future<bool> isPlayerInGame({
required String gameId, required String gameId,
required String playerId, required String playerId,
}) async { }) async {
await into(playerGameTable).insert( final count =
PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId), await (selectOnly(playerGameTable)
mode: InsertMode.insertOrReplace, ..where(playerGameTable.gameId.equals(gameId))
); ..where(playerGameTable.playerId.equals(playerId))
..addColumns([playerGameTable.playerId.count()]))
.map((row) => row.read(playerGameTable.playerId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Removes the association of a player with a game by deleting the record
/// from the [PlayerGameTable].
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
Future<bool> removePlayerFromGame({
required String gameId,
required String playerId,
}) async {
final query = delete(playerGameTable)
..where((pg) => pg.gameId.equals(gameId))
..where((pg) => pg.playerId.equals(playerId));
final rowsAffected = await query.go();
return rowsAffected > 0;
} }
} }

View File

@@ -10,8 +10,31 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
with _$PlayerGroupDaoMixin { with _$PlayerGroupDaoMixin {
PlayerGroupDao(super.db); PlayerGroupDao(super.db);
/// Adds a [player] to a group with the given [groupId].
/// If the player is already in the group, no action is taken.
/// If the player does not exist in the player table, they are added.
/// Returns `true` if the player was added, otherwise `false`.
Future<bool> addPlayerToGroup({
required Player player,
required String groupId,
}) async {
if (await isPlayerInGroup(playerId: player.id, groupId: groupId)) {
return false;
}
if (await db.playerDao.playerExists(playerId: player.id) == false) {
db.playerDao.addPlayer(player: player);
}
await into(playerGroupTable).insert(
PlayerGroupTableCompanion.insert(playerId: player.id, groupId: groupId),
);
return true;
}
/// Retrieves all players belonging to a specific group by [groupId]. /// Retrieves all players belonging to a specific group by [groupId].
Future<List<Player>> getPlayersOfGroupById({required String groupId}) async { Future<List<Player>> getPlayersOfGroup({required String groupId}) async {
final query = select(playerGroupTable) final query = select(playerGroupTable)
..where((pG) => pG.groupId.equals(groupId)); ..where((pG) => pG.groupId.equals(groupId));
final result = await query.get(); final result = await query.get();
@@ -38,29 +61,6 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
return rowsAffected > 0; return rowsAffected > 0;
} }
/// Adds a [player] to a group with the given [groupId].
/// If the player is already in the group, no action is taken.
/// If the player does not exist in the player table, they are added.
/// Returns `true` if the player was added, otherwise `false`.
Future<bool> addPlayerToGroup({
required Player player,
required String groupId,
}) async {
if (await isPlayerInGroup(playerId: player.id, groupId: groupId)) {
return false;
}
if (await db.playerDao.playerExists(playerId: player.id) == false) {
db.playerDao.addPlayer(player: player);
}
await into(playerGroupTable).insert(
PlayerGroupTableCompanion.insert(playerId: player.id, groupId: groupId),
);
return true;
}
/// Checks if a player with [playerId] is in the group with [groupId]. /// Checks if a player with [playerId] is in the group with [groupId].
/// Returns `true` if the player is in the group, otherwise `false`. /// Returns `true` if the player is in the group, otherwise `false`.
Future<bool> isPlayerInGroup({ Future<bool> isPlayerInGroup({

View File

@@ -16,8 +16,10 @@ void main() {
late Player player5; late Player player5;
late Group testgroup; late Group testgroup;
late Group testgroup2; late Group testgroup2;
late Game testgame; late Game testgame1;
late Game testgame2; late Game testgame2;
late Game testgameWithPlayer;
late Game testgameWithGroup;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
final fakeClock = Clock(() => fixedDate); final fakeClock = Clock(() => fixedDate);
@@ -37,14 +39,11 @@ void main() {
player4 = Player(name: 'Diana'); player4 = Player(name: 'Diana');
player5 = Player(name: 'Eve'); player5 = Player(name: 'Eve');
testgroup = Group( testgroup = Group(
name: 'Test Group', name: 'Test Group 2',
members: [player1, player2, player3], members: [player1, player2, player3],
); );
testgroup2 = Group( testgroup2 = Group(name: 'Test Group 2', members: [player4, player5]);
name: 'Test Group', testgame1 = Game(
members: [player1, player2, player3],
);
testgame = Game(
name: 'Test Game', name: 'Test Game',
group: testgroup, group: testgroup,
players: [player4, player5], players: [player4, player5],
@@ -54,6 +53,11 @@ void main() {
group: testgroup2, group: testgroup2,
players: [player1, player2, player3], players: [player1, player2, player3],
); );
testgameWithPlayer = Game(
name: 'Second Test Game',
players: [player1, player2, player3],
);
testgameWithGroup = Game(name: 'Second Test Game', group: testgroup2);
}); });
}); });
tearDown(() async { tearDown(() async {
@@ -62,14 +66,14 @@ void main() {
group('Game Tests', () { group('Game Tests', () {
test('Adding and fetching single game works correclty', () async { test('Adding and fetching single game works correclty', () async {
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
final result = await database.gameDao.getGameById(gameId: testgame.id); final result = await database.gameDao.getGameById(gameId: testgame1.id);
expect(result.id, testgame.id); expect(result.id, testgame1.id);
expect(result.name, testgame.name); expect(result.name, testgame1.name);
expect(result.winner, testgame.winner); expect(result.winner, testgame1.winner);
expect(result.createdAt, testgame.createdAt); expect(result.createdAt, testgame1.createdAt);
if (result.group != null) { if (result.group != null) {
expect(result.group!.members.length, testgroup.members.length); expect(result.group!.members.length, testgroup.members.length);
@@ -82,12 +86,12 @@ void main() {
fail('Group is null'); fail('Group is null');
} }
if (result.players != null) { if (result.players != null) {
expect(result.players!.length, testgame.players!.length); expect(result.players!.length, testgame1.players!.length);
for (int i = 0; i < testgame.players!.length; i++) { for (int i = 0; i < testgame1.players!.length; i++) {
expect(result.players![i].id, testgame.players![i].id); expect(result.players![i].id, testgame1.players![i].id);
expect(result.players![i].name, testgame.players![i].name); expect(result.players![i].name, testgame1.players![i].name);
expect(result.players![i].createdAt, testgame.players![i].createdAt); expect(result.players![i].createdAt, testgame1.players![i].createdAt);
} }
} else { } else {
fail('Players is null'); fail('Players is null');
@@ -95,51 +99,54 @@ void main() {
}); });
// TODO: Use upcoming addGames() method // TODO: Use upcoming addGames() method
// TODO: Iterate through games
test('Adding and fetching multiple games works correclty', () async { test('Adding and fetching multiple games works correclty', () async {
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
await database.gameDao.addGame(game: testgame2); await database.gameDao.addGame(game: testgame2);
await database.gameDao.addGame(game: testgameWithGroup);
await database.gameDao.addGame(game: testgameWithPlayer);
final allGames = await database.gameDao.getAllGames(); final allGames = await database.gameDao.getAllGames();
expect(allGames.length, 2); expect(allGames.length, 4);
final fetchedGame1 = allGames.firstWhere((g) => g.id == testgame.id); final fetchedGame1 = allGames.firstWhere((g) => g.id == testgame1.id);
// game checks // game checks
expect(fetchedGame1.id, testgame.id); expect(fetchedGame1.id, testgame1.id);
expect(fetchedGame1.name, testgame.name); expect(fetchedGame1.name, testgame1.name);
expect(fetchedGame1.createdAt, testgame.createdAt); expect(fetchedGame1.createdAt, testgame1.createdAt);
expect(fetchedGame1.winner, testgame.winner); expect(fetchedGame1.winner, testgame1.winner);
// group checks // group checks
expect(fetchedGame1.group!.id, testgame.group!.id); expect(fetchedGame1.group!.id, testgame1.group!.id);
expect(fetchedGame1.group!.name, testgame.group!.name); expect(fetchedGame1.group!.name, testgame1.group!.name);
expect(fetchedGame1.group!.createdAt, testgame.group!.createdAt); expect(fetchedGame1.group!.createdAt, testgame1.group!.createdAt);
// group members checks // group members checks
expect( expect(
fetchedGame1.group!.members.length, fetchedGame1.group!.members.length,
testgame.group!.members.length, testgame1.group!.members.length,
); );
for (int i = 0; i < testgame.group!.members.length; i++) { for (int i = 0; i < testgame1.group!.members.length; i++) {
expect( expect(
fetchedGame1.group!.members[i].id, fetchedGame1.group!.members[i].id,
testgame.group!.members[i].id, testgame1.group!.members[i].id,
); );
expect( expect(
fetchedGame1.group!.members[i].name, fetchedGame1.group!.members[i].name,
testgame.group!.members[i].name, testgame1.group!.members[i].name,
); );
expect( expect(
fetchedGame1.group!.members[i].createdAt, fetchedGame1.group!.members[i].createdAt,
testgame.group!.members[i].createdAt, testgame1.group!.members[i].createdAt,
); );
} }
// players checks // players checks
for (int i = 0; i < fetchedGame1.players!.length; i++) { for (int i = 0; i < fetchedGame1.players!.length; i++) {
expect(fetchedGame1.players![i].id, testgame.players![i].id); expect(fetchedGame1.players![i].id, testgame1.players![i].id);
expect(fetchedGame1.players![i].name, testgame.players![i].name); expect(fetchedGame1.players![i].name, testgame1.players![i].name);
expect( expect(
fetchedGame1.players![i].createdAt, fetchedGame1.players![i].createdAt,
testgame.players![i].createdAt, testgame1.players![i].createdAt,
); );
} }
@@ -186,32 +193,34 @@ void main() {
}); });
test('Adding the same game twice does not create duplicates', () async { test('Adding the same game twice does not create duplicates', () async {
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
final gameCount = await database.gameDao.getGameCount(); final gameCount = await database.gameDao.getGameCount();
expect(gameCount, 1); expect(gameCount, 1);
}); });
test('Game existence check works correctly', () async { test('Game existence check works correctly', () async {
var gameExists = await database.gameDao.gameExists(gameId: testgame.id); var gameExists = await database.gameDao.gameExists(gameId: testgame1.id);
expect(gameExists, false); expect(gameExists, false);
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
gameExists = await database.gameDao.gameExists(gameId: testgame.id); gameExists = await database.gameDao.gameExists(gameId: testgame1.id);
expect(gameExists, true); expect(gameExists, true);
}); });
test('Deleting a game works correclty', () async { test('Deleting a game works correclty', () async {
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
final gameDeleted = await database.gameDao.deleteGame( final gameDeleted = await database.gameDao.deleteGame(
gameId: testgame.id, gameId: testgame1.id,
); );
expect(gameDeleted, true); expect(gameDeleted, true);
final gameExists = await database.gameDao.gameExists(gameId: testgame.id); final gameExists = await database.gameDao.gameExists(
gameId: testgame1.id,
);
expect(gameExists, false); expect(gameExists, false);
}); });
@@ -219,7 +228,7 @@ void main() {
var gameCount = await database.gameDao.getGameCount(); var gameCount = await database.gameDao.getGameCount();
expect(gameCount, 0); expect(gameCount, 0);
await database.gameDao.addGame(game: testgame); await database.gameDao.addGame(game: testgame1);
gameCount = await database.gameDao.getGameCount(); gameCount = await database.gameDao.getGameCount();
expect(gameCount, 1); expect(gameCount, 1);
@@ -229,7 +238,7 @@ void main() {
gameCount = await database.gameDao.getGameCount(); gameCount = await database.gameDao.getGameCount();
expect(gameCount, 2); expect(gameCount, 2);
await database.gameDao.deleteGame(gameId: testgame.id); await database.gameDao.deleteGame(gameId: testgame1.id);
gameCount = await database.gameDao.getGameCount(); gameCount = await database.gameDao.getGameCount();
expect(gameCount, 1); expect(gameCount, 1);
@@ -239,11 +248,5 @@ void main() {
gameCount = await database.gameDao.getGameCount(); gameCount = await database.gameDao.getGameCount();
expect(gameCount, 0); expect(gameCount, 0);
}); });
// TODO: Implement
test('Adding a player to a game works correclty', () async {});
// TODO: Implement
test('Adding a group to a game works correclty', () async {});
}); });
} }

View File

@@ -0,0 +1,110 @@
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 Group testgroup;
late Game gameWithGroup;
late Game gameWithPlayers;
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');
testgroup = Group(
name: 'Test Group',
members: [player1, player2, player3],
);
gameWithPlayers = Game(
name: 'Game with Players',
players: [player4, player5],
);
gameWithGroup = Game(name: 'Game with Group', group: testgroup);
});
});
tearDown(() async {
await database.close();
});
group('Group-Game Tests', () {
test('Game has group works correctly', () async {
database.gameDao.addGame(game: gameWithPlayers);
database.groupDao.addGroup(group: testgroup);
var gameHasGroup = await database.groupGameDao.gameHasGroup(
gameId: gameWithPlayers.id,
);
expect(gameHasGroup, false);
database.groupGameDao.addGroupToGame(gameWithPlayers.id, testgroup.id);
gameHasGroup = await database.groupGameDao.gameHasGroup(
gameId: gameWithPlayers.id,
);
expect(gameHasGroup, true);
});
test('Adding a group to a game works correctly', () async {
database.gameDao.addGame(game: gameWithPlayers);
database.groupDao.addGroup(group: testgroup);
database.groupGameDao.addGroupToGame(gameWithPlayers.id, testgroup.id);
var groupAdded = await database.groupGameDao.isGroupInGame(
gameId: gameWithPlayers.id,
groupId: testgroup.id,
);
expect(groupAdded, true);
groupAdded = await database.groupGameDao.isGroupInGame(
gameId: gameWithPlayers.id,
groupId: '',
);
expect(groupAdded, false);
});
test('Removing group from game works correctly', () async {
await database.gameDao.addGame(game: gameWithGroup);
final groupToRemove = gameWithGroup.group!;
final removed = await database.groupGameDao.removeGroupFromGame(
groupId: groupToRemove.id,
gameId: gameWithGroup.id,
);
expect(removed, true);
final result = await database.gameDao.getGameById(
gameId: gameWithGroup.id,
);
expect(result.group, null);
});
// TODO: test getGroupOfGame()
test('Retrieving group of a game works correctly', () async {});
});
}

View File

@@ -68,7 +68,6 @@ void main() {
} }
}); });
// TODO: Use upcoming addGroups() method
test('Adding and fetching a single group works correctly', () async { test('Adding and fetching a single group works correctly', () async {
await database.groupDao.addGroup(group: testgroup); await database.groupDao.addGroup(group: testgroup);
await database.groupDao.addGroup(group: testgroup2); await database.groupDao.addGroup(group: testgroup2);
@@ -89,6 +88,8 @@ void main() {
expect(fetchedGroup2.members.elementAt(0).createdAt, player2.createdAt); expect(fetchedGroup2.members.elementAt(0).createdAt, player2.createdAt);
}); });
// TODO: Use upcoming addGroups() method
// TODO: An Test in Game Tests orientieren
test('Adding the same group twice does not create duplicates', () async { test('Adding the same group twice does not create duplicates', () async {
await database.groupDao.addGroup(group: testgroup); await database.groupDao.addGroup(group: testgroup);
await database.groupDao.addGroup(group: testgroup); await database.groupDao.addGroup(group: testgroup);
@@ -139,58 +140,6 @@ void main() {
expect(result.name, newGroupName); expect(result.name, newGroupName);
}); });
test('Adding player to group works correctly', () async {
await database.groupDao.addGroup(group: testgroup);
await database.playerGroupDao.addPlayerToGroup(
player: player4,
groupId: testgroup.id,
);
final playerAdded = await database.playerGroupDao.isPlayerInGroup(
playerId: player4.id,
groupId: testgroup.id,
);
expect(playerAdded, true);
final playerNotAdded = !await database.playerGroupDao.isPlayerInGroup(
playerId: '',
groupId: testgroup.id,
);
expect(playerNotAdded, true);
final result = await database.groupDao.getGroupById(
groupId: testgroup.id,
);
expect(result.members.length, testgroup.members.length + 1);
final addedPlayer = result.members.firstWhere((p) => p.id == player4.id);
expect(addedPlayer.name, player4.name);
expect(addedPlayer.createdAt, player4.createdAt);
});
test('Removing player from group works correctly', () async {
await database.groupDao.addGroup(group: testgroup);
final playerToRemove = testgroup.members[0];
final removed = await database.playerGroupDao.removePlayerFromGroup(
playerId: playerToRemove.id,
groupId: testgroup.id,
);
expect(removed, true);
final result = await database.groupDao.getGroupById(
groupId: testgroup.id,
);
expect(result.members.length, testgroup.members.length - 1);
final playerExists = result.members.any((p) => p.id == playerToRemove.id);
expect(playerExists, false);
});
test('Getting the group count works correctly', () async { test('Getting the group count works correctly', () async {
final initialCount = await database.groupDao.getGroupCount(); final initialCount = await database.groupDao.getGroupCount();
expect(initialCount, 0); expect(initialCount, 0);

View File

@@ -0,0 +1,126 @@
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: testgameWithGroup.id,
);
expect(result.players!.length, testgameWithGroup.players!.length - 1);
final playerExists = result.players!.any(
(p) => p.id == playerToRemove.id,
);
expect(playerExists, false);
});
//TODO: test getPlayersOfGame()
test('Retrieving players of a game works correctly', () async {});
});
}

View File

@@ -0,0 +1,90 @@
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/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 Group testgroup;
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');
testgroup = Group(
name: 'Test Group',
members: [player1, player2, player3],
);
});
});
tearDown(() async {
await database.close();
});
group('Player-Group Tests', () {
test('Adding a player to a group works correctly', () async {
await database.groupDao.addGroup(group: testgroup);
await database.playerDao.addPlayer(player: player4);
await database.playerGroupDao.addPlayerToGroup(
groupId: testgroup.id,
player: player4,
);
var playerAdded = await database.playerGroupDao.isPlayerInGroup(
groupId: testgroup.id,
playerId: player4.id,
);
expect(playerAdded, true);
playerAdded = await database.playerGroupDao.isPlayerInGroup(
groupId: testgroup.id,
playerId: '',
);
expect(playerAdded, false);
});
test('Removing player from group works correctly', () async {
await database.groupDao.addGroup(group: testgroup);
final playerToRemove = testgroup.members[0];
final removed = await database.playerGroupDao.removePlayerFromGroup(
playerId: playerToRemove.id,
groupId: testgroup.id,
);
expect(removed, true);
final result = await database.groupDao.getGroupById(
groupId: testgroup.id,
);
expect(result.members.length, testgroup.members.length - 1);
final playerExists = result.members.any((p) => p.id == playerToRemove.id);
expect(playerExists, false);
});
//TODO: test getPlayersOfGroup()
test('Retrieving players of a group works correctly', () async {});
});
}

View File

@@ -23,7 +23,7 @@ void main() {
withClock(fakeClock, () { withClock(fakeClock, () {
testPlayer = Player(name: 'Test Player'); testPlayer = Player(name: 'Test Player');
testPlayer2 = Player(name: 'Second Group'); testPlayer2 = Player(name: 'Second Player');
}); });
}); });
tearDown(() async { tearDown(() async {
@@ -52,6 +52,7 @@ void main() {
}); });
// TODO: Use upcoming addPlayers() method // TODO: Use upcoming addPlayers() method
// TODO: An Tests in Game orientieren
test('Adding and fetching multiple players works correclty', () async { test('Adding and fetching multiple players works correclty', () async {
await database.playerDao.addPlayer(player: testPlayer); await database.playerDao.addPlayer(player: testPlayer);
await database.playerDao.addPlayer(player: testPlayer2); await database.playerDao.addPlayer(player: testPlayer2);