Skip to content

Commit 0960234

Browse files
authored
test(firestore): create FirestorePagingSourceTest (#1989)
1 parent 7643930 commit 0960234

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.firebase.ui.firestore.paging;
2+
3+
import com.google.android.gms.tasks.Tasks;
4+
import com.google.firebase.firestore.DocumentSnapshot;
5+
import com.google.firebase.firestore.Query;
6+
import com.google.firebase.firestore.QuerySnapshot;
7+
import com.google.firebase.firestore.Source;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.mockito.Mock;
13+
import org.mockito.MockitoAnnotations;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
import androidx.paging.PagingSource;
19+
import androidx.paging.PagingSource.LoadParams.Append;
20+
import androidx.paging.PagingSource.LoadParams.Refresh;
21+
import androidx.paging.PagingSource.LoadResult.Page;
22+
import androidx.test.ext.junit.runners.AndroidJUnit4;
23+
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertTrue;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.ArgumentMatchers.anyLong;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
@RunWith(AndroidJUnit4.class)
32+
public class FirestorePagingSourceTest {
33+
34+
@Mock
35+
Query mMockQuery;
36+
37+
ArrayList<DocumentSnapshot> mMockSnapshots = new ArrayList<>();
38+
Exception mMockException = new Exception("Could not load Data");
39+
40+
@Before
41+
public void setUp() {
42+
MockitoAnnotations.initMocks(this);
43+
initMockQuery();
44+
45+
for (int i = 0; i < 2; i++) {
46+
mMockSnapshots.add(mock(DocumentSnapshot.class));
47+
}
48+
}
49+
50+
@Test
51+
public void testLoadInitial_success() {
52+
FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT);
53+
mockQuerySuccess(mMockSnapshots);
54+
Page<PageKey, DocumentSnapshot> expected = new Page<>(mMockSnapshots, null, new PageKey(null, null));
55+
56+
Refresh<PageKey> refreshRequest = new Refresh<>(null, 2, false);
57+
PagingSource.LoadResult<PageKey, DocumentSnapshot> actual =
58+
pagingSource.loadSingle(refreshRequest).blockingGet();
59+
60+
assertTrue(actual instanceof Page);
61+
assertEquals(expected, actual);
62+
}
63+
64+
@Test
65+
public void testLoadInitial_failure() {
66+
FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT);
67+
mockQueryFailure(mMockException);
68+
PagingSource.LoadResult.Error<PageKey, DocumentSnapshot> expected =
69+
new PagingSource.LoadResult.Error<>(mMockException);
70+
71+
Refresh<PageKey> refreshRequest = new Refresh<>(null, 2, false);
72+
PagingSource.LoadResult<PageKey, DocumentSnapshot> actual =
73+
pagingSource.loadSingle(refreshRequest).blockingGet();
74+
75+
assertEquals(expected, actual);
76+
}
77+
78+
@Test
79+
public void testLoadAfter_success() {
80+
FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT);
81+
mockQuerySuccess(mMockSnapshots);
82+
PageKey pageKey = new PageKey(null, null);
83+
Page<PageKey, DocumentSnapshot> expected = new Page<>(mMockSnapshots, null, pageKey);
84+
85+
Append<PageKey> appendRequest = new Append<>(pageKey, 2, false);
86+
PagingSource.LoadResult<PageKey, DocumentSnapshot> actual =
87+
pagingSource.loadSingle(appendRequest).blockingGet();
88+
89+
assertTrue(actual instanceof Page);
90+
assertEquals(expected, actual);
91+
}
92+
93+
@Test
94+
public void testLoadAfter_failure() {
95+
FirestorePagingSource pagingSource = new FirestorePagingSource(mMockQuery, Source.DEFAULT);
96+
mockQueryFailure(mMockException);
97+
PageKey pageKey = new PageKey(null, null);
98+
PagingSource.LoadResult.Error<PageKey, DocumentSnapshot> expected =
99+
new PagingSource.LoadResult.Error<>(mMockException);
100+
101+
Append<PageKey> appendRequest = new Append<>(pageKey, 2, false);
102+
PagingSource.LoadResult<PageKey, DocumentSnapshot> actual =
103+
pagingSource.loadSingle(appendRequest).blockingGet();
104+
105+
assertEquals(expected, actual);
106+
}
107+
108+
private void initMockQuery() {
109+
when(mMockQuery.startAfter(any(DocumentSnapshot.class))).thenReturn(mMockQuery);
110+
when(mMockQuery.endBefore(any(DocumentSnapshot.class))).thenReturn(mMockQuery);
111+
when(mMockQuery.limit(anyLong())).thenReturn(mMockQuery);
112+
}
113+
114+
private void mockQuerySuccess(List<DocumentSnapshot> snapshots) {
115+
QuerySnapshot mockSnapshot = mock(QuerySnapshot.class);
116+
when(mockSnapshot.getDocuments()).thenReturn(snapshots);
117+
118+
when(mMockQuery.get(Source.DEFAULT)).thenReturn(Tasks.forResult(mockSnapshot));
119+
}
120+
121+
private void mockQueryFailure(Exception exception) {
122+
when(mMockQuery.get(Source.DEFAULT)).thenReturn(Tasks.forException(exception));
123+
}
124+
}

firestore/src/main/java/com/firebase/ui/firestore/paging/PageKey.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ public Query getPageQuery(@NonNull Query baseQuery, int size) {
3838
return pageQuery;
3939
}
4040

41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) return true;
44+
if (o == null || getClass() != o.getClass()) return false;
45+
PageKey key = (PageKey) o;
46+
if (mStartAfter == null && key.mStartAfter == null)
47+
return true;
48+
if (mEndBefore == null && key.mEndBefore == null)
49+
return true;
50+
return mStartAfter.getId() == key.mStartAfter.getId() &&
51+
mEndBefore.getId() == key.mEndBefore.getId();
52+
}
53+
4154
@Override
4255
@NonNull
4356
public String toString() {

0 commit comments

Comments
 (0)