Compare commits
4 Commits
89b3f1ff69
...
961c6bb679
| Author | SHA1 | Date | |
|---|---|---|---|
| 961c6bb679 | |||
| b21ca54672 | |||
| 6055eb63a8 | |||
| 31589855f2 |
@@ -18,10 +18,8 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
|
||||
return Future.wait(
|
||||
result.map((row) async {
|
||||
final group = await db.groupGameDao.getGroupByGameId(gameId: row.id);
|
||||
final player = await db.playerGameDao.getPlayersByGameId(
|
||||
gameId: row.id,
|
||||
);
|
||||
final group = await db.groupGameDao.getGroupOfGame(gameId: row.id);
|
||||
final player = await db.playerGameDao.getPlayersOfGame(gameId: row.id);
|
||||
return Game(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
@@ -41,11 +39,11 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
|
||||
List<Player>? players;
|
||||
if (await db.playerGameDao.gameHasPlayers(gameId: gameId)) {
|
||||
players = await db.playerGameDao.getPlayersByGameId(gameId: gameId);
|
||||
players = await db.playerGameDao.getPlayersOfGame(gameId: gameId);
|
||||
}
|
||||
Group? group;
|
||||
if (await db.groupGameDao.hasGameGroup(gameId: gameId)) {
|
||||
group = await db.groupGameDao.getGroupByGameId(gameId: gameId);
|
||||
if (await db.groupGameDao.gameHasGroup(gameId: gameId)) {
|
||||
group = await db.groupGameDao.getGroupOfGame(gameId: gameId);
|
||||
}
|
||||
|
||||
return Game(
|
||||
|
||||
@@ -16,7 +16,7 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
final result = await query.get();
|
||||
return Future.wait(
|
||||
result.map((groupData) async {
|
||||
final members = await db.playerGroupDao.getPlayersOfGroupById(
|
||||
final members = await db.playerGroupDao.getPlayersOfGroup(
|
||||
groupId: groupData.id,
|
||||
);
|
||||
return Group(
|
||||
@@ -34,7 +34,7 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
||||
final result = await query.getSingle();
|
||||
|
||||
List<Player> members = await db.playerGroupDao.getPlayersOfGroupById(
|
||||
List<Player> members = await db.playerGroupDao.getPlayersOfGroup(
|
||||
groupId: groupId,
|
||||
);
|
||||
|
||||
|
||||
@@ -10,21 +10,18 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$GroupGameDaoMixin {
|
||||
GroupGameDao(super.db);
|
||||
|
||||
/// Checks if there is a group associated with the given [gameId].
|
||||
/// Returns `true` if there is a group, otherwise `false`.
|
||||
Future<bool> hasGameGroup({required String gameId}) async {
|
||||
final count =
|
||||
await (selectOnly(groupGameTable)
|
||||
..where(groupGameTable.gameId.equals(gameId))
|
||||
..addColumns([groupGameTable.groupId.count()]))
|
||||
.map((row) => row.read(groupGameTable.groupId.count()))
|
||||
.getSingle();
|
||||
return (count ?? 0) > 0;
|
||||
/// Associates a group with a game by inserting a record into the
|
||||
/// [GroupGameTable].
|
||||
Future<void> addGroupToGame(String gameId, String groupId) async {
|
||||
await into(groupGameTable).insert(
|
||||
GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
}
|
||||
|
||||
/// Retrieves the [Group] associated with the given [gameId].
|
||||
/// 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(
|
||||
groupGameTable,
|
||||
)..where((g) => g.gameId.equals(gameId))).getSingleOrNull();
|
||||
@@ -37,11 +34,46 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
|
||||
return group;
|
||||
}
|
||||
|
||||
/// Associates a group with a game by inserting a record into the
|
||||
/// [GroupGameTable].
|
||||
Future<void> addGroupToGame(String gameId, String groupId) async {
|
||||
await into(
|
||||
groupGameTable,
|
||||
).insert(GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId), mode: InsertMode.insertOrReplace);
|
||||
/// Checks if there is a group associated with the given [gameId].
|
||||
/// Returns `true` if there is a group, otherwise `false`.
|
||||
Future<bool> gameHasGroup({required String gameId}) async {
|
||||
final count =
|
||||
await (selectOnly(groupGameTable)
|
||||
..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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,21 +10,21 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$PlayerGameDaoMixin {
|
||||
PlayerGameDao(super.db);
|
||||
|
||||
/// Checks if there are any players associated with the given [gameId].
|
||||
/// Returns `true` if there are players, otherwise `false`.
|
||||
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;
|
||||
/// Associates a player with a game by inserting a record into the
|
||||
/// [PlayerGameTable].
|
||||
Future<void> addPlayerToGame({
|
||||
required String gameId,
|
||||
required String playerId,
|
||||
}) async {
|
||||
await into(playerGameTable).insert(
|
||||
PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
}
|
||||
|
||||
/// Retrieves a list of [Player]s associated with the given [gameId].
|
||||
/// 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(
|
||||
playerGameTable,
|
||||
)..where((p) => p.gameId.equals(gameId))).get();
|
||||
@@ -38,15 +38,45 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
|
||||
return players;
|
||||
}
|
||||
|
||||
/// Associates a player with a game by inserting a record into the
|
||||
/// [PlayerGameTable].
|
||||
Future<void> addPlayerToGame({
|
||||
/// Checks if there are any players associated with the given [gameId].
|
||||
/// Returns `true` if there are players, otherwise `false`.
|
||||
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 playerId,
|
||||
}) async {
|
||||
await into(playerGameTable).insert(
|
||||
PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
final count =
|
||||
await (selectOnly(playerGameTable)
|
||||
..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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,31 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$PlayerGroupDaoMixin {
|
||||
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].
|
||||
Future<List<Player>> getPlayersOfGroupById({required String groupId}) async {
|
||||
Future<List<Player>> getPlayersOfGroup({required String groupId}) async {
|
||||
final query = select(playerGroupTable)
|
||||
..where((pG) => pG.groupId.equals(groupId));
|
||||
final result = await query.get();
|
||||
@@ -38,29 +61,6 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
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].
|
||||
/// Returns `true` if the player is in the group, otherwise `false`.
|
||||
Future<bool> isPlayerInGroup({
|
||||
|
||||
@@ -16,8 +16,10 @@ void main() {
|
||||
late Player player5;
|
||||
late Group testgroup;
|
||||
late Group testgroup2;
|
||||
late Game testgame;
|
||||
late Game testgame1;
|
||||
late Game testgame2;
|
||||
late Game testgameWithPlayer;
|
||||
late Game testgameWithGroup;
|
||||
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
|
||||
final fakeClock = Clock(() => fixedDate);
|
||||
|
||||
@@ -37,14 +39,11 @@ void main() {
|
||||
player4 = Player(name: 'Diana');
|
||||
player5 = Player(name: 'Eve');
|
||||
testgroup = Group(
|
||||
name: 'Test Group',
|
||||
name: 'Test Group 2',
|
||||
members: [player1, player2, player3],
|
||||
);
|
||||
testgroup2 = Group(
|
||||
name: 'Test Group',
|
||||
members: [player1, player2, player3],
|
||||
);
|
||||
testgame = Game(
|
||||
testgroup2 = Group(name: 'Test Group 2', members: [player4, player5]);
|
||||
testgame1 = Game(
|
||||
name: 'Test Game',
|
||||
group: testgroup,
|
||||
players: [player4, player5],
|
||||
@@ -54,6 +53,11 @@ void main() {
|
||||
group: testgroup2,
|
||||
players: [player1, player2, player3],
|
||||
);
|
||||
testgameWithPlayer = Game(
|
||||
name: 'Second Test Game',
|
||||
players: [player1, player2, player3],
|
||||
);
|
||||
testgameWithGroup = Game(name: 'Second Test Game', group: testgroup2);
|
||||
});
|
||||
});
|
||||
tearDown(() async {
|
||||
@@ -62,14 +66,14 @@ void main() {
|
||||
|
||||
group('Game Tests', () {
|
||||
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.name, testgame.name);
|
||||
expect(result.winner, testgame.winner);
|
||||
expect(result.createdAt, testgame.createdAt);
|
||||
expect(result.id, testgame1.id);
|
||||
expect(result.name, testgame1.name);
|
||||
expect(result.winner, testgame1.winner);
|
||||
expect(result.createdAt, testgame1.createdAt);
|
||||
|
||||
if (result.group != null) {
|
||||
expect(result.group!.members.length, testgroup.members.length);
|
||||
@@ -82,12 +86,12 @@ void main() {
|
||||
fail('Group is 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++) {
|
||||
expect(result.players![i].id, testgame.players![i].id);
|
||||
expect(result.players![i].name, testgame.players![i].name);
|
||||
expect(result.players![i].createdAt, testgame.players![i].createdAt);
|
||||
for (int i = 0; i < testgame1.players!.length; i++) {
|
||||
expect(result.players![i].id, testgame1.players![i].id);
|
||||
expect(result.players![i].name, testgame1.players![i].name);
|
||||
expect(result.players![i].createdAt, testgame1.players![i].createdAt);
|
||||
}
|
||||
} else {
|
||||
fail('Players is null');
|
||||
@@ -95,51 +99,54 @@ void main() {
|
||||
});
|
||||
|
||||
// TODO: Use upcoming addGames() method
|
||||
// TODO: Iterate through games
|
||||
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: testgameWithGroup);
|
||||
await database.gameDao.addGame(game: testgameWithPlayer);
|
||||
|
||||
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
|
||||
expect(fetchedGame1.id, testgame.id);
|
||||
expect(fetchedGame1.name, testgame.name);
|
||||
expect(fetchedGame1.createdAt, testgame.createdAt);
|
||||
expect(fetchedGame1.winner, testgame.winner);
|
||||
expect(fetchedGame1.id, testgame1.id);
|
||||
expect(fetchedGame1.name, testgame1.name);
|
||||
expect(fetchedGame1.createdAt, testgame1.createdAt);
|
||||
expect(fetchedGame1.winner, testgame1.winner);
|
||||
|
||||
// group checks
|
||||
expect(fetchedGame1.group!.id, testgame.group!.id);
|
||||
expect(fetchedGame1.group!.name, testgame.group!.name);
|
||||
expect(fetchedGame1.group!.createdAt, testgame.group!.createdAt);
|
||||
expect(fetchedGame1.group!.id, testgame1.group!.id);
|
||||
expect(fetchedGame1.group!.name, testgame1.group!.name);
|
||||
expect(fetchedGame1.group!.createdAt, testgame1.group!.createdAt);
|
||||
// group members checks
|
||||
expect(
|
||||
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(
|
||||
fetchedGame1.group!.members[i].id,
|
||||
testgame.group!.members[i].id,
|
||||
testgame1.group!.members[i].id,
|
||||
);
|
||||
expect(
|
||||
fetchedGame1.group!.members[i].name,
|
||||
testgame.group!.members[i].name,
|
||||
testgame1.group!.members[i].name,
|
||||
);
|
||||
expect(
|
||||
fetchedGame1.group!.members[i].createdAt,
|
||||
testgame.group!.members[i].createdAt,
|
||||
testgame1.group!.members[i].createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
// players checks
|
||||
for (int i = 0; i < fetchedGame1.players!.length; i++) {
|
||||
expect(fetchedGame1.players![i].id, testgame.players![i].id);
|
||||
expect(fetchedGame1.players![i].name, testgame.players![i].name);
|
||||
expect(fetchedGame1.players![i].id, testgame1.players![i].id);
|
||||
expect(fetchedGame1.players![i].name, testgame1.players![i].name);
|
||||
expect(
|
||||
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 {
|
||||
await database.gameDao.addGame(game: testgame);
|
||||
await database.gameDao.addGame(game: testgame);
|
||||
await database.gameDao.addGame(game: testgame1);
|
||||
await database.gameDao.addGame(game: testgame1);
|
||||
|
||||
final gameCount = await database.gameDao.getGameCount();
|
||||
expect(gameCount, 1);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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(
|
||||
gameId: testgame.id,
|
||||
gameId: testgame1.id,
|
||||
);
|
||||
expect(gameDeleted, true);
|
||||
|
||||
final gameExists = await database.gameDao.gameExists(gameId: testgame.id);
|
||||
final gameExists = await database.gameDao.gameExists(
|
||||
gameId: testgame1.id,
|
||||
);
|
||||
expect(gameExists, false);
|
||||
});
|
||||
|
||||
@@ -219,7 +228,7 @@ void main() {
|
||||
var gameCount = await database.gameDao.getGameCount();
|
||||
expect(gameCount, 0);
|
||||
|
||||
await database.gameDao.addGame(game: testgame);
|
||||
await database.gameDao.addGame(game: testgame1);
|
||||
|
||||
gameCount = await database.gameDao.getGameCount();
|
||||
expect(gameCount, 1);
|
||||
@@ -229,7 +238,7 @@ void main() {
|
||||
gameCount = await database.gameDao.getGameCount();
|
||||
expect(gameCount, 2);
|
||||
|
||||
await database.gameDao.deleteGame(gameId: testgame.id);
|
||||
await database.gameDao.deleteGame(gameId: testgame1.id);
|
||||
|
||||
gameCount = await database.gameDao.getGameCount();
|
||||
expect(gameCount, 1);
|
||||
@@ -239,11 +248,5 @@ void main() {
|
||||
gameCount = await database.gameDao.getGameCount();
|
||||
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 {});
|
||||
});
|
||||
}
|
||||
|
||||
110
test/db_tests/group_game_test.dart
Normal file
110
test/db_tests/group_game_test.dart
Normal 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 {});
|
||||
});
|
||||
}
|
||||
@@ -68,7 +68,6 @@ void main() {
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: Use upcoming addGroups() method
|
||||
test('Adding and fetching a single group works correctly', () async {
|
||||
await database.groupDao.addGroup(group: testgroup);
|
||||
await database.groupDao.addGroup(group: testgroup2);
|
||||
@@ -89,6 +88,8 @@ void main() {
|
||||
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 {
|
||||
await database.groupDao.addGroup(group: testgroup);
|
||||
await database.groupDao.addGroup(group: testgroup);
|
||||
@@ -139,58 +140,6 @@ void main() {
|
||||
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 {
|
||||
final initialCount = await database.groupDao.getGroupCount();
|
||||
expect(initialCount, 0);
|
||||
|
||||
126
test/db_tests/player_game_test.dart
Normal file
126
test/db_tests/player_game_test.dart
Normal 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 {});
|
||||
});
|
||||
}
|
||||
90
test/db_tests/player_group_test.dart
Normal file
90
test/db_tests/player_group_test.dart
Normal 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 {});
|
||||
});
|
||||
}
|
||||
@@ -23,7 +23,7 @@ void main() {
|
||||
|
||||
withClock(fakeClock, () {
|
||||
testPlayer = Player(name: 'Test Player');
|
||||
testPlayer2 = Player(name: 'Second Group');
|
||||
testPlayer2 = Player(name: 'Second Player');
|
||||
});
|
||||
});
|
||||
tearDown(() async {
|
||||
@@ -52,6 +52,7 @@ void main() {
|
||||
});
|
||||
|
||||
// TODO: Use upcoming addPlayers() method
|
||||
// TODO: An Tests in Game orientieren
|
||||
test('Adding and fetching multiple players works correclty', () async {
|
||||
await database.playerDao.addPlayer(player: testPlayer);
|
||||
await database.playerDao.addPlayer(player: testPlayer2);
|
||||
|
||||
Reference in New Issue
Block a user