FIRQueryTests.mm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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)testLimitToLastQueriesWithCursors {
  114. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  115. @"a" : @{@"k" : @"a", @"sort" : @0},
  116. @"b" : @{@"k" : @"b", @"sort" : @1},
  117. @"c" : @{@"k" : @"c", @"sort" : @1},
  118. @"d" : @{@"k" : @"d", @"sort" : @2},
  119. }];
  120. FIRQuerySnapshot *snapshot =
  121. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  122. queryEndingBeforeValues:@[ @2 ]]];
  123. XCTAssertEqualObjects(
  124. FIRQuerySnapshotGetData(snapshot), (@[
  125. @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
  126. ]));
  127. snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
  128. queryLimitedToLast:3] queryEndingAtValues:@[ @1 ]]];
  129. XCTAssertEqualObjects(
  130. FIRQuerySnapshotGetData(snapshot), (@[
  131. @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
  132. ]));
  133. snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
  134. queryLimitedToLast:3] queryStartingAtValues:@[ @2 ]]];
  135. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"d", @"sort" : @2} ]));
  136. snapshot =
  137. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  138. queryStartingAfterValues:@[ @0 ]]];
  139. XCTAssertEqualObjects(
  140. FIRQuerySnapshotGetData(snapshot), (@[
  141. @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
  142. ]));
  143. snapshot =
  144. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  145. queryStartingAfterValues:@[ @-1 ]]];
  146. XCTAssertEqualObjects(
  147. FIRQuerySnapshotGetData(snapshot), (@[
  148. @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
  149. ]));
  150. }
  151. - (void)testKeyOrderIsDescendingForDescendingInequality {
  152. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  153. @"a" : @{@"foo" : @42},
  154. @"b" : @{@"foo" : @42.0},
  155. @"c" : @{@"foo" : @42},
  156. @"d" : @{@"foo" : @21},
  157. @"e" : @{@"foo" : @21.0},
  158. @"f" : @{@"foo" : @66},
  159. @"g" : @{@"foo" : @66.0},
  160. }];
  161. FIRQuerySnapshot *snapshot =
  162. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo"
  163. isGreaterThan:@21] queryOrderedByField:@"foo"
  164. descending:YES]];
  165. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  166. }
  167. - (void)testUnaryFilterQueries {
  168. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  169. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  170. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  171. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  172. }];
  173. FIRQuerySnapshot *results =
  174. [self readDocumentSetForRef:[[collRef queryWhereField:@"null"
  175. isEqualTo:[NSNull null]] queryWhereField:@"nan"
  176. isEqualTo:@(NAN)]];
  177. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results),
  178. (@[ @{@"null" : [NSNull null], @"nan" : @(NAN)} ]));
  179. }
  180. - (void)testQueryWithFieldPaths {
  181. FIRCollectionReference *collRef = [self
  182. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  183. FIRQuery *query = [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  184. isLessThan:@3];
  185. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  186. descending:YES];
  187. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  188. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  189. }
  190. - (void)testQueryWithPredicate {
  191. FIRCollectionReference *collRef = [self
  192. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  193. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  194. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  195. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  196. descending:YES];
  197. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  198. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  199. }
  200. - (void)testFilterOnInfinity {
  201. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  202. @"a" : @{@"inf" : @(INFINITY)},
  203. @"b" : @{@"inf" : @(-INFINITY)}
  204. }];
  205. FIRQuerySnapshot *results = [self readDocumentSetForRef:[collRef queryWhereField:@"inf"
  206. isEqualTo:@(INFINITY)]];
  207. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  208. }
  209. - (void)testCanExplicitlySortByDocumentID {
  210. NSDictionary *testDocs = @{
  211. @"a" : @{@"key" : @"a"},
  212. @"b" : @{@"key" : @"b"},
  213. @"c" : @{@"key" : @"c"},
  214. };
  215. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  216. // Ideally this would be descending to validate it's different than
  217. // the default, but that requires an extra index
  218. FIRQuerySnapshot *docs =
  219. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  220. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  221. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  222. }
  223. - (void)testCanQueryByDocumentID {
  224. NSDictionary *testDocs = @{
  225. @"aa" : @{@"key" : @"aa"},
  226. @"ab" : @{@"key" : @"ab"},
  227. @"ba" : @{@"key" : @"ba"},
  228. @"bb" : @{@"key" : @"bb"},
  229. };
  230. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  231. FIRQuerySnapshot *docs =
  232. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  233. isEqualTo:@"ab"]];
  234. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  235. }
  236. - (void)testCanQueryByDocumentIDs {
  237. NSDictionary *testDocs = @{
  238. @"aa" : @{@"key" : @"aa"},
  239. @"ab" : @{@"key" : @"ab"},
  240. @"ba" : @{@"key" : @"ba"},
  241. @"bb" : @{@"key" : @"bb"},
  242. };
  243. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  244. FIRQuerySnapshot *docs =
  245. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  246. isEqualTo:@"ab"]];
  247. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  248. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  249. isGreaterThan:@"aa"]
  250. queryWhereFieldPath:[FIRFieldPath documentID]
  251. isLessThanOrEqualTo:@"ba"]];
  252. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  253. }
  254. - (void)testCanQueryByDocumentIDsUsingRefs {
  255. NSDictionary *testDocs = @{
  256. @"aa" : @{@"key" : @"aa"},
  257. @"ab" : @{@"key" : @"ab"},
  258. @"ba" : @{@"key" : @"ba"},
  259. @"bb" : @{@"key" : @"bb"},
  260. };
  261. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  262. FIRQuerySnapshot *docs = [self
  263. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  264. isEqualTo:[collection documentWithPath:@"ab"]]];
  265. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  266. docs = [self
  267. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  268. isGreaterThan:[collection documentWithPath:@"aa"]]
  269. queryWhereFieldPath:[FIRFieldPath documentID]
  270. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  271. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  272. }
  273. - (void)testWatchSurvivesNetworkDisconnect {
  274. XCTestExpectation *testExpectiation =
  275. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  276. FIRCollectionReference *collectionRef = [self collectionRef];
  277. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  278. FIRFirestore *firestore = collectionRef.firestore;
  279. [collectionRef
  280. addSnapshotListenerWithIncludeMetadataChanges:YES
  281. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  282. XCTAssertNil(error);
  283. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  284. [testExpectiation fulfill];
  285. }
  286. }];
  287. [firestore disableNetworkWithCompletion:^(NSError *error) {
  288. XCTAssertNil(error);
  289. [docRef setData:@{@"foo" : @"bar"}];
  290. [firestore enableNetworkWithCompletion:^(NSError *error) {
  291. XCTAssertNil(error);
  292. }];
  293. }];
  294. [self awaitExpectations];
  295. }
  296. - (void)testQueriesFireFromCacheWhenOffline {
  297. NSDictionary *testDocs = @{
  298. @"a" : @{@"foo" : @1},
  299. };
  300. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  301. id<FIRListenerRegistration> registration = [collection
  302. addSnapshotListenerWithIncludeMetadataChanges:YES
  303. listener:self.eventAccumulator.valueEventHandler];
  304. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  305. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  306. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  307. [self disableNetwork];
  308. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  309. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  310. [self enableNetwork];
  311. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  312. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  313. [registration remove];
  314. }
  315. - (void)testDocumentChangesUseNSNotFound {
  316. NSDictionary *testDocs = @{
  317. @"a" : @{@"foo" : @1},
  318. };
  319. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  320. id<FIRListenerRegistration> registration =
  321. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  322. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  323. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  324. FIRDocumentChange *change = querySnap.documentChanges[0];
  325. XCTAssertEqual(change.oldIndex, NSNotFound);
  326. XCTAssertEqual(change.newIndex, 0ul);
  327. FIRDocumentReference *doc = change.document.reference;
  328. [self deleteDocumentRef:doc];
  329. querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
  330. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  331. change = querySnap.documentChanges[0];
  332. XCTAssertEqual(change.oldIndex, 0ul);
  333. XCTAssertEqual(change.newIndex, NSNotFound);
  334. [registration remove];
  335. }
  336. - (void)testCanHaveMultipleMutationsWhileOffline {
  337. FIRCollectionReference *col = [self collectionRef];
  338. // set a few docs to known values
  339. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  340. [self writeAllDocuments:initialDocs toCollection:col];
  341. // go offline for the rest of this test
  342. [self disableNetwork];
  343. // apply *multiple* mutations while offline
  344. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  345. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  346. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  347. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  348. @{@"key1b" : @"value1b"},
  349. @{@"key2b" : @"value2b"},
  350. ]));
  351. }
  352. - (void)testQueriesCanUseNotEqualFilters {
  353. // These documents are ordered by value in "zip" since notEquals filter is an inequality, which
  354. // results in documents being sorted by value.
  355. NSDictionary *testDocs = @{
  356. @"a" : @{@"zip" : @(NAN)},
  357. @"b" : @{@"zip" : @91102},
  358. @"c" : @{@"zip" : @98101},
  359. @"d" : @{@"zip" : @98103},
  360. @"e" : @{@"zip" : @[ @98101 ]},
  361. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  362. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  363. @"h" : @{@"zip" : @{@"code" : @500}},
  364. @"i" : @{@"zip" : [NSNull null]},
  365. @"j" : @{@"code" : @500},
  366. };
  367. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  368. // Search for zips not matching 98101.
  369. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  370. isNotEqualTo:@98101]];
  371. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  372. testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"],
  373. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  374. ]));
  375. // With objects.
  376. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  377. isNotEqualTo:@{@"code" : @500}]];
  378. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  379. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  380. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  381. ]));
  382. // With null.
  383. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  384. isNotEqualTo:@[ [NSNull null] ]]];
  385. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  386. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  387. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  388. ]));
  389. // With NAN.
  390. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" isNotEqualTo:@(NAN)]];
  391. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  392. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  393. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  394. ]));
  395. }
  396. - (void)testQueriesCanUseNotEqualFiltersWithDocIds {
  397. NSDictionary *testDocs = @{
  398. @"aa" : @{@"key" : @"aa"},
  399. @"ab" : @{@"key" : @"ab"},
  400. @"ba" : @{@"key" : @"ba"},
  401. @"bb" : @{@"key" : @"bb"},
  402. };
  403. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  404. FIRQuerySnapshot *snapshot =
  405. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  406. isNotEqualTo:@"aa"]];
  407. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  408. (@[ testDocs[@"ab"], testDocs[@"ba"], testDocs[@"bb"] ]));
  409. }
  410. - (void)testQueriesCanUseArrayContainsFilters {
  411. NSDictionary *testDocs = @{
  412. @"a" : @{@"array" : @[ @42 ]},
  413. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  414. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  415. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  416. @"e" : @{@"array" : @[ [NSNull null] ]},
  417. @"f" : @{@"array" : @[ @(NAN) ]},
  418. };
  419. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  420. // Search for 42
  421. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  422. arrayContains:@42]];
  423. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  424. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  425. // With null.
  426. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  427. arrayContains:[NSNull null]]];
  428. XCTAssertTrue(snapshot.isEmpty);
  429. // With NAN.
  430. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  431. arrayContains:@(NAN)]];
  432. XCTAssertTrue(snapshot.isEmpty);
  433. }
  434. - (void)testQueriesCanUseInFilters {
  435. NSDictionary *testDocs = @{
  436. @"a" : @{@"zip" : @98101},
  437. @"b" : @{@"zip" : @91102},
  438. @"c" : @{@"zip" : @98103},
  439. @"d" : @{@"zip" : @[ @98101 ]},
  440. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  441. @"f" : @{@"zip" : @{@"code" : @500}},
  442. @"g" : @{@"zip" : @[ @98101, @98102 ]},
  443. @"h" : @{@"zip" : [NSNull null]},
  444. @"i" : @{@"zip" : @(NAN)}
  445. };
  446. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  447. // Search for zips matching 98101, 98103, and [98101, 98102].
  448. FIRQuerySnapshot *snapshot = [self
  449. readDocumentSetForRef:[collection queryWhereField:@"zip"
  450. in:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  451. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  452. (@[ testDocs[@"a"], testDocs[@"c"], testDocs[@"g"] ]));
  453. // With objects
  454. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  455. in:@[ @{@"code" : @500} ]]];
  456. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  457. // With null.
  458. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ [NSNull null] ]]];
  459. XCTAssertTrue(snapshot.isEmpty);
  460. // With null and a value.
  461. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  462. in:@[ [NSNull null], @98101 ]]];
  463. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  464. // With NAN.
  465. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ @(NAN) ]]];
  466. XCTAssertTrue(snapshot.isEmpty);
  467. // With NAN and a value.
  468. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  469. in:@[ @(NAN), @98101 ]]];
  470. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  471. }
  472. - (void)testQueriesCanUseInFiltersWithDocIds {
  473. NSDictionary *testDocs = @{
  474. @"aa" : @{@"key" : @"aa"},
  475. @"ab" : @{@"key" : @"ab"},
  476. @"ba" : @{@"key" : @"ba"},
  477. @"bb" : @{@"key" : @"bb"},
  478. };
  479. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  480. FIRQuerySnapshot *snapshot =
  481. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  482. in:@[ @"aa", @"ab" ]]];
  483. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  484. }
  485. - (void)testQueriesCanUseNotInFilters {
  486. // These documents are ordered by value in "zip" since the NOT_IN filter is an inequality, which
  487. // results in documents being sorted by value.
  488. NSDictionary *testDocs = @{
  489. @"a" : @{@"zip" : @(NAN)},
  490. @"b" : @{@"zip" : @91102},
  491. @"c" : @{@"zip" : @98101},
  492. @"d" : @{@"zip" : @98103},
  493. @"e" : @{@"zip" : @[ @98101 ]},
  494. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  495. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  496. @"h" : @{@"zip" : @{@"code" : @500}},
  497. @"i" : @{@"zip" : [NSNull null]},
  498. @"j" : @{@"code" : @500},
  499. };
  500. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  501. // Search for zips not matching 98101, 98103, and [98101, 98102].
  502. FIRQuerySnapshot *snapshot = [self
  503. readDocumentSetForRef:[collection queryWhereField:@"zip"
  504. notIn:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  505. XCTAssertEqualObjects(
  506. FIRQuerySnapshotGetData(snapshot),
  507. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"e"], testDocs[@"g"], testDocs[@"h"] ]));
  508. // With objects.
  509. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  510. notIn:@[ @{@"code" : @500} ]]];
  511. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  512. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  513. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  514. ]));
  515. // With null.
  516. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  517. notIn:@[ [NSNull null] ]]];
  518. XCTAssertTrue(snapshot.isEmpty);
  519. // With NAN.
  520. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" notIn:@[ @(NAN) ]]];
  521. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  522. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  523. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  524. ]));
  525. // With NAN and a number.
  526. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  527. notIn:@[ @(NAN), @98101 ]]];
  528. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  529. testDocs[@"b"], testDocs[@"d"], testDocs[@"e"], testDocs[@"f"],
  530. testDocs[@"g"], testDocs[@"h"]
  531. ]));
  532. }
  533. - (void)testQueriesCanUseNotInFiltersWithDocIds {
  534. NSDictionary *testDocs = @{
  535. @"aa" : @{@"key" : @"aa"},
  536. @"ab" : @{@"key" : @"ab"},
  537. @"ba" : @{@"key" : @"ba"},
  538. @"bb" : @{@"key" : @"bb"},
  539. };
  540. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  541. FIRQuerySnapshot *snapshot =
  542. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  543. notIn:@[ @"aa", @"ab" ]]];
  544. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"ba"], testDocs[@"bb"] ]));
  545. }
  546. - (void)testQueriesCanUseArrayContainsAnyFilters {
  547. NSDictionary *testDocs = @{
  548. @"a" : @{@"array" : @[ @42 ]},
  549. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  550. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  551. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  552. @"e" : @{@"array" : @[ @43 ]},
  553. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  554. @"g" : @{@"array" : @42},
  555. @"h" : @{@"array" : @[ [NSNull null] ]},
  556. @"i" : @{@"array" : @[ @(NAN) ]},
  557. };
  558. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  559. // Search for zips matching [42, 43].
  560. FIRQuerySnapshot *snapshot = [self
  561. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  562. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  563. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  564. // With objects.
  565. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  566. arrayContainsAny:@[ @{@"a" : @42} ]]];
  567. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  568. testDocs[@"f"],
  569. ]));
  570. // With null.
  571. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  572. arrayContainsAny:@[ [NSNull null] ]]];
  573. XCTAssertTrue(snapshot.isEmpty);
  574. // With null and a value.
  575. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  576. arrayContainsAny:@[ [NSNull null], @43 ]]];
  577. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  578. // With NAN.
  579. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  580. arrayContainsAny:@[ @(NAN) ]]];
  581. XCTAssertTrue(snapshot.isEmpty);
  582. // With NAN and a value.
  583. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  584. arrayContainsAny:@[ @(NAN), @43 ]]];
  585. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  586. }
  587. - (void)testCollectionGroupQueries {
  588. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  589. // for predictable ordering.
  590. NSString *collectionGroup = [NSString
  591. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  592. NSArray *docPaths = @[
  593. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  594. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  595. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  596. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  597. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  598. @"abc/${collectionGroup}"
  599. ];
  600. FIRWriteBatch *batch = [self.db batch];
  601. for (NSString *docPath in docPaths) {
  602. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  603. withString:collectionGroup];
  604. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  605. }
  606. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  607. [batch commitWithCompletion:^(NSError *error) {
  608. XCTAssertNil(error);
  609. [expectation fulfill];
  610. }];
  611. [self awaitExpectations];
  612. FIRQuerySnapshot *querySnapshot =
  613. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  614. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  615. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  616. }
  617. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  618. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  619. // for predictable ordering.
  620. NSString *collectionGroup = [NSString
  621. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  622. NSArray *docPaths = @[
  623. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  624. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  625. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  626. ];
  627. FIRWriteBatch *batch = [self.db batch];
  628. for (NSString *docPath in docPaths) {
  629. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  630. withString:collectionGroup];
  631. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  632. }
  633. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  634. [batch commitWithCompletion:^(NSError *error) {
  635. XCTAssertNil(error);
  636. [expectation fulfill];
  637. }];
  638. [self awaitExpectations];
  639. FIRQuerySnapshot *querySnapshot = [self
  640. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  641. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  642. queryStartingAfterValues:@[ @"a/b" ]]
  643. queryEndingBeforeValues:@[
  644. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  645. ]]];
  646. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  647. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  648. }
  649. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  650. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  651. // for predictable ordering.
  652. NSString *collectionGroup = [NSString
  653. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  654. NSArray *docPaths = @[
  655. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  656. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  657. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  658. ];
  659. FIRWriteBatch *batch = [self.db batch];
  660. for (NSString *docPath in docPaths) {
  661. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  662. withString:collectionGroup];
  663. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  664. }
  665. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  666. [batch commitWithCompletion:^(NSError *error) {
  667. XCTAssertNil(error);
  668. [expectation fulfill];
  669. }];
  670. [self awaitExpectations];
  671. FIRQuerySnapshot *querySnapshot = [self
  672. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  673. queryWhereFieldPath:[FIRFieldPath documentID]
  674. isGreaterThanOrEqualTo:@"a/b"]
  675. queryWhereFieldPath:[FIRFieldPath documentID]
  676. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  677. collectionGroup]]];
  678. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  679. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  680. }
  681. @end