Skip to content

Commit

Permalink
refactor: keep same count reading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgalarraga committed Jun 28, 2024
1 parent 3a9ae0c commit 86947b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class BoardInfoRepository {
TargetPlatform? targetPlatform,
}) : _targetPlatform = targetPlatform ?? defaultTargetPlatform {
boardInfoCollection = firestore.collection('boardInfo');
solvedWordsCollection = firestore.collection('solvedWords');
}

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

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

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

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(
() => boardInfoCollection.where('type', isEqualTo: 'total_words_count'),
).thenReturn(boardInfoCollection);
() => collection.where('type', isEqualTo: 'total_words_count'),
).thenReturn(collection);
when(
() => collection.where('type', isEqualTo: 'solved_words_count'),
).thenReturn(collection);
when(
() => boardInfoCollection.where('type', isEqualTo: 'zoom_limit'),
).thenReturn(boardInfoCollection);
() => collection.where('type', isEqualTo: 'zoom_limit'),
).thenReturn(collection);
when(
() => boardInfoCollection.where('type', isEqualTo: 'section_size'),
).thenReturn(boardInfoCollection);
() => collection.where('type', isEqualTo: 'section_size'),
).thenReturn(collection);
when(
() => boardInfoCollection.where('type', isEqualTo: 'bottom_right'),
).thenReturn(boardInfoCollection);
() => collection.where('type', isEqualTo: 'bottom_right'),
).thenReturn(collection);

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

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

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

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

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

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

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

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

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

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

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

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

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

expect(
Expand Down

0 comments on commit 86947b4

Please sign in to comment.