diff --git a/lib/data/dao/player_dao.dart b/lib/data/dao/player_dao.dart index 01e7163..976d4b0 100644 --- a/lib/data/dao/player_dao.dart +++ b/lib/data/dao/player_dao.dart @@ -61,4 +61,13 @@ class PlayerDao extends DatabaseAccessor with _$PlayerDaoMixin { PlayerTableCompanion(name: Value(newName)), ); } + + /// Retrieves the total count of players in the database. + Future getPlayerCount() async { + final count = + await (selectOnly(playerTable)..addColumns([playerTable.id.count()])) + .map((row) => row.read(playerTable.id.count())) + .getSingle(); + return count ?? 0; + } } diff --git a/test/db_tests/group_test.dart b/test/db_tests/group_test.dart index cdf514f..a8fbcd5 100644 --- a/test/db_tests/group_test.dart +++ b/test/db_tests/group_test.dart @@ -127,4 +127,22 @@ void main() { final playerExists = result.members.any((p) => p.id == playerToRemove.id); expect(playerExists, false); }); + + test('get group count works correctly', () async { + final initialCount = await database.groupDao.getGroupCount(); + expect(initialCount, 0); + + await database.groupDao.addGroup(group: testgroup); + + final groupAdded = await database.groupDao.getGroupCount(); + expect(groupAdded, 1); + + final groupRemoved = await database.groupDao.deleteGroup( + groupId: testgroup.id, + ); + expect(groupRemoved, true); + + final finalCount = await database.groupDao.getGroupCount(); + expect(finalCount, 0); + }); } diff --git a/test/db_tests/player_test.dart b/test/db_tests/player_test.dart index a53f2cc..7fb9152 100644 --- a/test/db_tests/player_test.dart +++ b/test/db_tests/player_test.dart @@ -62,5 +62,23 @@ void main() { ); expect(result.name, newPlayerName); }); + + test('get player count works correctly', () async { + final initialCount = await database.playerDao.getPlayerCount(); + expect(initialCount, 0); + + await database.playerDao.addPlayer(player: testPlayer); + + final playerAdded = await database.playerDao.getPlayerCount(); + expect(playerAdded, 1); + + final playerRemoved = await database.playerDao.deletePlayer( + playerId: testPlayer.id, + ); + expect(playerRemoved, true); + + final finalCount = await database.playerDao.getPlayerCount(); + expect(finalCount, 0); + }); }); }