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 @@ -204,26 +204,10 @@ class CrosswordRepository {
}

/// 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,
},
),
Future<void> updateSolvedWordsCount(String wordId) async {
await _dbClient.set(
_solvedWordCollection,
DbEntityRecord(id: wordId),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.updateSolvedWordsCount('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.updateSolvedWordsCount(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.updateSolvedWordsCount(any()));
},
);

Expand All @@ -153,7 +153,7 @@ void main() {
),
);
when(
() => crosswordRepository.updateSolvedWordsCount(),
() => crosswordRepository.updateSolvedWordsCount('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.updateSolvedWordsCount('id'),
).called(1);
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BoardInfoRepository {
TargetPlatform? targetPlatform,
}) : _targetPlatform = targetPlatform ?? defaultTargetPlatform {
boardInfoCollection = firestore.collection('boardInfo');
solvedWordsCollection = firestore.collection('solvedWords');
}

/// The [FirebaseFirestore] instance.
Expand All @@ -63,6 +64,9 @@ class BoardInfoRepository {
/// The [CollectionReference] for the config.
late final CollectionReference<Map<String, dynamic>> boardInfoCollection;

/// The [CollectionReference] for the solved words.
late final CollectionReference<Map<String, dynamic>> solvedWordsCollection;

BehaviorSubject<bool>? _hintsEnabled;

/// Returns the total words count available in the crossword.
Expand All @@ -80,10 +84,7 @@ class BoardInfoRepository {
/// Returns the solved words count in the crossword.
Stream<int> getSolvedWordsCount() {
try {
return boardInfoCollection
.where('type', isEqualTo: 'solved_words_count')
.snapshots()
.map((event) => (event.docs.first.data()['value'] as num).toInt());
return solvedWordsCollection.snapshots().map((snapshot) => snapshot.size);
} catch (error, stackStrace) {
throw BoardInfoException(error, stackStrace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,57 @@ class _MockQueryDocumentSnapshot<T> extends Mock
void main() {
group('BoardInfoRepository', () {
late _MockFirebaseFirestore firestore;
late CollectionReference<Map<String, dynamic>> collection;
late CollectionReference<Map<String, dynamic>> boardInfoCollection;
late CollectionReference<Map<String, dynamic>> solvedWordsCollection;
late BoardInfoRepository boardInfoRepository;

setUp(() {
firestore = _MockFirebaseFirestore();
collection = _MockCollectionReference();
boardInfoCollection = _MockCollectionReference();
solvedWordsCollection = _MockCollectionReference();

when(() => firestore.collection('boardInfo')).thenReturn(collection);
when(
() => firestore.collection('boardInfo'),
).thenReturn(boardInfoCollection);
when(
() => firestore.collection('solvedWords'),
).thenReturn(solvedWordsCollection);

boardInfoRepository = BoardInfoRepository(firestore: firestore);
});

void mockSolvedWords(int wordNumber) {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();

when(boardInfoCollection.get).thenAnswer((_) async => query);
when(solvedWordsCollection.snapshots)
.thenAnswer((_) => Stream.value(query));

final docs = List.generate(wordNumber, (_) => doc);
when(() => query.size).thenReturn(wordNumber);
when(() => query.docs).thenReturn(docs);
}

void mockQueryResult(dynamic value) {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();
when(
() => collection.where('type', isEqualTo: 'total_words_count'),
).thenReturn(collection);
when(
() => collection.where('type', isEqualTo: 'solved_words_count'),
).thenReturn(collection);
() => boardInfoCollection.where('type', isEqualTo: 'total_words_count'),
).thenReturn(boardInfoCollection);
when(
() => collection.where('type', isEqualTo: 'zoom_limit'),
).thenReturn(collection);
() => boardInfoCollection.where('type', isEqualTo: 'zoom_limit'),
).thenReturn(boardInfoCollection);
when(
() => collection.where('type', isEqualTo: 'section_size'),
).thenReturn(collection);
() => boardInfoCollection.where('type', isEqualTo: 'section_size'),
).thenReturn(boardInfoCollection);
when(
() => collection.where('type', isEqualTo: 'bottom_right'),
).thenReturn(collection);
() => boardInfoCollection.where('type', isEqualTo: 'bottom_right'),
).thenReturn(boardInfoCollection);

when(collection.get).thenAnswer((_) async => query);
when(collection.snapshots).thenAnswer((_) => Stream.value(query));
when(boardInfoCollection.get).thenAnswer((_) async => query);
when(boardInfoCollection.snapshots)
.thenAnswer((_) => Stream.value(query));
when(() => query.docs).thenReturn([doc]);
when(doc.data).thenReturn({'value': value});
}
Expand All @@ -62,10 +80,10 @@ void main() {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();
when(
() => collection.where('type', isEqualTo: 'zoom_limit'),
).thenReturn(collection);
() => boardInfoCollection.where('type', isEqualTo: 'zoom_limit'),
).thenReturn(boardInfoCollection);

when(collection.get).thenAnswer((_) async => query);
when(boardInfoCollection.get).thenAnswer((_) async => query);
when(() => query.docs).thenReturn([doc]);
when(doc.data).thenReturn({'mobile': mobile, 'desktop': desktop});
}
Expand All @@ -86,7 +104,8 @@ void main() {

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'total_words_count'),
() =>
boardInfoCollection.where('type', isEqualTo: 'total_words_count'),
).thenThrow(Exception('oops'));
expect(
() => boardInfoRepository.getTotalWordsCount(),
Expand All @@ -97,14 +116,14 @@ void main() {

group('getSolvedWordsCount', () {
test('returns solved words count from firebase', () async {
mockQueryResult(66000);
mockSolvedWords(200);
final result = boardInfoRepository.getSolvedWordsCount();
expect(result, emits(66000));
expect(result, emits(200));
});

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'solved_words_count'),
() => solvedWordsCollection.snapshots(),
).thenThrow(Exception('oops'));
expect(
() => boardInfoRepository.getSolvedWordsCount(),
Expand All @@ -122,7 +141,7 @@ void main() {

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'section_size'),
() => boardInfoCollection.where('type', isEqualTo: 'section_size'),
).thenThrow(Exception('oops'));
expect(
() => boardInfoRepository.getSectionSize(),
Expand Down Expand Up @@ -158,7 +177,7 @@ void main() {

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'zoom_limit'),
() => boardInfoCollection.where('type', isEqualTo: 'zoom_limit'),
).thenThrow(Exception('oops'));
expect(
() => boardInfoRepository.getZoomLimit(),
Expand All @@ -176,7 +195,7 @@ void main() {

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'bottom_right'),
() => boardInfoCollection.where('type', isEqualTo: 'bottom_right'),
).thenThrow(Exception('oops'));
expect(
() => boardInfoRepository.getBottomRight(),
Expand All @@ -190,9 +209,11 @@ void main() {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();
when(
() => collection.where('type', isEqualTo: 'is_hints_enabled'),
).thenReturn(collection);
when(collection.snapshots).thenAnswer((_) => Stream.value(query));
() =>
boardInfoCollection.where('type', isEqualTo: 'is_hints_enabled'),
).thenReturn(boardInfoCollection);
when(boardInfoCollection.snapshots)
.thenAnswer((_) => Stream.value(query));
when(() => query.docs).thenReturn([doc]);
when(doc.data).thenReturn({'value': true});

Expand All @@ -206,9 +227,10 @@ void main() {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();
when(
() => collection.where('type', isEqualTo: 'game_status'),
).thenReturn(collection);
when(collection.snapshots).thenAnswer((_) => Stream.value(query));
() => boardInfoCollection.where('type', isEqualTo: 'game_status'),
).thenReturn(boardInfoCollection);
when(boardInfoCollection.snapshots)
.thenAnswer((_) => Stream.value(query));
when(() => query.docs).thenReturn([doc]);
when(doc.data).thenReturn({'value': 'in_progress'});

Expand All @@ -220,9 +242,10 @@ void main() {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();
when(
() => collection.where('type', isEqualTo: 'game_status'),
).thenReturn(collection);
when(collection.snapshots).thenAnswer((_) => Stream.value(query));
() => boardInfoCollection.where('type', isEqualTo: 'game_status'),
).thenReturn(boardInfoCollection);
when(boardInfoCollection.snapshots)
.thenAnswer((_) => Stream.value(query));
when(() => query.docs).thenReturn([doc]);
when(doc.data).thenReturn({'value': 'reset_in_progress'});

Expand All @@ -236,9 +259,10 @@ void main() {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
final query = _MockQuerySnapshot<Map<String, dynamic>>();
when(
() => collection.where('type', isEqualTo: 'game_status'),
).thenReturn(collection);
when(collection.snapshots).thenAnswer((_) => Stream.value(query));
() => boardInfoCollection.where('type', isEqualTo: 'game_status'),
).thenReturn(boardInfoCollection);
when(boardInfoCollection.snapshots)
.thenAnswer((_) => Stream.value(query));
when(() => query.docs).thenReturn([doc]);
when(doc.data).thenReturn({'value': 'unknown'});

Expand All @@ -248,7 +272,7 @@ void main() {

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'game_status'),
() => boardInfoCollection.where('type', isEqualTo: 'game_status'),
).thenThrow(Exception('oops'));

expect(
Expand Down