Skip to content

Commit a9d190d

Browse files
committed
Add test coverage
1 parent 9024cf0 commit a9d190d

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/AggregationTest.java

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package com.google.firebase.firestore;
1616

1717
import static com.google.firebase.firestore.AggregateField.average;
18+
import static com.google.firebase.firestore.AggregateField.count;
1819
import static com.google.firebase.firestore.AggregateField.sum;
20+
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.isRunningAgainstEmulator;
1921
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
2022
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
2123
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
@@ -30,10 +32,15 @@
3032
import static org.junit.Assert.assertNotEquals;
3133
import static org.junit.Assert.assertNotNull;
3234
import static org.junit.Assert.assertNull;
35+
import static org.junit.Assert.assertThrows;
3336
import static org.junit.Assert.assertTrue;
37+
import static org.junit.Assume.assumeFalse;
3438

3539
import androidx.test.ext.junit.runners.AndroidJUnit4;
40+
import com.google.android.gms.tasks.Task;
41+
import com.google.common.truth.Truth;
3642
import com.google.firebase.firestore.testutil.IntegrationTestUtil;
43+
import java.util.Collections;
3744
import java.util.Map;
3845
import org.junit.After;
3946
import org.junit.Ignore;
@@ -211,6 +218,44 @@ public void testAggregateAvgQueryEquals() {
211218
assertNotEquals(query2.hashCode(), query3.hashCode());
212219
}
213220

221+
@Test
222+
public void testAggregateQueryNotEquals() {
223+
CollectionReference coll = testCollection("foo");
224+
225+
AggregateQuery query1 = coll.aggregate(AggregateField.count());
226+
AggregateQuery query2 = coll.aggregate(sum("baz"));
227+
AggregateQuery query3 = coll.aggregate(average("baz"));
228+
229+
assertFalse(query1.equals(query2));
230+
assertFalse(query2.equals(query3));
231+
assertFalse(query3.equals(query1));
232+
assertNotEquals(query1.hashCode(), query2.hashCode());
233+
assertNotEquals(query2.hashCode(), query3.hashCode());
234+
assertNotEquals(query3.hashCode(), query1.hashCode());
235+
236+
AggregateQuery query4 =
237+
coll.document("bar").collection("baz").whereEqualTo("a", 1).limit(100).aggregate(count());
238+
AggregateQuery query5 =
239+
coll.document("bar")
240+
.collection("baz")
241+
.whereEqualTo("a", 1)
242+
.limit(100)
243+
.aggregate(sum("baz"));
244+
AggregateQuery query6 =
245+
coll.document("bar")
246+
.collection("baz")
247+
.whereEqualTo("a", 1)
248+
.limit(100)
249+
.aggregate(average("baz"));
250+
251+
assertFalse(query4.equals(query5));
252+
assertFalse(query5.equals(query6));
253+
assertFalse(query6.equals(query4));
254+
assertNotEquals(query4.hashCode(), query5.hashCode());
255+
assertNotEquals(query5.hashCode(), query6.hashCode());
256+
assertNotEquals(query6.hashCode(), query4.hashCode());
257+
}
258+
214259
@Test
215260
public void testCanRunCountUsingAggregationMethod() {
216261
CollectionReference collection = testCollectionWithDocs(testDocs1);
@@ -286,15 +331,16 @@ public void testCanGetMultipleAggregationsInTheSameQuery() {
286331
assertEquals((Double) 75.0, snapshot.getDouble(average("pages")));
287332
}
288333

289-
@Test
290-
public void testTerminateDoesNotCrashWithFlyingAggregateQuery() {
291-
CollectionReference collection = testCollectionWithDocs(testDocs1);
334+
@Test
335+
public void testTerminateDoesNotCrashWithFlyingAggregateQuery() {
336+
CollectionReference collection = testCollectionWithDocs(testDocs1);
292337

293-
collection.aggregate(AggregateField.count(), sum("pages"), average("pages"))
294-
.get(AggregateSource.SERVER);
338+
collection
339+
.aggregate(AggregateField.count(), sum("pages"), average("pages"))
340+
.get(AggregateSource.SERVER);
295341

296-
waitFor(collection.firestore.terminate());
297-
}
342+
waitFor(collection.firestore.terminate());
343+
}
298344

299345
@Test
300346
public void testCanGetCorrectTypeForSum() {
@@ -962,4 +1008,23 @@ public void testPerformsAverageOnlyOnNumericFields() {
9621008
assertEquals(snapshot.get(AggregateField.count()), 4L);
9631009
}
9641010

1011+
@Test
1012+
public void testAggregateFailWithGoodMessageIfMissingIndex() {
1013+
assumeFalse(
1014+
"Skip this test when running against the Firestore emulator because the Firestore emulator "
1015+
+ "does not use indexes and never fails with a 'missing index' error",
1016+
isRunningAgainstEmulator());
1017+
1018+
CollectionReference collection = testCollectionWithDocs(Collections.emptyMap());
1019+
Query compositeIndexQuery = collection.whereEqualTo("field1", 42).whereLessThan("field2", 99);
1020+
AggregateQuery compositeIndexCountQuery =
1021+
compositeIndexQuery.aggregate(AggregateField.count(), sum("pages"), average("pages"));
1022+
Task<AggregateQuerySnapshot> task = compositeIndexCountQuery.get(AggregateSource.SERVER);
1023+
1024+
Throwable throwable = assertThrows(Throwable.class, () -> waitFor(task));
1025+
1026+
Throwable cause = throwable.getCause();
1027+
Truth.assertThat(cause).hasMessageThat().ignoringCase().contains("index");
1028+
Truth.assertThat(cause).hasMessageThat().contains("https://console.firebase.google.com");
1029+
}
9651030
}

firebase-firestore/src/main/java/com/google/firebase/firestore/Query.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ private void validateHasExplicitOrderByForLimitToLast() {
12251225
* not the documents' data, is downloaded. The returned query can even count the documents if the
12261226
* result set would be prohibitively large to download entirely (e.g. thousands of documents).
12271227
*
1228-
* @return a query that counts the documents in the result set of this query.
1228+
* @return The {@code AggregateQuery} that counts the documents in the result set of this query.
12291229
*/
12301230
@NonNull
12311231
public AggregateQuery count() {
@@ -1241,7 +1241,8 @@ public AggregateQuery count() {
12411241
* the documents if the result set would be prohibitively large to download entirely (e.g.
12421242
* thousands of documents).
12431243
*
1244-
* @return a query that performs aggregations on the documents in the result set of this query.
1244+
* @return The {@code AggregateQuery} that performs aggregations on the documents in the result
1245+
* set of this query.
12451246
*/
12461247
// TODO(sumavg): Remove the `hide` and scope annotations.
12471248
/** @hide */

0 commit comments

Comments
 (0)