FIRQueryTests.mm 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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/Example/Tests/Util/FSTEventAccumulator.h"
  19. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  20. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  21. #import "Firestore/Example/Tests/Util/FSTTestingHooks.h"
  22. // TODO(MIEQ) update these imports with public imports when aggregate types are public
  23. #import "Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h"
  24. #import "Firestore/Source/API/FIRQuery+Internal.h"
  25. @interface FIRQueryTests : FSTIntegrationTestCase
  26. @end
  27. @implementation FIRQueryTests
  28. - (void)testLimitQueries {
  29. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  30. @"a" : @{@"k" : @"a"},
  31. @"b" : @{@"k" : @"b"},
  32. @"c" : @{@"k" : @"c"}
  33. }];
  34. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  35. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  36. }
  37. - (void)testLimitQueriesWithDescendingSortOrder {
  38. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  39. @"a" : @{@"k" : @"a", @"sort" : @0},
  40. @"b" : @{@"k" : @"b", @"sort" : @1},
  41. @"c" : @{@"k" : @"c", @"sort" : @1},
  42. @"d" : @{@"k" : @"d", @"sort" : @2},
  43. }];
  44. FIRQuerySnapshot *snapshot =
  45. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort"
  46. descending:YES] queryLimitedTo:2]];
  47. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  48. (@[ @{@"k" : @"d", @"sort" : @2}, @{@"k" : @"c", @"sort" : @1} ]));
  49. }
  50. - (void)testLimitToLastMustAlsoHaveExplicitOrderBy {
  51. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{}];
  52. FIRQuery *query = [collRef queryLimitedToLast:2];
  53. FSTAssertThrows([query getDocumentsWithCompletion:^(FIRQuerySnapshot *, NSError *){
  54. }],
  55. @"limit(toLast:) queries require specifying at least one OrderBy() clause.");
  56. }
  57. // Two queries that mapped to the same target ID are referred to as
  58. // "mirror queries". An example for a mirror query is a limitToLast()
  59. // query and a limit() query that share the same backend Target ID.
  60. // Since limitToLast() queries are sent to the backend with a modified
  61. // orderBy() clause, they can map to the same target representation as
  62. // limit() query, even if both queries appear separate to the user.
  63. - (void)testListenUnlistenRelistenSequenceOfMirrorQueries {
  64. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  65. @"a" : @{@"k" : @"a", @"sort" : @0},
  66. @"b" : @{@"k" : @"b", @"sort" : @1},
  67. @"c" : @{@"k" : @"c", @"sort" : @1},
  68. @"d" : @{@"k" : @"d", @"sort" : @2},
  69. }];
  70. // Setup a `limit` query.
  71. FIRQuery *limit = [[collRef queryOrderedByField:@"sort" descending:NO] queryLimitedTo:2];
  72. FSTEventAccumulator *limitAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  73. id<FIRListenerRegistration> limitRegistration =
  74. [limit addSnapshotListener:limitAccumulator.valueEventHandler];
  75. // Setup a mirroring `limitToLast` query.
  76. FIRQuery *limitToLast = [[collRef queryOrderedByField:@"sort"
  77. descending:YES] queryLimitedToLast:2];
  78. FSTEventAccumulator *limitToLastAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  79. id<FIRListenerRegistration> limitToLastRegistration =
  80. [limitToLast addSnapshotListener:limitToLastAccumulator.valueEventHandler];
  81. // Verify both queries get expected result.
  82. FIRQuerySnapshot *snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  83. NSArray *expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1} ];
  84. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  85. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  86. expected = @[ @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"a", @"sort" : @0} ];
  87. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  88. // Unlisten then re-listen limit query.
  89. [limitRegistration remove];
  90. [limit addSnapshotListener:[limitAccumulator valueEventHandler]];
  91. // Verify limit query still works.
  92. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  93. expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1} ];
  94. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  95. // Add a document that would change the result set.
  96. [self addDocumentRef:collRef data:@{@"k" : @"e", @"sort" : @-1}];
  97. // Verify both queries get expected result.
  98. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  99. expected = @[ @{@"k" : @"e", @"sort" : @-1}, @{@"k" : @"a", @"sort" : @0} ];
  100. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  101. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  102. expected = @[ @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"e", @"sort" : @-1} ];
  103. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  104. // Unlisten to limitToLast, update a doc, then relisten to limitToLast
  105. [limitToLastRegistration remove];
  106. [self updateDocumentRef:[collRef documentWithPath:@"a"] data:@{@"k" : @"a", @"sort" : @-2}];
  107. [limitToLast addSnapshotListener:[limitToLastAccumulator valueEventHandler]];
  108. // Verify both queries get expected result.
  109. snapshot = [limitAccumulator awaitEventWithName:@"Snapshot"];
  110. expected = @[ @{@"k" : @"a", @"sort" : @-2}, @{@"k" : @"e", @"sort" : @-1} ];
  111. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  112. snapshot = [limitToLastAccumulator awaitEventWithName:@"Snapshot"];
  113. expected = @[ @{@"k" : @"e", @"sort" : @-1}, @{@"k" : @"a", @"sort" : @-2} ];
  114. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  115. }
  116. - (void)testLimitToLastQueriesWithCursors {
  117. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  118. @"a" : @{@"k" : @"a", @"sort" : @0},
  119. @"b" : @{@"k" : @"b", @"sort" : @1},
  120. @"c" : @{@"k" : @"c", @"sort" : @1},
  121. @"d" : @{@"k" : @"d", @"sort" : @2},
  122. }];
  123. FIRQuerySnapshot *snapshot =
  124. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  125. queryEndingBeforeValues:@[ @2 ]]];
  126. XCTAssertEqualObjects(
  127. FIRQuerySnapshotGetData(snapshot), (@[
  128. @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
  129. ]));
  130. snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
  131. queryLimitedToLast:3] queryEndingAtValues:@[ @1 ]]];
  132. XCTAssertEqualObjects(
  133. FIRQuerySnapshotGetData(snapshot), (@[
  134. @{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
  135. ]));
  136. snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
  137. queryLimitedToLast:3] queryStartingAtValues:@[ @2 ]]];
  138. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"d", @"sort" : @2} ]));
  139. snapshot =
  140. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  141. queryStartingAfterValues:@[ @0 ]]];
  142. XCTAssertEqualObjects(
  143. FIRQuerySnapshotGetData(snapshot), (@[
  144. @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
  145. ]));
  146. snapshot =
  147. [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
  148. queryStartingAfterValues:@[ @-1 ]]];
  149. XCTAssertEqualObjects(
  150. FIRQuerySnapshotGetData(snapshot), (@[
  151. @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
  152. ]));
  153. }
  154. - (void)testKeyOrderIsDescendingForDescendingInequality {
  155. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  156. @"a" : @{@"foo" : @42},
  157. @"b" : @{@"foo" : @42.0},
  158. @"c" : @{@"foo" : @42},
  159. @"d" : @{@"foo" : @21},
  160. @"e" : @{@"foo" : @21.0},
  161. @"f" : @{@"foo" : @66},
  162. @"g" : @{@"foo" : @66.0},
  163. }];
  164. FIRQuerySnapshot *snapshot =
  165. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo"
  166. isGreaterThan:@21] queryOrderedByField:@"foo"
  167. descending:YES]];
  168. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  169. }
  170. - (void)testUnaryFilterQueries {
  171. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  172. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  173. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  174. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  175. }];
  176. FIRQuerySnapshot *results =
  177. [self readDocumentSetForRef:[[collRef queryWhereField:@"null"
  178. isEqualTo:[NSNull null]] queryWhereField:@"nan"
  179. isEqualTo:@(NAN)]];
  180. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results),
  181. (@[ @{@"null" : [NSNull null], @"nan" : @(NAN)} ]));
  182. }
  183. - (void)testQueryWithFieldPaths {
  184. FIRCollectionReference *collRef = [self
  185. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  186. FIRQuery *query = [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  187. isLessThan:@3];
  188. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  189. descending:YES];
  190. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  191. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  192. }
  193. - (void)testQueryWithPredicate {
  194. FIRCollectionReference *collRef = [self
  195. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  196. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  197. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  198. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  199. descending:YES];
  200. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  201. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  202. }
  203. - (void)testFilterOnInfinity {
  204. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  205. @"a" : @{@"inf" : @(INFINITY)},
  206. @"b" : @{@"inf" : @(-INFINITY)}
  207. }];
  208. FIRQuerySnapshot *results = [self readDocumentSetForRef:[collRef queryWhereField:@"inf"
  209. isEqualTo:@(INFINITY)]];
  210. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  211. }
  212. - (void)testCanExplicitlySortByDocumentID {
  213. NSDictionary *testDocs = @{
  214. @"a" : @{@"key" : @"a"},
  215. @"b" : @{@"key" : @"b"},
  216. @"c" : @{@"key" : @"c"},
  217. };
  218. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  219. // Ideally this would be descending to validate it's different than
  220. // the default, but that requires an extra index
  221. FIRQuerySnapshot *docs =
  222. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  223. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  224. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  225. }
  226. - (void)testCanQueryByDocumentID {
  227. NSDictionary *testDocs = @{
  228. @"aa" : @{@"key" : @"aa"},
  229. @"ab" : @{@"key" : @"ab"},
  230. @"ba" : @{@"key" : @"ba"},
  231. @"bb" : @{@"key" : @"bb"},
  232. };
  233. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  234. FIRQuerySnapshot *docs =
  235. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  236. isEqualTo:@"ab"]];
  237. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  238. }
  239. - (void)testCanQueryByDocumentIDs {
  240. NSDictionary *testDocs = @{
  241. @"aa" : @{@"key" : @"aa"},
  242. @"ab" : @{@"key" : @"ab"},
  243. @"ba" : @{@"key" : @"ba"},
  244. @"bb" : @{@"key" : @"bb"},
  245. };
  246. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  247. FIRQuerySnapshot *docs =
  248. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  249. isEqualTo:@"ab"]];
  250. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  251. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  252. isGreaterThan:@"aa"]
  253. queryWhereFieldPath:[FIRFieldPath documentID]
  254. isLessThanOrEqualTo:@"ba"]];
  255. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  256. }
  257. - (void)testCanQueryByDocumentIDsUsingRefs {
  258. NSDictionary *testDocs = @{
  259. @"aa" : @{@"key" : @"aa"},
  260. @"ab" : @{@"key" : @"ab"},
  261. @"ba" : @{@"key" : @"ba"},
  262. @"bb" : @{@"key" : @"bb"},
  263. };
  264. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  265. FIRQuerySnapshot *docs = [self
  266. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  267. isEqualTo:[collection documentWithPath:@"ab"]]];
  268. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  269. docs = [self
  270. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  271. isGreaterThan:[collection documentWithPath:@"aa"]]
  272. queryWhereFieldPath:[FIRFieldPath documentID]
  273. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  274. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  275. }
  276. - (void)testWatchSurvivesNetworkDisconnect {
  277. XCTestExpectation *testExpectiation =
  278. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  279. FIRCollectionReference *collectionRef = [self collectionRef];
  280. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  281. FIRFirestore *firestore = collectionRef.firestore;
  282. [collectionRef
  283. addSnapshotListenerWithIncludeMetadataChanges:YES
  284. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  285. XCTAssertNil(error);
  286. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  287. [testExpectiation fulfill];
  288. }
  289. }];
  290. [firestore disableNetworkWithCompletion:^(NSError *error) {
  291. XCTAssertNil(error);
  292. [docRef setData:@{@"foo" : @"bar"}];
  293. [firestore enableNetworkWithCompletion:^(NSError *error) {
  294. XCTAssertNil(error);
  295. }];
  296. }];
  297. [self awaitExpectations];
  298. }
  299. - (void)testQueriesFireFromCacheWhenOffline {
  300. NSDictionary *testDocs = @{
  301. @"a" : @{@"foo" : @1},
  302. };
  303. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  304. id<FIRListenerRegistration> registration = [collection
  305. addSnapshotListenerWithIncludeMetadataChanges:YES
  306. listener:self.eventAccumulator.valueEventHandler];
  307. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  308. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  309. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  310. [self disableNetwork];
  311. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  312. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  313. [self enableNetwork];
  314. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  315. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  316. [registration remove];
  317. }
  318. - (void)testQueriesCanRaiseInitialSnapshotFromCachedEmptyResults {
  319. FIRCollectionReference *collection = [self collectionRefWithDocuments:@{}];
  320. // Populate the cache with empty query result.
  321. FIRQuerySnapshot *querySnapshotA = [self readDocumentSetForRef:collection];
  322. XCTAssertFalse(querySnapshotA.metadata.fromCache);
  323. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotA), @[]);
  324. // Add a snapshot listener whose first event should be raised from cache.
  325. id<FIRListenerRegistration> registration = [collection
  326. addSnapshotListenerWithIncludeMetadataChanges:YES
  327. listener:self.eventAccumulator.valueEventHandler];
  328. FIRQuerySnapshot *querySnapshotB = [self.eventAccumulator awaitEventWithName:@"initial event"];
  329. XCTAssertTrue(querySnapshotB.metadata.fromCache);
  330. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotB), @[]);
  331. [registration remove];
  332. }
  333. - (void)testQueriesCanRaiseInitialSnapshotFromEmptyDueToDeleteCachedResults {
  334. NSDictionary *testDocs = @{
  335. @"a" : @{@"foo" : @1},
  336. };
  337. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  338. // Populate the cache with a single document.
  339. FIRQuerySnapshot *querySnapshotA = [self readDocumentSetForRef:collection];
  340. XCTAssertFalse(querySnapshotA.metadata.fromCache);
  341. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotA), @[ @{@"foo" : @1} ]);
  342. // Delete the document, making the cached query result empty.
  343. FIRDocumentReference *docRef = [collection documentWithPath:@"a"];
  344. [self deleteDocumentRef:docRef];
  345. // Add a snapshot listener whose first event should be raised from cache.
  346. id<FIRListenerRegistration> registration = [collection
  347. addSnapshotListenerWithIncludeMetadataChanges:YES
  348. listener:self.eventAccumulator.valueEventHandler];
  349. FIRQuerySnapshot *querySnapshotB = [self.eventAccumulator awaitEventWithName:@"initial event"];
  350. XCTAssertTrue(querySnapshotB.metadata.fromCache);
  351. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshotB), @[]);
  352. [registration remove];
  353. }
  354. - (void)testDocumentChangesUseNSNotFound {
  355. NSDictionary *testDocs = @{
  356. @"a" : @{@"foo" : @1},
  357. };
  358. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  359. id<FIRListenerRegistration> registration =
  360. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  361. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  362. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  363. FIRDocumentChange *change = querySnap.documentChanges[0];
  364. XCTAssertEqual(change.oldIndex, NSNotFound);
  365. XCTAssertEqual(change.newIndex, 0ul);
  366. FIRDocumentReference *doc = change.document.reference;
  367. [self deleteDocumentRef:doc];
  368. querySnap = [self.eventAccumulator awaitEventWithName:@"delete"];
  369. XCTAssertEqual(querySnap.documentChanges.count, 1ul);
  370. change = querySnap.documentChanges[0];
  371. XCTAssertEqual(change.oldIndex, 0ul);
  372. XCTAssertEqual(change.newIndex, NSNotFound);
  373. [registration remove];
  374. }
  375. - (void)testCanHaveMultipleMutationsWhileOffline {
  376. FIRCollectionReference *col = [self collectionRef];
  377. // set a few docs to known values
  378. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  379. [self writeAllDocuments:initialDocs toCollection:col];
  380. // go offline for the rest of this test
  381. [self disableNetwork];
  382. // apply *multiple* mutations while offline
  383. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  384. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  385. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  386. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  387. @{@"key1b" : @"value1b"},
  388. @{@"key2b" : @"value2b"},
  389. ]));
  390. }
  391. - (void)testQueriesCanUseNotEqualFilters {
  392. // These documents are ordered by value in "zip" since notEquals filter is an inequality, which
  393. // results in documents being sorted by value.
  394. NSDictionary *testDocs = @{
  395. @"a" : @{@"zip" : @(NAN)},
  396. @"b" : @{@"zip" : @91102},
  397. @"c" : @{@"zip" : @98101},
  398. @"d" : @{@"zip" : @98103},
  399. @"e" : @{@"zip" : @[ @98101 ]},
  400. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  401. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  402. @"h" : @{@"zip" : @{@"code" : @500}},
  403. @"i" : @{@"zip" : [NSNull null]},
  404. @"j" : @{@"code" : @500},
  405. };
  406. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  407. // Search for zips not matching 98101.
  408. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  409. isNotEqualTo:@98101]];
  410. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  411. testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"],
  412. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  413. ]));
  414. // With objects.
  415. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  416. isNotEqualTo:@{@"code" : @500}]];
  417. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  418. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  419. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  420. ]));
  421. // With null.
  422. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  423. isNotEqualTo:@[ [NSNull null] ]]];
  424. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  425. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  426. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  427. ]));
  428. // With NAN.
  429. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" isNotEqualTo:@(NAN)]];
  430. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  431. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  432. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  433. ]));
  434. }
  435. - (void)testQueriesCanUseNotEqualFiltersWithDocIds {
  436. NSDictionary *testDocs = @{
  437. @"aa" : @{@"key" : @"aa"},
  438. @"ab" : @{@"key" : @"ab"},
  439. @"ba" : @{@"key" : @"ba"},
  440. @"bb" : @{@"key" : @"bb"},
  441. };
  442. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  443. FIRQuerySnapshot *snapshot =
  444. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  445. isNotEqualTo:@"aa"]];
  446. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  447. (@[ testDocs[@"ab"], testDocs[@"ba"], testDocs[@"bb"] ]));
  448. }
  449. - (void)testQueriesCanUseArrayContainsFilters {
  450. NSDictionary *testDocs = @{
  451. @"a" : @{@"array" : @[ @42 ]},
  452. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  453. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  454. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  455. @"e" : @{@"array" : @[ [NSNull null] ]},
  456. @"f" : @{@"array" : @[ @(NAN) ]},
  457. };
  458. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  459. // Search for 42
  460. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  461. arrayContains:@42]];
  462. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  463. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"] ]));
  464. // With null.
  465. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  466. arrayContains:[NSNull null]]];
  467. XCTAssertTrue(snapshot.isEmpty);
  468. // With NAN.
  469. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  470. arrayContains:@(NAN)]];
  471. XCTAssertTrue(snapshot.isEmpty);
  472. }
  473. - (void)testQueriesCanUseInFilters {
  474. NSDictionary *testDocs = @{
  475. @"a" : @{@"zip" : @98101},
  476. @"b" : @{@"zip" : @91102},
  477. @"c" : @{@"zip" : @98103},
  478. @"d" : @{@"zip" : @[ @98101 ]},
  479. @"e" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  480. @"f" : @{@"zip" : @{@"code" : @500}},
  481. @"g" : @{@"zip" : @[ @98101, @98102 ]},
  482. @"h" : @{@"zip" : [NSNull null]},
  483. @"i" : @{@"zip" : @(NAN)}
  484. };
  485. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  486. // Search for zips matching 98101, 98103, and [98101, 98102].
  487. FIRQuerySnapshot *snapshot = [self
  488. readDocumentSetForRef:[collection queryWhereField:@"zip"
  489. in:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  490. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  491. (@[ testDocs[@"a"], testDocs[@"c"], testDocs[@"g"] ]));
  492. // With objects
  493. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  494. in:@[ @{@"code" : @500} ]]];
  495. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"f"] ]));
  496. // With null.
  497. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ [NSNull null] ]]];
  498. XCTAssertTrue(snapshot.isEmpty);
  499. // With null and a value.
  500. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  501. in:@[ [NSNull null], @98101 ]]];
  502. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  503. // With NAN.
  504. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" in:@[ @(NAN) ]]];
  505. XCTAssertTrue(snapshot.isEmpty);
  506. // With NAN and a value.
  507. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  508. in:@[ @(NAN), @98101 ]]];
  509. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"a"] ]));
  510. }
  511. - (void)testQueriesCanUseInFiltersWithDocIds {
  512. NSDictionary *testDocs = @{
  513. @"aa" : @{@"key" : @"aa"},
  514. @"ab" : @{@"key" : @"ab"},
  515. @"ba" : @{@"key" : @"ba"},
  516. @"bb" : @{@"key" : @"bb"},
  517. };
  518. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  519. FIRQuerySnapshot *snapshot =
  520. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  521. in:@[ @"aa", @"ab" ]]];
  522. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"aa"], testDocs[@"ab"] ]));
  523. }
  524. - (void)testQueriesCanUseNotInFilters {
  525. // These documents are ordered by value in "zip" since the NOT_IN filter is an inequality, which
  526. // results in documents being sorted by value.
  527. NSDictionary *testDocs = @{
  528. @"a" : @{@"zip" : @(NAN)},
  529. @"b" : @{@"zip" : @91102},
  530. @"c" : @{@"zip" : @98101},
  531. @"d" : @{@"zip" : @98103},
  532. @"e" : @{@"zip" : @[ @98101 ]},
  533. @"f" : @{@"zip" : @[ @98101, @98102 ]},
  534. @"g" : @{@"zip" : @[ @"98101", @{@"zip" : @98101} ]},
  535. @"h" : @{@"zip" : @{@"code" : @500}},
  536. @"i" : @{@"zip" : [NSNull null]},
  537. @"j" : @{@"code" : @500},
  538. };
  539. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  540. // Search for zips not matching 98101, 98103, and [98101, 98102].
  541. FIRQuerySnapshot *snapshot = [self
  542. readDocumentSetForRef:[collection queryWhereField:@"zip"
  543. notIn:@[ @98101, @98103, @[ @98101, @98102 ] ]]];
  544. XCTAssertEqualObjects(
  545. FIRQuerySnapshotGetData(snapshot),
  546. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"e"], testDocs[@"g"], testDocs[@"h"] ]));
  547. // With objects.
  548. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  549. notIn:@[ @{@"code" : @500} ]]];
  550. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  551. testDocs[@"a"], testDocs[@"b"], testDocs[@"c"], testDocs[@"d"],
  552. testDocs[@"e"], testDocs[@"f"], testDocs[@"g"]
  553. ]));
  554. // With null.
  555. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  556. notIn:@[ [NSNull null] ]]];
  557. XCTAssertTrue(snapshot.isEmpty);
  558. // With NAN.
  559. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip" notIn:@[ @(NAN) ]]];
  560. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  561. testDocs[@"b"], testDocs[@"c"], testDocs[@"d"], testDocs[@"e"],
  562. testDocs[@"f"], testDocs[@"g"], testDocs[@"h"]
  563. ]));
  564. // With NAN and a number.
  565. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"zip"
  566. notIn:@[ @(NAN), @98101 ]]];
  567. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  568. testDocs[@"b"], testDocs[@"d"], testDocs[@"e"], testDocs[@"f"],
  569. testDocs[@"g"], testDocs[@"h"]
  570. ]));
  571. }
  572. - (void)testQueriesCanUseNotInFiltersWithDocIds {
  573. NSDictionary *testDocs = @{
  574. @"aa" : @{@"key" : @"aa"},
  575. @"ab" : @{@"key" : @"ab"},
  576. @"ba" : @{@"key" : @"ba"},
  577. @"bb" : @{@"key" : @"bb"},
  578. };
  579. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  580. FIRQuerySnapshot *snapshot =
  581. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  582. notIn:@[ @"aa", @"ab" ]]];
  583. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"ba"], testDocs[@"bb"] ]));
  584. }
  585. - (void)testQueriesCanUseArrayContainsAnyFilters {
  586. NSDictionary *testDocs = @{
  587. @"a" : @{@"array" : @[ @42 ]},
  588. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  589. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  590. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]},
  591. @"e" : @{@"array" : @[ @43 ]},
  592. @"f" : @{@"array" : @[ @{@"a" : @42} ]},
  593. @"g" : @{@"array" : @42},
  594. @"h" : @{@"array" : @[ [NSNull null] ]},
  595. @"i" : @{@"array" : @[ @(NAN) ]},
  596. };
  597. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  598. // Search for zips matching [42, 43].
  599. FIRQuerySnapshot *snapshot = [self
  600. readDocumentSetForRef:[collection queryWhereField:@"array" arrayContainsAny:@[ @42, @43 ]]];
  601. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  602. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"d"], testDocs[@"e"] ]));
  603. // With objects.
  604. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  605. arrayContainsAny:@[ @{@"a" : @42} ]]];
  606. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  607. testDocs[@"f"],
  608. ]));
  609. // With null.
  610. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  611. arrayContainsAny:@[ [NSNull null] ]]];
  612. XCTAssertTrue(snapshot.isEmpty);
  613. // With null and a value.
  614. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  615. arrayContainsAny:@[ [NSNull null], @43 ]]];
  616. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  617. // With NAN.
  618. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  619. arrayContainsAny:@[ @(NAN) ]]];
  620. XCTAssertTrue(snapshot.isEmpty);
  621. // With NAN and a value.
  622. snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  623. arrayContainsAny:@[ @(NAN), @43 ]]];
  624. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ testDocs[@"e"] ]));
  625. }
  626. - (void)testCollectionGroupQueries {
  627. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  628. // for predictable ordering.
  629. NSString *collectionGroup = [NSString
  630. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  631. NSArray *docPaths = @[
  632. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  633. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  634. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  635. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  636. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  637. @"abc/${collectionGroup}"
  638. ];
  639. FIRWriteBatch *batch = [self.db batch];
  640. for (NSString *docPath in docPaths) {
  641. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  642. withString:collectionGroup];
  643. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  644. }
  645. [self commitWriteBatch:batch];
  646. FIRQuerySnapshot *querySnapshot =
  647. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  648. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  649. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  650. }
  651. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  652. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  653. // for predictable ordering.
  654. NSString *collectionGroup = [NSString
  655. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  656. NSArray *docPaths = @[
  657. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  658. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  659. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  660. ];
  661. FIRWriteBatch *batch = [self.db batch];
  662. for (NSString *docPath in docPaths) {
  663. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  664. withString:collectionGroup];
  665. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  666. }
  667. [self commitWriteBatch:batch];
  668. FIRQuerySnapshot *querySnapshot = [self
  669. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  670. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  671. queryStartingAfterValues:@[ @"a/b" ]]
  672. queryEndingBeforeValues:@[
  673. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  674. ]]];
  675. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  676. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  677. }
  678. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  679. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  680. // for predictable ordering.
  681. NSString *collectionGroup = [NSString
  682. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  683. NSArray *docPaths = @[
  684. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  685. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  686. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  687. ];
  688. FIRWriteBatch *batch = [self.db batch];
  689. for (NSString *docPath in docPaths) {
  690. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  691. withString:collectionGroup];
  692. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  693. }
  694. [self commitWriteBatch:batch];
  695. FIRQuerySnapshot *querySnapshot = [self
  696. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  697. queryWhereFieldPath:[FIRFieldPath documentID]
  698. isGreaterThanOrEqualTo:@"a/b"]
  699. queryWhereFieldPath:[FIRFieldPath documentID]
  700. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  701. collectionGroup]]];
  702. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  703. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  704. }
  705. - (void)testOrQueries {
  706. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  707. @"doc1" : @{@"a" : @1, @"b" : @0},
  708. @"doc2" : @{@"a" : @2, @"b" : @1},
  709. @"doc3" : @{@"a" : @3, @"b" : @2},
  710. @"doc4" : @{@"a" : @1, @"b" : @3},
  711. @"doc5" : @{@"a" : @1, @"b" : @1}
  712. }];
  713. // Two equalities: a==1 || b==1.
  714. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  715. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  716. ]];
  717. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  718. matchesResult:@[ @"doc1", @"doc2", @"doc4", @"doc5" ]];
  719. // (a==1 && b==0) || (a==3 && b==2)
  720. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  721. [FIRFilter andFilterWithFilters:@[
  722. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@0]
  723. ]],
  724. [FIRFilter andFilterWithFilters:@[
  725. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  726. ]]
  727. ]];
  728. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  729. matchesResult:@[ @"doc1", @"doc3" ]];
  730. // a==1 && (b==0 || b==3).
  731. FIRFilter *filter3 = [FIRFilter andFilterWithFilters:@[
  732. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter orFilterWithFilters:@[
  733. [FIRFilter filterWhereField:@"b" isEqualTo:@0], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  734. ]]
  735. ]];
  736. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter3]
  737. matchesResult:@[ @"doc1", @"doc4" ]];
  738. // (a==2 || b==2) && (a==3 || b==3)
  739. FIRFilter *filter4 = [FIRFilter andFilterWithFilters:@[
  740. [FIRFilter orFilterWithFilters:@[
  741. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  742. ]],
  743. [FIRFilter orFilterWithFilters:@[
  744. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  745. ]]
  746. ]];
  747. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter4] matchesResult:@[ @"doc3" ]];
  748. // Test with limits without orderBy (the __name__ ordering is the tie breaker).
  749. FIRFilter *filter5 = [FIRFilter orFilterWithFilters:@[
  750. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  751. ]];
  752. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:filter5] queryLimitedTo:1]
  753. matchesResult:@[ @"doc2" ]];
  754. }
  755. - (void)testOrQueriesWithIn {
  756. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  757. @"doc1" : @{@"a" : @1, @"b" : @0},
  758. @"doc2" : @{@"b" : @1},
  759. @"doc3" : @{@"a" : @3, @"b" : @2},
  760. @"doc4" : @{@"a" : @1, @"b" : @3},
  761. @"doc5" : @{@"a" : @1},
  762. @"doc6" : @{@"a" : @2}
  763. }];
  764. // a==2 || b in [2,3]
  765. FIRFilter *filter = [FIRFilter orFilterWithFilters:@[
  766. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" in:@[ @2, @3 ]]
  767. ]];
  768. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter]
  769. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  770. }
  771. - (void)testOrQueriesWithArrayMembership {
  772. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  773. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  774. @"doc2" : @{@"b" : @[ @1 ]},
  775. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ]},
  776. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  777. @"doc5" : @{@"a" : @1},
  778. @"doc6" : @{@"a" : @2}
  779. }];
  780. // a==2 || b array-contains 7
  781. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  782. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  783. arrayContains:@7]
  784. ]];
  785. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  786. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  787. // a==2 || b array-contains-any [0, 3]
  788. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  789. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  790. arrayContainsAny:@[ @0, @3 ]]
  791. ]];
  792. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  793. matchesResult:@[ @"doc1", @"doc4", @"doc6" ]];
  794. }
  795. - (void)testMultipleInOps {
  796. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  797. @"doc1" : @{@"a" : @1, @"b" : @0},
  798. @"doc2" : @{@"b" : @1},
  799. @"doc3" : @{@"a" : @3, @"b" : @2},
  800. @"doc4" : @{@"a" : @1, @"b" : @3},
  801. @"doc5" : @{@"a" : @1},
  802. @"doc6" : @{@"a" : @2}
  803. }];
  804. // Two IN operations on different fields with disjunction.
  805. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  806. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  807. in:@[ @0, @2 ]]
  808. ]];
  809. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  810. matchesResult:@[ @"doc1", @"doc3", @"doc6" ]];
  811. // Two IN operations on the same field with disjunction.
  812. // a IN [0,3] || a IN [0,2] should union them (similar to: a IN [0,2,3]).
  813. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  814. [FIRFilter filterWhereField:@"a" in:@[ @0, @3 ]], [FIRFilter filterWhereField:@"a"
  815. in:@[ @0, @2 ]]
  816. ]];
  817. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  818. matchesResult:@[ @"doc3", @"doc6" ]];
  819. }
  820. - (void)testUsingInWithArrayContainsAny {
  821. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  822. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  823. @"doc2" : @{@"b" : @[ @1 ]},
  824. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ], @"c" : @10},
  825. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  826. @"doc5" : @{@"a" : @1},
  827. @"doc6" : @{@"a" : @2, @"c" : @20}
  828. }];
  829. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  830. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  831. arrayContainsAny:@[ @0, @7 ]]
  832. ]];
  833. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  834. matchesResult:@[ @"doc1", @"doc3", @"doc4", @"doc6" ]];
  835. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  836. [FIRFilter andFilterWithFilters:@[
  837. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"c"
  838. isEqualTo:@10]
  839. ]],
  840. [FIRFilter filterWhereField:@"b" arrayContainsAny:@[ @0, @7 ]]
  841. ]];
  842. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  843. matchesResult:@[ @"doc1", @"doc3", @"doc4" ]];
  844. }
  845. - (void)testUseInWithArrayContains {
  846. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  847. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  848. @"doc2" : @{@"b" : @[ @1 ]},
  849. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ]},
  850. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  851. @"doc5" : @{@"a" : @1},
  852. @"doc6" : @{@"a" : @2}
  853. }];
  854. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  855. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  856. arrayContainsAny:@[ @3 ]]
  857. ]];
  858. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  859. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  860. FIRFilter *filter2 = [FIRFilter andFilterWithFilters:@[
  861. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  862. arrayContains:@7]
  863. ]];
  864. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2] matchesResult:@[ @"doc3" ]];
  865. FIRFilter *filter3 = [FIRFilter orFilterWithFilters:@[
  866. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter andFilterWithFilters:@[
  867. [FIRFilter filterWhereField:@"b" arrayContains:@3], [FIRFilter filterWhereField:@"a"
  868. isEqualTo:@1]
  869. ]]
  870. ]];
  871. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter3]
  872. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  873. FIRFilter *filter4 = [FIRFilter andFilterWithFilters:@[
  874. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter orFilterWithFilters:@[
  875. [FIRFilter filterWhereField:@"b" arrayContains:@7], [FIRFilter filterWhereField:@"a"
  876. isEqualTo:@1]
  877. ]]
  878. ]];
  879. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter4] matchesResult:@[ @"doc3" ]];
  880. }
  881. - (void)testOrderByEquality {
  882. // TODO(orquery): Enable this test against production when possible.
  883. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  884. "Skip this test if running against production because order-by-equality is not "
  885. "supported yet.");
  886. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  887. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  888. @"doc2" : @{@"b" : @[ @1 ]},
  889. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ], @"c" : @10},
  890. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  891. @"doc5" : @{@"a" : @1},
  892. @"doc6" : @{@"a" : @2, @"c" : @20}
  893. }];
  894. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:[FIRFilter filterWhereField:@"a"
  895. isEqualTo:@1]]
  896. queryOrderedByField:@"a"]
  897. matchesResult:@[ @"doc1", @"doc4", @"doc5" ]];
  898. [self checkOnlineAndOfflineQuery:[[collRef
  899. queryWhereFilter:[FIRFilter filterWhereField:@"a"
  900. in:@[ @2, @3 ]]]
  901. queryOrderedByField:@"a"]
  902. matchesResult:@[ @"doc6", @"doc3" ]];
  903. }
  904. // Multiple Inequality
  905. - (void)testMultipleInequalityOnDifferentFields {
  906. // TODO(MIEQ): Enable this test against production when possible.
  907. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  908. "Skip this test if running against production because multiple inequality is "
  909. "not supported yet.");
  910. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  911. @"doc1" : @{@"key" : @"a", @"sort" : @0, @"v" : @0},
  912. @"doc2" : @{@"key" : @"b", @"sort" : @3, @"v" : @1},
  913. @"doc3" : @{@"key" : @"c", @"sort" : @1, @"v" : @3},
  914. @"doc4" : @{@"key" : @"d", @"sort" : @2, @"v" : @2}
  915. }];
  916. // Multiple inequality fields
  917. FIRQuerySnapshot *snapshot = [self
  918. readDocumentSetForRef:[[[collRef queryWhereField:@"key" isNotEqualTo:@"a"]
  919. queryWhereField:@"sort"
  920. isLessThanOrEqualTo:@2] queryWhereField:@"v" isGreaterThan:@2]];
  921. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc3" ]));
  922. // Duplicate inequality fields
  923. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"key" isNotEqualTo:@"a"]
  924. queryWhereField:@"sort"
  925. isLessThanOrEqualTo:@2] queryWhereField:@"sort"
  926. isGreaterThan:@1]];
  927. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc4" ]));
  928. // With multiple IN
  929. snapshot = [self
  930. readDocumentSetForRef:[[[[collRef queryWhereField:@"key"
  931. isGreaterThanOrEqualTo:@"a"] queryWhereField:@"sort"
  932. isLessThanOrEqualTo:@2]
  933. queryWhereField:@"v"
  934. in:@[ @2, @3, @4 ]] queryWhereField:@"sort"
  935. in:@[ @2, @3 ]]];
  936. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc4" ]));
  937. // With NOT-IN
  938. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"key"
  939. isGreaterThanOrEqualTo:@"a"] queryWhereField:@"sort"
  940. isLessThanOrEqualTo:@2]
  941. queryWhereField:@"v"
  942. notIn:@[ @2, @4, @5 ]]];
  943. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc1", @"doc3" ]));
  944. // With orderby
  945. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"key"
  946. isGreaterThanOrEqualTo:@"a"] queryWhereField:@"sort"
  947. isLessThanOrEqualTo:@2]
  948. queryOrderedByField:@"v"
  949. descending:YES]];
  950. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc3", @"doc4", @"doc1" ]));
  951. // With limit
  952. snapshot = [self readDocumentSetForRef:[[[[collRef queryWhereField:@"key"
  953. isGreaterThanOrEqualTo:@"a"] queryWhereField:@"sort"
  954. isLessThanOrEqualTo:@2]
  955. queryOrderedByField:@"v"
  956. descending:YES] queryLimitedTo:2]];
  957. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc3", @"doc4" ]));
  958. // With limitedToLast
  959. snapshot = [self readDocumentSetForRef:[[[[collRef queryWhereField:@"key"
  960. isGreaterThanOrEqualTo:@"a"] queryWhereField:@"sort"
  961. isLessThanOrEqualTo:@2]
  962. queryOrderedByField:@"v"
  963. descending:YES] queryLimitedToLast:2]];
  964. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc4", @"doc1" ]));
  965. }
  966. - (void)testMultipleInequalityOnSpecialValues {
  967. // TODO(MIEQ): Enable this test against production when possible.
  968. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  969. "Skip this test if running against production because multiple inequality is "
  970. "not supported yet.");
  971. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  972. @"doc1" : @{@"key" : @"a", @"sort" : @0, @"v" : @0},
  973. @"doc2" : @{@"key" : @"b", @"sort" : @(NAN), @"v" : @1},
  974. @"doc3" : @{@"key" : @"c", @"sort" : [NSNull null], @"v" : @3},
  975. @"doc4" : @{@"key" : @"d", @"v" : @0},
  976. @"doc5" : @{@"key" : @"e", @"sort" : @1},
  977. @"doc6" : @{@"key" : @"f", @"sort" : @1, @"v" : @1}
  978. }];
  979. FIRQuerySnapshot *snapshot =
  980. [self readDocumentSetForRef:[[collRef queryWhereField:@"key"
  981. isNotEqualTo:@"a"] queryWhereField:@"sort"
  982. isLessThanOrEqualTo:@2]];
  983. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc5", @"doc6" ]));
  984. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"key" isNotEqualTo:@"a"]
  985. queryWhereField:@"sort"
  986. isLessThanOrEqualTo:@2] queryWhereField:@"v"
  987. isLessThanOrEqualTo:@1]];
  988. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc6" ]));
  989. }
  990. - (void)testMultipleInequalityWithArrayMembership {
  991. // TODO(MIEQ): Enable this test against production when possible.
  992. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  993. "Skip this test if running against production because multiple inequality is "
  994. "not supported yet.");
  995. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  996. @"doc1" : @{@"key" : @"a", @"sort" : @0, @"v" : @[ @0 ]},
  997. @"doc2" : @{@"key" : @"b", @"sort" : @1, @"v" : @[ @0, @1, @3 ]},
  998. @"doc3" : @{@"key" : @"c", @"sort" : @1, @"v" : @[]},
  999. @"doc4" : @{@"key" : @"d", @"sort" : @2, @"v" : @[ @1 ]},
  1000. @"doc5" : @{@"key" : @"e", @"sort" : @3, @"v" : @[ @2, @4 ]},
  1001. @"doc6" : @{@"key" : @"f", @"sort" : @4, @"v" : @[ @(NAN) ]},
  1002. @"doc7" : @{@"key" : @"g", @"sort" : @4, @"v" : @[ [NSNull null] ]}
  1003. }];
  1004. FIRQuerySnapshot *snapshot = [self
  1005. readDocumentSetForRef:[[[collRef queryWhereField:@"key" isNotEqualTo:@"a"]
  1006. queryWhereField:@"sort"
  1007. isGreaterThanOrEqualTo:@1] queryWhereField:@"v" arrayContains:@0]];
  1008. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc2" ]));
  1009. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"key"
  1010. isNotEqualTo:@"a"] queryWhereField:@"sort"
  1011. isGreaterThanOrEqualTo:@1]
  1012. queryWhereField:@"v"
  1013. arrayContainsAny:@[ @0, @1 ]]];
  1014. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc2", @"doc4" ]));
  1015. }
  1016. - (NSDictionary<NSString *, id> *)nestedData:(int)number {
  1017. return @{
  1018. @"name" : [NSString stringWithFormat:@"room %d", number],
  1019. @"metadata" : @{@"createdAt" : @(number)},
  1020. @"field" : [NSString stringWithFormat:@"field %d", number],
  1021. @"field.dot" : @(number),
  1022. @"field\\slash" : @(number)
  1023. };
  1024. }
  1025. - (void)testMultipleInequalityWithNestedField {
  1026. // TODO(MIEQ): Enable this test against production when possible.
  1027. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1028. "Skip this test if running against production because multiple inequality is "
  1029. "not supported yet.");
  1030. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1031. @"doc1" : [self nestedData:400],
  1032. @"doc2" : [self nestedData:200],
  1033. @"doc3" : [self nestedData:100],
  1034. @"doc4" : [self nestedData:300]
  1035. }];
  1036. FIRQuerySnapshot *snapshot = [self
  1037. readDocumentSetForRef:[[[[collRef queryWhereField:@"metadata.createdAt"
  1038. isLessThanOrEqualTo:@500] queryWhereField:@"metadata.createdAt"
  1039. isGreaterThan:@100]
  1040. queryWhereField:@"name"
  1041. isNotEqualTo:@"room 200"] queryOrderedByField:@"name"
  1042. descending:NO]];
  1043. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc4", @"doc1" ]));
  1044. snapshot =
  1045. [self readDocumentSetForRef:[[[[collRef queryWhereField:@"field"
  1046. isGreaterThanOrEqualTo:@"field 100"]
  1047. queryWhereFieldPath:[[FIRFieldPath alloc]
  1048. initWithFields:@[ @"field.dot" ]]
  1049. isNotEqualTo:@300] queryWhereField:@"field\\slash"
  1050. isLessThan:@400]
  1051. queryOrderedByField:@"name"
  1052. descending:YES]];
  1053. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc2", @"doc3" ]));
  1054. }
  1055. - (void)testMultipleInequalityWithCompositeFilters {
  1056. // TODO(MIEQ): Enable this test against production when possible.
  1057. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1058. "Skip this test if running against production because multiple inequality is "
  1059. "not supported yet.");
  1060. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1061. @"doc1" : @{@"key" : @"a", @"sort" : @0, @"v" : @5},
  1062. @"doc2" : @{@"key" : @"aa", @"sort" : @4, @"v" : @4},
  1063. @"doc3" : @{@"key" : @"c", @"sort" : @3, @"v" : @3},
  1064. @"doc4" : @{@"key" : @"b", @"sort" : @2, @"v" : @2},
  1065. @"doc5" : @{@"key" : @"b", @"sort" : @2, @"v" : @1},
  1066. @"doc6" : @{@"key" : @"b", @"sort" : @0, @"v" : @0}
  1067. }];
  1068. FIRQuerySnapshot *snapshot = [self
  1069. readDocumentSetForRef:[collRef queryWhereFilter:[FIRFilter orFilterWithFilters:@[
  1070. [FIRFilter andFilterWithFilters:@[
  1071. [FIRFilter filterWhereField:@"key" isEqualTo:@"b"],
  1072. [FIRFilter filterWhereField:@"sort" isLessThanOrEqualTo:@2]
  1073. ]],
  1074. [FIRFilter andFilterWithFilters:@[
  1075. [FIRFilter filterWhereField:@"key" isNotEqualTo:@"b"],
  1076. [FIRFilter filterWhereField:@"v" isGreaterThan:@4]
  1077. ]]
  1078. ]]]];
  1079. // Implicitly ordered by: 'key' asc, 'sort' asc, 'v' asc, __name__ asc
  1080. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1081. (@[ @"doc1", @"doc6", @"doc5", @"doc4" ]));
  1082. snapshot = [self
  1083. readDocumentSetForRef:[[[collRef queryWhereFilter:[FIRFilter orFilterWithFilters:@[
  1084. [FIRFilter andFilterWithFilters:@[
  1085. [FIRFilter filterWhereField:@"key" isEqualTo:@"b"],
  1086. [FIRFilter filterWhereField:@"sort"
  1087. isLessThanOrEqualTo:@2]
  1088. ]],
  1089. [FIRFilter andFilterWithFilters:@[
  1090. [FIRFilter filterWhereField:@"key" isNotEqualTo:@"b"],
  1091. [FIRFilter filterWhereField:@"v" isGreaterThan:@4]
  1092. ]]
  1093. ]]] queryOrderedByField:@"sort"
  1094. descending:YES] queryOrderedByField:@"key"]];
  1095. // Ordered by: 'sort' desc, 'key' asc, 'v' asc, __name__ asc
  1096. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1097. (@[ @"doc5", @"doc4", @"doc1", @"doc6" ]));
  1098. snapshot = [self
  1099. readDocumentSetForRef:[collRef
  1100. queryWhereFilter:[FIRFilter andFilterWithFilters:@[
  1101. [FIRFilter orFilterWithFilters:@[
  1102. [FIRFilter andFilterWithFilters:@[
  1103. [FIRFilter filterWhereField:@"key" isEqualTo:@"b"],
  1104. [FIRFilter filterWhereField:@"sort" isLessThanOrEqualTo:@4]
  1105. ]],
  1106. [FIRFilter andFilterWithFilters:@[
  1107. [FIRFilter filterWhereField:@"key" isNotEqualTo:@"b"],
  1108. [FIRFilter filterWhereField:@"v" isGreaterThanOrEqualTo:@4]
  1109. ]]
  1110. ]],
  1111. [FIRFilter orFilterWithFilters:@[
  1112. [FIRFilter andFilterWithFilters:@[
  1113. [FIRFilter filterWhereField:@"key" isGreaterThan:@"b"],
  1114. [FIRFilter filterWhereField:@"sort" isGreaterThanOrEqualTo:@1]
  1115. ]],
  1116. [FIRFilter andFilterWithFilters:@[
  1117. [FIRFilter filterWhereField:@"key" isLessThan:@"b"],
  1118. [FIRFilter filterWhereField:@"v" isGreaterThan:@0]
  1119. ]]
  1120. ]]
  1121. ]]]];
  1122. // Implicitly ordered by: 'key' asc, 'sort' asc, 'v' asc, __name__ asc
  1123. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc1", @"doc2" ]));
  1124. }
  1125. - (void)testMultipleInequalityFieldsWillBeImplicitlyOrderedLexicographically {
  1126. // TODO(MIEQ): Enable this test against production when possible.
  1127. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1128. "Skip this test if running against production because multiple inequality is "
  1129. "not supported yet.");
  1130. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1131. @"doc1" : @{@"key" : @"a", @"sort" : @0, @"v" : @5},
  1132. @"doc2" : @{@"key" : @"aa", @"sort" : @4, @"v" : @4},
  1133. @"doc3" : @{@"key" : @"b", @"sort" : @3, @"v" : @3},
  1134. @"doc4" : @{@"key" : @"b", @"sort" : @2, @"v" : @2},
  1135. @"doc5" : @{@"key" : @"b", @"sort" : @2, @"v" : @1},
  1136. @"doc6" : @{@"key" : @"b", @"sort" : @0, @"v" : @0}
  1137. }];
  1138. FIRQuerySnapshot *snapshot = [self
  1139. readDocumentSetForRef:[[[collRef queryWhereField:@"key" isNotEqualTo:@"a"]
  1140. queryWhereField:@"sort"
  1141. isGreaterThan:@1] queryWhereField:@"v" in:@[ @1, @2, @3, @4 ]]];
  1142. // Implicitly ordered by: 'key' asc, 'sort' asc, __name__ asc
  1143. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1144. (@[ @"doc2", @"doc4", @"doc5", @"doc3" ]));
  1145. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"sort"
  1146. isGreaterThan:@1] queryWhereField:@"key"
  1147. isNotEqualTo:@"a"]
  1148. queryWhereField:@"v"
  1149. in:@[ @1, @2, @3, @4 ]]];
  1150. // Implicitly ordered by: 'key' asc, 'sort' asc, __name__ asc
  1151. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1152. (@[ @"doc2", @"doc4", @"doc5", @"doc3" ]));
  1153. }
  1154. - (void)testMultipleInequalityWithMultipleExplicitOrderBy {
  1155. // TODO(MIEQ): Enable this test against production when possible.
  1156. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1157. "Skip this test if running against production because multiple inequality is "
  1158. "not supported yet.");
  1159. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1160. @"doc1" : @{@"key" : @"a", @"sort" : @5, @"v" : @0},
  1161. @"doc2" : @{@"key" : @"aa", @"sort" : @4, @"v" : @0},
  1162. @"doc3" : @{@"key" : @"b", @"sort" : @3, @"v" : @1},
  1163. @"doc4" : @{@"key" : @"b", @"sort" : @2, @"v" : @1},
  1164. @"doc5" : @{@"key" : @"bb", @"sort" : @1, @"v" : @1},
  1165. @"doc6" : @{@"key" : @"c", @"sort" : @0, @"v" : @2}
  1166. }];
  1167. FIRQuerySnapshot *snapshot = [self
  1168. readDocumentSetForRef:[[[collRef queryWhereField:@"key" isGreaterThan:@"a"]
  1169. queryWhereField:@"sort"
  1170. isGreaterThanOrEqualTo:@1] queryOrderedByField:@"v" descending:NO]];
  1171. // Ordered by: 'v' asc, 'key' asc, 'sort' asc, __name__ asc
  1172. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1173. (@[ @"doc2", @"doc4", @"doc3", @"doc5" ]));
  1174. snapshot = [self readDocumentSetForRef:[[[[collRef queryWhereField:@"key"
  1175. isGreaterThan:@"a"] queryWhereField:@"sort"
  1176. isGreaterThanOrEqualTo:@1]
  1177. queryOrderedByField:@"v"
  1178. descending:NO] queryOrderedByField:@"sort"
  1179. descending:NO]];
  1180. // Ordered by: 'v asc, 'sort' asc, 'key' asc, __name__ asc
  1181. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1182. (@[ @"doc2", @"doc5", @"doc4", @"doc3" ]));
  1183. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereField:@"key" isGreaterThan:@"a"]
  1184. queryWhereField:@"sort"
  1185. isGreaterThanOrEqualTo:@1] queryOrderedByField:@"v"
  1186. descending:YES]];
  1187. // Implicit order by matches the direction of last explicit order by.
  1188. // Ordered by: 'v' desc, 'key' desc, 'sort' desc, __name__ desc
  1189. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1190. (@[ @"doc5", @"doc3", @"doc4", @"doc2" ]));
  1191. snapshot = [self readDocumentSetForRef:[[[[collRef queryWhereField:@"key"
  1192. isGreaterThan:@"a"] queryWhereField:@"sort"
  1193. isGreaterThanOrEqualTo:@1]
  1194. queryOrderedByField:@"v"
  1195. descending:YES] queryOrderedByField:@"sort"
  1196. descending:NO]];
  1197. // Ordered by: 'v desc, 'sort' asc, 'key' asc, __name__ asc
  1198. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot),
  1199. (@[ @"doc5", @"doc4", @"doc3", @"doc2" ]));
  1200. }
  1201. - (void)testMultipleInequalityInAggregateQuery {
  1202. // TODO(MIEQ): Enable this test against production when possible.
  1203. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1204. "Skip this test if running against production because multiple inequality is "
  1205. "not supported yet.");
  1206. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1207. @"doc1" : @{@"key" : @"a", @"sort" : @5, @"v" : @0},
  1208. @"doc2" : @{@"key" : @"aa", @"sort" : @4, @"v" : @0},
  1209. @"doc3" : @{@"key" : @"b", @"sort" : @3, @"v" : @1},
  1210. @"doc4" : @{@"key" : @"b", @"sort" : @2, @"v" : @1},
  1211. @"doc5" : @{@"key" : @"bb", @"sort" : @1, @"v" : @1},
  1212. }];
  1213. FIRAggregateQuerySnapshot *snapshot =
  1214. [self readSnapshotForAggregate:[[[[collRef queryWhereField:@"key" isGreaterThan:@"a"]
  1215. queryWhereField:@"sort"
  1216. isGreaterThanOrEqualTo:@1] queryOrderedByField:@"v"
  1217. descending:NO]
  1218. aggregate:@[
  1219. [FIRAggregateField aggregateFieldForCount],
  1220. [FIRAggregateField aggregateFieldForSumOfField:@"sort"],
  1221. [FIRAggregateField aggregateFieldForAverageOfField:@"v"]
  1222. ]]];
  1223. XCTAssertEqual([snapshot count], [NSNumber numberWithLong:4L]);
  1224. snapshot = [self
  1225. readSnapshotForAggregate:[[[[collRef queryWhereField:@"key" isGreaterThan:@"a"]
  1226. queryWhereField:@"sort"
  1227. isGreaterThanOrEqualTo:@1] queryWhereField:@"v" isNotEqualTo:@0]
  1228. aggregate:@[
  1229. [FIRAggregateField aggregateFieldForCount],
  1230. [FIRAggregateField aggregateFieldForSumOfField:@"sort"],
  1231. [FIRAggregateField aggregateFieldForAverageOfField:@"v"],
  1232. ]]];
  1233. XCTAssertEqual([snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForCount]],
  1234. [NSNumber numberWithLong:3L]);
  1235. XCTAssertEqual(
  1236. [[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"sort"]]
  1237. longValue],
  1238. 6L);
  1239. XCTAssertEqual(
  1240. [snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForAverageOfField:@"v"]],
  1241. [NSNumber numberWithDouble:1.0]);
  1242. }
  1243. - (void)testMultipleInequalityFieldsWithDocumentKey {
  1244. // TODO(MIEQ): Enable this test against production when possible.
  1245. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1246. "Skip this test if running against production because multiple inequality is "
  1247. "not supported yet.");
  1248. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1249. @"doc1" : @{@"key" : @"a", @"sort" : @5},
  1250. @"doc2" : @{@"key" : @"aa", @"sort" : @4},
  1251. @"doc3" : @{@"key" : @"b", @"sort" : @3},
  1252. @"doc4" : @{@"key" : @"b", @"sort" : @2},
  1253. @"doc5" : @{@"key" : @"bb", @"sort" : @1}
  1254. }];
  1255. FIRQuerySnapshot *snapshot = [self
  1256. readDocumentSetForRef:[[[collRef queryWhereField:@"sort"
  1257. isGreaterThan:@1] queryWhereField:@"key" isNotEqualTo:@"a"]
  1258. queryWhereFieldPath:[FIRFieldPath documentID]
  1259. isLessThan:@"doc5"]];
  1260. // Document Key in inequality field will implicitly ordered to the last.
  1261. // Implicitly ordered by: 'key' asc, 'sort' asc, __name__ asc
  1262. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc2", @"doc4", @"doc3" ]));
  1263. snapshot = [self readDocumentSetForRef:[[[collRef queryWhereFieldPath:[FIRFieldPath documentID]
  1264. isLessThan:@"doc5"]
  1265. queryWhereField:@"sort"
  1266. isGreaterThan:@1] queryWhereField:@"key"
  1267. isNotEqualTo:@"a"]];
  1268. // Changing filters order will not effect implicit order.
  1269. // Implicitly ordered by: 'key' asc, 'sort' asc, __name__ asc
  1270. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc2", @"doc4", @"doc3" ]));
  1271. snapshot = [self
  1272. readDocumentSetForRef:[[[[collRef queryWhereFieldPath:[FIRFieldPath documentID]
  1273. isLessThan:@"doc5"] queryWhereField:@"sort"
  1274. isGreaterThan:@1]
  1275. queryWhereField:@"key"
  1276. isNotEqualTo:@"a"] queryOrderedByField:@"sort" descending:YES]];
  1277. // Ordered by: 'sort' desc,'key' desc, __name__ desc
  1278. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc2", @"doc3", @"doc4" ]));
  1279. }
  1280. - (void)testMultipleInequalityReadFromCacheWhenOffline {
  1281. // TODO(MIEQ): Enable this test against production when possible.
  1282. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1283. "Skip this test if running against production because multiple inequality is "
  1284. "not supported yet.");
  1285. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1286. @"doc1" : @{@"key" : @"a", @"sort" : @1},
  1287. @"doc2" : @{@"key" : @"aa", @"sort" : @4},
  1288. @"doc3" : @{@"key" : @"b", @"sort" : @3},
  1289. @"doc4" : @{@"key" : @"b", @"sort" : @2},
  1290. }];
  1291. FIRQuery *query = [[collRef queryWhereField:@"key" isNotEqualTo:@"a"] queryWhereField:@"sort"
  1292. isLessThanOrEqualTo:@3];
  1293. // populate the cache.
  1294. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  1295. XCTAssertEqual(snapshot.count, 2L);
  1296. XCTAssertEqual(snapshot.metadata.isFromCache, NO);
  1297. [self disableNetwork];
  1298. snapshot = [self readDocumentSetForRef:query];
  1299. XCTAssertEqual(snapshot.count, 2L);
  1300. XCTAssertEqual(snapshot.metadata.isFromCache, YES);
  1301. // Implicitly ordered by: 'key' asc, 'sort' asc, __name__ asc
  1302. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"doc4", @"doc3" ]));
  1303. }
  1304. - (void)testMultipleInequalityFromCacheAndFromServer {
  1305. // TODO(MIEQ): Enable this test against production when possible.
  1306. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1307. "Skip this test if running against production because multiple inequality is "
  1308. "not supported yet.");
  1309. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1310. @"doc1" : @{@"a" : @1, @"b" : @0},
  1311. @"doc2" : @{@"a" : @2, @"b" : @1},
  1312. @"doc3" : @{@"a" : @3, @"b" : @2},
  1313. @"doc4" : @{@"a" : @1, @"b" : @3},
  1314. @"doc5" : @{@"a" : @1, @"b" : @1},
  1315. }];
  1316. // implicit AND: a != 1 && b < 2
  1317. FIRQuery *query = [[collRef queryWhereField:@"a" isNotEqualTo:@1] queryWhereField:@"b"
  1318. isLessThan:@2];
  1319. [self checkOnlineAndOfflineQuery:query matchesResult:@[ @"doc2" ]];
  1320. // explicit AND: a != 1 && b < 2
  1321. FIRFilter *filter = [FIRFilter andFilterWithFilters:@[
  1322. [FIRFilter filterWhereField:@"a" isNotEqualTo:@1], [FIRFilter filterWhereField:@"b"
  1323. isLessThan:@2]
  1324. ]];
  1325. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter] matchesResult:@[ @"doc2" ]];
  1326. // explicit AND: a < 3 && b not-in [2, 3]
  1327. // Implicitly ordered by: a asc, b asc, __name__ asc
  1328. filter = [FIRFilter andFilterWithFilters:@[
  1329. [FIRFilter filterWhereField:@"a" isLessThan:@3], [FIRFilter filterWhereField:@"b"
  1330. notIn:@[ @2, @3 ]]
  1331. ]];
  1332. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter]
  1333. matchesResult:@[ @"doc1", @"doc5", @"doc2" ]];
  1334. // a <3 && b != 0, ordered by: b desc, a desc, __name__ desc
  1335. query = [[[[collRef queryWhereField:@"a" isLessThan:@3] queryWhereField:@"b" isNotEqualTo:@0]
  1336. queryOrderedByField:@"b"
  1337. descending:YES] queryLimitedTo:2];
  1338. [self checkOnlineAndOfflineQuery:query matchesResult:@[ @"doc4", @"doc2" ]];
  1339. // explicit OR: a>2 || b<1.
  1340. filter = [FIRFilter orFilterWithFilters:@[
  1341. [FIRFilter filterWhereField:@"a" isGreaterThan:@2], [FIRFilter filterWhereField:@"b"
  1342. isLessThan:@1]
  1343. ]];
  1344. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter]
  1345. matchesResult:@[ @"doc1", @"doc3" ]];
  1346. }
  1347. - (void)testMultipleInequalityRejectsIfDocumentKeyIsNotTheLastOrderByField {
  1348. // TODO(MIEQ): Enable this test against production when possible.
  1349. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1350. "Skip this test if running against production because multiple inequality is "
  1351. "not supported yet.");
  1352. FIRCollectionReference *collRef = [self collectionRef];
  1353. FIRQuery *query = [[collRef queryWhereField:@"key" isNotEqualTo:@42]
  1354. queryOrderedByFieldPath:[FIRFieldPath documentID]];
  1355. XCTestExpectation *queryCompletion = [self expectationWithDescription:@"query"];
  1356. [query getDocumentsWithCompletion:^(FIRQuerySnapshot *results, NSError *error) {
  1357. XCTAssertNil(results);
  1358. XCTAssertNotNil(error);
  1359. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  1360. [queryCompletion fulfill];
  1361. }];
  1362. [self awaitExpectations];
  1363. }
  1364. - (void)testMultipleInequalityRejectsIfDocumentKeyAppearsOnlyInEqualityFilter {
  1365. // TODO(MIEQ): Enable this test against production when possible.
  1366. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1367. "Skip this test if running against production because multiple inequality is "
  1368. "not supported yet.");
  1369. FIRCollectionReference *collRef = [self collectionRef];
  1370. FIRQuery *query = [[collRef queryWhereField:@"key"
  1371. isNotEqualTo:@42] queryWhereFieldPath:[FIRFieldPath documentID]
  1372. isEqualTo:@"doc1"];
  1373. XCTestExpectation *queryCompletion = [self expectationWithDescription:@"query"];
  1374. [query getDocumentsWithCompletion:^(FIRQuerySnapshot *results, NSError *error) {
  1375. XCTAssertNil(results);
  1376. XCTAssertNotNil(error);
  1377. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  1378. [queryCompletion fulfill];
  1379. }];
  1380. [self awaitExpectations];
  1381. }
  1382. - (void)testResumingAQueryShouldUseBloomFilterToAvoidFullRequery {
  1383. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1384. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1385. // sends.
  1386. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1387. "Skip this test when running against the Firestore emulator because the emulator does "
  1388. "not include a bloom filter when it sends existence filter messages, making it "
  1389. "impossible for this test to verify the correctness of the bloom filter.");
  1390. // Set this test to stop when the first failure occurs because some test assertion failures make
  1391. // the rest of the test not applicable or will even crash.
  1392. [self setContinueAfterFailure:NO];
  1393. // Prepare the names and contents of the 100 documents to create.
  1394. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1395. [[NSMutableDictionary alloc] init];
  1396. for (int i = 0; i < 100; i++) {
  1397. [testDocs setValue:@{@"key" : @42} forKey:[NSString stringWithFormat:@"doc%@", @(1000 + i)]];
  1398. }
  1399. // Each iteration of the "while" loop below runs a single iteration of the test. The test will
  1400. // be run multiple times only if a bloom filter false positive occurs.
  1401. int attemptNumber = 0;
  1402. while (true) {
  1403. attemptNumber++;
  1404. // Create 100 documents in a new collection.
  1405. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1406. // Run a query to populate the local cache with the 100 documents and a resume token.
  1407. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:collRef
  1408. source:FIRFirestoreSourceDefault];
  1409. XCTAssertEqual(querySnapshot1.count, 100, @"querySnapshot1.count has an unexpected value");
  1410. NSArray<FIRDocumentReference *> *createdDocuments =
  1411. FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
  1412. // Delete 50 of the 100 documents. Use a different Firestore instance to avoid affecting the
  1413. // local cache.
  1414. NSSet<NSString *> *deletedDocumentIds;
  1415. {
  1416. FIRFirestore *db2 = [self firestore];
  1417. FIRWriteBatch *batch = [db2 batch];
  1418. NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1419. for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
  1420. FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
  1421. [batch deleteDocument:documentToDelete];
  1422. [deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
  1423. }
  1424. [self commitWriteBatch:batch];
  1425. deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
  1426. }
  1427. XCTAssertEqual(deletedDocumentIds.count, 50u, @"deletedDocumentIds has the wrong size");
  1428. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1429. // existence filter rather than "delete" events when the query is resumed.
  1430. [NSThread sleepForTimeInterval:10.0f];
  1431. // Resume the query and save the resulting snapshot for verification.
  1432. // Use some internal testing hooks to "capture" the existence filter mismatches to verify that
  1433. // Watch sent a bloom filter, and it was used to avert a full requery.
  1434. __block FIRQuerySnapshot *querySnapshot2;
  1435. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1436. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1437. querySnapshot2 = [self readDocumentSetForRef:collRef source:FIRFirestoreSourceDefault];
  1438. }];
  1439. // Verify that the snapshot from the resumed query contains the expected documents; that is,
  1440. // that it contains the 50 documents that were _not_ deleted.
  1441. {
  1442. NSMutableArray<NSString *> *expectedDocumentIds = [[NSMutableArray alloc] init];
  1443. for (FIRDocumentReference *documentRef in createdDocuments) {
  1444. if (![deletedDocumentIds containsObject:documentRef.documentID]) {
  1445. [expectedDocumentIds addObject:documentRef.documentID];
  1446. }
  1447. }
  1448. XCTAssertEqualObjects([NSSet setWithArray:FIRQuerySnapshotGetIDs(querySnapshot2)],
  1449. [NSSet setWithArray:expectedDocumentIds],
  1450. @"querySnapshot2 has the wrong documents");
  1451. }
  1452. // Verify that Watch sent an existence filter with the correct counts when the query was
  1453. // resumed.
  1454. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1455. @"Watch should have sent exactly 1 existence filter");
  1456. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1457. existenceFilterMismatches[0];
  1458. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, 100);
  1459. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, 50);
  1460. // Verify that Watch sent a valid bloom filter.
  1461. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1462. XCTAssertNotNil(bloomFilter,
  1463. "Watch should have included a bloom filter in the existence filter");
  1464. XCTAssertGreaterThan(bloomFilter.hashCount, 0);
  1465. XCTAssertGreaterThan(bloomFilter.bitmapLength, 0);
  1466. XCTAssertGreaterThan(bloomFilter.padding, 0);
  1467. XCTAssertLessThan(bloomFilter.padding, 8);
  1468. // Verify that the bloom filter was successfully used to avert a full requery. If a false
  1469. // positive occurred then retry the entire test. Although statistically rare, false positives
  1470. // are expected to happen occasionally. When a false positive _does_ happen, just retry the test
  1471. // with a different set of documents. If that retry _also_ experiences a false positive, then
  1472. // fail the test because that is so improbable that something must have gone wrong.
  1473. if (attemptNumber == 1 && !bloomFilter.applied) {
  1474. continue;
  1475. }
  1476. XCTAssertTrue(bloomFilter.applied,
  1477. @"The bloom filter should have been successfully applied with attemptNumber=%@",
  1478. @(attemptNumber));
  1479. // Break out of the test loop now that the test passes.
  1480. break;
  1481. }
  1482. }
  1483. - (void)
  1484. testBloomFilterShouldAvertAFullRequeryWhenDocumentsWereAddedDeletedRemovedUpdatedAndUnchangedSinceTheResumeToken {
  1485. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1486. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1487. // sends.
  1488. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1489. "Skip this test when running against the Firestore emulator because the emulator does "
  1490. "not include a bloom filter when it sends existence filter messages, making it "
  1491. "impossible for this test to verify the correctness of the bloom filter.");
  1492. // Set this test to stop when the first failure occurs because some test assertion failures make
  1493. // the rest of the test not applicable or will even crash.
  1494. [self setContinueAfterFailure:NO];
  1495. // Prepare the names and contents of the 20 documents to create.
  1496. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1497. [[NSMutableDictionary alloc] init];
  1498. for (int i = 0; i < 20; i++) {
  1499. [testDocs setValue:@{@"key" : @42, @"removed" : @NO}
  1500. forKey:[NSString stringWithFormat:@"doc%@", @(1000 + i)]];
  1501. }
  1502. // Each iteration of the "while" loop below runs a single iteration of the test. The test will
  1503. // be run multiple times only if a bloom filter false positive occurs.
  1504. int attemptNumber = 0;
  1505. while (true) {
  1506. attemptNumber++;
  1507. // Create 20 documents in a new collection.
  1508. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1509. FIRQuery *query = [collRef queryWhereField:@"removed" isEqualTo:@NO];
  1510. // Run a query to populate the local cache with the 20 documents and a resume token.
  1511. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:query
  1512. source:FIRFirestoreSourceDefault];
  1513. XCTAssertEqual(querySnapshot1.count, 20u, @"querySnapshot1.count has an unexpected value");
  1514. NSArray<FIRDocumentReference *> *createdDocuments =
  1515. FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
  1516. // Out of the 20 existing documents, leave 5 docs untouched, delete 5 docs, remove 5 docs,
  1517. // update 5 docs, and add 15 new docs.
  1518. NSSet<NSString *> *deletedDocumentIds;
  1519. NSSet<NSString *> *removedDocumentIds;
  1520. NSSet<NSString *> *updatedDocumentIds;
  1521. NSMutableArray<NSString *> *addedDocumentIds = [[NSMutableArray alloc] init];
  1522. {
  1523. FIRFirestore *db2 = [self firestore];
  1524. FIRWriteBatch *batch = [db2 batch];
  1525. NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1526. for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 4) {
  1527. FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
  1528. [batch deleteDocument:documentToDelete];
  1529. [deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
  1530. }
  1531. deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
  1532. XCTAssertEqual(deletedDocumentIds.count, 5u, @"deletedDocumentIds has the wrong size");
  1533. // Update 5 documents to no longer match the query.
  1534. NSMutableArray<NSString *> *removedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1535. for (decltype(createdDocuments.count) i = 1; i < createdDocuments.count; i += 4) {
  1536. FIRDocumentReference *documentToRemove = [db2 documentWithPath:createdDocuments[i].path];
  1537. [batch updateData:@{@"removed" : @YES} forDocument:documentToRemove];
  1538. [removedDocumentIdsAccumulator addObject:documentToRemove.documentID];
  1539. }
  1540. removedDocumentIds = [NSSet setWithArray:removedDocumentIdsAccumulator];
  1541. XCTAssertEqual(removedDocumentIds.count, 5u, @"removedDocumentIds has the wrong size");
  1542. // Update 5 documents, but ensure they still match the query.
  1543. NSMutableArray<NSString *> *updatedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1544. for (decltype(createdDocuments.count) i = 2; i < createdDocuments.count; i += 4) {
  1545. FIRDocumentReference *documentToUpdate = [db2 documentWithPath:createdDocuments[i].path];
  1546. [batch updateData:@{@"key" : @43} forDocument:documentToUpdate];
  1547. [updatedDocumentIdsAccumulator addObject:documentToUpdate.documentID];
  1548. }
  1549. updatedDocumentIds = [NSSet setWithArray:updatedDocumentIdsAccumulator];
  1550. XCTAssertEqual(updatedDocumentIds.count, 5u, @"updatedDocumentIds has the wrong size");
  1551. for (int i = 0; i < 15; i += 1) {
  1552. FIRDocumentReference *documentToAdd = [db2
  1553. documentWithPath:[NSString stringWithFormat:@"%@/newDoc%@", collRef.path, @(1000 + i)]];
  1554. [batch setData:@{@"key" : @42, @"removed" : @NO} forDocument:documentToAdd];
  1555. [addedDocumentIds addObject:documentToAdd.documentID];
  1556. }
  1557. // Ensure the documentIds above are mutually exclusive.
  1558. NSMutableSet<NSString *> *mergedSet = [NSMutableSet setWithArray:addedDocumentIds];
  1559. [mergedSet unionSet:deletedDocumentIds];
  1560. [mergedSet unionSet:removedDocumentIds];
  1561. [mergedSet unionSet:updatedDocumentIds];
  1562. XCTAssertEqual(mergedSet.count, 30u, @"There are documents experienced multiple operations.");
  1563. [self commitWriteBatch:batch];
  1564. }
  1565. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1566. // existence filter rather than "delete" events when the query is resumed.
  1567. [NSThread sleepForTimeInterval:10.0f];
  1568. // Resume the query and save the resulting snapshot for verification. Use some internal testing
  1569. // hooks to "capture" the existence filter mismatches to verify that Watch sent a bloom
  1570. // filter, and it was used to avert a full requery.
  1571. __block FIRQuerySnapshot *querySnapshot2;
  1572. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1573. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1574. querySnapshot2 = [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  1575. }];
  1576. XCTAssertEqual(querySnapshot2.count, 25u, @"querySnapshot1.count has an unexpected value");
  1577. // Verify that the snapshot from the resumed query contains the expected documents; that is, 10
  1578. // existing documents that still match the query, and 15 documents that are newly added.
  1579. {
  1580. NSMutableArray<NSString *> *expectedDocumentIds = [[NSMutableArray alloc] init];
  1581. for (FIRDocumentReference *documentRef in createdDocuments) {
  1582. if (![deletedDocumentIds containsObject:documentRef.documentID] &&
  1583. ![removedDocumentIds containsObject:documentRef.documentID]) {
  1584. [expectedDocumentIds addObject:documentRef.documentID];
  1585. }
  1586. }
  1587. [expectedDocumentIds addObjectsFromArray:addedDocumentIds];
  1588. XCTAssertEqualObjects([NSSet setWithArray:FIRQuerySnapshotGetIDs(querySnapshot2)],
  1589. [NSSet setWithArray:expectedDocumentIds],
  1590. @"querySnapshot2 has the wrong documents");
  1591. }
  1592. // Verify that Watch sent an existence filter with the correct counts when the query was
  1593. // resumed.
  1594. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1595. @"Watch should have sent exactly 1 existence filter");
  1596. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1597. existenceFilterMismatches[0];
  1598. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, 35);
  1599. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, 25);
  1600. // Verify that Watch sent a valid bloom filter.
  1601. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1602. XCTAssertNotNil(bloomFilter,
  1603. "Watch should have included a bloom filter in the existence filter");
  1604. // Verify that the bloom filter was successfully used to avert a full requery. If a false
  1605. // positive occurred then retry the entire test. Although statistically rare, false positives
  1606. // are expected to happen occasionally. When a false positive _does_ happen, just retry the test
  1607. // with a different set of documents. If that retry _also_ experiences a false positive, then
  1608. // fail the test because that is so improbable that something must have gone wrong.
  1609. if (attemptNumber == 1 && !bloomFilter.applied) {
  1610. continue;
  1611. }
  1612. XCTAssertTrue(bloomFilter.applied,
  1613. @"The bloom filter should have been successfully applied with attemptNumber=%@",
  1614. @(attemptNumber));
  1615. // Break out of the test loop now that the test passes.
  1616. break;
  1617. }
  1618. }
  1619. - (void)testBloomFilterShouldCorrectlyEncodeComplexUnicodeCharacters {
  1620. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1621. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1622. // sends.
  1623. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1624. "Skip this test when running against the Firestore emulator because the emulator does "
  1625. "not include a bloom filter when it sends existence filter messages, making it "
  1626. "impossible for this test to verify the correctness of the bloom filter.");
  1627. // Set this test to stop when the first failure occurs because some test assertion failures make
  1628. // the rest of the test not applicable or will even crash.
  1629. [self setContinueAfterFailure:NO];
  1630. // Define a comparator that compares `NSString` objects in a way that orders canonically-
  1631. // equivalent, but distinct, strings in a consistent manner by using `NSForcedOrderingSearch`.
  1632. // Otherwise, the bare `[NSString compare:]` method considers canonically-equivalent, but
  1633. // distinct, strings as "equal" and orders them indeterminately.
  1634. NSComparator sortComparator = ^(NSString *string1, NSString *string2) {
  1635. return [string1 compare:string2 options:NSForcedOrderingSearch];
  1636. };
  1637. // Firestore does not do any Unicode normalization on the document IDs. Therefore, two document
  1638. // IDs that are canonically-equivalent (i.e. they visually appear identical) but are represented
  1639. // by a different sequence of Unicode code points are treated as distinct document IDs.
  1640. NSArray<NSString *> *testDocIds;
  1641. {
  1642. NSMutableArray<NSString *> *testDocIdsAccumulator = [[NSMutableArray alloc] init];
  1643. [testDocIdsAccumulator addObject:@"DocumentToDelete"];
  1644. // The next two strings both end with "e" with an accent: the first uses the dedicated Unicode
  1645. // code point for this character, while the second uses the standard lowercase "e" followed by
  1646. // the accent combining character.
  1647. [testDocIdsAccumulator addObject:@"LowercaseEWithAcuteAccent_\u00E9"];
  1648. [testDocIdsAccumulator addObject:@"LowercaseEWithAcuteAccent_\u0065\u0301"];
  1649. // The next two strings both end with an "e" with two different accents applied via the
  1650. // following two combining characters. The combining characters are specified in a different
  1651. // order and Firestore treats these document IDs as unique, despite the order of the combining
  1652. // characters being irrelevant.
  1653. [testDocIdsAccumulator addObject:@"LowercaseEWithMultipleAccents_\u0065\u0301\u0327"];
  1654. [testDocIdsAccumulator addObject:@"LowercaseEWithMultipleAccents_\u0065\u0327\u0301"];
  1655. // The next string contains a character outside the BMP (the "basic multilingual plane"); that
  1656. // is, its code point is greater than 0xFFFF. Since NSString stores text in sequences of 16-bit
  1657. // code units, using the UTF-16 encoding (according to
  1658. // https://www.objc.io/issues/9-strings/unicode) it is stored as a surrogate pair, two 16-bit
  1659. // code units U+D83D and U+DE00, to represent this character. Make sure that its presence is
  1660. // correctly tested in the bloom filter, which uses UTF-8 encoding.
  1661. [testDocIdsAccumulator addObject:@"Smiley_\U0001F600"];
  1662. testDocIds = [NSArray arrayWithArray:testDocIdsAccumulator];
  1663. }
  1664. // Verify assumptions about the equivalence of strings in `testDocIds`.
  1665. XCTAssertEqualObjects(testDocIds[1].decomposedStringWithCanonicalMapping,
  1666. testDocIds[2].decomposedStringWithCanonicalMapping);
  1667. XCTAssertEqualObjects(testDocIds[3].decomposedStringWithCanonicalMapping,
  1668. testDocIds[4].decomposedStringWithCanonicalMapping);
  1669. XCTAssertEqual([testDocIds[5] characterAtIndex:7], 0xD83D);
  1670. XCTAssertEqual([testDocIds[5] characterAtIndex:8], 0xDE00);
  1671. // Create the mapping from document ID to document data for the document IDs specified in
  1672. // `testDocIds`.
  1673. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1674. [[NSMutableDictionary alloc] init];
  1675. for (NSString *testDocId in testDocIds) {
  1676. [testDocs setValue:@{@"foo" : @42} forKey:testDocId];
  1677. }
  1678. // Create the documents whose names contain complex Unicode characters in a new collection.
  1679. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1680. // Run a query to populate the local cache with documents that have names with complex Unicode
  1681. // characters.
  1682. {
  1683. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:collRef
  1684. source:FIRFirestoreSourceDefault];
  1685. XCTAssertEqualObjects(
  1686. [FIRQuerySnapshotGetIDs(querySnapshot1) sortedArrayUsingComparator:sortComparator],
  1687. [testDocIds sortedArrayUsingComparator:sortComparator],
  1688. @"querySnapshot1 has the wrong documents");
  1689. }
  1690. // Delete one of the documents so that the next call to collection.get() will experience an
  1691. // existence filter mismatch. Use a different Firestore instance to avoid affecting the local
  1692. // cache.
  1693. FIRDocumentReference *documentToDelete = [collRef documentWithPath:@"DocumentToDelete"];
  1694. {
  1695. FIRFirestore *db2 = [self firestore];
  1696. [self deleteDocumentRef:[db2 documentWithPath:documentToDelete.path]];
  1697. }
  1698. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1699. // existence filter rather than "delete" events when the query is resumed.
  1700. [NSThread sleepForTimeInterval:10.0f];
  1701. // Resume the query and save the resulting snapshot for verification. Use some internal testing
  1702. // hooks to "capture" the existence filter mismatches.
  1703. __block FIRQuerySnapshot *querySnapshot2;
  1704. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1705. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1706. querySnapshot2 = [self readDocumentSetForRef:collRef source:FIRFirestoreSourceDefault];
  1707. }];
  1708. // Verify that the snapshot from the resumed query contains the expected documents; that is, that
  1709. // it contains the documents whose names contain complex Unicode characters and _not_ the document
  1710. // that was deleted.
  1711. {
  1712. NSMutableArray<NSString *> *querySnapshot2ExpectedDocumentIds =
  1713. [NSMutableArray arrayWithArray:testDocIds];
  1714. [querySnapshot2ExpectedDocumentIds removeObject:documentToDelete.documentID];
  1715. XCTAssertEqualObjects(
  1716. [FIRQuerySnapshotGetIDs(querySnapshot2) sortedArrayUsingComparator:sortComparator],
  1717. [querySnapshot2ExpectedDocumentIds sortedArrayUsingComparator:sortComparator],
  1718. @"querySnapshot2 has the wrong documents");
  1719. }
  1720. // Verify that Watch sent an existence filter with the correct counts.
  1721. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1722. @"Watch should have sent exactly 1 existence filter");
  1723. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1724. existenceFilterMismatches[0];
  1725. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, (int)testDocIds.count);
  1726. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, (int)testDocIds.count - 1);
  1727. // Verify that Watch sent a valid bloom filter.
  1728. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1729. XCTAssertNotNil(bloomFilter, "Watch should have included a bloom filter in the existence filter");
  1730. // The bloom filter application should statistically be successful almost every time; the _only_
  1731. // time when it would _not_ be successful is if there is a false positive when testing for
  1732. // 'DocumentToDelete' in the bloom filter. So verify that the bloom filter application is
  1733. // successful, unless there was a false positive.
  1734. BOOL isFalsePositive = [bloomFilter mightContain:documentToDelete];
  1735. XCTAssertEqual(bloomFilter.applied, !isFalsePositive);
  1736. // Verify that the bloom filter contains the document paths with complex Unicode characters.
  1737. for (FIRDocumentSnapshot *documentSnapshot in querySnapshot2.documents) {
  1738. XCTAssertTrue([bloomFilter mightContain:documentSnapshot.reference],
  1739. @"The bloom filter should contain %@", documentSnapshot.documentID);
  1740. }
  1741. }
  1742. @end