Added test for user retrieval from firestore
This commit is contained in:
parent
7e8b0b074a
commit
4aadfbde8c
3 changed files with 73 additions and 27 deletions
|
|
@ -14,17 +14,26 @@ class SummaryModel {
|
|||
int addedFriday;
|
||||
|
||||
SummaryModel({
|
||||
this.completedMonday,
|
||||
this.addedMonday,
|
||||
this.completedTuesday,
|
||||
this.addedTuesday,
|
||||
this.completedWednesday,
|
||||
this.addedWednesday,
|
||||
this.completedThursday,
|
||||
this.addedThursday,
|
||||
this.completedFriday,
|
||||
this.addedFriday,
|
||||
});
|
||||
int completedMonday,
|
||||
int addedMonday,
|
||||
int completedTuesday,
|
||||
int addedTuesday,
|
||||
int completedWednesday,
|
||||
int addedWednesday,
|
||||
int completedThursday,
|
||||
int addedThursday,
|
||||
int completedFriday,
|
||||
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.
|
||||
SummaryModel.fromMap(Map<String, dynamic> map) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:meta/meta.dart';
|
||||
import 'summary_model.dart';
|
||||
|
||||
/// An app user.
|
||||
|
|
@ -6,43 +7,49 @@ import 'summary_model.dart';
|
|||
class UserModel {
|
||||
final String id;
|
||||
final String username;
|
||||
final List<int> tasks;
|
||||
final List<String> tasks;
|
||||
final SummaryModel summary;
|
||||
final int userId;
|
||||
final int pendingHigh;
|
||||
final int pendingMedium;
|
||||
final int pendingLow;
|
||||
|
||||
UserModel({
|
||||
this.id,
|
||||
this.username,
|
||||
this.tasks,
|
||||
this.summary,
|
||||
this.userId,
|
||||
this.pendingHigh,
|
||||
this.pendingMedium,
|
||||
this.pendingLow,
|
||||
@required this.username,
|
||||
@required this.tasks,
|
||||
@required this.summary,
|
||||
@required this.pendingHigh,
|
||||
@required this.pendingMedium,
|
||||
@required this.pendingLow,
|
||||
});
|
||||
|
||||
///Returns a [UserModel] from a map.
|
||||
UserModel.fromFirestore(Map<String, dynamic> firestoreMap, {String id})
|
||||
: id = id,
|
||||
username = firestoreMap["username"],
|
||||
tasks = firestoreMap["tasks"].cast<int>(),
|
||||
tasks = firestoreMap["tasks"].cast<String>(),
|
||||
summary =
|
||||
SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()),
|
||||
userId = firestoreMap["userId"],
|
||||
pendingHigh = firestoreMap["pendingHigh"],
|
||||
pendingMedium = firestoreMap["pendingMedium"],
|
||||
pendingLow = firestoreMap["pendingLow"];
|
||||
|
||||
Map<String, dynamic> toFirestoreMap() {
|
||||
return <String, dynamic>{
|
||||
"username": username,
|
||||
"tasks": tasks,
|
||||
"summary": summary.toMap(),
|
||||
"pendingHigh": pendingHigh,
|
||||
"pendingMedium": pendingMedium,
|
||||
"pendingLow": pendingLow,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
id.hashCode ^
|
||||
username.hashCode ^
|
||||
tasks.hashCode ^
|
||||
summary.hashCode ^
|
||||
userId.hashCode ^
|
||||
pendingHigh.hashCode ^
|
||||
pendingMedium.hashCode ^
|
||||
pendingLow.hashCode;
|
||||
|
|
@ -54,9 +61,7 @@ class UserModel {
|
|||
other is UserModel &&
|
||||
id == other.id &&
|
||||
username == other.username &&
|
||||
tasks == other.tasks &&
|
||||
summary == other.summary &&
|
||||
userId == other.userId &&
|
||||
pendingHigh == other.pendingHigh &&
|
||||
pendingMedium == other.pendingMedium &&
|
||||
pendingLow == other.pendingLow;
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ import 'dart:async';
|
|||
|
||||
import 'package:cloud_firestore/cloud_firestore.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/user_model.dart';
|
||||
import 'package:do_more/src/resources/firestore_provider.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
class MockFirestore extends Mock implements Firestore {}
|
||||
|
||||
|
|
@ -48,7 +49,38 @@ main() {
|
|||
lowPriority: 0,
|
||||
);
|
||||
|
||||
final summary = SummaryModel();
|
||||
|
||||
final user = UserModel(
|
||||
id: '123',
|
||||
tasks: <String>[],
|
||||
pendingHigh: 0,
|
||||
pendingMedium: 0,
|
||||
pendingLow: 0,
|
||||
username: 'testUser',
|
||||
summary: summary,
|
||||
);
|
||||
|
||||
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', () {
|
||||
final firestore = MockFirestore();
|
||||
final collection = MockCollectionReference();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue