Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: solved word count #594

Merged
merged 9 commits into from
Jun 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CrosswordRepository {

static const _sectionsCollection = 'boardChunks';
static const _answersCollection = 'answers';
static const _boardInfoCollection = 'boardInfo';
static const _solvedWordCollection = 'solvedWords';

/// Fetches all sections from the board.
Future<List<BoardSection>> listAllSections() async {
Expand Down Expand Up @@ -203,27 +203,11 @@ class CrosswordRepository {
oldString.substring(index + 1);
}

/// Adds one to the solved words count in the crossword.
Future<void> updateSolvedWordsCount() async {
final snapshot = await _dbClient.find(
_boardInfoCollection,
{
'type': 'solved_words_count',
},
);

final document = snapshot.first;
final solvedWordsCount = (document.data['value'] as num).toInt();
final newValue = solvedWordsCount + 1;

await _dbClient.update(
_boardInfoCollection,
DbEntityRecord(
id: document.id,
data: {
'value': newValue,
},
),
/// Creates a new document for a solved word
Future<void> saveSolvedWord(String wordId) async {
await _dbClient.set(
_solvedWordCollection,
DbEntityRecord(id: wordId),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ void main() {
);
});

group('updateSolvedWordsCount', () {
group('saveSolvedWord', () {
late CrosswordRepository repository;

setUp(() {
Expand All @@ -532,25 +532,18 @@ void main() {
).thenAnswer((_) async {});
});

test('updates the document in the database', () async {
final record = _MockDbEntityRecord();
when(() => record.id).thenReturn('id');
when(() => record.data).thenReturn({'value': 80});
when(
() => dbClient.find('boardInfo', {'type': 'solved_words_count'}),
).thenAnswer((_) async => [record]);
test('creates a new document in the database', () async {
when(
() => dbClient.update('boardInfo', any()),
() => dbClient.set('solvedWords', any()),
).thenAnswer((_) async {});

await repository.updateSolvedWordsCount();
await repository.saveSolvedWord('id');

verify(
() => dbClient.update(
'boardInfo',
() => dbClient.set(
'solvedWords',
DbEntityRecord(
id: 'id',
data: {'value': 81},
),
),
).called(1);
Expand Down
2 changes: 1 addition & 1 deletion api/routes/game/answer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Future<Response> _onPost(RequestContext context) async {
points = await leaderboardRepository.updateScore(user.id);

if (!preSolved) {
await crosswordRepository.updateSolvedWordsCount();
await crosswordRepository.saveSolvedWord(wordId);
}
}

Expand Down
8 changes: 5 additions & 3 deletions api/test/routes/game/answer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void main() {
final response = await route.onRequest(requestContext);

expect(response.statusCode, HttpStatus.ok);
verifyNever(() => crosswordRepository.updateSolvedWordsCount());
verifyNever(() => crosswordRepository.saveSolvedWord(any()));
},
);

Expand All @@ -153,7 +153,7 @@ void main() {
),
);
when(
() => crosswordRepository.updateSolvedWordsCount(),
() => crosswordRepository.saveSolvedWord('id'),
).thenAnswer((_) async {});
when(
() => crosswordRepository.answerWord(
Expand All @@ -176,7 +176,9 @@ void main() {
final response = await route.onRequest(requestContext);

expect(response.statusCode, HttpStatus.ok);
verify(() => crosswordRepository.updateSolvedWordsCount()).called(1);
verify(
() => crosswordRepository.saveSolvedWord('id'),
).called(1);
},
);

Expand Down