FIRQueryTests.mm 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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/FIRFilter+Internal.h"
  19. #import "Firestore/Source/API/FIRQuery+Internal.h"
  20. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  21. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  22. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  23. @interface FIRQueryTests : FSTIntegrationTestCase
  24. @end
  25. @implementation FIRQueryTests
  26. /**
  27. * Checks that running the query while online (against the backend/emulator) results in the same
  28. * documents as running the query while offline. It also checks that both online and offline
  29. * query result is equal to the expected documents.
  30. *
  31. * @param query The query to check.
  32. * @param expectedDocs Array of document keys that are expected to match the query.
  33. */
  34. - (void)checkOnlineAndOfflineQuery:(FIRQuery *)query matchesResult:(NSArray *)expectedDocs {
  35. FIRQuerySnapshot *docsFromServer = [self readDocumentSetForRef:query
  36. source:FIRFirestoreSourceServer];
  37. FIRQuerySnapshot *docsFromCache = [self readDocumentSetForRef:query
  38. source:FIRFirestoreSourceCache];
  39. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(docsFromServer),
  40. FIRQuerySnapshotGetIDs(docsFromCache));
  41. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(docsFromCache), expectedDocs);
  42. }
  43. - (void)testLimitQueries {
  44. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  45. @"a" : @{@"k" : @"a"},
  46. @"b" : @{@"k" : @"b"},
  47. @"c" : @{@"k" : @"c"}
  48. }];
  49. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  50. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  51. }
  52. - (void)testLimitQueriesWithDescendingSortOrder {
  53. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  54. @"a" : @{@"k" : @"a", @"sort" : @0},
  55. @"b" : @{@"k" : @"b", @"sort" : @1},
  56. @"c" : @{@"k" : @"c", @"sort" : @1},
  57. @"d" : @{@"k" : @"d", @"sort" : @2},
  58. }];
  59. FIRQuerySnapshot *snapshot =
  60. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort"
  61. descending:YES] queryLimitedTo:2]];
  62. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  63. (@[ @{@"k" : @"d", @"sort" : @2}, @{@"k" : @"c", @"sort" : @1} ]));
  64. }
  65. - (void)testLimitToLastMustAlsoHaveExplicitOrderBy {
  66. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{}];
  67. FIRQuery *query = [collRef queryLimitedToLast:2];
  68. FSTAssertThrows([query getDocumentsWithCompletion:^(FIRQuerySnapshot *, NSError *){
  69. }],
  70. @"limit(toLast:) queries require specifying at least one OrderBy() clause.");
  71. }
  72. // Two queries that mapped to the same target ID are referred to as
  73. // "mirror queries". An example for a mirror query is a limitToLast()
  74. // query and a limit() query that share the same backend Target ID.
  75. // Since limitToLast() queries are sent to the backend with a modified
  76. // orderBy() clause, they can map to the same target representation as
  77. // limit() query, even if both queries appear separate to the user.
  78. - (void)testListenUnlistenRelistenSequenceOfMirrorQueries {
  79. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  80. @"a" : @{@"k" : @"a", @"sort" : @0},
  81. @"b" : @{@"k" : @"b", @"sort" : @1},
  82. @"c" : @{@"k" : @"c", @"sort" : @1},
  83. @"d" : @{@"k" : @"d", @"sort" : @2},
  84. }];
  85. // Setup a `limit` query.
  86. FIRQuery *limit = [[collRef queryOrderedByField:@"sort" descending:NO] queryLimitedTo:2];
  87. FSTEventAccumulator *limitAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  88. id<FIRListenerRegistration> limitRegistration =
  89. [limit addSnapshotListener:limitAccumulator.valueEventHandler];
  90. // Setup a mirroring `limitToLast` query.
  91. FIRQuery *limitToLast = [[collRef queryOrderedByField:@"sort"
  92. descending:YES] queryLimitedToLast:2];
  93. FSTEventAccumulator *limitToLastAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  94. id<FIRListenerRegistration> limitToLastRegistration =
  95. [limitToLast addSnapshotListener:limitToLastAccumulator.valueEventHandler];
  96. // Verify both queries get expected result.
  97. FIRQuerySnapshot *snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  98. NSArray *expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1} ];
  99. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  100. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  101. expected = @[ @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"a", @"sort" : @0} ];
  102. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  103. // Unlisten then re-listen limit query.
  104. [limitRegistration remove];
  105. [limit addSnapshotListener:[limitAccumulator valueEventHandler]];
  106. // Verify limit query still works.
  107. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  108. expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1} ];
  109. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  110. // Add a document that would change the result set.
  111. [self addDocumentRef:collRef data:@{@"k" : @"e", @"sort" : @-1}];
  112. // Verify both queries get expected result.
  113. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  114. expected = @[ @{@"k" : @"e", @"sort" : @-1}, @{@"k" : @"a", @"sort" : @0} ];
  115. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  116. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  117. expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"e", @"sort" : @-1} ];
  118. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  119. // Unlisten to limitToLast, update a doc, then relisten to limitToLast
  120. [limitToLastRegistration remove];
  121. [self updateDocumentRef:[collRef documentWithPath:@"a"] data:@{@"k" : @"a", @"sort" : @-2}];
  122. [limitToLast addSnapshotListener:[limitToLastAccumulator valueEventHandler]];
  123. // Verify both queries get expected result.
  124. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  125. expected = @[ @{@"k" : @"a", @"sort" : @-2}, @{@"k" : @"e", @"sort" : @-1} ];
  126. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  127. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  128. expected = @[ @{@"k" : @"e", @"sort" : @-1}, @{@"k" : @"a", @"sort" : @-2} ];
  129. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  130. }
  131. - (void)testLimitToLastQueriesWithCursors {
  132. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  133. @"a" : @{@"k" : @"a", @"sort" : @0},
  134. @"b" : @{@"k" : @"b", @"sort" : @1},
  135. @"c" : @{@"k" : @"c", @"sort" : @1},
  136. @"d" : @{@"k" : @"d", @"sort" : @2},
  137. }];
  138. FIRQuerySnapshot *snapshot =
  139. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  140. queryEndingBeforeValues:@[ @2 ]]];
  141. XCTAssertEqualObjects(
  142. FIRQuerySnapshotGetData(snapshot), (@[
  143. @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
  144. ]));
  145. snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
  146. queryLimitedToLast:3] queryEndingAtValues:@[ @1 ]]];
  147. XCTAssertEqualObjects(
  148. FIRQuerySnapshotGetData(snapshot), (@[
  149. @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
  150. ]));
  151. snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
  152. queryLimitedToLast:3] queryStartingAtValues:@[ @2 ]]];
  153. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"d", @"sort" : @2} ]));
  154. snapshot =
  155. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  156. queryStartingAfterValues:@[ @0 ]]];
  157. XCTAssertEqualObjects(
  158. FIRQuerySnapshotGetData(snapshot), (@[
  159. @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
  160. ]));
  161. snapshot =
  162. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  163. queryStartingAfterValues:@[ @-1 ]]];
  164. XCTAssertEqualObjects(
  165. FIRQuerySnapshotGetData(snapshot), (@[
  166. @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
  167. ]));
  168. }
  169. - (void)testKeyOrderIsDescendingForDescendingInequality {
  170. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  171. @"a" : @{@"foo" : @42},
  172. @"b" : @{@"foo" : @42.0},
  173. @"c" : @{@"foo" : @42},
  174. @"d" : @{@"foo" : @21},
  175. @"e" : @{@"foo" : @21.0},
  176. @"f" : @{@"foo" : @66},
  177. @"g" : @{@"foo" : @66.0},
  178. }];
  179. FIRQuerySnapshot *snapshot =
  180. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo"
  181. isGreaterThan:@21] queryOrderedByField:@"foo"
  182. descending:YES]];
  183. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  184. }
  185. - (void)testUnaryFilterQueries {
  186. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  187. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  188. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  189. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  190. }];
  191. FIRQuerySnapshot *results =
  192. [self readDocumentSetForRef:[[collRef queryWhereField:@"null"
  193. isEqualTo:[NSNull null]] queryWhereField:@"nan"
  194. isEqualTo:@(NAN)]];
  195. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results),
  196. (@[ @{@"null" : [NSNull null], @"nan" : @(NAN)} ]));
  197. }
  198. - (void)testQueryWithFieldPaths {
  199. FIRCollectionReference *collRef = [self
  200. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  201. FIRQuery *query = [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  202. isLessThan:@3];
  203. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  204. descending:YES];
  205. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  206. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  207. }
  208. - (void)testQueryWithPredicate {
  209. FIRCollectionReference *collRef = [self
  210. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  211. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  212. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  213. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  214. descending:YES];
  215. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  216. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  217. }
  218. - (void)testFilterOnInfinity {
  219. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  220. @"a" : @{@"inf" : @(INFINITY)},
  221. @"b" : @{@"inf" : @(-INFINITY)}
  222. }];
  223. FIRQuerySnapshot *results = [self readDocumentSetForRef:[collRef queryWhereField:@"inf"
  224. isEqualTo:@(INFINITY)]];
  225. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  226. }
  227. - (void)testCanExplicitlySortByDocumentID {
  228. NSDictionary *testDocs = @{
  229. @"a" : @{@"key" : @"a"},
  230. @"b" : @{@"key" : @"b"},
  231. @"c" : @{@"key" : @"c"},
  232. };
  233. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  234. // Ideally this would be descending to validate it's different than
  235. // the default, but that requires an extra index
  236. FIRQuerySnapshot *docs =
  237. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  238. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  239. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  240. }
  241. - (void)testCanQueryByDocumentID {
  242. NSDictionary *testDocs = @{
  243. @"aa" : @{@"key" : @"aa"},
  244. @"ab" : @{@"key" : @"ab"},
  245. @"ba" : @{@"key" : @"ba"},
  246. @"bb" : @{@"key" : @"bb"},
  247. };
  248. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  249. FIRQuerySnapshot *docs =
  250. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  251. isEqualTo:@"ab"]];
  252. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  253. }
  254. - (void)testCanQueryByDocumentIDs {
  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 =
  263. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  264. isEqualTo:@"ab"]];
  265. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  266. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  267. isGreaterThan:@"aa"]
  268. queryWhereFieldPath:[FIRFieldPath documentID]
  269. isLessThanOrEqualTo:@"ba"]];
  270. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  271. }
  272. - (void)testCanQueryByDocumentIDsUsingRefs {
  273. NSDictionary *testDocs = @{
  274. @"aa" : @{@"key" : @"aa"},
  275. @"ab" : @{@"key" : @"ab"},
  276. @"ba" : @{@"key" : @"ba"},
  277. @"bb" : @{@"key" : @"bb"},
  278. };
  279. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  280. FIRQuerySnapshot *docs = [self
  281. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  282. isEqualTo:[collection documentWithPath:@"ab"]]];
  283. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  284. docs = [self
  285. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  286. isGreaterThan:[collection documentWithPath:@"aa"]]
  287. queryWhereFieldPath:[FIRFieldPath documentID]
  288. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  289. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  290. }
  291. - (void)testWatchSurvivesNetworkDisconnect {
  292. XCTestExpectation *testExpectiation =
  293. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  294. FIRCollectionReference *collectionRef = [self collectionRef];
  295. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  296. FIRFirestore *firestore = collectionRef.firestore;
  297. [collectionRef
  298. addSnapshotListenerWithIncludeMetadataChanges:YES
  299. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  300. XCTAssertNil(error);
  301. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  302. [testExpectiation fulfill];
  303. }
  304. }];
  305. [firestore disableNetworkWithCompletion:^(NSError *error) {
  306. XCTAssertNil(error);
  307. [docRef setData:@{@"foo" : @"bar"}];
  308. [firestore enableNetworkWithCompletion:^(NSError *error) {
  309. XCTAssertNil(error);
  310. }];
  311. }];
  312. [self awaitExpectations];
  313. }
  314. - (void)testQueriesFireFromCacheWhenOffline {
  315. NSDictionary *testDocs = @{
  316. @"a" : @{@"foo" : @1},
  317. };
  318. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  319. id<FIRListenerRegistration> registration = [collection
  320. addSnapshotListenerWithIncludeMetadataChanges:YES
  321. listener:self.eventAccumulator.valueEventHandler];
  322. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  323. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  324. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  325. [self disableNetwork];
  326. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  327. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  328. [self enableNetwork];
  329. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  330. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  331. [registration remove];
  332. }
  333. - (void)testDocumentChangesUseNSNotFound {
  334. NSDictionary *testDocs = @{
  335. @"a" : @{@"foo" : @1},
  336. };
  337. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  338. id<FIRListenerRegistration> registration =
  339. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  340. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  341. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  342. FIRDocumentChange *change = querySnap.documentChanges[0];
  343. XCTAssertEqual(change.oldIndex, NSNotFound);
  344. XCTAssertEqual(change.newIndex, 0ul);
  345. FIRDocumentReference *doc = change.document.reference;
  346. [self deleteDocumentRef:doc];
  347. querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
  348. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  349. change = querySnap.documentChanges[0];
  350. XCTAssertEqual(change.oldIndex, 0ul);
  351. XCTAssertEqual(change.newIndex, NSNotFound);
  352. [registration remove];
  353. }
  354. - (void)testCanHaveMultipleMutationsWhileOffline {
  355. FIRCollectionReference *col = [self collectionRef];
  356. // set a few docs to known values
  357. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  358. [self writeAllDocuments:initialDocs toCollection:col];
  359. // go offline for the rest of this test
  360. [self disableNetwork];
  361. // apply *multiple* mutations while offline
  362. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  363. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  364. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  365. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  366. @{@"key1b" : @"value1b"},
  367. @{@"key2b" : @"value2b"},
  368. ]));
  369. }
  370. - (void)testQueriesCanUseNotEqualFilters {
  371. // These documents are ordered by value in "zip" since notEquals filter is an inequality, which
  372. // results in documents being sorted by value.
  373. NSDictionary *testDocs = @{
  374. @"a" : @{@"zip" : @(NAN)},
  375. @"b" : @{@"zip" : @91102},
  376. @"c" : @{@"zip" : @98101},
  377. @"d" : @{@"zip" : @98103},
  378. @"e" : @{@"zip" : @[ @98101 ]},
  379. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  380. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  381. @"h" : @{@"zip" : @{@"code" : @500}},
  382. @"i" : @{@"zip" : [NSNull null]},
  383. @"j" : @{@"code" : @500},
  384. };
  385. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  386. // Search for zips not matching 98101.
  387. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  388. isNotEqualTo:@98101]];
  389. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  390. testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"],
  391. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  392. ]));
  393. // With objects.
  394. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  395. isNotEqualTo:@{@"code" : @500}]];
  396. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  397. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  398. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  399. ]));
  400. // With null.
  401. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  402. isNotEqualTo:@[ [NSNull null] ]]];
  403. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  404. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  405. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  406. ]));
  407. // With NAN.
  408. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" isNotEqualTo:@(NAN)]];
  409. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  410. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  411. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  412. ]));
  413. }
  414. - (void)testQueriesCanUseNotEqualFiltersWithDocIds {
  415. NSDictionary *testDocs = @{
  416. @"aa" : @{@"key" : @"aa"},
  417. @"ab" : @{@"key" : @"ab"},
  418. @"ba" : @{@"key" : @"ba"},
  419. @"bb" : @{@"key" : @"bb"},
  420. };
  421. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  422. FIRQuerySnapshot *snapshot =
  423. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  424. isNotEqualTo:@"aa"]];
  425. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  426. (@[ testDocs[@"ab"], testDocs[@"ba"], testDocs[@"bb"] ]));
  427. }
  428. - (void)testQueriesCanUseArrayContainsFilters {
  429. NSDictionary *testDocs = @{
  430. @"a" : @{@"array" : @[ @42 ]},
  431. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  432. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  433. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  434. @"e" : @{@"array" : @[ [NSNull null] ]},
  435. @"f" : @{@"array" : @[ @(NAN) ]},
  436. };
  437. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  438. // Search for 42
  439. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  440. arrayContains:@42]];
  441. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  442. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  443. // With null.
  444. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  445. arrayContains:[NSNull null]]];
  446. XCTAssertTrue(snapshot.isEmpty);
  447. // With NAN.
  448. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  449. arrayContains:@(NAN)]];
  450. XCTAssertTrue(snapshot.isEmpty);
  451. }
  452. - (void)testQueriesCanUseInFilters {
  453. NSDictionary *testDocs = @{
  454. @"a" : @{@"zip" : @98101},
  455. @"b" : @{@"zip" : @91102},
  456. @"c" : @{@"zip" : @98103},
  457. @"d" : @{@"zip" : @[ @98101 ]},
  458. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  459. @"f" : @{@"zip" : @{@"code" : @500}},
  460. @"g" : @{@"zip" : @[ @98101, @98102 ]},
  461. @"h" : @{@"zip" : [NSNull null]},
  462. @"i" : @{@"zip" : @(NAN)}
  463. };
  464. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  465. // Search for zips matching 98101, 98103, and [98101, 98102].
  466. FIRQuerySnapshot *snapshot = [self
  467. readDocumentSetForRef:[collection queryWhereField:@"zip"
  468. in:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  469. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  470. (@[ testDocs[@"a"], testDocs[@"c"], testDocs[@"g"] ]));
  471. // With objects
  472. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  473. in:@[ @{@"code" : @500} ]]];
  474. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  475. // With null.
  476. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ [NSNull null] ]]];
  477. XCTAssertTrue(snapshot.isEmpty);
  478. // With null and a value.
  479. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  480. in:@[ [NSNull null], @98101 ]]];
  481. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  482. // With NAN.
  483. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ @(NAN) ]]];
  484. XCTAssertTrue(snapshot.isEmpty);
  485. // With NAN and a value.
  486. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  487. in:@[ @(NAN), @98101 ]]];
  488. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  489. }
  490. - (void)testQueriesCanUseInFiltersWithDocIds {
  491. NSDictionary *testDocs = @{
  492. @"aa" : @{@"key" : @"aa"},
  493. @"ab" : @{@"key" : @"ab"},
  494. @"ba" : @{@"key" : @"ba"},
  495. @"bb" : @{@"key" : @"bb"},
  496. };
  497. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  498. FIRQuerySnapshot *snapshot =
  499. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  500. in:@[ @"aa", @"ab" ]]];
  501. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  502. }
  503. - (void)testQueriesCanUseNotInFilters {
  504. // These documents are ordered by value in "zip" since the NOT_IN filter is an inequality, which
  505. // results in documents being sorted by value.
  506. NSDictionary *testDocs = @{
  507. @"a" : @{@"zip" : @(NAN)},
  508. @"b" : @{@"zip" : @91102},
  509. @"c" : @{@"zip" : @98101},
  510. @"d" : @{@"zip" : @98103},
  511. @"e" : @{@"zip" : @[ @98101 ]},
  512. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  513. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  514. @"h" : @{@"zip" : @{@"code" : @500}},
  515. @"i" : @{@"zip" : [NSNull null]},
  516. @"j" : @{@"code" : @500},
  517. };
  518. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  519. // Search for zips not matching 98101, 98103, and [98101, 98102].
  520. FIRQuerySnapshot *snapshot = [self
  521. readDocumentSetForRef:[collection queryWhereField:@"zip"
  522. notIn:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  523. XCTAssertEqualObjects(
  524. FIRQuerySnapshotGetData(snapshot),
  525. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"e"], testDocs[@"g"], testDocs[@"h"] ]));
  526. // With objects.
  527. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  528. notIn:@[ @{@"code" : @500} ]]];
  529. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  530. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  531. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  532. ]));
  533. // With null.
  534. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  535. notIn:@[ [NSNull null] ]]];
  536. XCTAssertTrue(snapshot.isEmpty);
  537. // With NAN.
  538. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" notIn:@[ @(NAN) ]]];
  539. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  540. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  541. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  542. ]));
  543. // With NAN and a number.
  544. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  545. notIn:@[ @(NAN), @98101 ]]];
  546. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  547. testDocs[@"b"], testDocs[@"d"], testDocs[@"e"], testDocs[@"f"],
  548. testDocs[@"g"], testDocs[@"h"]
  549. ]));
  550. }
  551. - (void)testQueriesCanUseNotInFiltersWithDocIds {
  552. NSDictionary *testDocs = @{
  553. @"aa" : @{@"key" : @"aa"},
  554. @"ab" : @{@"key" : @"ab"},
  555. @"ba" : @{@"key" : @"ba"},
  556. @"bb" : @{@"key" : @"bb"},
  557. };
  558. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  559. FIRQuerySnapshot *snapshot =
  560. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  561. notIn:@[ @"aa", @"ab" ]]];
  562. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"ba"], testDocs[@"bb"] ]));
  563. }
  564. - (void)testQueriesCanUseArrayContainsAnyFilters {
  565. NSDictionary *testDocs = @{
  566. @"a" : @{@"array" : @[ @42 ]},
  567. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  568. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  569. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  570. @"e" : @{@"array" : @[ @43 ]},
  571. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  572. @"g" : @{@"array" : @42},
  573. @"h" : @{@"array" : @[ [NSNull null] ]},
  574. @"i" : @{@"array" : @[ @(NAN) ]},
  575. };
  576. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  577. // Search for zips matching [42, 43].
  578. FIRQuerySnapshot *snapshot = [self
  579. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  580. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  581. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  582. // With objects.
  583. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  584. arrayContainsAny:@[ @{@"a" : @42} ]]];
  585. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  586. testDocs[@"f"],
  587. ]));
  588. // With null.
  589. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  590. arrayContainsAny:@[ [NSNull null] ]]];
  591. XCTAssertTrue(snapshot.isEmpty);
  592. // With null and a value.
  593. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  594. arrayContainsAny:@[ [NSNull null], @43 ]]];
  595. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  596. // With NAN.
  597. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  598. arrayContainsAny:@[ @(NAN) ]]];
  599. XCTAssertTrue(snapshot.isEmpty);
  600. // With NAN and a value.
  601. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  602. arrayContainsAny:@[ @(NAN), @43 ]]];
  603. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  604. }
  605. - (void)testCollectionGroupQueries {
  606. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  607. // for predictable ordering.
  608. NSString *collectionGroup = [NSString
  609. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  610. NSArray *docPaths = @[
  611. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  612. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  613. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  614. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  615. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  616. @"abc/${collectionGroup}"
  617. ];
  618. FIRWriteBatch *batch = [self.db batch];
  619. for (NSString *docPath in docPaths) {
  620. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  621. withString:collectionGroup];
  622. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  623. }
  624. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  625. [batch commitWithCompletion:^(NSError *error) {
  626. XCTAssertNil(error);
  627. [expectation fulfill];
  628. }];
  629. [self awaitExpectations];
  630. FIRQuerySnapshot *querySnapshot =
  631. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  632. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  633. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  634. }
  635. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  636. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  637. // for predictable ordering.
  638. NSString *collectionGroup = [NSString
  639. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  640. NSArray *docPaths = @[
  641. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  642. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  643. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  644. ];
  645. FIRWriteBatch *batch = [self.db batch];
  646. for (NSString *docPath in docPaths) {
  647. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  648. withString:collectionGroup];
  649. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  650. }
  651. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  652. [batch commitWithCompletion:^(NSError *error) {
  653. XCTAssertNil(error);
  654. [expectation fulfill];
  655. }];
  656. [self awaitExpectations];
  657. FIRQuerySnapshot *querySnapshot = [self
  658. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  659. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  660. queryStartingAfterValues:@[ @"a/b" ]]
  661. queryEndingBeforeValues:@[
  662. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  663. ]]];
  664. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  665. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  666. }
  667. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  668. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  669. // for predictable ordering.
  670. NSString *collectionGroup = [NSString
  671. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  672. NSArray *docPaths = @[
  673. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  674. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  675. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  676. ];
  677. FIRWriteBatch *batch = [self.db batch];
  678. for (NSString *docPath in docPaths) {
  679. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  680. withString:collectionGroup];
  681. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  682. }
  683. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  684. [batch commitWithCompletion:^(NSError *error) {
  685. XCTAssertNil(error);
  686. [expectation fulfill];
  687. }];
  688. [self awaitExpectations];
  689. FIRQuerySnapshot *querySnapshot = [self
  690. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  691. queryWhereFieldPath:[FIRFieldPath documentID]
  692. isGreaterThanOrEqualTo:@"a/b"]
  693. queryWhereFieldPath:[FIRFieldPath documentID]
  694. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  695. collectionGroup]]];
  696. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  697. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  698. }
  699. - (void)testOrQueries {
  700. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  701. @"doc1" : @{@"a" : @1, @"b" : @0},
  702. @"doc2" : @{@"a" : @2, @"b" : @1},
  703. @"doc3" : @{@"a" : @3, @"b" : @2},
  704. @"doc4" : @{@"a" : @1, @"b" : @3},
  705. @"doc5" : @{@"a" : @1, @"b" : @1}
  706. }];
  707. // Two equalities: a==1 || b==1.
  708. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  709. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  710. ]];
  711. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  712. matchesResult:@[ @"doc1", @"doc2", @"doc4", @"doc5" ]];
  713. // with one inequality: a>2 || b==1.
  714. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  715. [FIRFilter filterWhereField:@"a" isGreaterThan:@2], [FIRFilter filterWhereField:@"b"
  716. isEqualTo:@1]
  717. ]];
  718. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  719. matchesResult:@[ @"doc5", @"doc2", @"doc3" ]];
  720. // (a==1 && b==0) || (a==3 && b==2)
  721. FIRFilter *filter3 = [FIRFilter orFilterWithFilters:@[
  722. [FIRFilter andFilterWithFilters:@[
  723. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@0]
  724. ]],
  725. [FIRFilter andFilterWithFilters:@[
  726. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  727. ]]
  728. ]];
  729. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter3]
  730. matchesResult:@[ @"doc1", @"doc3" ]];
  731. // a==1 && (b==0 || b==3).
  732. FIRFilter *filter4 = [FIRFilter andFilterWithFilters:@[
  733. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter orFilterWithFilters:@[
  734. [FIRFilter filterWhereField:@"b" isEqualTo:@0], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  735. ]]
  736. ]];
  737. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter4]
  738. matchesResult:@[ @"doc1", @"doc4" ]];
  739. // (a==2 || b==2) && (a==3 || b==3)
  740. FIRFilter *filter5 = [FIRFilter andFilterWithFilters:@[
  741. [FIRFilter orFilterWithFilters:@[
  742. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  743. ]],
  744. [FIRFilter orFilterWithFilters:@[
  745. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  746. ]]
  747. ]];
  748. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter5] matchesResult:@[ @"doc3" ]];
  749. // Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2
  750. FIRFilter *filter6 = [FIRFilter orFilterWithFilters:@[
  751. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b"
  752. isGreaterThan:@0]
  753. ]];
  754. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:filter6] queryLimitedTo:2]
  755. matchesResult:@[ @"doc1", @"doc2" ]];
  756. // Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2
  757. // Note: The public query API does not allow implicit ordering when limitToLast is used.
  758. FIRFilter *filter7 = [FIRFilter orFilterWithFilters:@[
  759. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b"
  760. isGreaterThan:@0]
  761. ]];
  762. [self checkOnlineAndOfflineQuery:[[[collRef queryWhereFilter:filter7] queryLimitedToLast:2]
  763. queryOrderedByField:@"b"]
  764. matchesResult:@[ @"doc3", @"doc4" ]];
  765. // Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1
  766. FIRFilter *filter8 = [FIRFilter orFilterWithFilters:@[
  767. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  768. ]];
  769. [self checkOnlineAndOfflineQuery:[[[collRef queryWhereFilter:filter8] queryLimitedTo:1]
  770. queryOrderedByField:@"a"]
  771. matchesResult:@[ @"doc5" ]];
  772. // Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1
  773. FIRFilter *filter9 = [FIRFilter orFilterWithFilters:@[
  774. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  775. ]];
  776. [self checkOnlineAndOfflineQuery:[[[collRef queryWhereFilter:filter9] queryLimitedToLast:1]
  777. queryOrderedByField:@"a"]
  778. matchesResult:@[ @"doc2" ]];
  779. // Test with limits without orderBy (the __name__ ordering is the tie breaker).
  780. FIRFilter *filter10 = [FIRFilter orFilterWithFilters:@[
  781. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  782. ]];
  783. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:filter10] queryLimitedTo:1]
  784. matchesResult:@[ @"doc2" ]];
  785. }
  786. - (void)testOrQueriesWithInAndNotIn {
  787. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  788. @"doc1" : @{@"a" : @1, @"b" : @0},
  789. @"doc2" : @{@"b" : @1},
  790. @"doc3" : @{@"a" : @3, @"b" : @2},
  791. @"doc4" : @{@"a" : @1, @"b" : @3},
  792. @"doc5" : @{@"a" : @1},
  793. @"doc6" : @{@"a" : @2}
  794. }];
  795. // a==2 || b in [2,3]
  796. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  797. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" in:@[ @2, @3 ]]
  798. ]];
  799. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  800. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  801. // a==2 || b not-in [2,3]
  802. // Has implicit orderBy b.
  803. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  804. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  805. notIn:@[ @2, @3 ]]
  806. ]];
  807. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  808. matchesResult:@[ @"doc1", @"doc2" ]];
  809. }
  810. - (void)testOrQueriesWithArrayMembership {
  811. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  812. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  813. @"doc2" : @{@"b" : @[ @1 ]},
  814. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ]},
  815. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  816. @"doc5" : @{@"a" : @1},
  817. @"doc6" : @{@"a" : @2}
  818. }];
  819. // a==2 || b array-contains 7
  820. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  821. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  822. arrayContains:@7]
  823. ]];
  824. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  825. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  826. // a==2 || b array-contains-any [0, 3]
  827. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  828. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  829. arrayContainsAny:@[ @0, @3 ]]
  830. ]];
  831. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  832. matchesResult:@[ @"doc1", @"doc4", @"doc6" ]];
  833. }
  834. @end