Added test for user retrieval from firestore

This commit is contained in:
Mariano Uvalle 2019-02-25 16:19:36 -06:00
parent 7e8b0b074a
commit 4aadfbde8c
3 changed files with 73 additions and 27 deletions

View file

@ -14,17 +14,26 @@ class SummaryModel {
int addedFriday; int addedFriday;
SummaryModel({ SummaryModel({
this.completedMonday, int completedMonday,
this.addedMonday, int addedMonday,
this.completedTuesday, int completedTuesday,
this.addedTuesday, int addedTuesday,
this.completedWednesday, int completedWednesday,
this.addedWednesday, int addedWednesday,
this.completedThursday, int completedThursday,
this.addedThursday, int addedThursday,
this.completedFriday, int completedFriday,
this.addedFriday, int addedFriday,
}); }) : completedMonday = completedMonday ?? 0,
addedMonday = addedMonday ?? 0,
completedTuesday = completedTuesday ?? 0,
addedTuesday = addedTuesday ?? 0,
completedWednesday = completedWednesday ?? 0,
addedWednesday = addedWednesday ?? 0,
completedThursday = completedThursday ?? 0,
addedThursday = addedThursday ?? 0,
completedFriday = completedFriday ?? 0,
addedFriday = addedFriday ?? 0;
/// Returns a [SummaryModel] from a map. /// Returns a [SummaryModel] from a map.
SummaryModel.fromMap(Map<String, dynamic> map) { SummaryModel.fromMap(Map<String, dynamic> map) {

View file

@ -1,3 +1,4 @@
import 'package:meta/meta.dart';
import 'summary_model.dart'; import 'summary_model.dart';
/// An app user. /// An app user.
@ -6,43 +7,49 @@ import 'summary_model.dart';
class UserModel { class UserModel {
final String id; final String id;
final String username; final String username;
final List<int> tasks; final List<String> tasks;
final SummaryModel summary; final SummaryModel summary;
final int userId;
final int pendingHigh; final int pendingHigh;
final int pendingMedium; final int pendingMedium;
final int pendingLow; final int pendingLow;
UserModel({ UserModel({
this.id, this.id,
this.username, @required this.username,
this.tasks, @required this.tasks,
this.summary, @required this.summary,
this.userId, @required this.pendingHigh,
this.pendingHigh, @required this.pendingMedium,
this.pendingMedium, @required this.pendingLow,
this.pendingLow,
}); });
///Returns a [UserModel] from a map. ///Returns a [UserModel] from a map.
UserModel.fromFirestore(Map<String, dynamic> firestoreMap, {String id}) UserModel.fromFirestore(Map<String, dynamic> firestoreMap, {String id})
: id = id, : id = id,
username = firestoreMap["username"], username = firestoreMap["username"],
tasks = firestoreMap["tasks"].cast<int>(), tasks = firestoreMap["tasks"].cast<String>(),
summary = summary =
SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()), SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()),
userId = firestoreMap["userId"],
pendingHigh = firestoreMap["pendingHigh"], pendingHigh = firestoreMap["pendingHigh"],
pendingMedium = firestoreMap["pendingMedium"], pendingMedium = firestoreMap["pendingMedium"],
pendingLow = firestoreMap["pendingLow"]; pendingLow = firestoreMap["pendingLow"];
Map<String, dynamic> toFirestoreMap() {
return <String, dynamic>{
"username": username,
"tasks": tasks,
"summary": summary.toMap(),
"pendingHigh": pendingHigh,
"pendingMedium": pendingMedium,
"pendingLow": pendingLow,
};
}
@override @override
int get hashCode => int get hashCode =>
id.hashCode ^ id.hashCode ^
username.hashCode ^ username.hashCode ^
tasks.hashCode ^
summary.hashCode ^ summary.hashCode ^
userId.hashCode ^
pendingHigh.hashCode ^ pendingHigh.hashCode ^
pendingMedium.hashCode ^ pendingMedium.hashCode ^
pendingLow.hashCode; pendingLow.hashCode;
@ -54,9 +61,7 @@ class UserModel {
other is UserModel && other is UserModel &&
id == other.id && id == other.id &&
username == other.username && username == other.username &&
tasks == other.tasks &&
summary == other.summary && summary == other.summary &&
userId == other.userId &&
pendingHigh == other.pendingHigh && pendingHigh == other.pendingHigh &&
pendingMedium == other.pendingMedium && pendingMedium == other.pendingMedium &&
pendingLow == other.pendingLow; pendingLow == other.pendingLow;

View file

@ -2,11 +2,12 @@ import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:do_more/src/models/event_model.dart'; import 'package:do_more/src/models/event_model.dart';
import 'package:do_more/src/models/summary_model.dart';
import 'package:do_more/src/models/task_model.dart'; import 'package:do_more/src/models/task_model.dart';
import 'package:do_more/src/models/user_model.dart';
import 'package:do_more/src/resources/firestore_provider.dart'; import 'package:do_more/src/resources/firestore_provider.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
import 'package:rxdart/rxdart.dart';
class MockFirestore extends Mock implements Firestore {} class MockFirestore extends Mock implements Firestore {}
@ -48,7 +49,38 @@ main() {
lowPriority: 0, lowPriority: 0,
); );
final summary = SummaryModel();
final user = UserModel(
id: '123',
tasks: <String>[],
pendingHigh: 0,
pendingMedium: 0,
pendingLow: 0,
username: 'testUser',
summary: summary,
);
group('FirestoreProvider', () { group('FirestoreProvider', () {
test('should listen to updates of a single user object', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final query = MockQuery();
final querySnapshot = MockQuerySnapshot();
final snapshot = MockDocumentSnapshot(user.toFirestoreMap());
final snapshots = Stream.fromIterable([querySnapshot]);
final provider = FirestoreProvider(firestore);
when(firestore.collection('users')).thenReturn(collection);
when(collection.where('username', isEqualTo: user.username))
.thenReturn(query);
when(query.snapshots()).thenAnswer((_) => snapshots);
when(querySnapshot.documents).thenReturn([snapshot]);
when(snapshot.documentID).thenReturn(user.id);
expect(provider.getUser(user.username), emits(user));
});
test('should create task in firestore', () { test('should create task in firestore', () {
final firestore = MockFirestore(); final firestore = MockFirestore();
final collection = MockCollectionReference(); final collection = MockCollectionReference();