FIRQueryTests.mm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/FSTHelpers.h"
  21. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  22. @interface FIRQueryTests : FSTIntegrationTestCase
  23. @end
  24. @implementation FIRQueryTests
  25. - (void)testLimitQueries {
  26. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  27. @"a" : @{@"k" : @"a"},
  28. @"b" : @{@"k" : @"b"},
  29. @"c" : @{@"k" : @"c"}
  30. }];
  31. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  32. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  33. }
  34. - (void)testLimitQueriesWithDescendingSortOrder {
  35. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  36. @"a" : @{@"k" : @"a", @"sort" : @0},
  37. @"b" : @{@"k" : @"b", @"sort" : @1},
  38. @"c" : @{@"k" : @"c", @"sort" : @1},
  39. @"d" : @{@"k" : @"d", @"sort" : @2},
  40. }];
  41. FIRQuerySnapshot *snapshot =
  42. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort"
  43. descending:YES] queryLimitedTo:2]];
  44. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  45. (@[ @{@"k" : @"d", @"sort" : @2}, @{@"k" : @"c", @"sort" : @1} ]));
  46. }
  47. - (void)testLimitToLastMustAlsoHaveExplicitOrderBy {
  48. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{}];
  49. FIRQuery *query = [collRef queryLimitedToLast:2];
  50. FSTAssertThrows([query getDocumentsWithCompletion:^(FIRQuerySnapshot *, NSError *){
  51. }],
  52. @"limit(toLast:) queries require specifying at least one OrderBy() clause.");
  53. }
  54. // Two queries that mapped to the same target ID are referred to as
  55. // "mirror queries". An example for a mirror query is a limitToLast()
  56. // query and a limit() query that share the same backend Target ID.
  57. // Since limitToLast() queries are sent to the backend with a modified
  58. // orderBy() clause, they can map to the same target representation as
  59. // limit() query, even if both queries appear separate to the user.
  60. - (void)testListenUnlistenRelistenSequenceOfMirrorQueries {
  61. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  62. @"a" : @{@"k" : @"a", @"sort" : @0},
  63. @"b" : @{@"k" : @"b", @"sort" : @1},
  64. @"c" : @{@"k" : @"c", @"sort" : @1},
  65. @"d" : @{@"k" : @"d", @"sort" : @2},
  66. }];
  67. // Setup a `limit` query.
  68. FIRQuery *limit = [[collRef queryOrderedByField:@"sort" descending:NO] queryLimitedTo:2];
  69. FSTEventAccumulator *limitAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  70. id<FIRListenerRegistration> limitRegistration =
  71. [limit addSnapshotListener:limitAccumulator.valueEventHandler];
  72. // Setup a mirroring `limitToLast` query.
  73. FIRQuery *limitToLast = [[collRef queryOrderedByField:@"sort"
  74. descending:YES] queryLimitedToLast:2];
  75. FSTEventAccumulator *limitToLastAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  76. id<FIRListenerRegistration> limitToLastRegistration =
  77. [limitToLast addSnapshotListener:limitToLastAccumulator.valueEventHandler];
  78. // Verify both queries get expected result.
  79. FIRQuerySnapshot *snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  80. NSArray *expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1} ];
  81. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  82. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  83. expected = @[ @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"a", @"sort" : @0} ];
  84. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  85. // Unlisten then re-listen limit query.
  86. [limitRegistration remove];
  87. [limit addSnapshotListener:[limitAccumulator valueEventHandler]];
  88. // Verify limit query still works.
  89. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  90. expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1} ];
  91. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  92. // Add a document that would change the result set.
  93. [self addDocumentRef:collRef data:@{@"k" : @"e", @"sort" : @-1}];
  94. // Verify both queries get expected result.
  95. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  96. expected = @[ @{@"k" : @"e", @"sort" : @-1}, @{@"k" : @"a", @"sort" : @0} ];
  97. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  98. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  99. expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"e", @"sort" : @-1} ];
  100. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  101. // Unlisten to limitToLast, update a doc, then relisten to limitToLast
  102. [limitToLastRegistration remove];
  103. [self updateDocumentRef:[collRef documentWithPath:@"a"] data:@{@"k" : @"a", @"sort" : @-2}];
  104. [limitToLast addSnapshotListener:[limitToLastAccumulator valueEventHandler]];
  105. // Verify both queries get expected result.
  106. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  107. expected = @[ @{@"k" : @"a", @"sort" : @-2}, @{@"k" : @"e", @"sort" : @-1} ];
  108. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  109. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  110. expected = @[ @{@"k" : @"e", @"sort" : @-1}, @{@"k" : @"a", @"sort" : @-2} ];
  111. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  112. }
  113. - (void)testKeyOrderIsDescendingForDescendingInequality {
  114. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  115. @"a" : @{@"foo" : @42},
  116. @"b" : @{@"foo" : @42.0},
  117. @"c" : @{@"foo" : @42},
  118. @"d" : @{@"foo" : @21},
  119. @"e" : @{@"foo" : @21.0},
  120. @"f" : @{@"foo" : @66},
  121. @"g" : @{@"foo" : @66.0},
  122. }];
  123. FIRQuerySnapshot *snapshot =
  124. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo"
  125. isGreaterThan:@21] queryOrderedByField:@"foo"
  126. descending:YES]];
  127. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  128. }
  129. - (void)testUnaryFilterQueries {
  130. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  131. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  132. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  133. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  134. }];
  135. FIRQuerySnapshot *results =
  136. [self readDocumentSetForRef:[[collRef queryWhereField:@"null"
  137. isEqualTo:[NSNull null]] queryWhereField:@"nan"
  138. isEqualTo:@(NAN)]];
  139. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results),
  140. (@[ @{@"null" : [NSNull null], @"nan" : @(NAN)} ]));
  141. }
  142. - (void)testQueryWithFieldPaths {
  143. FIRCollectionReference *collRef = [self
  144. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  145. FIRQuery *query = [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  146. isLessThan:@3];
  147. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  148. descending:YES];
  149. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  150. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  151. }
  152. - (void)testQueryWithPredicate {
  153. FIRCollectionReference *collRef = [self
  154. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  155. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  156. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  157. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  158. descending:YES];
  159. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  160. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  161. }
  162. - (void)testFilterOnInfinity {
  163. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  164. @"a" : @{@"inf" : @(INFINITY)},
  165. @"b" : @{@"inf" : @(-INFINITY)}
  166. }];
  167. FIRQuerySnapshot *results = [self readDocumentSetForRef:[collRef queryWhereField:@"inf"
  168. isEqualTo:@(INFINITY)]];
  169. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  170. }
  171. - (void)testCanExplicitlySortByDocumentID {
  172. NSDictionary *testDocs = @{
  173. @"a" : @{@"key" : @"a"},
  174. @"b" : @{@"key" : @"b"},
  175. @"c" : @{@"key" : @"c"},
  176. };
  177. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  178. // Ideally this would be descending to validate it's different than
  179. // the default, but that requires an extra index
  180. FIRQuerySnapshot *docs =
  181. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  182. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  183. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  184. }
  185. - (void)testCanQueryByDocumentID {
  186. NSDictionary *testDocs = @{
  187. @"aa" : @{@"key" : @"aa"},
  188. @"ab" : @{@"key" : @"ab"},
  189. @"ba" : @{@"key" : @"ba"},
  190. @"bb" : @{@"key" : @"bb"},
  191. };
  192. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  193. FIRQuerySnapshot *docs =
  194. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  195. isEqualTo:@"ab"]];
  196. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  197. }
  198. - (void)testCanQueryByDocumentIDs {
  199. NSDictionary *testDocs = @{
  200. @"aa" : @{@"key" : @"aa"},
  201. @"ab" : @{@"key" : @"ab"},
  202. @"ba" : @{@"key" : @"ba"},
  203. @"bb" : @{@"key" : @"bb"},
  204. };
  205. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  206. FIRQuerySnapshot *docs =
  207. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  208. isEqualTo:@"ab"]];
  209. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  210. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  211. isGreaterThan:@"aa"]
  212. queryWhereFieldPath:[FIRFieldPath documentID]
  213. isLessThanOrEqualTo:@"ba"]];
  214. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  215. }
  216. - (void)testCanQueryByDocumentIDsUsingRefs {
  217. NSDictionary *testDocs = @{
  218. @"aa" : @{@"key" : @"aa"},
  219. @"ab" : @{@"key" : @"ab"},
  220. @"ba" : @{@"key" : @"ba"},
  221. @"bb" : @{@"key" : @"bb"},
  222. };
  223. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  224. FIRQuerySnapshot *docs = [self
  225. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  226. isEqualTo:[collection documentWithPath:@"ab"]]];
  227. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  228. docs = [self
  229. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  230. isGreaterThan:[collection documentWithPath:@"aa"]]
  231. queryWhereFieldPath:[FIRFieldPath documentID]
  232. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  233. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  234. }
  235. - (void)testWatchSurvivesNetworkDisconnect {
  236. XCTestExpectation *testExpectiation =
  237. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  238. FIRCollectionReference *collectionRef = [self collectionRef];
  239. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  240. FIRFirestore *firestore = collectionRef.firestore;
  241. [collectionRef
  242. addSnapshotListenerWithIncludeMetadataChanges:YES
  243. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  244. XCTAssertNil(error);
  245. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  246. [testExpectiation fulfill];
  247. }
  248. }];
  249. [firestore disableNetworkWithCompletion:^(NSError *error) {
  250. XCTAssertNil(error);
  251. [docRef setData:@{@"foo" : @"bar"}];
  252. [firestore enableNetworkWithCompletion:^(NSError *error) {
  253. XCTAssertNil(error);
  254. }];
  255. }];
  256. [self awaitExpectations];
  257. }
  258. - (void)testQueriesFireFromCacheWhenOffline {
  259. NSDictionary *testDocs = @{
  260. @"a" : @{@"foo" : @1},
  261. };
  262. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  263. id<FIRListenerRegistration> registration = [collection
  264. addSnapshotListenerWithIncludeMetadataChanges:YES
  265. listener:self.eventAccumulator.valueEventHandler];
  266. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  267. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  268. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  269. [self disableNetwork];
  270. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  271. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  272. [self enableNetwork];
  273. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  274. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  275. [registration remove];
  276. }
  277. - (void)testDocumentChangesUseNSNotFound {
  278. NSDictionary *testDocs = @{
  279. @"a" : @{@"foo" : @1},
  280. };
  281. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  282. id<FIRListenerRegistration> registration =
  283. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  284. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  285. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  286. FIRDocumentChange *change = querySnap.documentChanges[0];
  287. XCTAssertEqual(change.oldIndex, NSNotFound);
  288. XCTAssertEqual(change.newIndex, 0ul);
  289. FIRDocumentReference *doc = change.document.reference;
  290. [self deleteDocumentRef:doc];
  291. querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
  292. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  293. change = querySnap.documentChanges[0];
  294. XCTAssertEqual(change.oldIndex, 0ul);
  295. XCTAssertEqual(change.newIndex, NSNotFound);
  296. [registration remove];
  297. }
  298. - (void)testCanHaveMultipleMutationsWhileOffline {
  299. FIRCollectionReference *col = [self collectionRef];
  300. // set a few docs to known values
  301. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  302. [self writeAllDocuments:initialDocs toCollection:col];
  303. // go offline for the rest of this test
  304. [self disableNetwork];
  305. // apply *multiple* mutations while offline
  306. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  307. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  308. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  309. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  310. @{@"key1b" : @"value1b"},
  311. @{@"key2b" : @"value2b"},
  312. ]));
  313. }
  314. - (void)testQueriesCanUseArrayContainsFilters {
  315. NSDictionary *testDocs = @{
  316. @"a" : @{@"array" : @[ @42 ]},
  317. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  318. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  319. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]}
  320. };
  321. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  322. // Search for 42
  323. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  324. arrayContains:@42]];
  325. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  326. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  327. // NOTE: The backend doesn't currently support null, NaN, objects, or arrays, so there isn't much
  328. // of anything else interesting to test.
  329. }
  330. - (void)testQueriesCanUseInFilters {
  331. NSDictionary *testDocs = @{
  332. @"a" : @{@"zip" : @98101},
  333. @"b" : @{@"zip" : @91102},
  334. @"c" : @{@"zip" : @98103},
  335. @"d" : @{@"zip" : @[ @98101 ]},
  336. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  337. @"f" : @{@"zip" : @{@"code" : @500}},
  338. @"g" : @{@"zip" : @[ @98101, @98102 ]}
  339. };
  340. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  341. // Search for zips matching 98101, 98103, and [98101, 98102].
  342. FIRQuerySnapshot *snapshot = [self
  343. readDocumentSetForRef:[collection queryWhereField:@"zip"
  344. in:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  345. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  346. (@[ testDocs[@"a"], testDocs[@"c"], testDocs[@"g"] ]));
  347. // With objects
  348. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  349. in:@[ @{@"code" : @500} ]]];
  350. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  351. }
  352. - (void)testQueriesCanUseInFiltersWithDocIds {
  353. NSDictionary *testDocs = @{
  354. @"aa" : @{@"key" : @"aa"},
  355. @"ab" : @{@"key" : @"ab"},
  356. @"ba" : @{@"key" : @"ba"},
  357. @"bb" : @{@"key" : @"bb"},
  358. };
  359. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  360. FIRQuerySnapshot *snapshot =
  361. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  362. in:@[ @"aa", @"ab" ]]];
  363. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  364. }
  365. - (void)testQueriesCanUseArrayContainsAnyFilters {
  366. NSDictionary *testDocs = @{
  367. @"a" : @{@"array" : @[ @42 ]},
  368. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  369. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  370. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  371. @"e" : @{@"array" : @[ @43 ]},
  372. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  373. @"g" : @{@"array" : @42},
  374. };
  375. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  376. // Search for zips matching [42, 43].
  377. FIRQuerySnapshot *snapshot = [self
  378. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  379. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  380. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  381. // With objects.
  382. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  383. arrayContainsAny:@[ @{@"a" : @42} ]]];
  384. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  385. testDocs[@"f"],
  386. ]));
  387. }
  388. - (void)testCollectionGroupQueries {
  389. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  390. // for predictable ordering.
  391. NSString *collectionGroup = [NSString
  392. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  393. NSArray *docPaths = @[
  394. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  395. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  396. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  397. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  398. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  399. @"abc/${collectionGroup}"
  400. ];
  401. FIRWriteBatch *batch = [self.db batch];
  402. for (NSString *docPath in docPaths) {
  403. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  404. withString:collectionGroup];
  405. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  406. }
  407. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  408. [batch commitWithCompletion:^(NSError *error) {
  409. XCTAssertNil(error);
  410. [expectation fulfill];
  411. }];
  412. [self awaitExpectations];
  413. FIRQuerySnapshot *querySnapshot =
  414. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  415. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  416. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  417. }
  418. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  419. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  420. // for predictable ordering.
  421. NSString *collectionGroup = [NSString
  422. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  423. NSArray *docPaths = @[
  424. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  425. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  426. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  427. ];
  428. FIRWriteBatch *batch = [self.db batch];
  429. for (NSString *docPath in docPaths) {
  430. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  431. withString:collectionGroup];
  432. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  433. }
  434. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  435. [batch commitWithCompletion:^(NSError *error) {
  436. XCTAssertNil(error);
  437. [expectation fulfill];
  438. }];
  439. [self awaitExpectations];
  440. FIRQuerySnapshot *querySnapshot = [self
  441. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  442. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  443. queryStartingAfterValues:@[ @"a/b" ]]
  444. queryEndingBeforeValues:@[
  445. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  446. ]]];
  447. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  448. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  449. }
  450. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  451. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  452. // for predictable ordering.
  453. NSString *collectionGroup = [NSString
  454. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  455. NSArray *docPaths = @[
  456. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  457. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  458. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  459. ];
  460. FIRWriteBatch *batch = [self.db batch];
  461. for (NSString *docPath in docPaths) {
  462. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  463. withString:collectionGroup];
  464. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  465. }
  466. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  467. [batch commitWithCompletion:^(NSError *error) {
  468. XCTAssertNil(error);
  469. [expectation fulfill];
  470. }];
  471. [self awaitExpectations];
  472. FIRQuerySnapshot *querySnapshot = [self
  473. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  474. queryWhereFieldPath:[FIRFieldPath documentID]
  475. isGreaterThanOrEqualTo:@"a/b"]
  476. queryWhereFieldPath:[FIRFieldPath documentID]
  477. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  478. collectionGroup]]];
  479. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  480. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  481. }
  482. @end