updated team tests
This commit is contained in:
@@ -16,12 +16,12 @@ class ScoreEntryDao extends DatabaseAccessor<AppDatabase>
|
||||
/* Create */
|
||||
|
||||
/// Adds a score entry to the database.
|
||||
Future<void> addScore({
|
||||
Future<bool> addScore({
|
||||
required String playerId,
|
||||
required String matchId,
|
||||
required ScoreEntry entry,
|
||||
}) async {
|
||||
await into(scoreEntryTable).insert(
|
||||
final rowsAffected = await into(scoreEntryTable).insert(
|
||||
ScoreEntryTableCompanion.insert(
|
||||
playerId: playerId,
|
||||
matchId: matchId,
|
||||
@@ -31,6 +31,8 @@ class ScoreEntryDao extends DatabaseAccessor<AppDatabase>
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
Future<void> addScoresAsList({
|
||||
|
||||
@@ -212,7 +212,7 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||
const TeamTableCompanion(score: Value(null)),
|
||||
);
|
||||
await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId);
|
||||
await _deleteAllScoresForMembersOfTeam(teamId: teamId, matchId: matchId);
|
||||
|
||||
final rowsAffected =
|
||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||
@@ -238,7 +238,7 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||
const TeamTableCompanion(score: Value(null)),
|
||||
);
|
||||
await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId);
|
||||
await _deleteAllScoresForMembersOfTeam(teamId: teamId, matchId: matchId);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -300,16 +300,17 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
required List<Team> winners,
|
||||
required String matchId,
|
||||
}) async {
|
||||
List<bool?> success = List.generate(winners.length, (index) => null);
|
||||
|
||||
for (int i = 0; i < winners.length; i++) {
|
||||
success[i] = await updateTeamScore(
|
||||
teamId: winners[i].id,
|
||||
matchId: matchId,
|
||||
score: 1,
|
||||
);
|
||||
// Reset all team scores .
|
||||
await removeAllTeamScores(matchId: matchId);
|
||||
// Reset all score entries
|
||||
for (final team in winners) {
|
||||
await _deleteAllScoresForMembersOfTeam(teamId: team.id, matchId: matchId);
|
||||
}
|
||||
return success.every((result) => result == true);
|
||||
|
||||
for (final team in winners) {
|
||||
await updateTeamScore(teamId: team.id, matchId: matchId, score: 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Removes the winner status from all Teams with the given [matchId] by setting its score to null.
|
||||
@@ -349,4 +350,24 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
}
|
||||
return success.every((result) => result == true);
|
||||
}
|
||||
|
||||
/// Helper method to delete all scores for members of a team in a specific match.
|
||||
Future<bool> _deleteAllScoresForMembersOfTeam({
|
||||
required String teamId,
|
||||
required String matchId,
|
||||
}) async {
|
||||
final playerMatchQuery = select(db.playerMatchTable)
|
||||
..where((pm) => pm.teamId.equals(teamId) & pm.matchId.equals(matchId));
|
||||
final playerMatches = await playerMatchQuery.get();
|
||||
|
||||
if (playerMatches.isEmpty) return false;
|
||||
|
||||
for (final pm in playerMatches) {
|
||||
await db.scoreEntryDao.deleteAllScoresForPlayerInMatch(
|
||||
playerId: pm.playerId,
|
||||
matchId: matchId,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,10 +362,7 @@ class _MatchResultViewState extends State<MatchResultView> {
|
||||
Future<bool> _handleLoser() async {
|
||||
if (isTeamMatch) {
|
||||
if (_selectedTeam == null) {
|
||||
return await db.teamDao.removeLoserTeam(
|
||||
matchId: widget.match.id,
|
||||
teamId: _selectedTeam!.id,
|
||||
);
|
||||
return await db.teamDao.removeLoserTeam(matchId: widget.match.id);
|
||||
} else {
|
||||
return await db.teamDao.setLoserTeam(
|
||||
matchId: widget.match.id,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
name: tallee
|
||||
description: "Tracking App for Card Games"
|
||||
publish_to: 'none'
|
||||
version: 0.0.30+331
|
||||
version: 0.0.30+332
|
||||
|
||||
environment:
|
||||
sdk: ^3.8.1
|
||||
|
||||
@@ -507,7 +507,6 @@ void main() {
|
||||
deleted = await database.matchDao.deleteAllMatches();
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMatchesByGame() deletes all matches for a game', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
@@ -523,7 +522,9 @@ void main() {
|
||||
);
|
||||
expect(deletedCount, 2);
|
||||
|
||||
count = await database.matchDao.getMatchCountByGame(gameId: testGame.id);
|
||||
count = await database.matchDao.getMatchCountByGame(
|
||||
gameId: testGame.id,
|
||||
);
|
||||
expect(count, 0);
|
||||
|
||||
final allMatches = await database.matchDao.getAllMatches();
|
||||
@@ -537,4 +538,5 @@ void main() {
|
||||
expect(deletedCount, 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:core' hide Match;
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/drift.dart' hide isNotNull, isNull;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tallee/core/enums.dart';
|
||||
@@ -327,5 +327,200 @@ void main() {
|
||||
expect(deleted, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('SCORE', () {
|
||||
test('updateTeamScore() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final updated = await database.teamDao.updateTeamScore(
|
||||
teamId: testTeam1.id,
|
||||
matchId: testMatch1.id,
|
||||
score: 5,
|
||||
);
|
||||
expect(updated, isTrue);
|
||||
final team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, 5);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 5);
|
||||
}
|
||||
});
|
||||
|
||||
test('set-/removeWinnerTeam() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final set = await database.teamDao.setWinnerTeam(
|
||||
teamId: testTeam1.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(set, isTrue);
|
||||
|
||||
var team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, 1);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 1);
|
||||
}
|
||||
|
||||
final removed = await database.teamDao.removeWinnerTeam(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(removed, isTrue);
|
||||
|
||||
team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, isNull);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNull);
|
||||
}
|
||||
});
|
||||
|
||||
test('set-/removeLoserTeam() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final set = await database.teamDao.setLoserTeam(
|
||||
teamId: testTeam1.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(set, isTrue);
|
||||
|
||||
var team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, 0);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 0);
|
||||
}
|
||||
|
||||
final removed = await database.teamDao.removeLoserTeam(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(removed, isTrue);
|
||||
|
||||
team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, isNull);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNull);
|
||||
}
|
||||
});
|
||||
|
||||
test('set-/removeWinnerTeams() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final set = await database.teamDao.setWinnerTeams(
|
||||
winners: [testTeam1, testTeam2],
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(set, isTrue);
|
||||
|
||||
// check both teams got the winner score
|
||||
var team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, 1);
|
||||
team = await database.teamDao.getTeamById(teamId: testTeam2.id);
|
||||
expect(team.score, 1);
|
||||
|
||||
// check all members of both teams got the winner score
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 1);
|
||||
}
|
||||
|
||||
for (final member in testTeam2.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 1);
|
||||
}
|
||||
|
||||
final removed = await database.teamDao.removeWinnerTeam(
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(removed, isTrue);
|
||||
|
||||
team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, isNull);
|
||||
|
||||
team = await database.teamDao.getTeamById(teamId: testTeam2.id);
|
||||
expect(team.score, isNull);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNull);
|
||||
}
|
||||
|
||||
for (final member in testTeam2.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNull);
|
||||
}
|
||||
});
|
||||
|
||||
test('setTeamPlacements() works correctly', () async {
|
||||
await database.matchDao.addMatch(match: testMatch1);
|
||||
|
||||
final set = await database.teamDao.setTeamPlacements(
|
||||
teams: [testTeam1, testTeam2],
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(set, isTrue);
|
||||
|
||||
var team = await database.teamDao.getTeamById(teamId: testTeam1.id);
|
||||
expect(team.score, 2);
|
||||
team = await database.teamDao.getTeamById(teamId: testTeam2.id);
|
||||
expect(team.score, 1);
|
||||
|
||||
for (final member in testTeam1.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 2);
|
||||
}
|
||||
|
||||
for (final member in testTeam2.members) {
|
||||
final entry = await database.scoreEntryDao.getScore(
|
||||
playerId: member.id,
|
||||
matchId: testMatch1.id,
|
||||
);
|
||||
expect(entry, isNotNull);
|
||||
expect(entry!.score, 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user