Updated group dao + tests
This commit is contained in:
@@ -12,43 +12,7 @@ part 'group_dao.g.dart';
|
|||||||
class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||||
GroupDao(super.db);
|
GroupDao(super.db);
|
||||||
|
|
||||||
/// Retrieves all groups from the database.
|
/* Create */
|
||||||
Future<List<Group>> getAllGroups() async {
|
|
||||||
final query = select(groupTable);
|
|
||||||
final result = await query.get();
|
|
||||||
return Future.wait(
|
|
||||||
result.map((groupData) async {
|
|
||||||
final members = await db.playerGroupDao.getPlayersOfGroup(
|
|
||||||
groupId: groupData.id,
|
|
||||||
);
|
|
||||||
return Group(
|
|
||||||
id: groupData.id,
|
|
||||||
name: groupData.name,
|
|
||||||
description: groupData.description,
|
|
||||||
members: members,
|
|
||||||
createdAt: groupData.createdAt,
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Retrieves a [Group] by its [groupId], including its members.
|
|
||||||
Future<Group> getGroupById({required String groupId}) async {
|
|
||||||
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
|
||||||
final result = await query.getSingle();
|
|
||||||
|
|
||||||
List<Player> members = await db.playerGroupDao.getPlayersOfGroup(
|
|
||||||
groupId: groupId,
|
|
||||||
);
|
|
||||||
|
|
||||||
return Group(
|
|
||||||
id: result.id,
|
|
||||||
name: result.name,
|
|
||||||
description: result.description,
|
|
||||||
members: members,
|
|
||||||
createdAt: result.createdAt,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds a new group with the given [id] and [name] to the database.
|
/// Adds a new group with the given [id] and [name] to the database.
|
||||||
/// This method also adds the group's members to the [PlayerGroupTable].
|
/// This method also adds the group's members to the [PlayerGroupTable].
|
||||||
@@ -172,6 +136,65 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Read */
|
||||||
|
|
||||||
|
/// Retrieves all groups from the database.
|
||||||
|
Future<List<Group>> getAllGroups() async {
|
||||||
|
final query = select(groupTable);
|
||||||
|
final result = await query.get();
|
||||||
|
return Future.wait(
|
||||||
|
result.map((groupData) async {
|
||||||
|
final members = await db.playerGroupDao.getPlayersOfGroup(
|
||||||
|
groupId: groupData.id,
|
||||||
|
);
|
||||||
|
return Group(
|
||||||
|
id: groupData.id,
|
||||||
|
name: groupData.name,
|
||||||
|
description: groupData.description,
|
||||||
|
members: members,
|
||||||
|
createdAt: groupData.createdAt,
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves a [Group] by its [groupId], including its members.
|
||||||
|
Future<Group> getGroupById({required String groupId}) async {
|
||||||
|
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
||||||
|
final result = await query.getSingle();
|
||||||
|
|
||||||
|
List<Player> members = await db.playerGroupDao.getPlayersOfGroup(
|
||||||
|
groupId: groupId,
|
||||||
|
);
|
||||||
|
|
||||||
|
return Group(
|
||||||
|
id: result.id,
|
||||||
|
name: result.name,
|
||||||
|
description: result.description,
|
||||||
|
members: members,
|
||||||
|
createdAt: result.createdAt,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves the number of groups in the database.
|
||||||
|
Future<int> getGroupCount() async {
|
||||||
|
final count =
|
||||||
|
await (selectOnly(groupTable)..addColumns([groupTable.id.count()]))
|
||||||
|
.map((row) => row.read(groupTable.id.count()))
|
||||||
|
.getSingle();
|
||||||
|
return count ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if a group with the given [groupId] exists in the database.
|
||||||
|
/// Returns `true` if the group exists, `false` otherwise.
|
||||||
|
Future<bool> groupExists({required String groupId}) async {
|
||||||
|
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
||||||
|
final result = await query.getSingleOrNull();
|
||||||
|
return result != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Delete */
|
||||||
|
|
||||||
/// Deletes the group with the given [id] from the database.
|
/// Deletes the group with the given [id] from the database.
|
||||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
Future<bool> deleteGroup({required String groupId}) async {
|
Future<bool> deleteGroup({required String groupId}) async {
|
||||||
@@ -180,6 +203,16 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
return rowsAffected > 0;
|
return rowsAffected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes all groups from the database.
|
||||||
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
|
Future<bool> deleteAllGroups() async {
|
||||||
|
final query = delete(groupTable);
|
||||||
|
final rowsAffected = await query.go();
|
||||||
|
return rowsAffected > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update */
|
||||||
|
|
||||||
/// Updates the name of the group with the given [id] to [newName].
|
/// Updates the name of the group with the given [id] to [newName].
|
||||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
Future<bool> updateGroupName({
|
Future<bool> updateGroupName({
|
||||||
@@ -206,31 +239,6 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
return rowsAffected > 0;
|
return rowsAffected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the number of groups in the database.
|
|
||||||
Future<int> getGroupCount() async {
|
|
||||||
final count =
|
|
||||||
await (selectOnly(groupTable)..addColumns([groupTable.id.count()]))
|
|
||||||
.map((row) => row.read(groupTable.id.count()))
|
|
||||||
.getSingle();
|
|
||||||
return count ?? 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks if a group with the given [groupId] exists in the database.
|
|
||||||
/// Returns `true` if the group exists, `false` otherwise.
|
|
||||||
Future<bool> groupExists({required String groupId}) async {
|
|
||||||
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
|
||||||
final result = await query.getSingleOrNull();
|
|
||||||
return result != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deletes all groups from the database.
|
|
||||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
|
||||||
Future<bool> deleteAllGroups() async {
|
|
||||||
final query = delete(groupTable);
|
|
||||||
final rowsAffected = await query.go();
|
|
||||||
return rowsAffected > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replaces all players in a group with the provided list of players.
|
/// Replaces all players in a group with the provided list of players.
|
||||||
/// Removes all existing players from the group and adds the new players.
|
/// Removes all existing players from the group and adds the new players.
|
||||||
/// Also adds any new players to the player table if they don't exist.
|
/// Also adds any new players to the player table if they don't exist.
|
||||||
|
|||||||
@@ -35,25 +35,23 @@ void main() {
|
|||||||
testPlayer4 = Player(name: 'Diana');
|
testPlayer4 = Player(name: 'Diana');
|
||||||
testGroup1 = Group(
|
testGroup1 = Group(
|
||||||
name: 'Test Group',
|
name: 'Test Group',
|
||||||
description: '',
|
|
||||||
members: [testPlayer1, testPlayer2, testPlayer3],
|
members: [testPlayer1, testPlayer2, testPlayer3],
|
||||||
|
description: 'description of the test group 1',
|
||||||
);
|
);
|
||||||
testGroup2 = Group(
|
testGroup2 = Group(
|
||||||
id: 'gr2',
|
id: 'gr2',
|
||||||
name: 'Second Group',
|
name: 'Second Group',
|
||||||
description: '',
|
|
||||||
members: [testPlayer2, testPlayer3, testPlayer4],
|
members: [testPlayer2, testPlayer3, testPlayer4],
|
||||||
|
description: 'description of the test group 2',
|
||||||
);
|
);
|
||||||
testGroup3 = Group(
|
testGroup3 = Group(
|
||||||
id: 'gr2',
|
id: 'gr2',
|
||||||
name: 'Second Group',
|
name: 'Second Group',
|
||||||
description: '',
|
|
||||||
members: [testPlayer2, testPlayer4],
|
members: [testPlayer2, testPlayer4],
|
||||||
);
|
);
|
||||||
testGroup4 = Group(
|
testGroup4 = Group(
|
||||||
id: 'gr2',
|
id: 'gr2',
|
||||||
name: 'Second Group',
|
name: 'Second Group',
|
||||||
description: '',
|
|
||||||
members: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
|
members: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -62,312 +60,361 @@ void main() {
|
|||||||
await database.close();
|
await database.close();
|
||||||
});
|
});
|
||||||
group('Group Tests', () {
|
group('Group Tests', () {
|
||||||
// Verifies that a single group can be added and retrieved with all fields and members intact.
|
group('CREATE', () {
|
||||||
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: testGroup1);
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
|
||||||
final fetchedGroup = await database.groupDao.getGroupById(
|
final fetchedGroup = await database.groupDao.getGroupById(
|
||||||
groupId: testGroup1.id,
|
groupId: testGroup1.id,
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchedGroup.id, testGroup1.id);
|
|
||||||
expect(fetchedGroup.name, testGroup1.name);
|
|
||||||
expect(fetchedGroup.createdAt, testGroup1.createdAt);
|
|
||||||
|
|
||||||
expect(fetchedGroup.members.length, testGroup1.members.length);
|
|
||||||
for (int i = 0; i < testGroup1.members.length; i++) {
|
|
||||||
expect(fetchedGroup.members[i].id, testGroup1.members[i].id);
|
|
||||||
expect(fetchedGroup.members[i].name, testGroup1.members[i].name);
|
|
||||||
expect(
|
|
||||||
fetchedGroup.members[i].createdAt,
|
|
||||||
testGroup1.members[i].createdAt,
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that multiple groups can be added and retrieved with correct members.
|
expect(fetchedGroup.id, testGroup1.id);
|
||||||
test('Adding and fetching multiple groups works correctly', () async {
|
expect(fetchedGroup.name, testGroup1.name);
|
||||||
await database.groupDao.addGroupsAsList(
|
expect(fetchedGroup.createdAt, testGroup1.createdAt);
|
||||||
groups: [testGroup1, testGroup2, testGroup3, testGroup4],
|
|
||||||
);
|
|
||||||
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
expect(fetchedGroup.members.length, testGroup1.members.length);
|
||||||
expect(allGroups.length, 2);
|
for (int i = 0; i < testGroup1.members.length; i++) {
|
||||||
|
expect(fetchedGroup.members[i].id, testGroup1.members[i].id);
|
||||||
final testGroups = {testGroup1.id: testGroup1, testGroup2.id: testGroup2};
|
expect(fetchedGroup.members[i].name, testGroup1.members[i].name);
|
||||||
|
expect(
|
||||||
for (final group in allGroups) {
|
fetchedGroup.members[i].createdAt,
|
||||||
final testGroup = testGroups[group.id]!;
|
testGroup1.members[i].createdAt,
|
||||||
|
);
|
||||||
expect(group.id, testGroup.id);
|
|
||||||
expect(group.name, testGroup.name);
|
|
||||||
expect(group.createdAt, testGroup.createdAt);
|
|
||||||
|
|
||||||
expect(group.members.length, testGroup.members.length);
|
|
||||||
for (int i = 0; i < testGroup.members.length; i++) {
|
|
||||||
expect(group.members[i].id, testGroup.members[i].id);
|
|
||||||
expect(group.members[i].name, testGroup.members[i].name);
|
|
||||||
expect(group.members[i].createdAt, testGroup.members[i].createdAt);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
test('Adding the same group twice does not create duplicates', () async {
|
||||||
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
|
||||||
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
|
expect(allGroups.length, 1);
|
||||||
|
|
||||||
|
final fetchedGroup = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(fetchedGroup.id, testGroup1.id);
|
||||||
|
expect(fetchedGroup.members.length, testGroup1.members.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('addGroup() returns false when group already exists', () async {
|
||||||
|
final firstAdd = await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
expect(firstAdd, true);
|
||||||
|
|
||||||
|
final secondAdd = await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
expect(secondAdd, false);
|
||||||
|
|
||||||
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
|
expect(allGroups.length, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Adding and fetching multiple groups works correctly', () async {
|
||||||
|
await database.groupDao.addGroupsAsList(
|
||||||
|
groups: [testGroup1, testGroup2, testGroup3, testGroup4],
|
||||||
|
);
|
||||||
|
|
||||||
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
|
expect(allGroups.length, 2);
|
||||||
|
|
||||||
|
final testGroups = {
|
||||||
|
testGroup1.id: testGroup1,
|
||||||
|
testGroup2.id: testGroup2,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (final group in allGroups) {
|
||||||
|
final testGroup = testGroups[group.id]!;
|
||||||
|
|
||||||
|
expect(group.id, testGroup.id);
|
||||||
|
expect(group.name, testGroup.name);
|
||||||
|
expect(group.createdAt, testGroup.createdAt);
|
||||||
|
|
||||||
|
expect(group.members.length, testGroup.members.length);
|
||||||
|
for (int i = 0; i < testGroup.members.length; i++) {
|
||||||
|
expect(group.members[i].id, testGroup.members[i].id);
|
||||||
|
expect(group.members[i].name, testGroup.members[i].name);
|
||||||
|
expect(group.members[i].createdAt, testGroup.members[i].createdAt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('addGroupsAsList() handles empty list correctly', () async {
|
||||||
|
await database.groupDao.addGroupsAsList(groups: []);
|
||||||
|
|
||||||
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
|
expect(allGroups.length, 0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verifies that adding the same group twice does not create duplicates.
|
group('READ', () {
|
||||||
test('Adding the same group twice does not create duplicates', () async {
|
test('groupExists() works correctly', () async {
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
var groupExists = await database.groupDao.groupExists(
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(groupExists, false);
|
||||||
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
expect(allGroups.length, 1);
|
|
||||||
|
groupExists = await database.groupDao.groupExists(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(groupExists, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getGroupCount() works correctly', () async {
|
||||||
|
var count = await database.groupDao.getGroupCount();
|
||||||
|
expect(count, 0);
|
||||||
|
|
||||||
|
var added = await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
expect(added, true);
|
||||||
|
count = await database.groupDao.getGroupCount();
|
||||||
|
expect(count, 1);
|
||||||
|
|
||||||
|
added = await database.groupDao.addGroup(group: testGroup2);
|
||||||
|
expect(added, true);
|
||||||
|
count = await database.groupDao.getGroupCount();
|
||||||
|
expect(count, 2);
|
||||||
|
|
||||||
|
final removed = await database.groupDao.deleteGroup(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(removed, true);
|
||||||
|
count = await database.groupDao.getGroupCount();
|
||||||
|
expect(count, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getGroupById() throws exception for non-existent group', () async {
|
||||||
|
expect(
|
||||||
|
() => database.groupDao.getGroupById(groupId: 'non-existent-id'),
|
||||||
|
throwsA(isA<StateError>()),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getAllGroups() returns empty list when no groups exist', () async {
|
||||||
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
|
expect(allGroups, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('addGroupsAsList() with duplicate groups only adds once', () async {
|
||||||
|
await database.groupDao.addGroupsAsList(
|
||||||
|
groups: [testGroup1, testGroup1, testGroup1],
|
||||||
|
);
|
||||||
|
|
||||||
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
|
expect(allGroups.length, 1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verifies that groupExists returns correct boolean based on group presence.
|
group('UPDATE', () {
|
||||||
test('Group existence check works correctly', () async {
|
test('updateGroupName() works correctly', () async {
|
||||||
var groupExists = await database.groupDao.groupExists(
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(groupExists, false);
|
|
||||||
|
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
const newName = 'New name';
|
||||||
|
await database.groupDao.updateGroupName(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
newName: newName,
|
||||||
|
);
|
||||||
|
|
||||||
groupExists = await database.groupDao.groupExists(groupId: testGroup1.id);
|
final result = await database.groupDao.getGroupById(
|
||||||
expect(groupExists, true);
|
groupId: testGroup1.id,
|
||||||
});
|
);
|
||||||
|
expect(result.name, newName);
|
||||||
|
});
|
||||||
|
|
||||||
// Verifies that deleteGroup removes the group and returns true.
|
test('updateGroupName() returns false for non-existent group', () async {
|
||||||
test('Deleting a group works correctly', () async {
|
final updated = await database.groupDao.updateGroupName(
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
|
|
||||||
final groupDeleted = await database.groupDao.deleteGroup(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(groupDeleted, true);
|
|
||||||
|
|
||||||
final groupExists = await database.groupDao.groupExists(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(groupExists, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that updateGroupName correctly updates only the name field.
|
|
||||||
test('Updating a group name works correctly', () async {
|
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
|
|
||||||
const newGroupName = 'new group name';
|
|
||||||
|
|
||||||
await database.groupDao.updateGroupName(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
newName: newGroupName,
|
|
||||||
);
|
|
||||||
|
|
||||||
final result = await database.groupDao.getGroupById(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(result.name, newGroupName);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that getGroupCount returns correct count through add/delete operations.
|
|
||||||
test('Getting the group count works correctly', () async {
|
|
||||||
final initialCount = await database.groupDao.getGroupCount();
|
|
||||||
expect(initialCount, 0);
|
|
||||||
|
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
|
|
||||||
final groupAdded = await database.groupDao.getGroupCount();
|
|
||||||
expect(groupAdded, 1);
|
|
||||||
|
|
||||||
final groupRemoved = await database.groupDao.deleteGroup(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(groupRemoved, true);
|
|
||||||
|
|
||||||
final finalCount = await database.groupDao.getGroupCount();
|
|
||||||
expect(finalCount, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that getAllGroups returns an empty list when no groups exist.
|
|
||||||
test('getAllGroups returns empty list when no groups exist', () async {
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
|
||||||
expect(allGroups, isEmpty);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that getGroupById throws StateError for non-existent group ID.
|
|
||||||
test('getGroupById throws exception for non-existent group', () async {
|
|
||||||
expect(
|
|
||||||
() => database.groupDao.getGroupById(groupId: 'non-existent-id'),
|
|
||||||
throwsA(isA<StateError>()),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that addGroup returns false when trying to add a duplicate group.
|
|
||||||
test('addGroup returns false when group already exists', () async {
|
|
||||||
final firstAdd = await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
expect(firstAdd, true);
|
|
||||||
|
|
||||||
final secondAdd = await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
expect(secondAdd, false);
|
|
||||||
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
|
||||||
expect(allGroups.length, 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that addGroupsAsList handles an empty list without errors.
|
|
||||||
test('addGroupsAsList handles empty list correctly', () async {
|
|
||||||
await database.groupDao.addGroupsAsList(groups: []);
|
|
||||||
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
|
||||||
expect(allGroups.length, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that deleteGroup returns false for a non-existent group ID.
|
|
||||||
test('deleteGroup returns false for non-existent group', () async {
|
|
||||||
final deleted = await database.groupDao.deleteGroup(
|
|
||||||
groupId: 'non-existent-id',
|
|
||||||
);
|
|
||||||
expect(deleted, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that updateGroupName returns false for a non-existent group ID.
|
|
||||||
test('updateGroupName returns false for non-existent group', () async {
|
|
||||||
final updated = await database.groupDao.updateGroupName(
|
|
||||||
groupId: 'non-existent-id',
|
|
||||||
newName: 'New Name',
|
|
||||||
);
|
|
||||||
expect(updated, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that updateGroupDescription correctly updates the description field.
|
|
||||||
test('Updating a group description works correctly', () async {
|
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
|
|
||||||
const newDescription = 'This is a new description';
|
|
||||||
|
|
||||||
final updated = await database.groupDao.updateGroupDescription(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
newDescription: newDescription,
|
|
||||||
);
|
|
||||||
expect(updated, true);
|
|
||||||
|
|
||||||
final result = await database.groupDao.getGroupById(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(result.description, newDescription);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that updateGroupDescription can set the description to null.
|
|
||||||
test('updateGroupDescription can set description to null', () async {
|
|
||||||
final groupWithDescription = Group(
|
|
||||||
name: 'Group with description',
|
|
||||||
description: 'Initial description',
|
|
||||||
members: [testPlayer1],
|
|
||||||
);
|
|
||||||
await database.groupDao.addGroup(group: groupWithDescription);
|
|
||||||
|
|
||||||
final updated = await database.groupDao.updateGroupDescription(
|
|
||||||
groupId: groupWithDescription.id,
|
|
||||||
newDescription: 'Updated description',
|
|
||||||
);
|
|
||||||
expect(updated, true);
|
|
||||||
|
|
||||||
final result = await database.groupDao.getGroupById(
|
|
||||||
groupId: groupWithDescription.id,
|
|
||||||
);
|
|
||||||
expect(result.description, 'Updated description');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that updateGroupDescription returns false for a non-existent group.
|
|
||||||
test(
|
|
||||||
'updateGroupDescription returns false for non-existent group',
|
|
||||||
() async {
|
|
||||||
final updated = await database.groupDao.updateGroupDescription(
|
|
||||||
groupId: 'non-existent-id',
|
groupId: 'non-existent-id',
|
||||||
newDescription: 'New Description',
|
newName: 'New name',
|
||||||
);
|
);
|
||||||
expect(updated, false);
|
expect(updated, false);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// Verifies that deleteAllGroups removes all groups from the database.
|
test('updateGroupDescription() works correctly', () async {
|
||||||
test('deleteAllGroups removes all groups', () async {
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
await database.groupDao.addGroupsAsList(groups: [testGroup1, testGroup2]);
|
|
||||||
|
|
||||||
final countBefore = await database.groupDao.getGroupCount();
|
const newDescription = 'New description';
|
||||||
expect(countBefore, 2);
|
final updated = await database.groupDao.updateGroupDescription(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
newDescription: newDescription,
|
||||||
|
);
|
||||||
|
expect(updated, true);
|
||||||
|
|
||||||
final deleted = await database.groupDao.deleteAllGroups();
|
final group = await database.groupDao.getGroupById(
|
||||||
expect(deleted, true);
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(group.description, newDescription);
|
||||||
|
});
|
||||||
|
|
||||||
final countAfter = await database.groupDao.getGroupCount();
|
test(
|
||||||
expect(countAfter, 0);
|
'updateGroupDescription() returns false for non-existent group',
|
||||||
|
() async {
|
||||||
|
final updated = await database.groupDao.updateGroupDescription(
|
||||||
|
groupId: 'non-existent-id',
|
||||||
|
newDescription: 'New description',
|
||||||
|
);
|
||||||
|
expect(updated, false);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('Multiple updates to the same group work correctly', () async {
|
||||||
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
const newName = 'New name';
|
||||||
|
const newDescription = 'New description';
|
||||||
|
|
||||||
|
await database.groupDao.updateGroupName(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
newName: newName,
|
||||||
|
);
|
||||||
|
await database.groupDao.updateGroupDescription(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
newDescription: newDescription,
|
||||||
|
);
|
||||||
|
|
||||||
|
final updatedGroup = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(updatedGroup.name, newName);
|
||||||
|
expect(updatedGroup.description, newDescription);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('replaceGroupPlayers() works correctly', () async {
|
||||||
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
|
||||||
|
final initialGroup = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(initialGroup.members.length, 3);
|
||||||
|
expect(
|
||||||
|
initialGroup.members
|
||||||
|
.map((p) => p.id)
|
||||||
|
.toList()
|
||||||
|
.contains(testPlayer1.id),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
initialGroup.members
|
||||||
|
.map((p) => p.id)
|
||||||
|
.toList()
|
||||||
|
.contains(testPlayer2.id),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
initialGroup.members
|
||||||
|
.map((p) => p.id)
|
||||||
|
.toList()
|
||||||
|
.contains(testPlayer3.id),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
final newPlayers = [testPlayer2, testPlayer4];
|
||||||
|
final replaced = await database.groupDao.replaceGroupPlayers(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
newPlayers: newPlayers,
|
||||||
|
);
|
||||||
|
expect(replaced, true);
|
||||||
|
|
||||||
|
final updatedGroup = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(updatedGroup.members.length, 2);
|
||||||
|
|
||||||
|
final memberIds = updatedGroup.members.map((p) => p.id).toList();
|
||||||
|
expect(memberIds.contains(testPlayer2.id), true);
|
||||||
|
expect(memberIds.contains(testPlayer4.id), true);
|
||||||
|
expect(memberIds.contains(testPlayer1.id), false);
|
||||||
|
expect(memberIds.contains(testPlayer3.id), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('replaceGroupPlayers() with empty list works correctly', () async {
|
||||||
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
|
||||||
|
final initialGroup = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(initialGroup.members.length, 3);
|
||||||
|
|
||||||
|
final replaced = await database.groupDao.replaceGroupPlayers(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
newPlayers: [],
|
||||||
|
);
|
||||||
|
expect(replaced, true);
|
||||||
|
|
||||||
|
final updatedGroup = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(updatedGroup.members.length, 0);
|
||||||
|
expect(updatedGroup.members, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'replaceGroupPlayers() returns false for non-existent group',
|
||||||
|
() async {
|
||||||
|
final replaced = await database.groupDao.replaceGroupPlayers(
|
||||||
|
groupId: 'non-existent-id',
|
||||||
|
newPlayers: [testPlayer1],
|
||||||
|
);
|
||||||
|
expect(replaced, false);
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verifies that deleteAllGroups returns false when no groups exist.
|
group('DELETE', () {
|
||||||
test('deleteAllGroups returns false when no groups exist', () async {
|
test('deleteGroup() works correctly', () async {
|
||||||
final deleted = await database.groupDao.deleteAllGroups();
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
expect(deleted, false);
|
|
||||||
|
final groupDeleted = await database.groupDao.deleteGroup(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(groupDeleted, true);
|
||||||
|
|
||||||
|
final groupExists = await database.groupDao.groupExists(
|
||||||
|
groupId: testGroup1.id,
|
||||||
|
);
|
||||||
|
expect(groupExists, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deleteGroup() returns false for non-existent group', () async {
|
||||||
|
final deleted = await database.groupDao.deleteGroup(
|
||||||
|
groupId: 'non-existent-id',
|
||||||
|
);
|
||||||
|
expect(deleted, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deleteAllGroups() works correctly', () async {
|
||||||
|
await database.groupDao.addGroupsAsList(
|
||||||
|
groups: [testGroup1, testGroup2],
|
||||||
|
);
|
||||||
|
|
||||||
|
var count = await database.groupDao.getGroupCount();
|
||||||
|
expect(count, 2);
|
||||||
|
|
||||||
|
final deleted = await database.groupDao.deleteAllGroups();
|
||||||
|
expect(deleted, true);
|
||||||
|
|
||||||
|
count = await database.groupDao.getGroupCount();
|
||||||
|
expect(count, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deleteAllGroups() returns false when no groups exist', () async {
|
||||||
|
final deleted = await database.groupDao.deleteAllGroups();
|
||||||
|
expect(deleted, false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verifies that groups with special characters (quotes, emojis) are stored correctly.
|
group('Edge Cases', () {
|
||||||
test('Group with special characters in name is stored correctly', () async {
|
test('Group with special characters is stored correctly', () async {
|
||||||
final specialGroup = Group(
|
final specialGroup = Group(
|
||||||
name: 'Group\'s & "Special" <Name>',
|
name: 'Group\'s & "Special" <Name>',
|
||||||
description: 'Description with émojis 🎮🎲',
|
description: 'Description with émojis 🎮🎲',
|
||||||
members: [testPlayer1],
|
members: [testPlayer1],
|
||||||
);
|
);
|
||||||
await database.groupDao.addGroup(group: specialGroup);
|
await database.groupDao.addGroup(group: specialGroup);
|
||||||
|
|
||||||
final fetchedGroup = await database.groupDao.getGroupById(
|
final fetchedGroup = await database.groupDao.getGroupById(
|
||||||
groupId: specialGroup.id,
|
groupId: specialGroup.id,
|
||||||
);
|
);
|
||||||
expect(fetchedGroup.name, 'Group\'s & "Special" <Name>');
|
expect(fetchedGroup.name, 'Group\'s & "Special" <Name>');
|
||||||
expect(fetchedGroup.description, 'Description with émojis 🎮🎲');
|
expect(fetchedGroup.description, 'Description with émojis 🎮🎲');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verifies that a group with an empty members list can be stored and retrieved.
|
|
||||||
test('Group with empty members list is stored correctly', () async {
|
|
||||||
final emptyGroup = Group(
|
|
||||||
name: 'Empty Group',
|
|
||||||
description: '',
|
|
||||||
members: [],
|
|
||||||
);
|
|
||||||
await database.groupDao.addGroup(group: emptyGroup);
|
|
||||||
|
|
||||||
final fetchedGroup = await database.groupDao.getGroupById(
|
|
||||||
groupId: emptyGroup.id,
|
|
||||||
);
|
|
||||||
expect(fetchedGroup.name, 'Empty Group');
|
|
||||||
expect(fetchedGroup.members, isEmpty);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that multiple sequential updates to the same group work correctly.
|
|
||||||
test('Multiple updates to the same group work correctly', () async {
|
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
|
||||||
|
|
||||||
await database.groupDao.updateGroupName(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
newName: 'Updated Name',
|
|
||||||
);
|
|
||||||
await database.groupDao.updateGroupDescription(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
newDescription: 'Updated Description',
|
|
||||||
);
|
|
||||||
|
|
||||||
final updatedGroup = await database.groupDao.getGroupById(
|
|
||||||
groupId: testGroup1.id,
|
|
||||||
);
|
|
||||||
expect(updatedGroup.name, 'Updated Name');
|
|
||||||
expect(updatedGroup.description, 'Updated Description');
|
|
||||||
expect(updatedGroup.members.length, testGroup1.members.length);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verifies that addGroupsAsList with duplicate groups only adds unique ones.
|
|
||||||
test('addGroupsAsList with duplicate groups only adds once', () async {
|
|
||||||
await database.groupDao.addGroupsAsList(
|
|
||||||
groups: [testGroup1, testGroup1, testGroup1],
|
|
||||||
);
|
|
||||||
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
|
||||||
expect(allGroups.length, 1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user