Skip to content

Commit 1c7dd64

Browse files
Clean up RemoteDocumentCacheTestCase (#3194)
Small PR that extracts common data elements from the tests.
1 parent 2b7812e commit 1c7dd64

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

firebase-firestore/src/test/java/com/google/firebase/firestore/local/RemoteDocumentCacheTestCase.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.firestore.local;
1616

17+
import static com.google.common.truth.Truth.assertThat;
1718
import static com.google.firebase.firestore.testutil.TestUtil.assertDoesNotThrow;
1819
import static com.google.firebase.firestore.testutil.TestUtil.deletedDoc;
1920
import static com.google.firebase.firestore.testutil.TestUtil.doc;
@@ -22,7 +23,6 @@
2223
import static com.google.firebase.firestore.testutil.TestUtil.path;
2324
import static com.google.firebase.firestore.testutil.TestUtil.values;
2425
import static com.google.firebase.firestore.testutil.TestUtil.version;
25-
import static java.util.Arrays.asList;
2626
import static org.junit.Assert.assertEquals;
2727
import static org.junit.Assert.assertFalse;
2828
import static org.junit.Assert.assertNotEquals;
@@ -54,6 +54,7 @@
5454
* </ol>
5555
*/
5656
abstract class RemoteDocumentCacheTestCase {
57+
private final Map<String, Object> DOC_DATA = map("data", 2);
5758

5859
private Persistence persistence;
5960
private RemoteDocumentCache remoteDocumentCache;
@@ -127,7 +128,7 @@ public void testSetAndReadLotsOfDocuments() {
127128
List<String> paths = new ArrayList<>();
128129
Map<DocumentKey, MutableDocument> expected = new HashMap<>();
129130
for (int i = 0; i < lotsOfDocuments; i++) {
130-
String path = "foo/" + String.valueOf(i);
131+
String path = "foo/" + i;
131132
paths.add(path);
132133
expected.put(DocumentKey.fromPathString(path), addTestDocumentAtPath(path));
133134
}
@@ -173,7 +174,6 @@ public void testRemoveNonExistentDocument() {
173174
public void testDocumentsMatchingQuery() {
174175
// TODO: This just verifies that we do a prefix scan against the
175176
// query path. We'll need more tests once we add index support.
176-
Map<String, Object> docData = map("data", 2);
177177
addTestDocumentAtPath("a/1");
178178
addTestDocumentAtPath("b/1");
179179
addTestDocumentAtPath("b/2");
@@ -182,41 +182,47 @@ public void testDocumentsMatchingQuery() {
182182
Query query = Query.atPath(path("b"));
183183
ImmutableSortedMap<DocumentKey, MutableDocument> results =
184184
remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.NONE);
185-
List<MutableDocument> expected = asList(doc("b/1", 42, docData), doc("b/2", 42, docData));
186-
// assertEquals(expected, values(results));
185+
assertThat(values(results)).containsExactly(doc("b/1", 42, DOC_DATA), doc("b/2", 42, DOC_DATA));
186+
}
187+
188+
@Test
189+
public void testDocumentsMatchingQueryExcludesSubcollections() {
190+
addTestDocumentAtPath("a/1");
191+
addTestDocumentAtPath("a/1/b/1");
192+
addTestDocumentAtPath("a/2");
193+
194+
Query query = Query.atPath(path("a"));
195+
ImmutableSortedMap<DocumentKey, MutableDocument> results =
196+
remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.NONE);
197+
assertThat(values(results)).containsExactly(doc("a/1", 42, DOC_DATA), doc("a/2", 42, DOC_DATA));
187198
}
188199

189200
@Test
190201
public void testDocumentsMatchingQuerySinceReadTimeAndSeconds() {
191-
Map<String, Object> docData = map("data", 2);
192202
addTestDocumentAtPath("b/old", /* updateTime= */ 1, /* readTime= */ 11);
193203
addTestDocumentAtPath("b/current", /* updateTime= */ 2, /* readTime= = */ 12);
194204
addTestDocumentAtPath("b/new", /* updateTime= */ 3, /* readTime= = */ 13);
195205

196206
Query query = Query.atPath(path("b"));
197207
ImmutableSortedMap<DocumentKey, MutableDocument> results =
198208
remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.create(version(12)));
199-
List<MutableDocument> expected = asList(doc("b/new", 3, docData));
200-
assertEquals(expected, values(results));
209+
assertThat(values(results)).containsExactly(doc("b/new", 3, DOC_DATA));
201210
}
202211

203212
@Test
204213
public void testDocumentsMatchingQuerySinceReadTimeAndNanoseconds() {
205-
Map<String, Object> docData = map("data", 2);
206-
add(doc("b/old", 1, docData), version(1, 1));
207-
add(doc("b/current", 1, docData), version(1, 2));
208-
add(doc("b/new", 1, docData), version(1, 3));
214+
add(doc("b/old", 1, DOC_DATA), version(1, 1));
215+
add(doc("b/current", 1, DOC_DATA), version(1, 2));
216+
add(doc("b/new", 1, DOC_DATA), version(1, 3));
209217

210218
Query query = Query.atPath(path("b"));
211219
ImmutableSortedMap<DocumentKey, MutableDocument> results =
212220
remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.create(version(1, 2)));
213-
List<MutableDocument> expected = asList(doc("b/new", 1, docData));
214-
assertEquals(expected, values(results));
221+
assertThat(values(results)).containsExactly(doc("b/new", 1, DOC_DATA));
215222
}
216223

217224
@Test
218225
public void testDocumentsMatchingQuerySinceReadTimeAndDocumentKey() {
219-
Map<String, Object> docData = map("data", 2);
220226
addTestDocumentAtPath("b/a", /* updateTime= */ 1, /* readTime= */ 11);
221227
addTestDocumentAtPath("b/b", /* updateTime= */ 2, /* readTime= = */ 11);
222228
addTestDocumentAtPath("b/c", /* updateTime= */ 3, /* readTime= = */ 11);
@@ -226,21 +232,18 @@ public void testDocumentsMatchingQuerySinceReadTimeAndDocumentKey() {
226232
ImmutableSortedMap<DocumentKey, MutableDocument> results =
227233
remoteDocumentCache.getAllDocumentsMatchingQuery(
228234
query, IndexOffset.create(version(11), key("b/b")));
229-
List<MutableDocument> expected = asList(doc("b/c", 3, docData), doc("b/d", 4, docData));
230-
assertEquals(expected, values(results));
235+
assertThat(values(results)).containsExactly(doc("b/c", 3, DOC_DATA), doc("b/d", 4, DOC_DATA));
231236
}
232237

233238
@Test
234239
public void testDocumentsMatchingUsesReadTimeNotUpdateTime() {
235-
Map<String, Object> docData = map("data", 2);
236240
addTestDocumentAtPath("b/old", /* updateTime= */ 1, /* readTime= */ 2);
237241
addTestDocumentAtPath("b/new", /* updateTime= */ 2, /* readTime= */ 1);
238242

239243
Query query = Query.atPath(path("b"));
240244
ImmutableSortedMap<DocumentKey, MutableDocument> results =
241245
remoteDocumentCache.getAllDocumentsMatchingQuery(query, IndexOffset.create(version(1)));
242-
List<MutableDocument> expected = asList(doc("b/old", 1, docData));
243-
assertEquals(expected, values(results));
246+
assertThat(values(results)).containsExactly(doc("b/old", 1, DOC_DATA));
244247
}
245248

246249
@Test

0 commit comments

Comments
 (0)