|
15 | 15 | package com.google.firebase.firestore;
|
16 | 16 |
|
17 | 17 | import static com.google.firebase.firestore.AggregateField.average;
|
| 18 | +import static com.google.firebase.firestore.AggregateField.count; |
18 | 19 | import static com.google.firebase.firestore.AggregateField.sum;
|
| 20 | +import static com.google.firebase.firestore.testutil.IntegrationTestUtil.isRunningAgainstEmulator; |
19 | 21 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
|
20 | 22 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
|
21 | 23 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
|
|
30 | 32 | import static org.junit.Assert.assertNotEquals;
|
31 | 33 | import static org.junit.Assert.assertNotNull;
|
32 | 34 | import static org.junit.Assert.assertNull;
|
| 35 | +import static org.junit.Assert.assertThrows; |
33 | 36 | import static org.junit.Assert.assertTrue;
|
| 37 | +import static org.junit.Assume.assumeFalse; |
34 | 38 |
|
35 | 39 | import androidx.test.ext.junit.runners.AndroidJUnit4;
|
| 40 | +import com.google.android.gms.tasks.Task; |
| 41 | +import com.google.common.truth.Truth; |
36 | 42 | import com.google.firebase.firestore.testutil.IntegrationTestUtil;
|
| 43 | +import java.util.Collections; |
37 | 44 | import java.util.Map;
|
38 | 45 | import org.junit.After;
|
39 | 46 | import org.junit.Ignore;
|
@@ -211,6 +218,44 @@ public void testAggregateAvgQueryEquals() {
|
211 | 218 | assertNotEquals(query2.hashCode(), query3.hashCode());
|
212 | 219 | }
|
213 | 220 |
|
| 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 | + |
214 | 259 | @Test
|
215 | 260 | public void testCanRunCountUsingAggregationMethod() {
|
216 | 261 | CollectionReference collection = testCollectionWithDocs(testDocs1);
|
@@ -286,15 +331,16 @@ public void testCanGetMultipleAggregationsInTheSameQuery() {
|
286 | 331 | assertEquals((Double) 75.0, snapshot.getDouble(average("pages")));
|
287 | 332 | }
|
288 | 333 |
|
289 |
| - @Test |
290 |
| - public void testTerminateDoesNotCrashWithFlyingAggregateQuery() { |
291 |
| - CollectionReference collection = testCollectionWithDocs(testDocs1); |
| 334 | + @Test |
| 335 | + public void testTerminateDoesNotCrashWithFlyingAggregateQuery() { |
| 336 | + CollectionReference collection = testCollectionWithDocs(testDocs1); |
292 | 337 |
|
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); |
295 | 341 |
|
296 |
| - waitFor(collection.firestore.terminate()); |
297 |
| - } |
| 342 | + waitFor(collection.firestore.terminate()); |
| 343 | + } |
298 | 344 |
|
299 | 345 | @Test
|
300 | 346 | public void testCanGetCorrectTypeForSum() {
|
@@ -962,4 +1008,23 @@ public void testPerformsAverageOnlyOnNumericFields() {
|
962 | 1008 | assertEquals(snapshot.get(AggregateField.count()), 4L);
|
963 | 1009 | }
|
964 | 1010 |
|
| 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 | + } |
965 | 1030 | }
|
0 commit comments