Added tests for the events operations in firebase

This commit is contained in:
Mariano Uvalle 2019-02-24 21:38:12 -06:00
parent dbdb25a6ca
commit 7e8b0b074a
3 changed files with 139 additions and 11 deletions

View file

@ -59,6 +59,7 @@ class EventModel {
mediumPriority.hashCode ^ mediumPriority.hashCode ^
lowPriority.hashCode; lowPriority.hashCode;
// TODO: find a way to compare the 3 commented properties.
@override @override
bool operator ==(Object other) => bool operator ==(Object other) =>
identical(this, other) || identical(this, other) ||
@ -67,9 +68,9 @@ class EventModel {
id == other.id && id == other.id &&
name == other.name && name == other.name &&
pendigTasks == other.pendigTasks && pendigTasks == other.pendigTasks &&
when == other.when && //when == other.when &&
media == other.media && //media == other.media &&
tasks == other.tasks && //tasks == other.tasks &&
highPriority == other.highPriority && highPriority == other.highPriority &&
mediumPriority == other.mediumPriority && mediumPriority == other.mediumPriority &&
lowPriority == other.lowPriority; lowPriority == other.lowPriority;

View file

@ -231,12 +231,8 @@ class FirestoreProvider {
/// Returns a stream of [List<EventModel] that correspond to /// Returns a stream of [List<EventModel] that correspond to
/// a particular user. /// a particular user.
Observable<List<EventModel>> getUserEvents(String userDocumentId) { Observable<List<EventModel>> getUserEvents(String userDocumentId) {
final mappedStream = firestore final mappedStream =
.collection('users') firestore.collection('users/$userDocumentId/Events').snapshots().map(
.document(userDocumentId)
.collection('Events')
.snapshots()
.map(
(QuerySnapshot snapshot) { (QuerySnapshot snapshot) {
return snapshot.documents.map((DocumentSnapshot documentSnapshot) { return snapshot.documents.map((DocumentSnapshot documentSnapshot) {
return EventModel.fromFirestore( return EventModel.fromFirestore(

View file

@ -24,6 +24,8 @@ class MockDocumentSnapshot extends Mock implements DocumentSnapshot {
class MockQuerySnapshot extends Mock implements QuerySnapshot {} class MockQuerySnapshot extends Mock implements QuerySnapshot {}
class MockQuery extends Mock implements Query {}
main() { main() {
final task = TaskModel( final task = TaskModel(
id: '1', id: '1',
@ -47,7 +49,7 @@ main() {
); );
group('FirestoreProvider', () { group('FirestoreProvider', () {
test('should send task to firestore', () { test('should create task in firestore', () {
final firestore = MockFirestore(); final firestore = MockFirestore();
final collection = MockCollectionReference(); final collection = MockCollectionReference();
final provider = FirestoreProvider(firestore); final provider = FirestoreProvider(firestore);
@ -74,7 +76,7 @@ main() {
expectLater(provider.getTask('1'), emits(task)); expectLater(provider.getTask('1'), emits(task));
}); });
test('should delete task from firestore', () { test('should delete a task from firestore', () {
final firestore = MockFirestore(); final firestore = MockFirestore();
final collection = MockCollectionReference(); final collection = MockCollectionReference();
final document = MockDocumentReference(); final document = MockDocumentReference();
@ -88,5 +90,134 @@ main() {
verify(document.delete()); verify(document.delete());
}); });
test('should update a task in firestore', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final document = MockDocumentReference();
final provider = FirestoreProvider(firestore);
when(firestore.collection('tasks')).thenReturn(collection);
when(collection.document(task.id)).thenReturn(document);
provider.updateTask(task.id, done: true);
verify(document.setData({'done': true}, merge: true));
});
group('should listen for updates in user\'s tasks', () {
test('with an event specified', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final query = MockQuery();
final snapshot = MockQuerySnapshot();
final snapshots = Stream.fromIterable([snapshot]);
final document = MockDocumentSnapshot(task.toFirestoreMap());
final provider = FirestoreProvider(firestore);
when(firestore.collection('tasks')).thenReturn(collection);
when(collection.where('ownerUsername', isEqualTo: 'testUser'))
.thenReturn(query);
when(query.where('done', isEqualTo: false)).thenReturn(query);
when(query.where('event', isEqualTo: 'testEvent')).thenReturn(query);
when(query.snapshots()).thenAnswer((_) => snapshots);
when(snapshot.documents).thenReturn([document]);
when(document.documentID).thenReturn(task.id);
expect(provider.getUserTasks('testUser', event: 'testEvent'),
emits([task]));
});
test('with no event specified', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final query = MockQuery();
final snapshot = MockQuerySnapshot();
final snapshots = Stream.fromIterable([snapshot]);
final document = MockDocumentSnapshot(task.toFirestoreMap());
final provider = FirestoreProvider(firestore);
when(firestore.collection('tasks')).thenReturn(collection);
when(collection.where('ownerUsername', isEqualTo: 'testUser'))
.thenReturn(query);
when(query.where('done', isEqualTo: false)).thenReturn(query);
when(query.snapshots()).thenAnswer((_) => snapshots);
when(snapshot.documents).thenReturn([document]);
when(document.documentID).thenReturn(task.id);
expect(provider.getUserTasks('testUser'), emits([task]));
});
});
test('should create event in firestore', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final provider = FirestoreProvider(firestore);
when(firestore.collection('users/123/Events')).thenReturn(collection);
provider.addEvent('123', event);
verify(collection.add(event.toFirestoreMap()));
});
test('should listen for updates to a single Event', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final snapshot = MockDocumentSnapshot(event.toFirestoreMap());
final snapshots = Stream.fromIterable([snapshot]);
final document = MockDocumentReference();
final provider = FirestoreProvider(firestore);
when(firestore.collection('users/123/Events')).thenReturn(collection);
when(collection.document(event.id)).thenReturn(document);
when(document.snapshots()).thenAnswer((_) => snapshots);
when(snapshot.documentID).thenReturn(event.id);
expectLater(provider.getEvent('123', event.id), emits(event));
});
test('should delete an event from firestore', () {
final firestore = MockFirestore();
final document = MockDocumentReference();
final provider = FirestoreProvider(firestore);
when(firestore.document('users/123/Events/${event.id}'))
.thenReturn(document);
when(document.delete()).thenAnswer((_) => Future<void>.value());
provider.deleteEvent('123', event.id);
verify(document.delete());
});
test('should update an event in firestore', () {
final firestore = MockFirestore();
final document = MockDocumentReference();
final provider = FirestoreProvider(firestore);
when(firestore.document('users/123/Events/${event.id}'))
.thenReturn(document);
provider.updateEvent('123', event.id, name: 'new name');
verify(document.setData({'name': 'new name'}, merge: true));
});
test('should listen for changes in user\'s events', () {
final firestore = MockFirestore();
final collection = MockCollectionReference();
final snapshot = MockQuerySnapshot();
final snapshots = Stream.fromIterable([snapshot]);
final document = MockDocumentSnapshot(event.toFirestoreMap());
final provider = FirestoreProvider(firestore);
when(firestore.collection('users/123/Events')).thenReturn(collection);
when(collection.snapshots()).thenAnswer((_) => snapshots);
when(snapshot.documents).thenReturn([document]);
when(document.documentID).thenReturn(event.id);
expect(provider.getUserEvents('123'), emits([event]));
});
}); });
} }