Skip to content

Commit a8264f0

Browse files
authored
Firestore: Add test that verifies aggregate query error message when missing index (#12189)
1 parent 9f8909a commit a8264f0

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Firestore/Example/Tests/Integration/API/FIRAggregateTests.mm

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,4 +1093,45 @@ - (void)testPerformsAverageOnlyOnNumericFields {
10931093
[[NSNumber numberWithDouble:5] doubleValue]);
10941094
}
10951095

1096+
- (void)testFailWithMessageWithConsoleLinkIfMissingIndex {
1097+
XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
1098+
"Skip this test when running against the Firestore emulator because the Firestore "
1099+
"emulator does not use indexes and never fails with a 'missing index' error.");
1100+
1101+
FIRCollectionReference* testCollection = [self collectionRef];
1102+
FIRQuery* compositeIndexQuery = [[testCollection queryWhereField:@"field1"
1103+
isEqualTo:@42] queryWhereField:@"field2"
1104+
isLessThan:@99];
1105+
FIRAggregateQuery* compositeIndexAggregateQuery = [compositeIndexQuery aggregate:@[
1106+
[FIRAggregateField aggregateFieldForCount],
1107+
[FIRAggregateField aggregateFieldForSumOfField:@"pages"],
1108+
[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]
1109+
]];
1110+
1111+
XCTestExpectation* queryCompletion = [self expectationWithDescription:@"query"];
1112+
[compositeIndexAggregateQuery
1113+
aggregationWithSource:FIRAggregateSourceServer
1114+
completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
1115+
XCTAssertNotNil(error);
1116+
if (error) {
1117+
NSString* errorDescription = [error localizedDescription];
1118+
XCTAssertTrue([errorDescription.lowercaseString containsString:@"index"],
1119+
"The NSError should have contained the word 'index' "
1120+
"(case-insensitive), but got: %@",
1121+
errorDescription);
1122+
// TODO(b/316359394) Remove this check for the default databases once
1123+
// cl/582465034 is rolled out to production.
1124+
if ([[FSTIntegrationTestCase databaseID] isEqualToString:@"(default)"]) {
1125+
XCTAssertTrue(
1126+
[errorDescription containsString:@"https://console.firebase.google.com"],
1127+
"The NSError should have contained the string "
1128+
"'https://console.firebase.google.com', but got: %@",
1129+
errorDescription);
1130+
}
1131+
}
1132+
XCTAssertNil(snapshot);
1133+
[queryCompletion fulfill];
1134+
}];
1135+
[self awaitExpectations];
1136+
}
10961137
@end

Firestore/Example/Tests/Integration/API/FIRCountTests.mm

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,42 @@ - (void)testFailWithoutNetwork {
227227
XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
228228
}
229229

230+
- (void)testFailWithMessageWithConsoleLinkIfMissingIndex {
231+
XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
232+
"Skip this test when running against the Firestore emulator because the Firestore "
233+
"emulator does not use indexes and never fails with a 'missing index' error.");
234+
235+
FIRCollectionReference* testCollection = [self collectionRef];
236+
FIRQuery* compositeIndexQuery = [[testCollection queryWhereField:@"field1"
237+
isEqualTo:@42] queryWhereField:@"field2"
238+
isLessThan:@99];
239+
FIRAggregateQuery* compositeIndexCountQuery = [compositeIndexQuery count];
240+
241+
XCTestExpectation* queryCompletion = [self expectationWithDescription:@"query"];
242+
[compositeIndexCountQuery
243+
aggregationWithSource:FIRAggregateSourceServer
244+
completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
245+
XCTAssertNotNil(error);
246+
if (error) {
247+
NSString* errorDescription = [error localizedDescription];
248+
XCTAssertTrue([errorDescription.lowercaseString containsString:@"index"],
249+
"The NSError should have contained the word 'index' "
250+
"(case-insensitive), but got: %@",
251+
errorDescription);
252+
// TODO(b/316359394) Remove this check for the default databases once
253+
// cl/582465034 is rolled out to production.
254+
if ([[FSTIntegrationTestCase databaseID] isEqualToString:@"(default)"]) {
255+
XCTAssertTrue(
256+
[errorDescription containsString:@"https://console.firebase.google.com"],
257+
"The NSError should have contained the string "
258+
"'https://console.firebase.google.com', but got: %@",
259+
errorDescription);
260+
}
261+
}
262+
XCTAssertNil(snapshot);
263+
[queryCompletion fulfill];
264+
}];
265+
[self awaitExpectations];
266+
}
267+
230268
@end

0 commit comments

Comments
 (0)