FIRQueryTests.mm 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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)testQueriesCanRaiseInitialSnapshotFromCachedEmptyResults {
  334. FIRCollectionReference *collection = [self collectionRefWithDocuments:@{}];
  335. // Populate the cache with empty query result.
  336. FIRQuerySnapshot *querySnapshotA = [self readDocumentSetForRef:collection];
  337. XCTAssertFalse(querySnapshotA.metadata.fromCache);
  338. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotA), @[]);
  339. // Add a snapshot listener whose first event should be raised from cache.
  340. id<FIRListenerRegistration> registration = [collection
  341. addSnapshotListenerWithIncludeMetadataChanges:YES
  342. listener:self.eventAccumulator.valueEventHandler];
  343. FIRQuerySnapshot *querySnapshotB = [self.eventAccumulator awaitEventWithName:@"initial event"];
  344. XCTAssertTrue(querySnapshotB.metadata.fromCache);
  345. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotB), @[]);
  346. [registration remove];
  347. }
  348. - (void)testQueriesCanRaiseInitialSnapshotFromEmptyDueToDeleteCachedResults {
  349. NSDictionary *testDocs = @{
  350. @"a" : @{@"foo" : @1},
  351. };
  352. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  353. // Populate the cache with a single document.
  354. FIRQuerySnapshot *querySnapshotA = [self readDocumentSetForRef:collection];
  355. XCTAssertFalse(querySnapshotA.metadata.fromCache);
  356. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotA), @[ @{@"foo" : @1} ]);
  357. // Delete the document, making the cached query result empty.
  358. FIRDocumentReference *docRef = [collection documentWithPath:@"a"];
  359. [self deleteDocumentRef:docRef];
  360. // Add a snapshot listener whose first event should be raised from cache.
  361. id<FIRListenerRegistration> registration = [collection
  362. addSnapshotListenerWithIncludeMetadataChanges:YES
  363. listener:self.eventAccumulator.valueEventHandler];
  364. FIRQuerySnapshot *querySnapshotB = [self.eventAccumulator awaitEventWithName:@"initial event"];
  365. XCTAssertTrue(querySnapshotB.metadata.fromCache);
  366. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotB), @[]);
  367. [registration remove];
  368. }
  369. - (void)testDocumentChangesUseNSNotFound {
  370. NSDictionary *testDocs = @{
  371. @"a" : @{@"foo" : @1},
  372. };
  373. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  374. id<FIRListenerRegistration> registration =
  375. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  376. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  377. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  378. FIRDocumentChange *change = querySnap.documentChanges[0];
  379. XCTAssertEqual(change.oldIndex, NSNotFound);
  380. XCTAssertEqual(change.newIndex, 0ul);
  381. FIRDocumentReference *doc = change.document.reference;
  382. [self deleteDocumentRef:doc];
  383. querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
  384. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  385. change = querySnap.documentChanges[0];
  386. XCTAssertEqual(change.oldIndex, 0ul);
  387. XCTAssertEqual(change.newIndex, NSNotFound);
  388. [registration remove];
  389. }
  390. - (void)testCanHaveMultipleMutationsWhileOffline {
  391. FIRCollectionReference *col = [self collectionRef];
  392. // set a few docs to known values
  393. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  394. [self writeAllDocuments:initialDocs toCollection:col];
  395. // go offline for the rest of this test
  396. [self disableNetwork];
  397. // apply *multiple* mutations while offline
  398. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  399. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  400. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  401. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  402. @{@"key1b" : @"value1b"},
  403. @{@"key2b" : @"value2b"},
  404. ]));
  405. }
  406. - (void)testQueriesCanUseNotEqualFilters {
  407. // These documents are ordered by value in "zip" since notEquals filter is an inequality, which
  408. // results in documents being sorted by value.
  409. NSDictionary *testDocs = @{
  410. @"a" : @{@"zip" : @(NAN)},
  411. @"b" : @{@"zip" : @91102},
  412. @"c" : @{@"zip" : @98101},
  413. @"d" : @{@"zip" : @98103},
  414. @"e" : @{@"zip" : @[ @98101 ]},
  415. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  416. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  417. @"h" : @{@"zip" : @{@"code" : @500}},
  418. @"i" : @{@"zip" : [NSNull null]},
  419. @"j" : @{@"code" : @500},
  420. };
  421. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  422. // Search for zips not matching 98101.
  423. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  424. isNotEqualTo:@98101]];
  425. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  426. testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"],
  427. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  428. ]));
  429. // With objects.
  430. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  431. isNotEqualTo:@{@"code" : @500}]];
  432. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  433. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  434. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  435. ]));
  436. // With null.
  437. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  438. isNotEqualTo:@[ [NSNull null] ]]];
  439. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  440. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  441. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  442. ]));
  443. // With NAN.
  444. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" isNotEqualTo:@(NAN)]];
  445. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  446. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  447. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  448. ]));
  449. }
  450. - (void)testQueriesCanUseNotEqualFiltersWithDocIds {
  451. NSDictionary *testDocs = @{
  452. @"aa" : @{@"key" : @"aa"},
  453. @"ab" : @{@"key" : @"ab"},
  454. @"ba" : @{@"key" : @"ba"},
  455. @"bb" : @{@"key" : @"bb"},
  456. };
  457. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  458. FIRQuerySnapshot *snapshot =
  459. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  460. isNotEqualTo:@"aa"]];
  461. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  462. (@[ testDocs[@"ab"], testDocs[@"ba"], testDocs[@"bb"] ]));
  463. }
  464. - (void)testQueriesCanUseArrayContainsFilters {
  465. NSDictionary *testDocs = @{
  466. @"a" : @{@"array" : @[ @42 ]},
  467. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  468. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  469. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  470. @"e" : @{@"array" : @[ [NSNull null] ]},
  471. @"f" : @{@"array" : @[ @(NAN) ]},
  472. };
  473. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  474. // Search for 42
  475. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  476. arrayContains:@42]];
  477. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  478. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  479. // With null.
  480. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  481. arrayContains:[NSNull null]]];
  482. XCTAssertTrue(snapshot.isEmpty);
  483. // With NAN.
  484. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  485. arrayContains:@(NAN)]];
  486. XCTAssertTrue(snapshot.isEmpty);
  487. }
  488. - (void)testQueriesCanUseInFilters {
  489. NSDictionary *testDocs = @{
  490. @"a" : @{@"zip" : @98101},
  491. @"b" : @{@"zip" : @91102},
  492. @"c" : @{@"zip" : @98103},
  493. @"d" : @{@"zip" : @[ @98101 ]},
  494. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  495. @"f" : @{@"zip" : @{@"code" : @500}},
  496. @"g" : @{@"zip" : @[ @98101, @98102 ]},
  497. @"h" : @{@"zip" : [NSNull null]},
  498. @"i" : @{@"zip" : @(NAN)}
  499. };
  500. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  501. // Search for zips matching 98101, 98103, and [98101, 98102].
  502. FIRQuerySnapshot *snapshot = [self
  503. readDocumentSetForRef:[collection queryWhereField:@"zip"
  504. in:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  505. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  506. (@[ testDocs[@"a"], testDocs[@"c"], testDocs[@"g"] ]));
  507. // With objects
  508. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  509. in:@[ @{@"code" : @500} ]]];
  510. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  511. // With null.
  512. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ [NSNull null] ]]];
  513. XCTAssertTrue(snapshot.isEmpty);
  514. // With null and a value.
  515. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  516. in:@[ [NSNull null], @98101 ]]];
  517. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  518. // With NAN.
  519. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ @(NAN) ]]];
  520. XCTAssertTrue(snapshot.isEmpty);
  521. // With NAN and a value.
  522. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  523. in:@[ @(NAN), @98101 ]]];
  524. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  525. }
  526. - (void)testQueriesCanUseInFiltersWithDocIds {
  527. NSDictionary *testDocs = @{
  528. @"aa" : @{@"key" : @"aa"},
  529. @"ab" : @{@"key" : @"ab"},
  530. @"ba" : @{@"key" : @"ba"},
  531. @"bb" : @{@"key" : @"bb"},
  532. };
  533. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  534. FIRQuerySnapshot *snapshot =
  535. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  536. in:@[ @"aa", @"ab" ]]];
  537. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  538. }
  539. - (void)testQueriesCanUseNotInFilters {
  540. // These documents are ordered by value in "zip" since the NOT_IN filter is an inequality, which
  541. // results in documents being sorted by value.
  542. NSDictionary *testDocs = @{
  543. @"a" : @{@"zip" : @(NAN)},
  544. @"b" : @{@"zip" : @91102},
  545. @"c" : @{@"zip" : @98101},
  546. @"d" : @{@"zip" : @98103},
  547. @"e" : @{@"zip" : @[ @98101 ]},
  548. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  549. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  550. @"h" : @{@"zip" : @{@"code" : @500}},
  551. @"i" : @{@"zip" : [NSNull null]},
  552. @"j" : @{@"code" : @500},
  553. };
  554. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  555. // Search for zips not matching 98101, 98103, and [98101, 98102].
  556. FIRQuerySnapshot *snapshot = [self
  557. readDocumentSetForRef:[collection queryWhereField:@"zip"
  558. notIn:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  559. XCTAssertEqualObjects(
  560. FIRQuerySnapshotGetData(snapshot),
  561. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"e"], testDocs[@"g"], testDocs[@"h"] ]));
  562. // With objects.
  563. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  564. notIn:@[ @{@"code" : @500} ]]];
  565. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  566. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  567. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  568. ]));
  569. // With null.
  570. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  571. notIn:@[ [NSNull null] ]]];
  572. XCTAssertTrue(snapshot.isEmpty);
  573. // With NAN.
  574. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" notIn:@[ @(NAN) ]]];
  575. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  576. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  577. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  578. ]));
  579. // With NAN and a number.
  580. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  581. notIn:@[ @(NAN), @98101 ]]];
  582. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  583. testDocs[@"b"], testDocs[@"d"], testDocs[@"e"], testDocs[@"f"],
  584. testDocs[@"g"], testDocs[@"h"]
  585. ]));
  586. }
  587. - (void)testQueriesCanUseNotInFiltersWithDocIds {
  588. NSDictionary *testDocs = @{
  589. @"aa" : @{@"key" : @"aa"},
  590. @"ab" : @{@"key" : @"ab"},
  591. @"ba" : @{@"key" : @"ba"},
  592. @"bb" : @{@"key" : @"bb"},
  593. };
  594. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  595. FIRQuerySnapshot *snapshot =
  596. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  597. notIn:@[ @"aa", @"ab" ]]];
  598. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"ba"], testDocs[@"bb"] ]));
  599. }
  600. - (void)testQueriesCanUseArrayContainsAnyFilters {
  601. NSDictionary *testDocs = @{
  602. @"a" : @{@"array" : @[ @42 ]},
  603. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  604. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  605. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  606. @"e" : @{@"array" : @[ @43 ]},
  607. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  608. @"g" : @{@"array" : @42},
  609. @"h" : @{@"array" : @[ [NSNull null] ]},
  610. @"i" : @{@"array" : @[ @(NAN) ]},
  611. };
  612. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  613. // Search for zips matching [42, 43].
  614. FIRQuerySnapshot *snapshot = [self
  615. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  616. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  617. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  618. // With objects.
  619. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  620. arrayContainsAny:@[ @{@"a" : @42} ]]];
  621. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  622. testDocs[@"f"],
  623. ]));
  624. // With null.
  625. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  626. arrayContainsAny:@[ [NSNull null] ]]];
  627. XCTAssertTrue(snapshot.isEmpty);
  628. // With null and a value.
  629. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  630. arrayContainsAny:@[ [NSNull null], @43 ]]];
  631. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  632. // With NAN.
  633. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  634. arrayContainsAny:@[ @(NAN) ]]];
  635. XCTAssertTrue(snapshot.isEmpty);
  636. // With NAN and a value.
  637. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  638. arrayContainsAny:@[ @(NAN), @43 ]]];
  639. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  640. }
  641. - (void)testCollectionGroupQueries {
  642. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  643. // for predictable ordering.
  644. NSString *collectionGroup = [NSString
  645. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  646. NSArray *docPaths = @[
  647. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  648. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  649. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  650. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  651. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  652. @"abc/${collectionGroup}"
  653. ];
  654. FIRWriteBatch *batch = [self.db batch];
  655. for (NSString *docPath in docPaths) {
  656. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  657. withString:collectionGroup];
  658. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  659. }
  660. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  661. [batch commitWithCompletion:^(NSError *error) {
  662. XCTAssertNil(error);
  663. [expectation fulfill];
  664. }];
  665. [self awaitExpectations];
  666. FIRQuerySnapshot *querySnapshot =
  667. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  668. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  669. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  670. }
  671. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  672. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  673. // for predictable ordering.
  674. NSString *collectionGroup = [NSString
  675. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  676. NSArray *docPaths = @[
  677. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  678. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  679. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  680. ];
  681. FIRWriteBatch *batch = [self.db batch];
  682. for (NSString *docPath in docPaths) {
  683. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  684. withString:collectionGroup];
  685. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  686. }
  687. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  688. [batch commitWithCompletion:^(NSError *error) {
  689. XCTAssertNil(error);
  690. [expectation fulfill];
  691. }];
  692. [self awaitExpectations];
  693. FIRQuerySnapshot *querySnapshot = [self
  694. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  695. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  696. queryStartingAfterValues:@[ @"a/b" ]]
  697. queryEndingBeforeValues:@[
  698. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  699. ]]];
  700. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  701. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  702. }
  703. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  704. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  705. // for predictable ordering.
  706. NSString *collectionGroup = [NSString
  707. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  708. NSArray *docPaths = @[
  709. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  710. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  711. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  712. ];
  713. FIRWriteBatch *batch = [self.db batch];
  714. for (NSString *docPath in docPaths) {
  715. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  716. withString:collectionGroup];
  717. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  718. }
  719. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  720. [batch commitWithCompletion:^(NSError *error) {
  721. XCTAssertNil(error);
  722. [expectation fulfill];
  723. }];
  724. [self awaitExpectations];
  725. FIRQuerySnapshot *querySnapshot = [self
  726. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  727. queryWhereFieldPath:[FIRFieldPath documentID]
  728. isGreaterThanOrEqualTo:@"a/b"]
  729. queryWhereFieldPath:[FIRFieldPath documentID]
  730. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  731. collectionGroup]]];
  732. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  733. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  734. }
  735. - (void)testOrQueries {
  736. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  737. @"doc1" : @{@"a" : @1, @"b" : @0},
  738. @"doc2" : @{@"a" : @2, @"b" : @1},
  739. @"doc3" : @{@"a" : @3, @"b" : @2},
  740. @"doc4" : @{@"a" : @1, @"b" : @3},
  741. @"doc5" : @{@"a" : @1, @"b" : @1}
  742. }];
  743. // Two equalities: a==1 || b==1.
  744. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  745. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  746. ]];
  747. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  748. matchesResult:@[ @"doc1", @"doc2", @"doc4", @"doc5" ]];
  749. // with one inequality: a>2 || b==1.
  750. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  751. [FIRFilter filterWhereField:@"a" isGreaterThan:@2], [FIRFilter filterWhereField:@"b"
  752. isEqualTo:@1]
  753. ]];
  754. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  755. matchesResult:@[ @"doc5", @"doc2", @"doc3" ]];
  756. // (a==1 && b==0) || (a==3 && b==2)
  757. FIRFilter *filter3 = [FIRFilter orFilterWithFilters:@[
  758. [FIRFilter andFilterWithFilters:@[
  759. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@0]
  760. ]],
  761. [FIRFilter andFilterWithFilters:@[
  762. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  763. ]]
  764. ]];
  765. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter3]
  766. matchesResult:@[ @"doc1", @"doc3" ]];
  767. // a==1 && (b==0 || b==3).
  768. FIRFilter *filter4 = [FIRFilter andFilterWithFilters:@[
  769. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter orFilterWithFilters:@[
  770. [FIRFilter filterWhereField:@"b" isEqualTo:@0], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  771. ]]
  772. ]];
  773. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter4]
  774. matchesResult:@[ @"doc1", @"doc4" ]];
  775. // (a==2 || b==2) && (a==3 || b==3)
  776. FIRFilter *filter5 = [FIRFilter andFilterWithFilters:@[
  777. [FIRFilter orFilterWithFilters:@[
  778. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  779. ]],
  780. [FIRFilter orFilterWithFilters:@[
  781. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  782. ]]
  783. ]];
  784. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter5] matchesResult:@[ @"doc3" ]];
  785. // Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2
  786. FIRFilter *filter6 = [FIRFilter orFilterWithFilters:@[
  787. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b"
  788. isGreaterThan:@0]
  789. ]];
  790. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:filter6] queryLimitedTo:2]
  791. matchesResult:@[ @"doc1", @"doc2" ]];
  792. // Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2
  793. // Note: The public query API does not allow implicit ordering when limitToLast is used.
  794. FIRFilter *filter7 = [FIRFilter orFilterWithFilters:@[
  795. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b"
  796. isGreaterThan:@0]
  797. ]];
  798. [self checkOnlineAndOfflineQuery:[[[collRef queryWhereFilter:filter7] queryLimitedToLast:2]
  799. queryOrderedByField:@"b"]
  800. matchesResult:@[ @"doc3", @"doc4" ]];
  801. // Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1
  802. FIRFilter *filter8 = [FIRFilter orFilterWithFilters:@[
  803. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  804. ]];
  805. [self checkOnlineAndOfflineQuery:[[[collRef queryWhereFilter:filter8] queryLimitedTo:1]
  806. queryOrderedByField:@"a"]
  807. matchesResult:@[ @"doc5" ]];
  808. // Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1
  809. FIRFilter *filter9 = [FIRFilter orFilterWithFilters:@[
  810. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  811. ]];
  812. [self checkOnlineAndOfflineQuery:[[[collRef queryWhereFilter:filter9] queryLimitedToLast:1]
  813. queryOrderedByField:@"a"]
  814. matchesResult:@[ @"doc2" ]];
  815. // Test with limits without orderBy (the __name__ ordering is the tie breaker).
  816. FIRFilter *filter10 = [FIRFilter orFilterWithFilters:@[
  817. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  818. ]];
  819. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:filter10] queryLimitedTo:1]
  820. matchesResult:@[ @"doc2" ]];
  821. }
  822. - (void)testOrQueriesWithInAndNotIn {
  823. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  824. @"doc1" : @{@"a" : @1, @"b" : @0},
  825. @"doc2" : @{@"b" : @1},
  826. @"doc3" : @{@"a" : @3, @"b" : @2},
  827. @"doc4" : @{@"a" : @1, @"b" : @3},
  828. @"doc5" : @{@"a" : @1},
  829. @"doc6" : @{@"a" : @2}
  830. }];
  831. // a==2 || b in [2,3]
  832. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  833. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" in:@[ @2, @3 ]]
  834. ]];
  835. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  836. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  837. // a==2 || b not-in [2,3]
  838. // Has implicit orderBy b.
  839. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  840. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  841. notIn:@[ @2, @3 ]]
  842. ]];
  843. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  844. matchesResult:@[ @"doc1", @"doc2" ]];
  845. }
  846. - (void)testOrQueriesWithArrayMembership {
  847. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  848. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  849. @"doc2" : @{@"b" : @[ @1 ]},
  850. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ]},
  851. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  852. @"doc5" : @{@"a" : @1},
  853. @"doc6" : @{@"a" : @2}
  854. }];
  855. // a==2 || b array-contains 7
  856. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  857. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  858. arrayContains:@7]
  859. ]];
  860. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  861. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  862. // a==2 || b array-contains-any [0, 3]
  863. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  864. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  865. arrayContainsAny:@[ @0, @3 ]]
  866. ]];
  867. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  868. matchesResult:@[ @"doc1", @"doc4", @"doc6" ]];
  869. }
  870. @end