updated team tests
This commit is contained in:
@@ -16,12 +16,12 @@ class ScoreEntryDao extends DatabaseAccessor<AppDatabase>
|
|||||||
/* Create */
|
/* Create */
|
||||||
|
|
||||||
/// Adds a score entry to the database.
|
/// Adds a score entry to the database.
|
||||||
Future<void> addScore({
|
Future<bool> addScore({
|
||||||
required String playerId,
|
required String playerId,
|
||||||
required String matchId,
|
required String matchId,
|
||||||
required ScoreEntry entry,
|
required ScoreEntry entry,
|
||||||
}) async {
|
}) async {
|
||||||
await into(scoreEntryTable).insert(
|
final rowsAffected = await into(scoreEntryTable).insert(
|
||||||
ScoreEntryTableCompanion.insert(
|
ScoreEntryTableCompanion.insert(
|
||||||
playerId: playerId,
|
playerId: playerId,
|
||||||
matchId: matchId,
|
matchId: matchId,
|
||||||
@@ -31,6 +31,8 @@ class ScoreEntryDao extends DatabaseAccessor<AppDatabase>
|
|||||||
),
|
),
|
||||||
mode: InsertMode.insertOrReplace,
|
mode: InsertMode.insertOrReplace,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return rowsAffected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> addScoresAsList({
|
Future<void> addScoresAsList({
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
|||||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||||
const TeamTableCompanion(score: Value(null)),
|
const TeamTableCompanion(score: Value(null)),
|
||||||
);
|
);
|
||||||
await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId);
|
await _deleteAllScoresForMembersOfTeam(teamId: teamId, matchId: matchId);
|
||||||
|
|
||||||
final rowsAffected =
|
final rowsAffected =
|
||||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
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(
|
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||||
const TeamTableCompanion(score: Value(null)),
|
const TeamTableCompanion(score: Value(null)),
|
||||||
);
|
);
|
||||||
await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId);
|
await _deleteAllScoresForMembersOfTeam(teamId: teamId, matchId: matchId);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,16 +300,17 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
|||||||
required List<Team> winners,
|
required List<Team> winners,
|
||||||
required String matchId,
|
required String matchId,
|
||||||
}) async {
|
}) async {
|
||||||
List<bool?> success = List.generate(winners.length, (index) => null);
|
// Reset all team scores .
|
||||||
|
await removeAllTeamScores(matchId: matchId);
|
||||||
for (int i = 0; i < winners.length; i++) {
|
// Reset all score entries
|
||||||
success[i] = await updateTeamScore(
|
for (final team in winners) {
|
||||||
teamId: winners[i].id,
|
await _deleteAllScoresForMembersOfTeam(teamId: team.id, matchId: matchId);
|
||||||
matchId: matchId,
|
|
||||||
score: 1,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
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.
|
/// 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);
|
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 {
|
Future<bool> _handleLoser() async {
|
||||||
if (isTeamMatch) {
|
if (isTeamMatch) {
|
||||||
if (_selectedTeam == null) {
|
if (_selectedTeam == null) {
|
||||||
return await db.teamDao.removeLoserTeam(
|
return await db.teamDao.removeLoserTeam(matchId: widget.match.id);
|
||||||
matchId: widget.match.id,
|
|
||||||
teamId: _selectedTeam!.id,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
return await db.teamDao.setLoserTeam(
|
return await db.teamDao.setLoserTeam(
|
||||||
matchId: widget.match.id,
|
matchId: widget.match.id,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
name: tallee
|
name: tallee
|
||||||
description: "Tracking App for Card Games"
|
description: "Tracking App for Card Games"
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
version: 0.0.30+331
|
version: 0.0.30+332
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.8.1
|
sdk: ^3.8.1
|
||||||
|
|||||||
@@ -507,34 +507,36 @@ void main() {
|
|||||||
deleted = await database.matchDao.deleteAllMatches();
|
deleted = await database.matchDao.deleteAllMatches();
|
||||||
expect(deleted, isFalse);
|
expect(deleted, isFalse);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
test('deleteMatchesByGame() deletes all matches for a game', () async {
|
test('deleteMatchesByGame() deletes all matches for a game', () async {
|
||||||
await database.matchDao.addMatch(match: testMatch1);
|
await database.matchDao.addMatch(match: testMatch1);
|
||||||
await database.matchDao.addMatch(match: testMatch2);
|
await database.matchDao.addMatch(match: testMatch2);
|
||||||
|
|
||||||
var count = await database.matchDao.getMatchCountByGame(
|
var count = await database.matchDao.getMatchCountByGame(
|
||||||
gameId: testGame.id,
|
gameId: testGame.id,
|
||||||
);
|
);
|
||||||
expect(count, 2);
|
expect(count, 2);
|
||||||
|
|
||||||
final deletedCount = await database.matchDao.deleteMatchesByGame(
|
final deletedCount = await database.matchDao.deleteMatchesByGame(
|
||||||
gameId: testGame.id,
|
gameId: testGame.id,
|
||||||
);
|
);
|
||||||
expect(deletedCount, 2);
|
expect(deletedCount, 2);
|
||||||
|
|
||||||
count = await database.matchDao.getMatchCountByGame(gameId: testGame.id);
|
count = await database.matchDao.getMatchCountByGame(
|
||||||
expect(count, 0);
|
gameId: testGame.id,
|
||||||
|
);
|
||||||
|
expect(count, 0);
|
||||||
|
|
||||||
final allMatches = await database.matchDao.getAllMatches();
|
final allMatches = await database.matchDao.getAllMatches();
|
||||||
expect(allMatches, isEmpty);
|
expect(allMatches, isEmpty);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('deleteMatchesByGame() returns 0 for non-existent game', () async {
|
test('deleteMatchesByGame() returns 0 for non-existent game', () async {
|
||||||
final deletedCount = await database.matchDao.deleteMatchesByGame(
|
final deletedCount = await database.matchDao.deleteMatchesByGame(
|
||||||
gameId: 'non-existent-game-id',
|
gameId: 'non-existent-game-id',
|
||||||
);
|
);
|
||||||
expect(deletedCount, 0);
|
expect(deletedCount, 0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import 'dart:core' hide Match;
|
import 'dart:core' hide Match;
|
||||||
|
|
||||||
import 'package:clock/clock.dart';
|
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:drift/native.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:tallee/core/enums.dart';
|
import 'package:tallee/core/enums.dart';
|
||||||
@@ -327,5 +327,200 @@ void main() {
|
|||||||
expect(deleted, isFalse);
|
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