FIRQueryTests.mm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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)testQueriesCanUseNotEqualFilters {
  315. // These documents are ordered by value in "zip" since notEquals filter is an inequality, which
  316. // results in documents being sorted by value.
  317. NSDictionary *testDocs = @{
  318. @"a" : @{@"zip" : @(NAN)},
  319. @"b" : @{@"zip" : @91102},
  320. @"c" : @{@"zip" : @98101},
  321. @"d" : @{@"zip" : @98103},
  322. @"e" : @{@"zip" : @[ @98101 ]},
  323. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  324. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  325. @"h" : @{@"zip" : @{@"code" : @500}},
  326. @"i" : @{@"zip" : [NSNull null]},
  327. @"j" : @{@"code" : @500},
  328. };
  329. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  330. // Search for zips not matching 98101.
  331. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  332. isNotEqualTo:@98101]];
  333. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  334. testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"],
  335. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  336. ]));
  337. // With objects.
  338. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  339. isNotEqualTo:@{@"code" : @500}]];
  340. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  341. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  342. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  343. ]));
  344. // With null.
  345. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  346. isNotEqualTo:@[ [NSNull null] ]]];
  347. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  348. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  349. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  350. ]));
  351. // With NAN.
  352. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" isNotEqualTo:@(NAN)]];
  353. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  354. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  355. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  356. ]));
  357. }
  358. - (void)testQueriesCanUseNotEqualFiltersWithDocIds {
  359. NSDictionary *testDocs = @{
  360. @"aa" : @{@"key" : @"aa"},
  361. @"ab" : @{@"key" : @"ab"},
  362. @"ba" : @{@"key" : @"ba"},
  363. @"bb" : @{@"key" : @"bb"},
  364. };
  365. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  366. FIRQuerySnapshot *snapshot =
  367. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  368. isNotEqualTo:@"aa"]];
  369. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  370. (@[ testDocs[@"ab"], testDocs[@"ba"], testDocs[@"bb"] ]));
  371. }
  372. - (void)testQueriesCanUseArrayContainsFilters {
  373. NSDictionary *testDocs = @{
  374. @"a" : @{@"array" : @[ @42 ]},
  375. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  376. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  377. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  378. @"e" : @{@"array" : @[ [NSNull null] ]},
  379. @"f" : @{@"array" : @[ @(NAN) ]},
  380. };
  381. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  382. // Search for 42
  383. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  384. arrayContains:@42]];
  385. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  386. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  387. // With null.
  388. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  389. arrayContains:[NSNull null]]];
  390. XCTAssertTrue(snapshot.isEmpty);
  391. // With NAN.
  392. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  393. arrayContains:@(NAN)]];
  394. XCTAssertTrue(snapshot.isEmpty);
  395. }
  396. - (void)testQueriesCanUseInFilters {
  397. NSDictionary *testDocs = @{
  398. @"a" : @{@"zip" : @98101},
  399. @"b" : @{@"zip" : @91102},
  400. @"c" : @{@"zip" : @98103},
  401. @"d" : @{@"zip" : @[ @98101 ]},
  402. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  403. @"f" : @{@"zip" : @{@"code" : @500}},
  404. @"g" : @{@"zip" : @[ @98101, @98102 ]},
  405. @"h" : @{@"zip" : [NSNull null]},
  406. @"i" : @{@"zip" : @(NAN)}
  407. };
  408. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  409. // Search for zips matching 98101, 98103, and [98101, 98102].
  410. FIRQuerySnapshot *snapshot = [self
  411. readDocumentSetForRef:[collection queryWhereField:@"zip"
  412. in:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  413. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  414. (@[ testDocs[@"a"], testDocs[@"c"], testDocs[@"g"] ]));
  415. // With objects
  416. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  417. in:@[ @{@"code" : @500} ]]];
  418. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  419. // With null.
  420. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ [NSNull null] ]]];
  421. XCTAssertTrue(snapshot.isEmpty);
  422. // With null and a value.
  423. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  424. in:@[ [NSNull null], @98101 ]]];
  425. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  426. // With NAN.
  427. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ @(NAN) ]]];
  428. XCTAssertTrue(snapshot.isEmpty);
  429. // With NAN and a value.
  430. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  431. in:@[ @(NAN), @98101 ]]];
  432. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  433. }
  434. - (void)testQueriesCanUseInFiltersWithDocIds {
  435. NSDictionary *testDocs = @{
  436. @"aa" : @{@"key" : @"aa"},
  437. @"ab" : @{@"key" : @"ab"},
  438. @"ba" : @{@"key" : @"ba"},
  439. @"bb" : @{@"key" : @"bb"},
  440. };
  441. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  442. FIRQuerySnapshot *snapshot =
  443. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  444. in:@[ @"aa", @"ab" ]]];
  445. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  446. }
  447. - (void)testQueriesCanUseNotInFilters {
  448. // These documents are ordered by value in "zip" since the NOT_IN filter is an inequality, which
  449. // results in documents being sorted by value.
  450. NSDictionary *testDocs = @{
  451. @"a" : @{@"zip" : @(NAN)},
  452. @"b" : @{@"zip" : @91102},
  453. @"c" : @{@"zip" : @98101},
  454. @"d" : @{@"zip" : @98103},
  455. @"e" : @{@"zip" : @[ @98101 ]},
  456. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  457. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  458. @"h" : @{@"zip" : @{@"code" : @500}},
  459. @"i" : @{@"zip" : [NSNull null]},
  460. @"j" : @{@"code" : @500},
  461. };
  462. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  463. // Search for zips not matching 98101, 98103, and [98101, 98102].
  464. FIRQuerySnapshot *snapshot = [self
  465. readDocumentSetForRef:[collection queryWhereField:@"zip"
  466. notIn:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  467. XCTAssertEqualObjects(
  468. FIRQuerySnapshotGetData(snapshot),
  469. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"e"], testDocs[@"g"], testDocs[@"h"] ]));
  470. // With objects.
  471. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  472. notIn:@[ @{@"code" : @500} ]]];
  473. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  474. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  475. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  476. ]));
  477. // With null.
  478. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  479. notIn:@[ [NSNull null] ]]];
  480. XCTAssertTrue(snapshot.isEmpty);
  481. // With NAN.
  482. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" notIn:@[ @(NAN) ]]];
  483. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  484. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  485. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  486. ]));
  487. // With NAN and a number.
  488. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  489. notIn:@[ @(NAN), @98101 ]]];
  490. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  491. testDocs[@"b"], testDocs[@"d"], testDocs[@"e"], testDocs[@"f"],
  492. testDocs[@"g"], testDocs[@"h"]
  493. ]));
  494. }
  495. - (void)testQueriesCanUseNotInFiltersWithDocIds {
  496. NSDictionary *testDocs = @{
  497. @"aa" : @{@"key" : @"aa"},
  498. @"ab" : @{@"key" : @"ab"},
  499. @"ba" : @{@"key" : @"ba"},
  500. @"bb" : @{@"key" : @"bb"},
  501. };
  502. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  503. FIRQuerySnapshot *snapshot =
  504. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  505. notIn:@[ @"aa", @"ab" ]]];
  506. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"ba"], testDocs[@"bb"] ]));
  507. }
  508. - (void)testQueriesCanUseArrayContainsAnyFilters {
  509. NSDictionary *testDocs = @{
  510. @"a" : @{@"array" : @[ @42 ]},
  511. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  512. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  513. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  514. @"e" : @{@"array" : @[ @43 ]},
  515. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  516. @"g" : @{@"array" : @42},
  517. @"h" : @{@"array" : @[ [NSNull null] ]},
  518. @"g" : @{@"array" : @[ @(NAN) ]},
  519. };
  520. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  521. // Search for zips matching [42, 43].
  522. FIRQuerySnapshot *snapshot = [self
  523. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  524. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  525. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  526. // With objects.
  527. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  528. arrayContainsAny:@[ @{@"a" : @42} ]]];
  529. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  530. testDocs[@"f"],
  531. ]));
  532. // With null.
  533. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  534. arrayContainsAny:@[ [NSNull null] ]]];
  535. XCTAssertTrue(snapshot.isEmpty);
  536. // With null and a value.
  537. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  538. arrayContainsAny:@[ [NSNull null], @43 ]]];
  539. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  540. // With NAN.
  541. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  542. arrayContainsAny:@[ @(NAN) ]]];
  543. XCTAssertTrue(snapshot.isEmpty);
  544. // With NAN and a value.
  545. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  546. arrayContainsAny:@[ @(NAN), @43 ]]];
  547. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  548. }
  549. - (void)testCollectionGroupQueries {
  550. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  551. // for predictable ordering.
  552. NSString *collectionGroup = [NSString
  553. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  554. NSArray *docPaths = @[
  555. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  556. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  557. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  558. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  559. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  560. @"abc/${collectionGroup}"
  561. ];
  562. FIRWriteBatch *batch = [self.db batch];
  563. for (NSString *docPath in docPaths) {
  564. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  565. withString:collectionGroup];
  566. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  567. }
  568. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  569. [batch commitWithCompletion:^(NSError *error) {
  570. XCTAssertNil(error);
  571. [expectation fulfill];
  572. }];
  573. [self awaitExpectations];
  574. FIRQuerySnapshot *querySnapshot =
  575. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  576. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  577. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  578. }
  579. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  580. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  581. // for predictable ordering.
  582. NSString *collectionGroup = [NSString
  583. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  584. NSArray *docPaths = @[
  585. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  586. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  587. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  588. ];
  589. FIRWriteBatch *batch = [self.db batch];
  590. for (NSString *docPath in docPaths) {
  591. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  592. withString:collectionGroup];
  593. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  594. }
  595. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  596. [batch commitWithCompletion:^(NSError *error) {
  597. XCTAssertNil(error);
  598. [expectation fulfill];
  599. }];
  600. [self awaitExpectations];
  601. FIRQuerySnapshot *querySnapshot = [self
  602. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  603. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  604. queryStartingAfterValues:@[ @"a/b" ]]
  605. queryEndingBeforeValues:@[
  606. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  607. ]]];
  608. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  609. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  610. }
  611. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  612. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  613. // for predictable ordering.
  614. NSString *collectionGroup = [NSString
  615. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  616. NSArray *docPaths = @[
  617. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  618. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  619. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  620. ];
  621. FIRWriteBatch *batch = [self.db batch];
  622. for (NSString *docPath in docPaths) {
  623. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  624. withString:collectionGroup];
  625. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  626. }
  627. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  628. [batch commitWithCompletion:^(NSError *error) {
  629. XCTAssertNil(error);
  630. [expectation fulfill];
  631. }];
  632. [self awaitExpectations];
  633. FIRQuerySnapshot *querySnapshot = [self
  634. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  635. queryWhereFieldPath:[FIRFieldPath documentID]
  636. isGreaterThanOrEqualTo:@"a/b"]
  637. queryWhereFieldPath:[FIRFieldPath documentID]
  638. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  639. collectionGroup]]];
  640. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  641. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  642. }
  643. @end