Updated match dao + tests

This commit is contained in:
2026-05-01 11:57:03 +02:00
parent 785873d3c8
commit be6b968a43
3 changed files with 438 additions and 347 deletions

View File

@@ -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,
newName: 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',
newName: '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,
newGroupId: 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',
newGroupId: '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,
newName: 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);
});
});
});
}