4
4
import com .google .firebase .firestore .CollectionReference ;
5
5
import com .google .firebase .firestore .DocumentReference ;
6
6
import com .google .firebase .firestore .DocumentSnapshot ;
7
+ import com .google .firebase .firestore .ListenerRegistration ;
7
8
import com .google .firebase .firestore .Query ;
8
- import com .google .firebase .firestore .QueryDocumentSnapshot ;
9
9
import com .google .firebase .firestore .QuerySnapshot ;
10
10
11
11
import org .junit .Before ;
17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .Collections ;
20
+ import java .util .HashMap ;
20
21
import java .util .Iterator ;
21
22
import java .util .List ;
22
23
23
24
import io .reactivex .observers .TestObserver ;
24
25
26
+ import static durdinapps .rxfirebase2 .RxTestUtil .eventSnapshotListener ;
27
+ import static durdinapps .rxfirebase2 .RxTestUtil .setupOfflineTask ;
25
28
import static durdinapps .rxfirebase2 .RxTestUtil .setupTask ;
29
+ import static durdinapps .rxfirebase2 .RxTestUtil .testOnCompleteListener ;
26
30
import static durdinapps .rxfirebase2 .RxTestUtil .testOnSuccessListener ;
27
31
import static org .mockito .Mockito .verify ;
28
32
import static org .mockito .Mockito .when ;
@@ -71,13 +75,21 @@ public class RxFirestoreTest {
71
75
@ Mock
72
76
private Task <DocumentSnapshot > documentSnapshotTask ;
73
77
78
+ @ Mock
79
+ private Task <DocumentReference > documentRefTask ;
80
+
74
81
@ Mock
75
82
private Task <QuerySnapshot > queryResultTask ;
76
83
77
84
@ Mock
78
85
private Task <QuerySnapshot > emptyQueryResultTask ;
79
86
87
+ @ Mock
88
+ private ListenerRegistration registration ;
89
+
80
90
91
+ private HashMap <String , Object > updateMap = new HashMap <>();
92
+ private ChildDocData setData = new ChildDocData ();
81
93
private ChildDocData childData = new ChildDocData ();
82
94
private List <ChildDocData > childDataList = new ArrayList <>();
83
95
@@ -90,13 +102,17 @@ public void setup() {
90
102
setupTask (queryResultTask );
91
103
setupTask (emptyQueryResultTask );
92
104
setupTask (mockVoidTask );
105
+ setupOfflineTask (documentReference , registration );
93
106
94
107
when (documentReference .get ()).thenReturn (documentSnapshotTask );
95
108
when (emptyDocumentReference .get ()).thenReturn (emptyDocumentSnapshotTask );
96
109
when (collectionReference .get ()).thenReturn (queryResultTask );
97
110
when (emptyCollectionReference .get ()).thenReturn (emptyQueryResultTask );
98
111
when (queryReference .get ()).thenReturn (queryResultTask );
99
112
when (emptyQueryReference .get ()).thenReturn (emptyQueryResultTask );
113
+ when (documentReference .delete ()).thenReturn (mockVoidTask );
114
+ when (documentReference .update (updateMap )).thenReturn (mockVoidTask );
115
+ when (collectionReference .add (setData )).thenReturn (documentRefTask );
100
116
when (documentSnapshot .toObject (ChildDocData .class )).thenReturn (childData );
101
117
when (documentSnapshot .exists ()).thenReturn (true ); //This snapshots exist
102
118
when (documentSnapshot .exists ()).thenReturn (true ); //This snapshots exist
@@ -308,6 +324,85 @@ public void testMappedGetEmptyQuery() throws InterruptedException {
308
324
.assertComplete ();
309
325
}
310
326
327
+ @ Test
328
+ public void testSetDocumentOffline () throws InterruptedException {
329
+ TestObserver <Void > testObserver = RxFirestore
330
+ .setDocumentOffline (documentReference , setData )
331
+ .test ();
332
+
333
+ eventSnapshotListener .getValue ().onEvent (documentSnapshot , null );
334
+
335
+ verify (documentReference ).set (setData );
336
+
337
+ testObserver
338
+ .assertNoErrors ()
339
+ .assertComplete ();
340
+ }
341
+
342
+ @ Test
343
+ public void testUpdateDocument () throws InterruptedException {
344
+
345
+ TestObserver <Void > storageTestObserver =
346
+ RxFirestore .updateDocument (documentReference , updateMap )
347
+ .test ();
348
+
349
+ testOnCompleteListener .getValue ().onComplete (mockVoidTask );
350
+
351
+ verify (documentReference ).update (updateMap );
352
+
353
+ storageTestObserver .assertNoErrors ()
354
+ .assertComplete ()
355
+ .dispose ();
356
+ }
357
+
358
+
359
+ @ Test
360
+ public void testUpdateDocumentOffline () throws InterruptedException {
361
+ TestObserver <Void > testObserver = RxFirestore
362
+ .updateDocumentOffline (documentReference , updateMap )
363
+ .test ();
364
+
365
+ eventSnapshotListener .getValue ().onEvent (documentSnapshot , null );
366
+
367
+ verify (documentReference ).update (updateMap );
368
+
369
+ testObserver
370
+ .assertNoErrors ()
371
+ .assertComplete ();
372
+ }
373
+
374
+
375
+ @ Test
376
+ public void testDeleteDocument () throws InterruptedException {
377
+
378
+ TestObserver <Void > storageTestObserver =
379
+ RxFirestore .deleteDocument (documentReference )
380
+ .test ();
381
+
382
+ testOnCompleteListener .getValue ().onComplete (mockVoidTask );
383
+
384
+ verify (documentReference ).delete ();
385
+
386
+ storageTestObserver .assertNoErrors ()
387
+ .assertComplete ()
388
+ .dispose ();
389
+ }
390
+
391
+ @ Test
392
+ public void testDeleteDocumentOffline () throws InterruptedException {
393
+ TestObserver <Void > testObserver = RxFirestore
394
+ .deleteDocumentOffline (documentReference )
395
+ .test ();
396
+
397
+ eventSnapshotListener .getValue ().onEvent (documentSnapshot , null );
398
+ verify (documentReference ).delete ();
399
+
400
+ testObserver
401
+ .assertNoErrors ()
402
+ .assertComplete ();
403
+ }
404
+
405
+
311
406
class ChildDocData {
312
407
int id ;
313
408
String str ;
0 commit comments