Merge branch 'development' into feature/119-implementierung-der-games-2
# Conflicts: # lib/data/dao/match_dao.dart
This commit is contained in:
@@ -35,25 +35,23 @@ void main() {
|
||||
testPlayer4 = Player(name: 'Diana');
|
||||
testGroup1 = Group(
|
||||
name: 'Test Group',
|
||||
description: '',
|
||||
members: [testPlayer1, testPlayer2, testPlayer3],
|
||||
description: 'description of the test group 1',
|
||||
);
|
||||
testGroup2 = Group(
|
||||
id: 'gr2',
|
||||
name: 'Second Group',
|
||||
description: '',
|
||||
members: [testPlayer2, testPlayer3, testPlayer4],
|
||||
description: 'description of the test group 2',
|
||||
);
|
||||
testGroup3 = Group(
|
||||
id: 'gr2',
|
||||
name: 'Second Group',
|
||||
description: '',
|
||||
members: [testPlayer2, testPlayer4],
|
||||
);
|
||||
testGroup4 = Group(
|
||||
id: 'gr2',
|
||||
name: 'Second Group',
|
||||
description: '',
|
||||
members: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
|
||||
);
|
||||
});
|
||||
@@ -62,312 +60,355 @@ void main() {
|
||||
await database.close();
|
||||
});
|
||||
group('Group Tests', () {
|
||||
// Verifies that a single group can be added and retrieved with all fields and members intact.
|
||||
test('Adding and fetching a single group works correctly', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
group('CREATE', () {
|
||||
test('Adding and fetching a single group works correctly', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
final fetchedGroup = await database.groupDao.getGroupById(
|
||||
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,
|
||||
final fetchedGroup = await database.groupDao.getGroupById(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Verifies that multiple groups can be added and retrieved with correct members.
|
||||
test('Adding and fetching multiple groups works correctly', () async {
|
||||
await database.groupDao.addGroupsAsList(
|
||||
groups: [testGroup1, testGroup2, testGroup3, testGroup4],
|
||||
);
|
||||
expect(fetchedGroup.id, testGroup1.id);
|
||||
expect(fetchedGroup.name, testGroup1.name);
|
||||
expect(fetchedGroup.createdAt, testGroup1.createdAt);
|
||||
|
||||
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);
|
||||
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 adding the same group twice does not create duplicates.
|
||||
test('Adding the same group twice does not create duplicates', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
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 allGroups = await database.groupDao.getAllGroups();
|
||||
expect(allGroups.length, 1);
|
||||
|
||||
// Verifies that groupExists returns correct boolean based on group presence.
|
||||
test('Group existence check works correctly', () async {
|
||||
var groupExists = await database.groupDao.groupExists(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(groupExists, false);
|
||||
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
groupExists = await database.groupDao.groupExists(groupId: testGroup1.id);
|
||||
expect(groupExists, true);
|
||||
});
|
||||
|
||||
// Verifies that deleteGroup removes the group and returns true.
|
||||
test('Deleting a group works correctly', () async {
|
||||
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',
|
||||
newDescription: 'New Description',
|
||||
final fetchedGroup = await database.groupDao.getGroupById(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(updated, false);
|
||||
},
|
||||
);
|
||||
expect(fetchedGroup.id, testGroup1.id);
|
||||
expect(fetchedGroup.members.length, testGroup1.members.length);
|
||||
});
|
||||
|
||||
// Verifies that deleteAllGroups removes all groups from the database.
|
||||
test('deleteAllGroups removes all groups', () async {
|
||||
await database.groupDao.addGroupsAsList(groups: [testGroup1, testGroup2]);
|
||||
test('addGroup() returns false when group already exists', () async {
|
||||
final firstAdd = await database.groupDao.addGroup(group: testGroup1);
|
||||
expect(firstAdd, isTrue);
|
||||
|
||||
final countBefore = await database.groupDao.getGroupCount();
|
||||
expect(countBefore, 2);
|
||||
final secondAdd = await database.groupDao.addGroup(group: testGroup1);
|
||||
expect(secondAdd, isFalse);
|
||||
|
||||
final deleted = await database.groupDao.deleteAllGroups();
|
||||
expect(deleted, true);
|
||||
final allGroups = await database.groupDao.getAllGroups();
|
||||
expect(allGroups.length, 1);
|
||||
});
|
||||
|
||||
final countAfter = await database.groupDao.getGroupCount();
|
||||
expect(countAfter, 0);
|
||||
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 deleteAllGroups returns false when no groups exist.
|
||||
test('deleteAllGroups returns false when no groups exist', () async {
|
||||
final deleted = await database.groupDao.deleteAllGroups();
|
||||
expect(deleted, false);
|
||||
group('READ', () {
|
||||
test('groupExists() works correctly', () async {
|
||||
var groupExists = await database.groupDao.groupExists(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(groupExists, isFalse);
|
||||
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
groupExists = await database.groupDao.groupExists(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(groupExists, isTrue);
|
||||
});
|
||||
|
||||
test('getGroupCount() works correctly', () async {
|
||||
var count = await database.groupDao.getGroupCount();
|
||||
expect(count, 0);
|
||||
|
||||
var added = await database.groupDao.addGroup(group: testGroup1);
|
||||
expect(added, isTrue);
|
||||
count = await database.groupDao.getGroupCount();
|
||||
expect(count, 1);
|
||||
|
||||
added = await database.groupDao.addGroup(group: testGroup2);
|
||||
expect(added, isTrue);
|
||||
count = await database.groupDao.getGroupCount();
|
||||
expect(count, 2);
|
||||
|
||||
final removed = await database.groupDao.deleteGroup(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(removed, isTrue);
|
||||
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 groups with special characters (quotes, emojis) are stored correctly.
|
||||
test('Group with special characters in name is stored correctly', () async {
|
||||
final specialGroup = Group(
|
||||
name: 'Group\'s & "Special" <Name>',
|
||||
description: 'Description with émojis 🎮🎲',
|
||||
members: [testPlayer1],
|
||||
);
|
||||
await database.groupDao.addGroup(group: specialGroup);
|
||||
group('UPDATE', () {
|
||||
test('updateGroupName() works correctly', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
final fetchedGroup = await database.groupDao.getGroupById(
|
||||
groupId: specialGroup.id,
|
||||
const newName = 'New name';
|
||||
await database.groupDao.updateGroupName(
|
||||
groupId: testGroup1.id,
|
||||
name: newName,
|
||||
);
|
||||
|
||||
final result = await database.groupDao.getGroupById(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(result.name, newName);
|
||||
});
|
||||
|
||||
test('updateGroupName() returns false for non-existent group', () async {
|
||||
final updated = await database.groupDao.updateGroupName(
|
||||
groupId: 'non-existent-id',
|
||||
name: 'New name',
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
});
|
||||
|
||||
test('updateGroupDescription() works correctly', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
const newDescription = 'New description';
|
||||
final updated = await database.groupDao.updateGroupDescription(
|
||||
groupId: testGroup1.id,
|
||||
description: newDescription,
|
||||
);
|
||||
expect(updated, isTrue);
|
||||
|
||||
final group = await database.groupDao.getGroupById(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(group.description, newDescription);
|
||||
});
|
||||
|
||||
test(
|
||||
'updateGroupDescription() returns false for non-existent group',
|
||||
() async {
|
||||
final updated = await database.groupDao.updateGroupDescription(
|
||||
groupId: 'non-existent-id',
|
||||
description: 'New description',
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
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,
|
||||
name: newName,
|
||||
);
|
||||
await database.groupDao.updateGroupDescription(
|
||||
groupId: testGroup1.id,
|
||||
description: 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),
|
||||
isTrue,
|
||||
);
|
||||
expect(
|
||||
initialGroup.members
|
||||
.map((p) => p.id)
|
||||
.toList()
|
||||
.contains(testPlayer2.id),
|
||||
isTrue,
|
||||
);
|
||||
expect(
|
||||
initialGroup.members
|
||||
.map((p) => p.id)
|
||||
.toList()
|
||||
.contains(testPlayer3.id),
|
||||
isTrue,
|
||||
);
|
||||
|
||||
final newPlayers = [testPlayer2, testPlayer4];
|
||||
final replaced = await database.playerGroupDao.replaceGroupPlayers(
|
||||
groupId: testGroup1.id,
|
||||
newPlayers: newPlayers,
|
||||
);
|
||||
expect(replaced, isTrue);
|
||||
|
||||
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), isTrue);
|
||||
expect(memberIds.contains(testPlayer4.id), isTrue);
|
||||
expect(memberIds.contains(testPlayer1.id), isFalse);
|
||||
expect(memberIds.contains(testPlayer3.id), isFalse);
|
||||
});
|
||||
|
||||
test('replaceGroupPlayers() ignores empty list ', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
final replaced = await database.playerGroupDao.replaceGroupPlayers(
|
||||
groupId: testGroup1.id,
|
||||
newPlayers: [],
|
||||
);
|
||||
expect(replaced, isFalse);
|
||||
|
||||
final updatedGroup = await database.groupDao.getGroupById(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(updatedGroup.members.length, testGroup1.members.length);
|
||||
});
|
||||
|
||||
test(
|
||||
'replaceGroupPlayers() returns false for non-existent group',
|
||||
() async {
|
||||
final replaced = await database.playerGroupDao.replaceGroupPlayers(
|
||||
groupId: 'non-existent-id',
|
||||
newPlayers: [testPlayer1],
|
||||
);
|
||||
expect(replaced, isFalse);
|
||||
},
|
||||
);
|
||||
expect(fetchedGroup.name, 'Group\'s & "Special" <Name>');
|
||||
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);
|
||||
group('DELETE', () {
|
||||
test('deleteGroup() works correctly', () async {
|
||||
await database.groupDao.addGroup(group: testGroup1);
|
||||
|
||||
final fetchedGroup = await database.groupDao.getGroupById(
|
||||
groupId: emptyGroup.id,
|
||||
);
|
||||
expect(fetchedGroup.name, 'Empty Group');
|
||||
expect(fetchedGroup.members, isEmpty);
|
||||
final groupDeleted = await database.groupDao.deleteGroup(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(groupDeleted, isTrue);
|
||||
|
||||
final groupExists = await database.groupDao.groupExists(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(groupExists, isFalse);
|
||||
});
|
||||
|
||||
test('deleteGroup() returns false for non-existent group', () async {
|
||||
final deleted = await database.groupDao.deleteGroup(
|
||||
groupId: 'non-existent-id',
|
||||
);
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
|
||||
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, isTrue);
|
||||
|
||||
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, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
// 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);
|
||||
group('Edge Cases', () {
|
||||
test('Group with special characters is stored correctly', () async {
|
||||
final specialGroup = Group(
|
||||
name: 'Group\'s & "Special" <Name>',
|
||||
description: 'Description with émojis 🎮🎲',
|
||||
members: [testPlayer1],
|
||||
);
|
||||
await database.groupDao.addGroup(group: specialGroup);
|
||||
|
||||
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);
|
||||
final fetchedGroup = await database.groupDao.getGroupById(
|
||||
groupId: specialGroup.id,
|
||||
);
|
||||
expect(fetchedGroup.name, 'Group\'s & "Special" <Name>');
|
||||
expect(fetchedGroup.description, 'Description with émojis 🎮🎲');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -101,212 +101,221 @@ void main() {
|
||||
});
|
||||
|
||||
group('Match Tests', () {
|
||||
// Verifies that a single match can be added and retrieved with all fields, group, and players intact.
|
||||
test('Adding and fetching single match works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
group('CREATE', () {
|
||||
test('Adding and fetching single match works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final result = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
final result = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
|
||||
expect(result.id, testMatch1.id);
|
||||
expect(result.name, testMatch1.name);
|
||||
expect(result.createdAt, testMatch1.createdAt);
|
||||
expect(result.id, testMatch1.id);
|
||||
expect(result.name, testMatch1.name);
|
||||
expect(result.createdAt, testMatch1.createdAt);
|
||||
|
||||
if (result.group != null) {
|
||||
expect(result.group!.members.length, testGroup1.members.length);
|
||||
if (result.group != null) {
|
||||
expect(result.group!.members.length, testGroup1.members.length);
|
||||
|
||||
for (int i = 0; i < testGroup1.members.length; i++) {
|
||||
expect(result.group!.members[i].id, testGroup1.members[i].id);
|
||||
expect(result.group!.members[i].name, testGroup1.members[i].name);
|
||||
}
|
||||
} else {
|
||||
fail('Group is null');
|
||||
}
|
||||
expect(result.players.length, testMatch1.players.length);
|
||||
|
||||
for (int i = 0; i < testMatch1.players.length; i++) {
|
||||
expect(result.players[i].id, testMatch1.players[i].id);
|
||||
expect(result.players[i].name, testMatch1.players[i].name);
|
||||
expect(result.players[i].createdAt, testMatch1.players[i].createdAt);
|
||||
}
|
||||
});
|
||||
|
||||
// Verifies that multiple matches can be added and retrieved with correct groups and players.
|
||||
test('Adding and fetching multiple matches works correctly', () async {
|
||||
await database.matchDao.addMatchAsList(
|
||||
matches: [
|
||||
testMatch1,
|
||||
testMatch2,
|
||||
testMatchOnlyGroup,
|
||||
testMatchOnlyPlayers,
|
||||
],
|
||||
);
|
||||
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
expect(allMatches.length, 4);
|
||||
|
||||
final testMatches = {
|
||||
testMatch1.id: testMatch1,
|
||||
testMatch2.id: testMatch2,
|
||||
testMatchOnlyGroup.id: testMatchOnlyGroup,
|
||||
testMatchOnlyPlayers.id: testMatchOnlyPlayers,
|
||||
};
|
||||
|
||||
for (final match in allMatches) {
|
||||
final testMatch = testMatches[match.id]!;
|
||||
|
||||
// Match-Checks
|
||||
expect(match.id, testMatch.id);
|
||||
expect(match.name, testMatch.name);
|
||||
expect(match.createdAt, testMatch.createdAt);
|
||||
|
||||
// Group-Checks
|
||||
if (testMatch.group != null) {
|
||||
expect(match.group!.id, testMatch.group!.id);
|
||||
expect(match.group!.name, testMatch.group!.name);
|
||||
expect(match.group!.createdAt, testMatch.group!.createdAt);
|
||||
|
||||
// Group Members-Checks
|
||||
expect(match.group!.members.length, testMatch.group!.members.length);
|
||||
for (int i = 0; i < testMatch.group!.members.length; i++) {
|
||||
expect(match.group!.members[i].id, testMatch.group!.members[i].id);
|
||||
expect(
|
||||
match.group!.members[i].name,
|
||||
testMatch.group!.members[i].name,
|
||||
);
|
||||
expect(
|
||||
match.group!.members[i].createdAt,
|
||||
testMatch.group!.members[i].createdAt,
|
||||
);
|
||||
for (int i = 0; i < testGroup1.members.length; i++) {
|
||||
expect(result.group!.members[i].id, testGroup1.members[i].id);
|
||||
expect(result.group!.members[i].name, testGroup1.members[i].name);
|
||||
}
|
||||
} else {
|
||||
expect(match.group, null);
|
||||
fail('Group is null');
|
||||
}
|
||||
expect(result.players.length, testMatch1.players.length);
|
||||
|
||||
// Players-Checks
|
||||
expect(match.players.length, testMatch.players.length);
|
||||
for (int i = 0; i < testMatch.players.length; i++) {
|
||||
expect(match.players[i].id, testMatch.players[i].id);
|
||||
expect(match.players[i].name, testMatch.players[i].name);
|
||||
expect(match.players[i].createdAt, testMatch.players[i].createdAt);
|
||||
for (int i = 0; i < testMatch1.players.length; i++) {
|
||||
expect(result.players[i].id, testMatch1.players[i].id);
|
||||
expect(result.players[i].name, testMatch1.players[i].name);
|
||||
expect(result.players[i].createdAt, testMatch1.players[i].createdAt);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('Adding and fetching multiple matches works correctly', () async {
|
||||
await database.matchDao.addMatchesAsList(
|
||||
matches: [
|
||||
testMatch1,
|
||||
testMatch2,
|
||||
testMatchOnlyGroup,
|
||||
testMatchOnlyPlayers,
|
||||
],
|
||||
);
|
||||
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
expect(allMatches.length, 4);
|
||||
|
||||
final testMatches = {
|
||||
testMatch1.id: testMatch1,
|
||||
testMatch2.id: testMatch2,
|
||||
testMatchOnlyGroup.id: testMatchOnlyGroup,
|
||||
testMatchOnlyPlayers.id: testMatchOnlyPlayers,
|
||||
};
|
||||
|
||||
for (final match in allMatches) {
|
||||
final testMatch = testMatches[match.id]!;
|
||||
|
||||
// Match-Checks
|
||||
expect(match.id, testMatch.id);
|
||||
expect(match.name, testMatch.name);
|
||||
expect(match.createdAt, testMatch.createdAt);
|
||||
|
||||
// Group-Checks
|
||||
if (testMatch.group != null) {
|
||||
expect(match.group!.id, testMatch.group!.id);
|
||||
expect(match.group!.name, testMatch.group!.name);
|
||||
expect(match.group!.createdAt, testMatch.group!.createdAt);
|
||||
|
||||
// Group Members-Checks
|
||||
expect(
|
||||
match.group!.members.length,
|
||||
testMatch.group!.members.length,
|
||||
);
|
||||
for (int i = 0; i < testMatch.group!.members.length; i++) {
|
||||
expect(
|
||||
match.group!.members[i].id,
|
||||
testMatch.group!.members[i].id,
|
||||
);
|
||||
expect(
|
||||
match.group!.members[i].name,
|
||||
testMatch.group!.members[i].name,
|
||||
);
|
||||
expect(
|
||||
match.group!.members[i].createdAt,
|
||||
testMatch.group!.members[i].createdAt,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
expect(match.group, null);
|
||||
}
|
||||
|
||||
// Players-Checks
|
||||
expect(match.players.length, testMatch.players.length);
|
||||
for (int i = 0; i < testMatch.players.length; i++) {
|
||||
expect(match.players[i].id, testMatch.players[i].id);
|
||||
expect(match.players[i].name, testMatch.players[i].name);
|
||||
expect(match.players[i].createdAt, testMatch.players[i].createdAt);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('addMatch() ignores duplicate games', () async {
|
||||
var added = await database.matchDao.addMatch(match: testMatch1);
|
||||
expect(added, isTrue);
|
||||
|
||||
added = await database.matchDao.addMatch(match: testMatch1);
|
||||
expect(added, isFalse);
|
||||
|
||||
final matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 1);
|
||||
});
|
||||
|
||||
test('addMatchesAsList() returns isFalse for empty list', () async {
|
||||
var added = await database.matchDao.addMatchesAsList(matches: []);
|
||||
expect(added, isFalse);
|
||||
});
|
||||
|
||||
test('addMatchesAsList() ignores duplicate games', () async {
|
||||
final added = await database.matchDao.addMatchesAsList(
|
||||
matches: [testMatch1, testMatch2, testMatch1],
|
||||
);
|
||||
expect(added, isTrue);
|
||||
|
||||
final matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 2);
|
||||
});
|
||||
});
|
||||
|
||||
// Verifies that adding the same match twice does not create duplicates.
|
||||
test('Adding the same match twice does not create duplicates', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
group('READ', () {
|
||||
test('matchExists() works correctly', () async {
|
||||
var matchExists = await database.matchDao.matchExists(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(matchExists, isFalse);
|
||||
|
||||
final matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 1);
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
matchExists = await database.matchDao.matchExists(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(matchExists, isTrue);
|
||||
});
|
||||
|
||||
test('getGroupMatches() works correctly', () async {
|
||||
var matches = await database.matchDao.getGroupMatches(
|
||||
groupId: 'non-existing-id',
|
||||
);
|
||||
|
||||
expect(matches, isEmpty);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
matches = await database.matchDao.getGroupMatches(
|
||||
groupId: testGroup1.id,
|
||||
);
|
||||
expect(matches, isNotEmpty);
|
||||
|
||||
final match = matches.first;
|
||||
expect(match.id, testMatch1.id);
|
||||
expect(match.group, isNotNull);
|
||||
expect(match.group!.id, testGroup1.id);
|
||||
});
|
||||
});
|
||||
|
||||
// Verifies that matchExists returns correct boolean based on match presence.
|
||||
test('Match existence check works correctly', () async {
|
||||
var matchExists = await database.matchDao.matchExists(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(matchExists, false);
|
||||
group('UPDATE', () {
|
||||
test('updateMatchName() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
const newName = 'New name';
|
||||
await database.matchDao.updateMatchName(
|
||||
matchId: testMatch1.id,
|
||||
name: newName,
|
||||
);
|
||||
|
||||
matchExists = await database.matchDao.matchExists(matchId: testMatch1.id);
|
||||
expect(matchExists, true);
|
||||
});
|
||||
final fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.name, newName);
|
||||
});
|
||||
|
||||
// Verifies that deleteMatch removes the match and returns true.
|
||||
test('Deleting a match works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
test('updateMatchName() does nothing for non-existent match', () async {
|
||||
final updated = await database.matchDao.updateMatchName(
|
||||
matchId: 'non-existing-id',
|
||||
name: 'New Name',
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
|
||||
final matchDeleted = await database.matchDao.deleteMatch(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(matchDeleted, true);
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
expect(allMatches, isEmpty);
|
||||
});
|
||||
|
||||
final matchExists = await database.matchDao.matchExists(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(matchExists, false);
|
||||
});
|
||||
test('updateMatchGroup() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
await database.groupDao.addGroup(group: testGroup2);
|
||||
|
||||
// Verifies that getMatchCount returns correct count through add/delete operations.
|
||||
test('Getting the match count works correctly', () async {
|
||||
var matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 0);
|
||||
await database.matchDao.updateMatchGroup(
|
||||
matchId: testMatch1.id,
|
||||
groupId: testGroup2.id,
|
||||
);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
final fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.group?.id, testGroup2.id);
|
||||
});
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 1);
|
||||
test('updateMatchGroup() does nothing for non-existent match', () async {
|
||||
final updated = await database.matchDao.updateMatchGroup(
|
||||
matchId: 'non-existing-id',
|
||||
groupId: 'group-id',
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch2);
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
expect(allMatches, isEmpty);
|
||||
});
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 2);
|
||||
|
||||
await database.matchDao.deleteMatch(matchId: testMatch1.id);
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 1);
|
||||
|
||||
await database.matchDao.deleteMatch(matchId: testMatch2.id);
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 0);
|
||||
});
|
||||
|
||||
// Verifies that updateMatchName correctly updates only the name field.
|
||||
test('Renaming a match works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
var fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.name, testMatch1.name);
|
||||
|
||||
const newName = 'Updated Match Name';
|
||||
await database.matchDao.updateMatchName(
|
||||
matchId: testMatch1.id,
|
||||
newName: newName,
|
||||
);
|
||||
|
||||
fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.name, newName);
|
||||
});
|
||||
|
||||
test('Fetching a winner works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
var fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
|
||||
expect(fetchedMatch.mvp, isNotNull);
|
||||
expect(fetchedMatch.mvp.first.id, testPlayer4.id);
|
||||
});
|
||||
|
||||
test('Setting a winner works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
await database.scoreEntryDao.setWinner(
|
||||
matchId: testMatch1.id,
|
||||
playerId: testPlayer5.id,
|
||||
);
|
||||
|
||||
final fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.mvp, isNotNull);
|
||||
expect(fetchedMatch.mvp.first.id, testPlayer5.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'removeMatchGroup removes group from match with existing group',
|
||||
() async {
|
||||
test('removeMatchGroup() works correctly', () async {
|
||||
expect(testMatch1.group, isNotNull);
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final removed = await database.matchDao.removeMatchGroup(
|
||||
@@ -318,53 +327,149 @@ void main() {
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(updatedMatch.group, null);
|
||||
expect(updatedMatch.game.id, testMatch1.game.id);
|
||||
expect(updatedMatch.name, testMatch1.name);
|
||||
expect(updatedMatch.notes, testMatch1.notes);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
'removeMatchGroup on match that already has no group still succeeds',
|
||||
() async {
|
||||
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
|
||||
test(
|
||||
'removeMatchGroup() on match that already has no group still succeeds',
|
||||
() async {
|
||||
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
|
||||
|
||||
final removed = await database.matchDao.removeMatchGroup(
|
||||
matchId: testMatchOnlyPlayers.id,
|
||||
);
|
||||
expect(removed, isTrue);
|
||||
final removed = await database.matchDao.removeMatchGroup(
|
||||
matchId: testMatchOnlyPlayers.id,
|
||||
);
|
||||
expect(removed, isTrue);
|
||||
|
||||
final updatedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatchOnlyPlayers.id,
|
||||
);
|
||||
expect(updatedMatch.group, null);
|
||||
},
|
||||
);
|
||||
|
||||
test('removeMatchGroup on non-existing match returns false', () async {
|
||||
final removed = await database.matchDao.removeMatchGroup(
|
||||
matchId: 'non-existing-id',
|
||||
final updatedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatchOnlyPlayers.id,
|
||||
);
|
||||
expect(updatedMatch.group, null);
|
||||
},
|
||||
);
|
||||
expect(removed, isFalse);
|
||||
|
||||
test(
|
||||
'removeMatchGroup() on non-existing match returns isFalse',
|
||||
() async {
|
||||
final removed = await database.matchDao.removeMatchGroup(
|
||||
matchId: 'non-existing-id',
|
||||
);
|
||||
expect(removed, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test('updateMatchNotes() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
const newName = 'New name';
|
||||
await database.matchDao.updateMatchName(
|
||||
matchId: testMatch1.id,
|
||||
name: newName,
|
||||
);
|
||||
|
||||
final fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.name, newName);
|
||||
});
|
||||
|
||||
test('updateMatchNotes() does nothing for non-existent game', () async {
|
||||
final updated = await database.matchDao.updateMatchNotes(
|
||||
matchId: 'non-existing-id',
|
||||
notes: 'New notes',
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
expect(allMatches, isEmpty);
|
||||
});
|
||||
|
||||
test('updateMatchEndedAt() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
DateTime newEndedAt = DateTime(2030, 1, 1, 12, 0, 0);
|
||||
print(newEndedAt);
|
||||
await database.matchDao.updateMatchEndedAt(
|
||||
matchId: testMatch1.id,
|
||||
endedAt: newEndedAt,
|
||||
);
|
||||
|
||||
final fetchedMatch = await database.matchDao.getMatchById(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(fetchedMatch.endedAt, newEndedAt);
|
||||
});
|
||||
|
||||
test('updateMatchEndedAt() does nothing for non-existent game', () async {
|
||||
final updated = await database.matchDao.updateMatchEndedAt(
|
||||
matchId: 'non-existing-id',
|
||||
endedAt: DateTime.now(),
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
expect(allMatches, isEmpty);
|
||||
});
|
||||
|
||||
test('Getting the match count works correctly', () async {
|
||||
var matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 0);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 1);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch2);
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 2);
|
||||
|
||||
await database.matchDao.deleteMatch(matchId: testMatch1.id);
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 1);
|
||||
|
||||
await database.matchDao.deleteMatch(matchId: testMatch2.id);
|
||||
|
||||
matchCount = await database.matchDao.getMatchCount();
|
||||
expect(matchCount, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test('Fetching all matches related to a group', () async {
|
||||
var matches = await database.matchDao.getGroupMatches(
|
||||
groupId: 'non-existing-id',
|
||||
);
|
||||
group('DELETE', () {
|
||||
test('deleteMatch() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
expect(matches, isEmpty);
|
||||
var deleted = await database.matchDao.deleteMatch(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(deleted, isTrue);
|
||||
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
final matchExists = await database.matchDao.matchExists(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(matchExists, isFalse);
|
||||
|
||||
matches = await database.matchDao.getGroupMatches(groupId: testGroup1.id);
|
||||
deleted = await database.matchDao.deleteMatch(matchId: testMatch1.id);
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
|
||||
expect(matches, isNotEmpty);
|
||||
test('deleteAllMatches() works correctly', () async {
|
||||
await database.matchDao.addMatchesAsList(
|
||||
matches: [testMatch1, testMatch2, testMatchOnlyPlayers],
|
||||
);
|
||||
|
||||
final match = matches.first;
|
||||
expect(match.id, testMatch1.id);
|
||||
expect(match.group, isNotNull);
|
||||
expect(match.group!.id, testGroup1.id);
|
||||
var count = await database.matchDao.getMatchCount();
|
||||
expect(count, 3);
|
||||
|
||||
var deleted = await database.matchDao.deleteAllMatches();
|
||||
expect(deleted, isTrue);
|
||||
|
||||
count = await database.matchDao.getMatchCount();
|
||||
expect(count, 0);
|
||||
|
||||
deleted = await database.matchDao.deleteAllMatches();
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
test('getMatchCountByGame() works correctly', () async {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:core' hide Match;
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
@@ -18,8 +20,11 @@ void main() {
|
||||
late Team testTeam1;
|
||||
late Team testTeam2;
|
||||
late Team testTeam3;
|
||||
late Game testGame1;
|
||||
late Game testGame2;
|
||||
late Team testTeam4;
|
||||
late Game testGame;
|
||||
late Match testMatch1;
|
||||
late Match testMatch2;
|
||||
late Match matchWithNoTeams;
|
||||
final fixedDate = DateTime(2025, 11, 19, 00, 11, 23);
|
||||
final fakeClock = Clock(() => fixedDate);
|
||||
|
||||
@@ -40,27 +45,35 @@ void main() {
|
||||
testTeam1 = Team(name: 'Team Alpha', members: [testPlayer1, testPlayer2]);
|
||||
testTeam2 = Team(name: 'Team Beta', members: [testPlayer3, testPlayer4]);
|
||||
testTeam3 = Team(name: 'Team Gamma', members: [testPlayer1, testPlayer3]);
|
||||
testGame1 = Game(
|
||||
name: 'Game 1',
|
||||
ruleset: Ruleset.singleWinner,
|
||||
description: 'Test game 1',
|
||||
testTeam4 = Team(name: 'Team Omega', members: [testPlayer2, testPlayer4]);
|
||||
testGame = Game(
|
||||
name: 'Test Game',
|
||||
ruleset: Ruleset.highestScore,
|
||||
color: GameColor.blue,
|
||||
icon: '',
|
||||
);
|
||||
testGame2 = Game(
|
||||
name: 'Game 2',
|
||||
ruleset: Ruleset.highestScore,
|
||||
description: 'Test game 2',
|
||||
color: GameColor.red,
|
||||
icon: '',
|
||||
testMatch1 = Match(
|
||||
name: 'Match 1',
|
||||
game: testGame,
|
||||
players: [],
|
||||
teams: [testTeam1, testTeam2],
|
||||
);
|
||||
testMatch2 = Match(
|
||||
name: 'Match 2',
|
||||
game: testGame,
|
||||
players: [],
|
||||
teams: [testTeam3, testTeam4],
|
||||
);
|
||||
matchWithNoTeams = Match(
|
||||
name: 'Match with no teams',
|
||||
game: testGame,
|
||||
players: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
|
||||
);
|
||||
});
|
||||
|
||||
await database.gameDao.addGame(game: testGame);
|
||||
await database.playerDao.addPlayersAsList(
|
||||
players: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
|
||||
);
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
await database.gameDao.addGame(game: testGame2);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
@@ -68,460 +81,251 @@ void main() {
|
||||
});
|
||||
|
||||
group('Team Tests', () {
|
||||
// Verifies that a single team can be added and retrieved with all fields intact.
|
||||
test('Adding and fetching a single team works correctly', () async {
|
||||
final added = await database.teamDao.addTeam(team: testTeam1);
|
||||
expect(added, true);
|
||||
|
||||
final fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(fetchedTeam.id, testTeam1.id);
|
||||
expect(fetchedTeam.name, testTeam1.name);
|
||||
expect(fetchedTeam.createdAt, testTeam1.createdAt);
|
||||
});
|
||||
|
||||
// Verifies that multiple teams can be added at once and retrieved correctly.
|
||||
test('Adding and fetching multiple teams works correctly', () async {
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams.length, 3);
|
||||
|
||||
final testTeams = {
|
||||
testTeam1.id: testTeam1,
|
||||
testTeam2.id: testTeam2,
|
||||
testTeam3.id: testTeam3,
|
||||
};
|
||||
|
||||
for (final team in allTeams) {
|
||||
final testTeam = testTeams[team.id]!;
|
||||
|
||||
expect(team.id, testTeam.id);
|
||||
expect(team.name, testTeam.name);
|
||||
expect(team.createdAt, testTeam.createdAt);
|
||||
}
|
||||
});
|
||||
|
||||
// Verifies that adding the same team twice does not create duplicates and returns false.
|
||||
test('Adding the same team twice does not create duplicates', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
final addedAgain = await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
expect(addedAgain, false);
|
||||
|
||||
final teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 1);
|
||||
});
|
||||
|
||||
// Verifies that teamExists returns correct boolean based on team presence.
|
||||
test('Team existence check works correctly', () async {
|
||||
var teamExists = await database.teamDao.teamExists(teamId: testTeam1.id);
|
||||
expect(teamExists, false);
|
||||
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
teamExists = await database.teamDao.teamExists(teamId: testTeam1.id);
|
||||
expect(teamExists, true);
|
||||
});
|
||||
|
||||
// Verifies that deleteTeam removes the team and returns true.
|
||||
test('Deleting a team works correctly', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
final teamDeleted = await database.teamDao.deleteTeam(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(teamDeleted, true);
|
||||
|
||||
final teamExists = await database.teamDao.teamExists(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(teamExists, false);
|
||||
});
|
||||
|
||||
// Verifies that deleteTeam returns false for a non-existent team ID.
|
||||
test('Deleting a non-existent team returns false', () async {
|
||||
final teamDeleted = await database.teamDao.deleteTeam(
|
||||
teamId: 'non-existent-id',
|
||||
);
|
||||
expect(teamDeleted, false);
|
||||
});
|
||||
|
||||
// Verifies that getTeamCount returns correct count through add/delete operations.
|
||||
test('Getting the team count works correctly', () async {
|
||||
var teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 0);
|
||||
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 1);
|
||||
|
||||
await database.teamDao.addTeam(team: testTeam2);
|
||||
|
||||
teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 2);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam1.id);
|
||||
|
||||
teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 1);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam2.id);
|
||||
|
||||
teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 0);
|
||||
});
|
||||
|
||||
// Verifies that updateTeamName correctly updates only the name field.
|
||||
test('Updating team name works correctly', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
var fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(fetchedTeam.name, testTeam1.name);
|
||||
|
||||
const newName = 'Updated Team Name';
|
||||
await database.teamDao.updateTeamName(
|
||||
teamId: testTeam1.id,
|
||||
newName: newName,
|
||||
);
|
||||
|
||||
fetchedTeam = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(fetchedTeam.name, newName);
|
||||
});
|
||||
|
||||
// Verifies that deleteAllTeams removes all teams from the database.
|
||||
test('Deleting all teams works correctly', () async {
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
);
|
||||
|
||||
var teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 3);
|
||||
|
||||
final deleted = await database.teamDao.deleteAllTeams();
|
||||
expect(deleted, true);
|
||||
|
||||
teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 0);
|
||||
});
|
||||
|
||||
// Verifies that deleteAllTeams returns false when no teams exist.
|
||||
test('Deleting all teams when empty returns false', () async {
|
||||
final deleted = await database.teamDao.deleteAllTeams();
|
||||
expect(deleted, false);
|
||||
});
|
||||
|
||||
// Verifies that addTeamsAsList returns false when given an empty list.
|
||||
test('Adding teams as list with empty list returns false', () async {
|
||||
final added = await database.teamDao.addTeamsAsList(teams: []);
|
||||
expect(added, false);
|
||||
});
|
||||
|
||||
// Verifies that addTeamsAsList with duplicate IDs ignores duplicates and keeps the first.
|
||||
test('Adding teams with duplicate IDs ignores duplicates', () async {
|
||||
final duplicateTeam = Team(
|
||||
id: testTeam1.id,
|
||||
name: 'Duplicate Team',
|
||||
members: [testPlayer4],
|
||||
);
|
||||
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, duplicateTeam, testTeam2],
|
||||
);
|
||||
|
||||
final teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 2);
|
||||
|
||||
// The first one should be kept (insertOrIgnore)
|
||||
final fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(fetchedTeam.name, testTeam1.name);
|
||||
});
|
||||
|
||||
// Verifies that getAllTeams returns empty list when no teams exist.
|
||||
test('Getting all teams when empty returns empty list', () async {
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams.isEmpty, true);
|
||||
});
|
||||
|
||||
// Verifies that getTeamById throws exception for non-existent team.
|
||||
test('Getting non-existent team throws exception', () async {
|
||||
expect(
|
||||
() => database.teamDao.getTeamById(teamId: 'non-existent-id'),
|
||||
throwsA(isA<StateError>()),
|
||||
);
|
||||
});
|
||||
|
||||
// Verifies that updating team name preserves other fields.
|
||||
test('Updating team name preserves other team fields', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
final originalTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
final originalCreatedAt = originalTeam.createdAt;
|
||||
|
||||
const newName = 'Brand New Team Name';
|
||||
await database.teamDao.updateTeamName(
|
||||
teamId: testTeam1.id,
|
||||
newName: newName,
|
||||
);
|
||||
|
||||
final updatedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(updatedTeam.name, newName);
|
||||
expect(updatedTeam.id, testTeam1.id);
|
||||
expect(updatedTeam.createdAt, originalCreatedAt);
|
||||
});
|
||||
|
||||
// Verifies that team name can be updated to an empty string.
|
||||
test('Updating team name to empty string works', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
await database.teamDao.updateTeamName(teamId: testTeam1.id, newName: '');
|
||||
|
||||
final updatedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(updatedTeam.name, '');
|
||||
});
|
||||
|
||||
// Verifies that team name can be updated to a very long string.
|
||||
test('Updating team name to long string works', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
final longName = 'A' * 500; // 500 character name
|
||||
|
||||
await database.teamDao.updateTeamName(
|
||||
teamId: testTeam1.id,
|
||||
newName: longName,
|
||||
);
|
||||
|
||||
final updatedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(updatedTeam.name, longName);
|
||||
expect(updatedTeam.name.length, 500);
|
||||
});
|
||||
|
||||
// Verifies that updating non-existent team name doesn't throw error.
|
||||
test('Updating non-existent team name completes without error', () async {
|
||||
expect(
|
||||
() => database.teamDao.updateTeamName(
|
||||
teamId: 'non-existent-id',
|
||||
newName: 'New Name',
|
||||
),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
// Verifies that deleteTeam only affects the specified team.
|
||||
test('Deleting one team does not affect other teams', () async {
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam2.id);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams.length, 2);
|
||||
expect(allTeams.any((t) => t.id == testTeam1.id), true);
|
||||
expect(allTeams.any((t) => t.id == testTeam2.id), false);
|
||||
expect(allTeams.any((t) => t.id == testTeam3.id), true);
|
||||
});
|
||||
|
||||
// Verifies that teams with overlapping members are independent.
|
||||
test('Teams with overlapping members are independent', () async {
|
||||
// Create two matches since player_match has primary key {playerId, matchId}
|
||||
final match1 = Match(
|
||||
name: 'Match 1',
|
||||
game: testGame1,
|
||||
players: [testPlayer1, testPlayer2],
|
||||
);
|
||||
final match2 = Match(
|
||||
name: 'Match 2',
|
||||
game: testGame2,
|
||||
players: [testPlayer1, testPlayer2],
|
||||
);
|
||||
await database.matchDao.addMatch(match: match1);
|
||||
await database.matchDao.addMatch(match: match2);
|
||||
|
||||
// Add teams to database
|
||||
await database.teamDao.addTeamsAsList(teams: [testTeam1, testTeam3]);
|
||||
|
||||
// Associate players with teams through match1
|
||||
// testTeam1: player1, player2
|
||||
await database.playerMatchDao.addPlayerToMatch(
|
||||
playerId: testPlayer1.id,
|
||||
matchId: match1.id,
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
await database.playerMatchDao.addPlayerToMatch(
|
||||
playerId: testPlayer2.id,
|
||||
matchId: match1.id,
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
// Associate players with teams through match2
|
||||
// testTeam3: player1, player3 (overlapping player1)
|
||||
await database.playerMatchDao.addPlayerToMatch(
|
||||
playerId: testPlayer1.id,
|
||||
matchId: match2.id,
|
||||
teamId: testTeam3.id,
|
||||
);
|
||||
await database.playerMatchDao.addPlayerToMatch(
|
||||
playerId: testPlayer3.id,
|
||||
matchId: match2.id,
|
||||
teamId: testTeam3.id,
|
||||
);
|
||||
|
||||
final team1 = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
final team3 = await database.teamDao.getTeamById(teamId: testTeam3.id);
|
||||
|
||||
expect(team1.members.length, 2);
|
||||
expect(team3.members.length, 2);
|
||||
expect(team1.members.any((p) => p.id == testPlayer1.id), true);
|
||||
expect(team3.members.any((p) => p.id == testPlayer1.id), true);
|
||||
});
|
||||
|
||||
// Verifies that adding teams sequentially works correctly.
|
||||
test('Adding teams sequentially maintains correct count', () async {
|
||||
var count = await database.teamDao.getTeamCount();
|
||||
expect(count, 0);
|
||||
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 1);
|
||||
|
||||
await database.teamDao.addTeam(team: testTeam2);
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 2);
|
||||
|
||||
await database.teamDao.addTeam(team: testTeam3);
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 3);
|
||||
});
|
||||
|
||||
// Verifies that getAllTeams returns all teams with correct data.
|
||||
test('Getting all teams returns all teams with correct data', () async {
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
|
||||
expect(allTeams.length, 3);
|
||||
expect(allTeams.map((t) => t.id).toSet(), {
|
||||
testTeam1.id,
|
||||
testTeam2.id,
|
||||
testTeam3.id,
|
||||
group('CREATE', () {
|
||||
test('Adding and fetching a single team works correctly', () async {
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
final added = await database.teamDao.addTeam(
|
||||
team: testTeam1,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
expect(added, isTrue);
|
||||
|
||||
final fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(fetchedTeam.id, testTeam1.id);
|
||||
expect(fetchedTeam.name, testTeam1.name);
|
||||
expect(fetchedTeam.createdAt, testTeam1.createdAt);
|
||||
expect(fetchedTeam.members.length, testTeam1.members.length);
|
||||
for (int i = 0; i < fetchedTeam.members.length; i++) {
|
||||
expect(fetchedTeam.members[i].id, testTeam1.members[i].id);
|
||||
expect(fetchedTeam.members[i].name, testTeam1.members[i].name);
|
||||
}
|
||||
});
|
||||
|
||||
test('Adding and fetching multiple teams works correctly', () async {
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams.length, 3);
|
||||
|
||||
final testTeams = {
|
||||
testTeam1.id: testTeam1,
|
||||
testTeam2.id: testTeam2,
|
||||
testTeam3.id: testTeam3,
|
||||
};
|
||||
|
||||
for (final team in allTeams) {
|
||||
final testTeam = testTeams[team.id]!;
|
||||
|
||||
expect(team.id, testTeam.id);
|
||||
expect(team.name, testTeam.name);
|
||||
expect(team.createdAt, testTeam.createdAt);
|
||||
}
|
||||
});
|
||||
|
||||
test('addTeam() ignores duplicates', () async {
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
var added = await database.teamDao.addTeam(
|
||||
team: testTeam1,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
expect(added, isTrue);
|
||||
|
||||
added = await database.teamDao.addTeam(
|
||||
team: testTeam1,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
expect(added, isFalse);
|
||||
|
||||
final teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 1);
|
||||
});
|
||||
|
||||
test('addTeamsAsList() with empty list returns isFalse', () async {
|
||||
final added = await database.teamDao.addTeamsAsList(
|
||||
teams: [],
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
expect(added, isFalse);
|
||||
});
|
||||
|
||||
test('addTeamsAsList() ignores duplicates', () async {
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
final added = await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam1],
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
expect(added, isTrue);
|
||||
|
||||
final teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 2);
|
||||
});
|
||||
});
|
||||
|
||||
// Verifies that teamExists returns false for deleted teams.
|
||||
test('Team existence returns false after deletion', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
expect(await database.teamDao.teamExists(teamId: testTeam1.id), true);
|
||||
group('READ', () {
|
||||
test('getTeamCount works correctly', () async {
|
||||
var count = await database.teamDao.getTeamCount();
|
||||
expect(count, 0);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam1.id);
|
||||
expect(await database.teamDao.teamExists(teamId: testTeam1.id), false);
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 2);
|
||||
|
||||
await database.teamDao.addTeam(
|
||||
team: testTeam2,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 2);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam1.id);
|
||||
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 1);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam2.id);
|
||||
|
||||
count = await database.teamDao.getTeamCount();
|
||||
expect(count, 0);
|
||||
});
|
||||
|
||||
test('teamExists() works correctly', () async {
|
||||
var teamExists = await database.teamDao.teamExists(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(teamExists, isFalse);
|
||||
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
|
||||
await database.teamDao.addTeam(
|
||||
team: testTeam1,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
|
||||
teamExists = await database.teamDao.teamExists(teamId: testTeam1.id);
|
||||
expect(teamExists, isTrue);
|
||||
});
|
||||
|
||||
test('getAllTeams() with no teams returns empty list', () async {
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams, isA<List<Team>>());
|
||||
expect(allTeams.isEmpty, isTrue);
|
||||
});
|
||||
|
||||
test('getAllTeams() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
|
||||
expect(allTeams.length, 3);
|
||||
expect(allTeams.map((t) => t.id).toSet(), {
|
||||
testTeam1.id,
|
||||
testTeam2.id,
|
||||
testTeam3.id,
|
||||
});
|
||||
});
|
||||
|
||||
test('Getting non-existent team throws exception', () async {
|
||||
expect(
|
||||
() => database.teamDao.getTeamById(teamId: 'non-existent-id'),
|
||||
throwsA(isA<StateError>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// Verifies that adding multiple teams in batch then deleting returns correct count.
|
||||
test('Batch add then partial delete maintains correct count', () async {
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [testTeam1, testTeam2, testTeam3],
|
||||
);
|
||||
group('UPDATED', () {
|
||||
test('updateTeamName() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
|
||||
expect(await database.teamDao.getTeamCount(), 3);
|
||||
await database.teamDao.addTeam(
|
||||
team: testTeam1,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam1.id);
|
||||
expect(await database.teamDao.getTeamCount(), 2);
|
||||
var fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(fetchedTeam.name, testTeam1.name);
|
||||
|
||||
await database.teamDao.deleteTeam(teamId: testTeam3.id);
|
||||
expect(await database.teamDao.getTeamCount(), 1);
|
||||
const newName = 'New name';
|
||||
await database.teamDao.updateTeamName(
|
||||
teamId: testTeam1.id,
|
||||
name: newName,
|
||||
);
|
||||
|
||||
fetchedTeam = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(fetchedTeam.name, newName);
|
||||
});
|
||||
|
||||
test('updateTeamName() does nothing for non-existent team', () async {
|
||||
final updated = await database.teamDao.updateTeamName(
|
||||
teamId: 'non-existing-id',
|
||||
name: 'New Name',
|
||||
);
|
||||
expect(updated, isFalse);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
// Verifies that deleteAllTeams with single team works.
|
||||
test('Deleting all teams with single team returns true', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
expect(await database.teamDao.getTeamCount(), 1);
|
||||
group('DELETE', () {
|
||||
test('deleteTeam() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
await database.matchDao.addMatch(match: matchWithNoTeams);
|
||||
|
||||
final deleted = await database.teamDao.deleteAllTeams();
|
||||
expect(deleted, true);
|
||||
expect(await database.teamDao.getTeamCount(), 0);
|
||||
});
|
||||
await database.teamDao.addTeam(
|
||||
team: testTeam1,
|
||||
matchId: matchWithNoTeams.id,
|
||||
);
|
||||
|
||||
// Verifies that addTeam after deleteAllTeams works correctly.
|
||||
test('Adding team after deleteAllTeams works correctly', () async {
|
||||
await database.teamDao.addTeamsAsList(teams: [testTeam1, testTeam2]);
|
||||
expect(await database.teamDao.getTeamCount(), 2);
|
||||
final deleted = await database.teamDao.deleteTeam(teamId: testTeam1.id);
|
||||
expect(deleted, isTrue);
|
||||
|
||||
await database.teamDao.deleteAllTeams();
|
||||
expect(await database.teamDao.getTeamCount(), 0);
|
||||
final teamExists = await database.teamDao.teamExists(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
expect(teamExists, isFalse);
|
||||
});
|
||||
|
||||
final added = await database.teamDao.addTeam(team: testTeam3);
|
||||
expect(added, true);
|
||||
expect(await database.teamDao.getTeamCount(), 1);
|
||||
test('Deleting a non-existent team returns isFalse', () async {
|
||||
final deleted = await database.teamDao.deleteTeam(
|
||||
teamId: 'non-existent-id',
|
||||
);
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
|
||||
final fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam3.id,
|
||||
);
|
||||
expect(fetchedTeam.name, testTeam3.name);
|
||||
});
|
||||
test('deleteAllTeams() works correctly', () async {
|
||||
await database.matchDao.addMatchesAsList(
|
||||
matches: [testMatch1, testMatch2],
|
||||
);
|
||||
var teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 4);
|
||||
|
||||
// Verifies that addTeamsAsList with partial duplicates ignores duplicates.
|
||||
test('Adding teams with some duplicates ignores only duplicates', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
final deleted = await database.teamDao.deleteAllTeams();
|
||||
expect(deleted, isTrue);
|
||||
|
||||
final duplicateTeam1 = Team(
|
||||
id: testTeam1.id,
|
||||
name: 'Different Name',
|
||||
members: [testPlayer3],
|
||||
);
|
||||
teamCount = await database.teamDao.getTeamCount();
|
||||
expect(teamCount, 0);
|
||||
});
|
||||
|
||||
await database.teamDao.addTeamsAsList(
|
||||
teams: [duplicateTeam1, testTeam2, testTeam3],
|
||||
);
|
||||
|
||||
final allTeams = await database.teamDao.getAllTeams();
|
||||
expect(allTeams.length, 3);
|
||||
|
||||
// Verify testTeam1 retained original name (was inserted first)
|
||||
final team1 = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team1.name, testTeam1.name);
|
||||
});
|
||||
|
||||
// Verifies that team IDs are preserved correctly.
|
||||
test('Team IDs are preserved through add and retrieve', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
final fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(fetchedTeam.id, testTeam1.id);
|
||||
});
|
||||
|
||||
// Verifies that createdAt timestamps are preserved.
|
||||
test('Team createdAt timestamps are preserved', () async {
|
||||
await database.teamDao.addTeam(team: testTeam1);
|
||||
|
||||
final fetchedTeam = await database.teamDao.getTeamById(
|
||||
teamId: testTeam1.id,
|
||||
);
|
||||
|
||||
expect(fetchedTeam.createdAt, testTeam1.createdAt);
|
||||
test('deleteAllTeams() with empty list returns false', () async {
|
||||
final deleted = await database.teamDao.deleteAllTeams();
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user