Skip to content

Commit 7557d7c

Browse files
authored
test: Verify aggregate query error message when missing index (#1073)
1 parent b98e38d commit 7557d7c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryAggregationsTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.Assume.assumeFalse;
2929
import static org.junit.Assume.assumeTrue;
3030

31+
import com.google.api.core.ApiFuture;
3132
import com.google.cloud.firestore.*;
3233
import java.util.Map;
3334
import java.util.concurrent.ExecutionException;
@@ -1081,4 +1082,29 @@ public void aggregateInequalityFilterNoOrderByDocumentSnapshotReference2() throw
10811082
AggregateQuerySnapshot snapshot = query.get().get();
10821083
assertThat(snapshot.get(sum("num"))).isEqualTo(7);
10831084
}
1085+
1086+
@Test
1087+
public void aggregateQueryShouldFailWithMessageWithConsoleLinkIfMissingIndex() {
1088+
assumeFalse(
1089+
"Skip this test when running against the Firestore emulator because the Firestore emulator "
1090+
+ "does not use indexes and never fails with a 'missing index' error",
1091+
isRunningAgainstFirestoreEmulator(firestore));
1092+
1093+
CollectionReference collection = testCollection();
1094+
Query compositeIndexQuery = collection.whereEqualTo("field1", 42).whereLessThan("field2", 99);
1095+
AggregateQuery compositeIndexAggregateQuery =
1096+
compositeIndexQuery.aggregate(
1097+
AggregateField.count(), AggregateField.sum("pages"), AggregateField.average("pages"));
1098+
ApiFuture<AggregateQuerySnapshot> future = compositeIndexAggregateQuery.get();
1099+
1100+
ExecutionException executionException = assertThrows(ExecutionException.class, future::get);
1101+
1102+
Throwable throwable = executionException.getCause();
1103+
assertThat(throwable).hasMessageThat().ignoringCase().contains("index");
1104+
// TODO(b/316359394) Remove this check for the default databases once cl/582465034 is rolled out
1105+
// to production.
1106+
if (collection.getFirestore().getOptions().getDatabaseId().equals("(default)")) {
1107+
assertThat(throwable).hasMessageThat().contains("https://console.firebase.google.com");
1108+
}
1109+
}
10841110
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryCountTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.truth.Truth.assertThat;
2323
import static java.util.Collections.singletonMap;
2424
import static org.junit.Assert.assertThrows;
25+
import static org.junit.Assume.assumeFalse;
2526
import static org.junit.Assume.assumeTrue;
2627

2728
import com.google.api.core.ApiFuture;
@@ -369,6 +370,29 @@ public void aggregateQueryInATransactionShouldRespectReadTime() throws Exception
369370
assertThat(transactionCount).isEqualTo(5);
370371
}
371372

373+
@Test
374+
public void countQueryShouldFailWithMessageWithConsoleLinkIfMissingIndex() {
375+
assumeFalse(
376+
"Skip this test when running against the Firestore emulator because the Firestore emulator "
377+
+ "does not use indexes and never fails with a 'missing index' error",
378+
isRunningAgainstFirestoreEmulator(firestore));
379+
380+
CollectionReference collection = createEmptyCollection();
381+
Query compositeIndexQuery = collection.whereEqualTo("field1", 42).whereLessThan("field2", 99);
382+
AggregateQuery compositeIndexCountQuery = compositeIndexQuery.count();
383+
ApiFuture<AggregateQuerySnapshot> future = compositeIndexCountQuery.get();
384+
385+
ExecutionException executionException = assertThrows(ExecutionException.class, future::get);
386+
387+
Throwable throwable = executionException.getCause();
388+
assertThat(throwable).hasMessageThat().ignoringCase().contains("index");
389+
// TODO(b/316359394) Remove this check for the default databases once cl/582465034 is rolled out
390+
// to production.
391+
if (collection.getFirestore().getOptions().getDatabaseId().equals("(default)")) {
392+
assertThat(throwable).hasMessageThat().contains("https://console.firebase.google.com");
393+
}
394+
}
395+
372396
private CollectionReference createEmptyCollection() {
373397
String collectionPath = "java-" + testName.getMethodName() + "-" + autoId();
374398
return firestore.collection(collectionPath);

0 commit comments

Comments
 (0)