Updated games dao + tests
This commit is contained in:
@@ -10,39 +10,7 @@ part 'game_dao.g.dart';
|
||||
class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
GameDao(super.db);
|
||||
|
||||
/// Retrieves all games from the database.
|
||||
Future<List<Game>> getAllGames() async {
|
||||
final query = select(gameTable);
|
||||
final result = await query.get();
|
||||
return result
|
||||
.map(
|
||||
(row) => Game(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
ruleset: Ruleset.values.firstWhere((e) => e.name == row.ruleset),
|
||||
description: row.description,
|
||||
color: GameColor.values.firstWhere((e) => e.name == row.color),
|
||||
icon: row.icon,
|
||||
createdAt: row.createdAt,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Retrieves a [Game] by its [gameId].
|
||||
Future<Game> getGameById({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingle();
|
||||
return Game(
|
||||
id: result.id,
|
||||
name: result.name,
|
||||
ruleset: Ruleset.values.firstWhere((e) => e.name == result.ruleset),
|
||||
description: result.description,
|
||||
color: GameColor.values.firstWhere((e) => e.name == result.color),
|
||||
icon: result.icon,
|
||||
createdAt: result.createdAt,
|
||||
);
|
||||
}
|
||||
/* Create */
|
||||
|
||||
/// Adds a new [game] to the database.
|
||||
/// If a game with the same ID already exists, no action is taken.
|
||||
@@ -94,71 +62,7 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Deletes the game with the given [gameId] from the database.
|
||||
/// Returns `true` if the game was deleted, `false` if the game did not exist.
|
||||
Future<bool> deleteGame({required String gameId}) async {
|
||||
final query = delete(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Checks if a game with the given [gameId] exists in the database.
|
||||
/// Returns `true` if the game exists, `false` otherwise.
|
||||
Future<bool> gameExists({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
|
||||
/// Updates the name of the game with the given [gameId] to [newName].
|
||||
Future<void> updateGameName({
|
||||
required String gameId,
|
||||
required String newName,
|
||||
}) async {
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(name: Value(newName)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Updates the ruleset of the game with the given [gameId].
|
||||
Future<void> updateGameRuleset({
|
||||
required String gameId,
|
||||
required Ruleset newRuleset,
|
||||
}) async {
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(ruleset: Value(newRuleset.name)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Updates the description of the game with the given [gameId].
|
||||
Future<void> updateGameDescription({
|
||||
required String gameId,
|
||||
required String newDescription,
|
||||
}) async {
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(description: Value(newDescription)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Updates the color of the game with the given [gameId].
|
||||
Future<void> updateGameColor({
|
||||
required String gameId,
|
||||
required GameColor newColor,
|
||||
}) async {
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(color: Value(newColor.name)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Updates the icon of the game with the given [gameId].
|
||||
Future<void> updateGameIcon({
|
||||
required String gameId,
|
||||
required String newIcon,
|
||||
}) async {
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(icon: Value(newIcon)),
|
||||
);
|
||||
}
|
||||
/* Read */
|
||||
|
||||
/// Retrieves the total count of games in the database.
|
||||
Future<int> getGameCount() async {
|
||||
@@ -169,6 +73,120 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
/// Checks if a game with the given [gameId] exists in the database.
|
||||
/// Returns `true` if the game exists, `false` otherwise.
|
||||
Future<bool> gameExists({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
|
||||
/// Retrieves all games from the database.
|
||||
Future<List<Game>> getAllGames() async {
|
||||
final query = select(gameTable);
|
||||
final result = await query.get();
|
||||
return result
|
||||
.map(
|
||||
(row) => Game(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
ruleset: Ruleset.values.firstWhere((e) => e.name == row.ruleset),
|
||||
description: row.description,
|
||||
color: GameColor.values.firstWhere((e) => e.name == row.color),
|
||||
icon: row.icon,
|
||||
createdAt: row.createdAt,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Retrieves a [Game] by its [gameId].
|
||||
Future<Game> getGameById({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingle();
|
||||
return Game(
|
||||
id: result.id,
|
||||
name: result.name,
|
||||
ruleset: Ruleset.values.firstWhere((e) => e.name == result.ruleset),
|
||||
description: result.description,
|
||||
color: GameColor.values.firstWhere((e) => e.name == result.color),
|
||||
icon: result.icon,
|
||||
createdAt: result.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
/* Update */
|
||||
|
||||
/// Updates the name of the game with the given [gameId] to [newName].
|
||||
Future<bool> updateGameName({
|
||||
required String gameId,
|
||||
required String newName,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(name: Value(newName)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Updates the ruleset of the game with the given [gameId].
|
||||
Future<bool> updateGameRuleset({
|
||||
required String gameId,
|
||||
required Ruleset newRuleset,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(ruleset: Value(newRuleset.name)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Updates the description of the game with the given [gameId].
|
||||
Future<bool> updateGameDescription({
|
||||
required String gameId,
|
||||
required String newDescription,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(description: Value(newDescription)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Updates the color of the game with the given [gameId].
|
||||
Future<bool> updateGameColor({
|
||||
required String gameId,
|
||||
required GameColor newColor,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(color: Value(newColor.name)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Updates the icon of the game with the given [gameId].
|
||||
Future<bool> updateGameIcon({
|
||||
required String gameId,
|
||||
required String newIcon,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(gameTable)..where((g) => g.id.equals(gameId))).write(
|
||||
GameTableCompanion(icon: Value(newIcon)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/* Delete */
|
||||
|
||||
/// Deletes the game with the given [gameId] from the database.
|
||||
/// Returns `true` if the game was deleted, `false` if the game did not exist.
|
||||
Future<bool> deleteGame({required String gameId}) async {
|
||||
final query = delete(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Deletes all games from the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> deleteAllGames() async {
|
||||
|
||||
@@ -54,117 +54,51 @@ void main() {
|
||||
});
|
||||
|
||||
group('Game Tests', () {
|
||||
// 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);
|
||||
});
|
||||
|
||||
// Verifies that a single game can be added and retrieved with all fields intact.
|
||||
group('CREATE', () {
|
||||
test('Adding and fetching a single game works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
final added = await database.gameDao.addGame(game: testGame1);
|
||||
expect(added, true);
|
||||
|
||||
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);
|
||||
final game = await database.gameDao.getGameById(gameId: testGame1.id);
|
||||
expect(game.id, testGame1.id);
|
||||
expect(game.name, testGame1.name);
|
||||
expect(game.ruleset, testGame1.ruleset);
|
||||
expect(game.description, testGame1.description);
|
||||
expect(game.color, testGame1.color);
|
||||
expect(game.icon, testGame1.icon);
|
||||
expect(game.createdAt, testGame1.createdAt);
|
||||
});
|
||||
|
||||
// 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 allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames.length, 3);
|
||||
|
||||
final names = allGames.map((g) => g.name).toList();
|
||||
expect(names, containsAll(['Chess', 'Poker', 'Monopoly']));
|
||||
});
|
||||
|
||||
// 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);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
// 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>()),
|
||||
);
|
||||
});
|
||||
|
||||
// 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);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames.length, 1);
|
||||
});
|
||||
|
||||
// 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);
|
||||
|
||||
final secondAdd = await database.gameDao.addGame(game: testGame1);
|
||||
expect(secondAdd, false);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames.length, 1);
|
||||
});
|
||||
|
||||
// Verifies that a game with empty optional fields can be added and retrieved.
|
||||
test('addGame handles game with null optional fields', () async {
|
||||
final gameWithNulls = Game(
|
||||
name: 'Simple Game',
|
||||
ruleset: Ruleset.lowestScore,
|
||||
description: 'A simple game',
|
||||
color: GameColor.green,
|
||||
icon: '',
|
||||
);
|
||||
final result = await database.gameDao.addGame(game: gameWithNulls);
|
||||
expect(result, true);
|
||||
|
||||
final fetchedGame = await database.gameDao.getGameById(
|
||||
gameId: gameWithNulls.id,
|
||||
);
|
||||
expect(fetchedGame.name, 'Simple Game');
|
||||
expect(fetchedGame.description, 'A simple game');
|
||||
expect(fetchedGame.color, GameColor.green);
|
||||
expect(fetchedGame.icon, '');
|
||||
});
|
||||
|
||||
// Verifies that multiple games can be added at once using addGamesAsList.
|
||||
test('addGamesAsList adds multiple games correctly', () async {
|
||||
final result = await database.gameDao.addGamesAsList(
|
||||
final added = await database.gameDao.addGamesAsList(
|
||||
games: [testGame1, testGame2, testGame3],
|
||||
);
|
||||
expect(result, true);
|
||||
expect(added, true);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames.length, 3);
|
||||
|
||||
// Map for connecting fetched games with expected games
|
||||
final testGames = {
|
||||
testGame1.id: testGame1,
|
||||
testGame2.id: testGame2,
|
||||
testGame3.id: testGame3,
|
||||
};
|
||||
|
||||
for (final game in allGames) {
|
||||
final testGame = testGames[game.id]!;
|
||||
|
||||
expect(game.id, testGame.id);
|
||||
expect(game.name, testGame.name);
|
||||
expect(game.createdAt, testGame.createdAt);
|
||||
expect(game.description, testGame.description);
|
||||
expect(game.ruleset, testGame.ruleset);
|
||||
expect(game.color, testGame.color);
|
||||
expect(game.icon, testGame.icon);
|
||||
}
|
||||
});
|
||||
|
||||
// Verifies that addGamesAsList returns false when given an empty list.
|
||||
test('addGamesAsList returns false for empty list', () async {
|
||||
test('addGamesAsList() returns false for empty list', () async {
|
||||
final result = await database.gameDao.addGamesAsList(games: []);
|
||||
expect(result, false);
|
||||
|
||||
@@ -172,293 +106,19 @@ void main() {
|
||||
expect(allGames.length, 0);
|
||||
});
|
||||
|
||||
// Verifies that addGamesAsList ignores duplicate games when adding.
|
||||
test('addGamesAsList ignores duplicate games', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
final result = await database.gameDao.addGamesAsList(
|
||||
games: [testGame1, testGame2],
|
||||
test('addGamesAsList() ignores duplicate games', () async {
|
||||
final added = await database.gameDao.addGamesAsList(
|
||||
games: [testGame1, testGame2, testGame1],
|
||||
);
|
||||
expect(result, true);
|
||||
expect(added, true);
|
||||
|
||||
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],
|
||||
);
|
||||
|
||||
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(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: Ruleset.highestScore,
|
||||
);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.ruleset, Ruleset.highestScore);
|
||||
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: Ruleset.lowestScore,
|
||||
);
|
||||
|
||||
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 an empty string.
|
||||
test('updateGameDescription can set description to empty string', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
await database.gameDao.updateGameDescription(
|
||||
gameId: testGame1.id,
|
||||
newDescription: '',
|
||||
);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.description, '');
|
||||
});
|
||||
|
||||
// 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: GameColor.green,
|
||||
);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.color, GameColor.green);
|
||||
});
|
||||
|
||||
// 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: GameColor.green,
|
||||
);
|
||||
|
||||
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 update the icon.
|
||||
test('updateGameIcon updates icon correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
await database.gameDao.updateGameIcon(
|
||||
gameId: testGame1.id,
|
||||
newIcon: 'new_icon',
|
||||
);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.icon, 'new_icon');
|
||||
});
|
||||
|
||||
// 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 {
|
||||
test(
|
||||
'Game with special characters in name is stored correctly',
|
||||
() async {
|
||||
final specialGame = Game(
|
||||
name: 'Game\'s & "Special" <Name>',
|
||||
ruleset: Ruleset.multipleWinners,
|
||||
@@ -473,73 +133,274 @@ void main() {
|
||||
);
|
||||
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: Ruleset.singleWinner,
|
||||
description: '',
|
||||
icon: '',
|
||||
color: GameColor.red,
|
||||
);
|
||||
await database.gameDao.addGame(game: emptyGame);
|
||||
|
||||
final fetchedGame = await database.gameDao.getGameById(
|
||||
gameId: emptyGame.id,
|
||||
);
|
||||
expect(fetchedGame.name, '');
|
||||
expect(fetchedGame.ruleset, Ruleset.singleWinner);
|
||||
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: Ruleset.multipleWinners,
|
||||
color: GameColor.yellow,
|
||||
icon: '',
|
||||
);
|
||||
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, Ruleset.multipleWinners);
|
||||
});
|
||||
|
||||
// Verifies that multiple sequential updates to the same game work correctly.
|
||||
test('Multiple updates to the same game work correctly', () async {
|
||||
group('READ', () {
|
||||
test('getGameById() works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
await database.gameDao.updateGameName(
|
||||
gameId: testGame1.id,
|
||||
newName: 'Updated Name',
|
||||
final game = await database.gameDao.getGameById(gameId: testGame1.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('getGameById() throws exception for non-existent game', () async {
|
||||
expect(
|
||||
() => database.gameDao.getGameById(gameId: 'non-existent-id'),
|
||||
throwsA(isA<StateError>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('gameExists() works correctly', () async {
|
||||
var exists = await database.gameDao.gameExists(gameId: testGame1.id);
|
||||
expect(exists, false);
|
||||
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
exists = await database.gameDao.gameExists(gameId: testGame1.id);
|
||||
expect(exists, true);
|
||||
});
|
||||
|
||||
test('getAllGames() returns empty list when no games exist', () async {
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
});
|
||||
|
||||
test('getGameCount() works correctly', () async {
|
||||
var count = await database.gameDao.getGameCount();
|
||||
expect(count, 0);
|
||||
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
count = await database.gameDao.getGameCount();
|
||||
expect(count, 1);
|
||||
|
||||
await database.gameDao.addGame(game: testGame2);
|
||||
count = await database.gameDao.getGameCount();
|
||||
expect(count, 2);
|
||||
|
||||
await database.gameDao.deleteGame(gameId: testGame1.id);
|
||||
count = await database.gameDao.getGameCount();
|
||||
expect(count, 1);
|
||||
});
|
||||
});
|
||||
|
||||
group('UPDATE', () {
|
||||
test('updateGameName() updates the name correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
const newName = 'New name';
|
||||
|
||||
final updated = await database.gameDao.updateGameName(
|
||||
gameId: testGame1.id,
|
||||
newName: newName,
|
||||
);
|
||||
expect(updated, true);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.name, newName);
|
||||
});
|
||||
|
||||
test('updateGameName() does nothing for non-existent game', () async {
|
||||
final updated = await database.gameDao.updateGameName(
|
||||
gameId: 'non-existent-id',
|
||||
newName: 'New name',
|
||||
);
|
||||
expect(updated, false);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
});
|
||||
|
||||
test('updateGameRuleset() updates the ruleset correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
const ruleset = Ruleset.highestScore;
|
||||
|
||||
final updated = await database.gameDao.updateGameRuleset(
|
||||
gameId: testGame1.id,
|
||||
newRuleset: ruleset,
|
||||
);
|
||||
expect(updated, true);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.ruleset, ruleset);
|
||||
});
|
||||
|
||||
test('updateGameRuleset() does nothing for non-existent game', () async {
|
||||
final updated = await database.gameDao.updateGameRuleset(
|
||||
gameId: 'non-existent-id',
|
||||
newRuleset: Ruleset.lowestScore,
|
||||
);
|
||||
expect(updated, false);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
});
|
||||
|
||||
test(
|
||||
'updateGameDescription() updates the description correctly',
|
||||
() async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
const newDescription = 'New description';
|
||||
|
||||
final updated = await database.gameDao.updateGameDescription(
|
||||
gameId: testGame1.id,
|
||||
newDescription: newDescription,
|
||||
);
|
||||
expect(updated, true);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.description, newDescription);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'updateGameDescription() does nothing for non-existent game',
|
||||
() async {
|
||||
final updated = await database.gameDao.updateGameDescription(
|
||||
gameId: 'non-existent-id',
|
||||
newDescription: 'New description',
|
||||
);
|
||||
expect(updated, false);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test('updateGameColor() works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
await database.gameDao.updateGameColor(
|
||||
gameId: testGame1.id,
|
||||
newColor: GameColor.teal,
|
||||
);
|
||||
await database.gameDao.updateGameDescription(
|
||||
gameId: testGame1.id,
|
||||
newDescription: 'Updated Description',
|
||||
newColor: GameColor.green,
|
||||
);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.name, 'Updated Name');
|
||||
expect(updatedGame.color, GameColor.teal);
|
||||
expect(updatedGame.description, 'Updated Description');
|
||||
expect(updatedGame.color, GameColor.green);
|
||||
});
|
||||
|
||||
test('updateGameColor() does nothing for non-existent game', () async {
|
||||
final updated = await database.gameDao.updateGameColor(
|
||||
gameId: 'non-existent-id',
|
||||
newColor: GameColor.green,
|
||||
);
|
||||
expect(updated, false);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
});
|
||||
|
||||
test('updateGameIcon() works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
const newIcon = 'new_chess_icon';
|
||||
|
||||
final updated = await database.gameDao.updateGameIcon(
|
||||
gameId: testGame1.id,
|
||||
newIcon: newIcon,
|
||||
);
|
||||
expect(updated, true);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
expect(updatedGame.icon, newIcon);
|
||||
});
|
||||
|
||||
test('updateGameIcon() does nothing for non-existent game', () async {
|
||||
final updated = await database.gameDao.updateGameIcon(
|
||||
gameId: 'non-existent-id',
|
||||
newIcon: 'New icon',
|
||||
);
|
||||
expect(updated, false);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
});
|
||||
|
||||
test('Multiple updates to the same game work correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
const newName = 'New name';
|
||||
await database.gameDao.updateGameName(
|
||||
gameId: testGame1.id,
|
||||
newName: newName,
|
||||
);
|
||||
|
||||
const newGameColor = GameColor.teal;
|
||||
await database.gameDao.updateGameColor(
|
||||
gameId: testGame1.id,
|
||||
newColor: newGameColor,
|
||||
);
|
||||
|
||||
const newDescription = 'New description';
|
||||
await database.gameDao.updateGameDescription(
|
||||
gameId: testGame1.id,
|
||||
newDescription: newDescription,
|
||||
);
|
||||
|
||||
final updatedGame = await database.gameDao.getGameById(
|
||||
gameId: testGame1.id,
|
||||
);
|
||||
|
||||
// Changed values
|
||||
expect(updatedGame.name, newName);
|
||||
expect(updatedGame.color, newGameColor);
|
||||
expect(updatedGame.description, newDescription);
|
||||
|
||||
// Staying the same
|
||||
expect(updatedGame.ruleset, testGame1.ruleset);
|
||||
expect(updatedGame.icon, testGame1.icon);
|
||||
});
|
||||
});
|
||||
group('DELETE', () {
|
||||
test('deleteGame() works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
final deleted = await database.gameDao.deleteGame(gameId: testGame1.id);
|
||||
expect(deleted, true);
|
||||
|
||||
final allGames = await database.gameDao.getAllGames();
|
||||
expect(allGames, isEmpty);
|
||||
});
|
||||
|
||||
test('deleteGame() returns false for non-existent game', () async {
|
||||
final deleted = await database.gameDao.deleteGame(
|
||||
gameId: 'non-existent-id',
|
||||
);
|
||||
expect(deleted, false);
|
||||
});
|
||||
|
||||
test('deleteAllGames() removes all games', () async {
|
||||
await database.gameDao.addGamesAsList(
|
||||
games: [testGame1, testGame2, testGame3],
|
||||
);
|
||||
|
||||
var count = await database.gameDao.getGameCount();
|
||||
expect(count, 3);
|
||||
|
||||
final deleted = await database.gameDao.deleteAllGames();
|
||||
expect(deleted, true);
|
||||
|
||||
count = await database.gameDao.getGameCount();
|
||||
expect(count, 0);
|
||||
});
|
||||
|
||||
test('deleteAllGames() returns false when no games exist', () async {
|
||||
final deleted = await database.gameDao.deleteAllGames();
|
||||
expect(deleted, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user