FIRQueryTests.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRQuery+Internal.h"
  19. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  20. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  21. @interface FIRQueryTests : FSTIntegrationTestCase
  22. @end
  23. @implementation FIRQueryTests
  24. - (void)testLimitQueries {
  25. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  26. @"a" : @{@"k" : @"a"},
  27. @"b" : @{@"k" : @"b"},
  28. @"c" : @{@"k" : @"c"}
  29. }];
  30. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  31. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  32. }
  33. - (void)testLimitQueriesWithDescendingSortOrder {
  34. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  35. @"a" : @{@"k" : @"a", @"sort" : @0},
  36. @"b" : @{@"k" : @"b", @"sort" : @1},
  37. @"c" : @{@"k" : @"c", @"sort" : @1},
  38. @"d" : @{@"k" : @"d", @"sort" : @2},
  39. }];
  40. FIRQuerySnapshot *snapshot =
  41. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort"
  42. descending:YES] queryLimitedTo:2]];
  43. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  44. (@[ @{@"k" : @"d", @"sort" : @2}, @{@"k" : @"c", @"sort" : @1} ]));
  45. }
  46. - (void)testKeyOrderIsDescendingForDescendingInequality {
  47. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  48. @"a" : @{@"foo" : @42},
  49. @"b" : @{@"foo" : @42.0},
  50. @"c" : @{@"foo" : @42},
  51. @"d" : @{@"foo" : @21},
  52. @"e" : @{@"foo" : @21.0},
  53. @"f" : @{@"foo" : @66},
  54. @"g" : @{@"foo" : @66.0},
  55. }];
  56. FIRQuerySnapshot *snapshot =
  57. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo"
  58. isGreaterThan:@21] queryOrderedByField:@"foo"
  59. descending:YES]];
  60. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  61. }
  62. - (void)testUnaryFilterQueries {
  63. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  64. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  65. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  66. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  67. }];
  68. FIRQuerySnapshot *results =
  69. [self readDocumentSetForRef:[[collRef queryWhereField:@"null"
  70. isEqualTo:[NSNull null]] queryWhereField:@"nan"
  71. isEqualTo:@(NAN)]];
  72. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results),
  73. (@[ @{@"null" : [NSNull null], @"nan" : @(NAN)} ]));
  74. }
  75. - (void)testQueryWithFieldPaths {
  76. FIRCollectionReference *collRef = [self
  77. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  78. FIRQuery *query = [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  79. isLessThan:@3];
  80. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  81. descending:YES];
  82. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  83. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  84. }
  85. - (void)testQueryWithPredicate {
  86. FIRCollectionReference *collRef = [self
  87. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  88. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  89. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  90. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  91. descending:YES];
  92. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  93. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  94. }
  95. - (void)testFilterOnInfinity {
  96. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  97. @"a" : @{@"inf" : @(INFINITY)},
  98. @"b" : @{@"inf" : @(-INFINITY)}
  99. }];
  100. FIRQuerySnapshot *results = [self readDocumentSetForRef:[collRef queryWhereField:@"inf"
  101. isEqualTo:@(INFINITY)]];
  102. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  103. }
  104. - (void)testCanExplicitlySortByDocumentID {
  105. NSDictionary *testDocs = @{
  106. @"a" : @{@"key" : @"a"},
  107. @"b" : @{@"key" : @"b"},
  108. @"c" : @{@"key" : @"c"},
  109. };
  110. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  111. // Ideally this would be descending to validate it's different than
  112. // the default, but that requires an extra index
  113. FIRQuerySnapshot *docs =
  114. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  115. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  116. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  117. }
  118. - (void)testCanQueryByDocumentID {
  119. NSDictionary *testDocs = @{
  120. @"aa" : @{@"key" : @"aa"},
  121. @"ab" : @{@"key" : @"ab"},
  122. @"ba" : @{@"key" : @"ba"},
  123. @"bb" : @{@"key" : @"bb"},
  124. };
  125. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  126. FIRQuerySnapshot *docs =
  127. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  128. isEqualTo:@"ab"]];
  129. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  130. }
  131. - (void)testCanQueryByDocumentIDs {
  132. NSDictionary *testDocs = @{
  133. @"aa" : @{@"key" : @"aa"},
  134. @"ab" : @{@"key" : @"ab"},
  135. @"ba" : @{@"key" : @"ba"},
  136. @"bb" : @{@"key" : @"bb"},
  137. };
  138. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  139. FIRQuerySnapshot *docs =
  140. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  141. isEqualTo:@"ab"]];
  142. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  143. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  144. isGreaterThan:@"aa"]
  145. queryWhereFieldPath:[FIRFieldPath documentID]
  146. isLessThanOrEqualTo:@"ba"]];
  147. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  148. }
  149. - (void)testCanQueryByDocumentIDsUsingRefs {
  150. NSDictionary *testDocs = @{
  151. @"aa" : @{@"key" : @"aa"},
  152. @"ab" : @{@"key" : @"ab"},
  153. @"ba" : @{@"key" : @"ba"},
  154. @"bb" : @{@"key" : @"bb"},
  155. };
  156. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  157. FIRQuerySnapshot *docs = [self
  158. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  159. isEqualTo:[collection documentWithPath:@"ab"]]];
  160. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  161. docs = [self
  162. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  163. isGreaterThan:[collection documentWithPath:@"aa"]]
  164. queryWhereFieldPath:[FIRFieldPath documentID]
  165. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  166. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  167. }
  168. - (void)testWatchSurvivesNetworkDisconnect {
  169. XCTestExpectation *testExpectiation =
  170. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  171. FIRCollectionReference *collectionRef = [self collectionRef];
  172. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  173. FIRFirestore *firestore = collectionRef.firestore;
  174. [collectionRef
  175. addSnapshotListenerWithIncludeMetadataChanges:YES
  176. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  177. XCTAssertNil(error);
  178. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  179. [testExpectiation fulfill];
  180. }
  181. }];
  182. [firestore disableNetworkWithCompletion:^(NSError *error) {
  183. XCTAssertNil(error);
  184. [docRef setData:@{@"foo" : @"bar"}];
  185. [firestore enableNetworkWithCompletion:^(NSError *error) {
  186. XCTAssertNil(error);
  187. }];
  188. }];
  189. [self awaitExpectations];
  190. }
  191. - (void)testQueriesFireFromCacheWhenOffline {
  192. NSDictionary *testDocs = @{
  193. @"a" : @{@"foo" : @1},
  194. };
  195. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  196. id<FIRListenerRegistration> registration = [collection
  197. addSnapshotListenerWithIncludeMetadataChanges:YES
  198. listener:self.eventAccumulator.valueEventHandler];
  199. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  200. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  201. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  202. [self disableNetwork];
  203. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  204. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  205. [self enableNetwork];
  206. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  207. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  208. [registration remove];
  209. }
  210. - (void)testDocumentChangesUseNSNotFound {
  211. NSDictionary *testDocs = @{
  212. @"a" : @{@"foo" : @1},
  213. };
  214. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  215. id<FIRListenerRegistration> registration =
  216. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  217. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  218. XCTAssertEqual(querySnap.documentChanges.count, 1);
  219. FIRDocumentChange *change = querySnap.documentChanges[0];
  220. XCTAssertEqual(change.oldIndex, NSNotFound);
  221. XCTAssertEqual(change.newIndex, 0);
  222. FIRDocumentReference *doc = change.document.reference;
  223. [self deleteDocumentRef:doc];
  224. querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
  225. XCTAssertEqual(querySnap.documentChanges.count, 1);
  226. change = querySnap.documentChanges[0];
  227. XCTAssertEqual(change.oldIndex, 0);
  228. XCTAssertEqual(change.newIndex, NSNotFound);
  229. [registration remove];
  230. }
  231. - (void)testCanHaveMultipleMutationsWhileOffline {
  232. FIRCollectionReference *col = [self collectionRef];
  233. // set a few docs to known values
  234. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  235. [self writeAllDocuments:initialDocs toCollection:col];
  236. // go offline for the rest of this test
  237. [self disableNetwork];
  238. // apply *multiple* mutations while offline
  239. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  240. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  241. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  242. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  243. @{@"key1b" : @"value1b"},
  244. @{@"key2b" : @"value2b"},
  245. ]));
  246. }
  247. - (void)testQueriesCanUseArrayContainsFilters {
  248. NSDictionary *testDocs = @{
  249. @"a" : @{@"array" : @[ @42 ]},
  250. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  251. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  252. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]}
  253. };
  254. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  255. // Search for 42
  256. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  257. arrayContains:@42]];
  258. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  259. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  260. // NOTE: The backend doesn't currently support null, NaN, objects, or arrays, so there isn't much
  261. // of anything else interesting to test.
  262. }
  263. - (void)testQueriesCanUseInFilters {
  264. // TODO(b/138855186): Re-enable in prod once feature lands in backend.
  265. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  266. NSDictionary *testDocs = @{
  267. @"a" : @{@"zip" : @98101},
  268. @"b" : @{@"zip" : @91102},
  269. @"c" : @{@"zip" : @98103},
  270. @"d" : @{@"zip" : @[ @98101 ]},
  271. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  272. @"f" : @{@"zip" : @{@"code" : @500}}
  273. };
  274. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  275. // Search for zips matching [98101, 98103].
  276. FIRQuerySnapshot *snapshot =
  277. [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ @98101, @98103 ]]];
  278. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"], testDocs[@"c"] ]));
  279. // With objects
  280. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  281. in:@[ @{@"code" : @500} ]]];
  282. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  283. }
  284. - (void)testQueriesCanUseInFiltersWithDocIds {
  285. // TODO(b/138855186): Re-enable in prod once feature lands in backend.
  286. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  287. NSDictionary *testDocs = @{
  288. @"aa" : @{@"key" : @"aa"},
  289. @"ab" : @{@"key" : @"ab"},
  290. @"ba" : @{@"key" : @"ba"},
  291. @"bb" : @{@"key" : @"bb"},
  292. };
  293. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  294. FIRQuerySnapshot *snapshot =
  295. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  296. in:@[ @"aa", @"ab" ]]];
  297. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  298. }
  299. - (void)testQueriesCanUseArrayContainsAnyFilters {
  300. // TODO(b/138855186): Re-enable in prod once feature lands in backend.
  301. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  302. NSDictionary *testDocs = @{
  303. @"a" : @{@"array" : @[ @42 ]},
  304. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  305. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  306. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  307. @"e" : @{@"array" : @[ @43 ]},
  308. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  309. @"g" : @{@"array" : @42},
  310. };
  311. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  312. // Search for zips matching [42, 43].
  313. FIRQuerySnapshot *snapshot = [self
  314. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  315. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  316. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  317. // With objects.
  318. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  319. arrayContainsAny:@[ @{@"a" : @42} ]]];
  320. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  321. testDocs[@"f"],
  322. ]));
  323. }
  324. - (void)testCollectionGroupQueries {
  325. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  326. // for predictable ordering.
  327. NSString *collectionGroup = [NSString
  328. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  329. NSArray *docPaths = @[
  330. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  331. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  332. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  333. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  334. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  335. @"abc/${collectionGroup}"
  336. ];
  337. FIRWriteBatch *batch = [self.db batch];
  338. for (NSString *docPath in docPaths) {
  339. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  340. withString:collectionGroup];
  341. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  342. }
  343. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  344. [batch commitWithCompletion:^(NSError *error) {
  345. XCTAssertNil(error);
  346. [expectation fulfill];
  347. }];
  348. [self awaitExpectations];
  349. FIRQuerySnapshot *querySnapshot =
  350. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  351. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  352. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  353. }
  354. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  355. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  356. // for predictable ordering.
  357. NSString *collectionGroup = [NSString
  358. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  359. NSArray *docPaths = @[
  360. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  361. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  362. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  363. ];
  364. FIRWriteBatch *batch = [self.db batch];
  365. for (NSString *docPath in docPaths) {
  366. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  367. withString:collectionGroup];
  368. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  369. }
  370. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  371. [batch commitWithCompletion:^(NSError *error) {
  372. XCTAssertNil(error);
  373. [expectation fulfill];
  374. }];
  375. [self awaitExpectations];
  376. FIRQuerySnapshot *querySnapshot = [self
  377. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  378. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  379. queryStartingAfterValues:@[ @"a/b" ]]
  380. queryEndingBeforeValues:@[
  381. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  382. ]]];
  383. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  384. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  385. }
  386. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  387. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  388. // for predictable ordering.
  389. NSString *collectionGroup = [NSString
  390. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  391. NSArray *docPaths = @[
  392. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  393. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  394. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  395. ];
  396. FIRWriteBatch *batch = [self.db batch];
  397. for (NSString *docPath in docPaths) {
  398. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  399. withString:collectionGroup];
  400. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  401. }
  402. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  403. [batch commitWithCompletion:^(NSError *error) {
  404. XCTAssertNil(error);
  405. [expectation fulfill];
  406. }];
  407. [self awaitExpectations];
  408. FIRQuerySnapshot *querySnapshot = [self
  409. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  410. queryWhereFieldPath:[FIRFieldPath documentID]
  411. isGreaterThanOrEqualTo:@"a/b"]
  412. queryWhereFieldPath:[FIRFieldPath documentID]
  413. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  414. collectionGroup]]];
  415. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  416. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  417. }
  418. @end