FIRQueryTests.mm 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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)testSnapshotListenerSortsQueryByDocumentIdInTheSameOrderAsServer {
  679. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  680. @"A" : @{@"a" : @1},
  681. @"a" : @{@"a" : @1},
  682. @"Aa" : @{@"a" : @1},
  683. @"7" : @{@"a" : @1},
  684. @"12" : @{@"a" : @1},
  685. @"__id7__" : @{@"a" : @1},
  686. @"__id12__" : @{@"a" : @1},
  687. @"__id-2__" : @{@"a" : @1},
  688. @"__id1_" : @{@"a" : @1},
  689. @"_id1__" : @{@"a" : @1},
  690. @"__id" : @{@"a" : @1},
  691. @"__id9223372036854775807__" : @{@"a" : @1},
  692. @"__id-9223372036854775808__" : @{@"a" : @1},
  693. }];
  694. FIRQuery *query = [collRef queryOrderedByFieldPath:[FIRFieldPath documentID]];
  695. NSArray<NSString *> *expectedDocs = @[
  696. @"__id-9223372036854775808__", @"__id-2__", @"__id7__", @"__id12__",
  697. @"__id9223372036854775807__", @"12", @"7", @"A", @"Aa", @"__id", @"__id1_", @"_id1__", @"a"
  698. ];
  699. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  700. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  701. id<FIRListenerRegistration> registration =
  702. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  703. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  704. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  705. [registration remove];
  706. }
  707. - (void)testSnapshotListenerSortsFilteredQueryByDocumentIdInTheSameOrderAsServer {
  708. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  709. @"A" : @{@"a" : @1},
  710. @"a" : @{@"a" : @1},
  711. @"Aa" : @{@"a" : @1},
  712. @"7" : @{@"a" : @1},
  713. @"12" : @{@"a" : @1},
  714. @"__id7__" : @{@"a" : @1},
  715. @"__id12__" : @{@"a" : @1},
  716. @"__id-2__" : @{@"a" : @1},
  717. @"__id1_" : @{@"a" : @1},
  718. @"_id1__" : @{@"a" : @1},
  719. @"__id" : @{@"a" : @1},
  720. @"__id9223372036854775807__" : @{@"a" : @1},
  721. @"__id-9223372036854775808__" : @{@"a" : @1},
  722. }];
  723. FIRQuery *query = [[[collRef queryWhereFieldPath:[FIRFieldPath documentID]
  724. isGreaterThan:@"__id7__"]
  725. queryWhereFieldPath:[FIRFieldPath documentID]
  726. isLessThanOrEqualTo:@"A"] queryOrderedByFieldPath:[FIRFieldPath documentID]];
  727. NSArray<NSString *> *expectedDocs =
  728. @[ @"__id12__", @"__id9223372036854775807__", @"12", @"7", @"A" ];
  729. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  730. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  731. id<FIRListenerRegistration> registration =
  732. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  733. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  734. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  735. [registration remove];
  736. }
  737. - (void)testSdkOrdersQueryByDocumentIdTheSameWayOnlineAndOffline {
  738. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  739. @"A" : @{@"a" : @1},
  740. @"a" : @{@"a" : @1},
  741. @"Aa" : @{@"a" : @1},
  742. @"7" : @{@"a" : @1},
  743. @"12" : @{@"a" : @1},
  744. @"__id7__" : @{@"a" : @1},
  745. @"__id12__" : @{@"a" : @1},
  746. @"__id-2__" : @{@"a" : @1},
  747. @"__id1_" : @{@"a" : @1},
  748. @"_id1__" : @{@"a" : @1},
  749. @"__id" : @{@"a" : @1},
  750. @"__id9223372036854775807__" : @{@"a" : @1},
  751. @"__id-9223372036854775808__" : @{@"a" : @1},
  752. }];
  753. [self checkOnlineAndOfflineQuery:[collRef queryOrderedByFieldPath:[FIRFieldPath documentID]]
  754. matchesResult:@[
  755. @"__id-9223372036854775808__", @"__id-2__", @"__id7__", @"__id12__",
  756. @"__id9223372036854775807__", @"12", @"7", @"A", @"Aa", @"__id", @"__id1_",
  757. @"_id1__", @"a"
  758. ]];
  759. }
  760. - (void)testSnapshotListenerSortsUnicodeStringsInTheSameOrderAsServer {
  761. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  762. @"a" : @{@"value" : @"Łukasiewicz"},
  763. @"b" : @{@"value" : @"Sierpiński"},
  764. @"c" : @{@"value" : @"岩澤"},
  765. @"d" : @{@"value" : @"🄟"},
  766. @"e" : @{@"value" : @"P"},
  767. @"f" : @{@"value" : @"︒"},
  768. @"g" : @{@"value" : @"🐵"}
  769. }];
  770. FIRQuery *query = [collRef queryOrderedByField:@"value"];
  771. NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
  772. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  773. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  774. id<FIRListenerRegistration> registration =
  775. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  776. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  777. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  778. [registration remove];
  779. [self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
  780. }
  781. - (void)testSnapshotListenerSortsUnicodeStringsInArrayInTheSameOrderAsServer {
  782. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  783. @"a" : @{@"value" : @[ @"Łukasiewicz" ]},
  784. @"b" : @{@"value" : @[ @"Sierpiński" ]},
  785. @"c" : @{@"value" : @[ @"岩澤" ]},
  786. @"d" : @{@"value" : @[ @"🄟" ]},
  787. @"e" : @{@"value" : @[ @"P" ]},
  788. @"f" : @{@"value" : @[ @"︒" ]},
  789. @"g" : @{@"value" : @[ @"🐵" ]}
  790. }];
  791. FIRQuery *query = [collRef queryOrderedByField:@"value"];
  792. NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
  793. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  794. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  795. id<FIRListenerRegistration> registration =
  796. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  797. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  798. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  799. [registration remove];
  800. [self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
  801. }
  802. - (void)testSnapshotListenerSortsUnicodeStringsInMapInTheSameOrderAsServer {
  803. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  804. @"a" : @{@"value" : @{@"foo" : @"Łukasiewicz"}},
  805. @"b" : @{@"value" : @{@"foo" : @"Sierpiński"}},
  806. @"c" : @{@"value" : @{@"foo" : @"岩澤"}},
  807. @"d" : @{@"value" : @{@"foo" : @"🄟"}},
  808. @"e" : @{@"value" : @{@"foo" : @"P"}},
  809. @"f" : @{@"value" : @{@"foo" : @"︒"}},
  810. @"g" : @{@"value" : @{@"foo" : @"🐵"}}
  811. }];
  812. FIRQuery *query = [collRef queryOrderedByField:@"value"];
  813. NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
  814. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  815. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  816. id<FIRListenerRegistration> registration =
  817. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  818. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  819. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  820. [registration remove];
  821. [self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
  822. }
  823. - (void)testSnapshotListenerSortsUnicodeStringsInMapKeyInTheSameOrderAsServer {
  824. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  825. @"a" : @{@"value" : @{@"Łukasiewicz" : @"foo"}},
  826. @"b" : @{@"value" : @{@"Sierpiński" : @"foo"}},
  827. @"c" : @{@"value" : @{@"岩澤" : @"foo"}},
  828. @"d" : @{@"value" : @{@"🄟" : @"foo"}},
  829. @"e" : @{@"value" : @{@"P" : @"foo"}},
  830. @"f" : @{@"value" : @{@"︒" : @"foo"}},
  831. @"g" : @{@"value" : @{@"🐵" : @"foo"}}
  832. }];
  833. FIRQuery *query = [collRef queryOrderedByField:@"value"];
  834. NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
  835. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  836. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  837. id<FIRListenerRegistration> registration =
  838. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  839. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  840. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  841. [registration remove];
  842. [self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
  843. }
  844. - (void)testSnapshotListenerSortsUnicodeStringsInDocumentKeyInTheSameOrderAsServer {
  845. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  846. @"Łukasiewicz" : @{@"value" : @"foo"},
  847. @"Sierpiński" : @{@"value" : @"foo"},
  848. @"岩澤" : @{@"value" : @"foo"},
  849. @"🄟" : @{@"value" : @"foo"},
  850. @"P" : @{@"value" : @"foo"},
  851. @"︒" : @{@"value" : @"foo"},
  852. @"🐵" : @{@"value" : @"foo"}
  853. }];
  854. FIRQuery *query = [collRef queryOrderedByFieldPath:[FIRFieldPath documentID]];
  855. NSArray<NSString *> *expectedDocs =
  856. @[ @"Sierpiński", @"Łukasiewicz", @"岩澤", @"︒", @"P", @"🄟", @"🐵" ];
  857. FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
  858. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
  859. id<FIRListenerRegistration> registration =
  860. [query addSnapshotListener:self.eventAccumulator.valueEventHandler];
  861. FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
  862. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);
  863. [registration remove];
  864. [self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
  865. }
  866. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  867. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  868. // for predictable ordering.
  869. NSString *collectionGroup = [NSString
  870. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  871. NSArray *docPaths = @[
  872. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  873. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  874. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  875. ];
  876. FIRWriteBatch *batch = [self.db batch];
  877. for (NSString *docPath in docPaths) {
  878. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  879. withString:collectionGroup];
  880. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  881. }
  882. [self commitWriteBatch:batch];
  883. FIRQuerySnapshot *querySnapshot = [self
  884. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  885. queryWhereFieldPath:[FIRFieldPath documentID]
  886. isGreaterThanOrEqualTo:@"a/b"]
  887. queryWhereFieldPath:[FIRFieldPath documentID]
  888. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  889. collectionGroup]]];
  890. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  891. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  892. }
  893. - (void)testOrQueries {
  894. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  895. @"doc1" : @{@"a" : @1, @"b" : @0},
  896. @"doc2" : @{@"a" : @2, @"b" : @1},
  897. @"doc3" : @{@"a" : @3, @"b" : @2},
  898. @"doc4" : @{@"a" : @1, @"b" : @3},
  899. @"doc5" : @{@"a" : @1, @"b" : @1}
  900. }];
  901. // Two equalities: a==1 || b==1.
  902. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  903. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  904. ]];
  905. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  906. matchesResult:@[ @"doc1", @"doc2", @"doc4", @"doc5" ]];
  907. // (a==1 && b==0) || (a==3 && b==2)
  908. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  909. [FIRFilter andFilterWithFilters:@[
  910. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter filterWhereField:@"b" isEqualTo:@0]
  911. ]],
  912. [FIRFilter andFilterWithFilters:@[
  913. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  914. ]]
  915. ]];
  916. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  917. matchesResult:@[ @"doc1", @"doc3" ]];
  918. // a==1 && (b==0 || b==3).
  919. FIRFilter *filter3 = [FIRFilter andFilterWithFilters:@[
  920. [FIRFilter filterWhereField:@"a" isEqualTo:@1], [FIRFilter orFilterWithFilters:@[
  921. [FIRFilter filterWhereField:@"b" isEqualTo:@0], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  922. ]]
  923. ]];
  924. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter3]
  925. matchesResult:@[ @"doc1", @"doc4" ]];
  926. // (a==2 || b==2) && (a==3 || b==3)
  927. FIRFilter *filter4 = [FIRFilter andFilterWithFilters:@[
  928. [FIRFilter orFilterWithFilters:@[
  929. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@2]
  930. ]],
  931. [FIRFilter orFilterWithFilters:@[
  932. [FIRFilter filterWhereField:@"a" isEqualTo:@3], [FIRFilter filterWhereField:@"b" isEqualTo:@3]
  933. ]]
  934. ]];
  935. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter4] matchesResult:@[ @"doc3" ]];
  936. // Test with limits without orderBy (the __name__ ordering is the tie breaker).
  937. FIRFilter *filter5 = [FIRFilter orFilterWithFilters:@[
  938. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" isEqualTo:@1]
  939. ]];
  940. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:filter5] queryLimitedTo:1]
  941. matchesResult:@[ @"doc2" ]];
  942. }
  943. - (void)testOrQueriesWithIn {
  944. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  945. @"doc1" : @{@"a" : @1, @"b" : @0},
  946. @"doc2" : @{@"b" : @1},
  947. @"doc3" : @{@"a" : @3, @"b" : @2},
  948. @"doc4" : @{@"a" : @1, @"b" : @3},
  949. @"doc5" : @{@"a" : @1},
  950. @"doc6" : @{@"a" : @2}
  951. }];
  952. // a==2 || b in [2,3]
  953. FIRFilter *filter = [FIRFilter orFilterWithFilters:@[
  954. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b" in:@[ @2, @3 ]]
  955. ]];
  956. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter]
  957. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  958. }
  959. - (void)testOrQueriesWithArrayMembership {
  960. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  961. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  962. @"doc2" : @{@"b" : @[ @1 ]},
  963. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ]},
  964. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  965. @"doc5" : @{@"a" : @1},
  966. @"doc6" : @{@"a" : @2}
  967. }];
  968. // a==2 || b array-contains 7
  969. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  970. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  971. arrayContains:@7]
  972. ]];
  973. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  974. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  975. // a==2 || b array-contains-any [0, 3]
  976. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  977. [FIRFilter filterWhereField:@"a" isEqualTo:@2], [FIRFilter filterWhereField:@"b"
  978. arrayContainsAny:@[ @0, @3 ]]
  979. ]];
  980. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  981. matchesResult:@[ @"doc1", @"doc4", @"doc6" ]];
  982. }
  983. - (void)testMultipleInOps {
  984. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  985. @"doc1" : @{@"a" : @1, @"b" : @0},
  986. @"doc2" : @{@"b" : @1},
  987. @"doc3" : @{@"a" : @3, @"b" : @2},
  988. @"doc4" : @{@"a" : @1, @"b" : @3},
  989. @"doc5" : @{@"a" : @1},
  990. @"doc6" : @{@"a" : @2}
  991. }];
  992. // Two IN operations on different fields with disjunction.
  993. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  994. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  995. in:@[ @0, @2 ]]
  996. ]];
  997. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  998. matchesResult:@[ @"doc1", @"doc3", @"doc6" ]];
  999. // Two IN operations on the same field with disjunction.
  1000. // a IN [0,3] || a IN [0,2] should union them (similar to: a IN [0,2,3]).
  1001. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  1002. [FIRFilter filterWhereField:@"a" in:@[ @0, @3 ]], [FIRFilter filterWhereField:@"a"
  1003. in:@[ @0, @2 ]]
  1004. ]];
  1005. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  1006. matchesResult:@[ @"doc3", @"doc6" ]];
  1007. }
  1008. - (void)testUsingInWithArrayContainsAny {
  1009. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1010. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  1011. @"doc2" : @{@"b" : @[ @1 ]},
  1012. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ], @"c" : @10},
  1013. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  1014. @"doc5" : @{@"a" : @1},
  1015. @"doc6" : @{@"a" : @2, @"c" : @20}
  1016. }];
  1017. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  1018. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  1019. arrayContainsAny:@[ @0, @7 ]]
  1020. ]];
  1021. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  1022. matchesResult:@[ @"doc1", @"doc3", @"doc4", @"doc6" ]];
  1023. FIRFilter *filter2 = [FIRFilter orFilterWithFilters:@[
  1024. [FIRFilter andFilterWithFilters:@[
  1025. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"c"
  1026. isEqualTo:@10]
  1027. ]],
  1028. [FIRFilter filterWhereField:@"b" arrayContainsAny:@[ @0, @7 ]]
  1029. ]];
  1030. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2]
  1031. matchesResult:@[ @"doc1", @"doc3", @"doc4" ]];
  1032. }
  1033. - (void)testUseInWithArrayContains {
  1034. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1035. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  1036. @"doc2" : @{@"b" : @[ @1 ]},
  1037. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ]},
  1038. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  1039. @"doc5" : @{@"a" : @1},
  1040. @"doc6" : @{@"a" : @2}
  1041. }];
  1042. FIRFilter *filter1 = [FIRFilter orFilterWithFilters:@[
  1043. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  1044. arrayContainsAny:@[ @3 ]]
  1045. ]];
  1046. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter1]
  1047. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  1048. FIRFilter *filter2 = [FIRFilter andFilterWithFilters:@[
  1049. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter filterWhereField:@"b"
  1050. arrayContains:@7]
  1051. ]];
  1052. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter2] matchesResult:@[ @"doc3" ]];
  1053. FIRFilter *filter3 = [FIRFilter orFilterWithFilters:@[
  1054. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter andFilterWithFilters:@[
  1055. [FIRFilter filterWhereField:@"b" arrayContains:@3], [FIRFilter filterWhereField:@"a"
  1056. isEqualTo:@1]
  1057. ]]
  1058. ]];
  1059. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter3]
  1060. matchesResult:@[ @"doc3", @"doc4", @"doc6" ]];
  1061. FIRFilter *filter4 = [FIRFilter andFilterWithFilters:@[
  1062. [FIRFilter filterWhereField:@"a" in:@[ @2, @3 ]], [FIRFilter orFilterWithFilters:@[
  1063. [FIRFilter filterWhereField:@"b" arrayContains:@7], [FIRFilter filterWhereField:@"a"
  1064. isEqualTo:@1]
  1065. ]]
  1066. ]];
  1067. [self checkOnlineAndOfflineQuery:[collRef queryWhereFilter:filter4] matchesResult:@[ @"doc3" ]];
  1068. }
  1069. - (void)testOrderByEquality {
  1070. // TODO(orquery): Enable this test against production when possible.
  1071. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
  1072. "Skip this test if running against production because order-by-equality is not "
  1073. "supported yet.");
  1074. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  1075. @"doc1" : @{@"a" : @1, @"b" : @[ @0 ]},
  1076. @"doc2" : @{@"b" : @[ @1 ]},
  1077. @"doc3" : @{@"a" : @3, @"b" : @[ @2, @7 ], @"c" : @10},
  1078. @"doc4" : @{@"a" : @1, @"b" : @[ @3, @7 ]},
  1079. @"doc5" : @{@"a" : @1},
  1080. @"doc6" : @{@"a" : @2, @"c" : @20}
  1081. }];
  1082. [self checkOnlineAndOfflineQuery:[[collRef queryWhereFilter:[FIRFilter filterWhereField:@"a"
  1083. isEqualTo:@1]]
  1084. queryOrderedByField:@"a"]
  1085. matchesResult:@[ @"doc1", @"doc4", @"doc5" ]];
  1086. [self checkOnlineAndOfflineQuery:[[collRef
  1087. queryWhereFilter:[FIRFilter filterWhereField:@"a"
  1088. in:@[ @2, @3 ]]]
  1089. queryOrderedByField:@"a"]
  1090. matchesResult:@[ @"doc6", @"doc3" ]];
  1091. }
  1092. - (void)testResumingAQueryShouldUseBloomFilterToAvoidFullRequery {
  1093. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1094. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1095. // sends.
  1096. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1097. "Skip this test when running against the Firestore emulator because the emulator does "
  1098. "not include a bloom filter when it sends existence filter messages, making it "
  1099. "impossible for this test to verify the correctness of the bloom filter.");
  1100. // Set this test to stop when the first failure occurs because some test assertion failures make
  1101. // the rest of the test not applicable or will even crash.
  1102. [self setContinueAfterFailure:NO];
  1103. // Prepare the names and contents of the 100 documents to create.
  1104. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1105. [[NSMutableDictionary alloc] init];
  1106. for (int i = 0; i < 100; i++) {
  1107. [testDocs setValue:@{@"key" : @42} forKey:[NSString stringWithFormat:@"doc%@", @(1000 + i)]];
  1108. }
  1109. // Each iteration of the "while" loop below runs a single iteration of the test. The test will
  1110. // be run multiple times only if a bloom filter false positive occurs.
  1111. int attemptNumber = 0;
  1112. while (true) {
  1113. attemptNumber++;
  1114. // Create 100 documents in a new collection.
  1115. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1116. // Run a query to populate the local cache with the 100 documents and a resume token.
  1117. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:collRef
  1118. source:FIRFirestoreSourceDefault];
  1119. XCTAssertEqual(querySnapshot1.count, 100, @"querySnapshot1.count has an unexpected value");
  1120. NSArray<FIRDocumentReference *> *createdDocuments =
  1121. FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
  1122. // Delete 50 of the 100 documents. Use a different Firestore instance to avoid affecting the
  1123. // local cache.
  1124. NSSet<NSString *> *deletedDocumentIds;
  1125. {
  1126. FIRFirestore *db2 = [self firestore];
  1127. FIRWriteBatch *batch = [db2 batch];
  1128. NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1129. for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
  1130. FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
  1131. [batch deleteDocument:documentToDelete];
  1132. [deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
  1133. }
  1134. [self commitWriteBatch:batch];
  1135. deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
  1136. }
  1137. XCTAssertEqual(deletedDocumentIds.count, 50u, @"deletedDocumentIds has the wrong size");
  1138. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1139. // existence filter rather than "delete" events when the query is resumed.
  1140. [NSThread sleepForTimeInterval:10.0f];
  1141. // Resume the query and save the resulting snapshot for verification.
  1142. // Use some internal testing hooks to "capture" the existence filter mismatches to verify that
  1143. // Watch sent a bloom filter, and it was used to avert a full requery.
  1144. __block FIRQuerySnapshot *querySnapshot2;
  1145. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1146. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1147. querySnapshot2 = [self readDocumentSetForRef:collRef source:FIRFirestoreSourceDefault];
  1148. }];
  1149. // Verify that the snapshot from the resumed query contains the expected documents; that is,
  1150. // that it contains the 50 documents that were _not_ deleted.
  1151. {
  1152. NSMutableArray<NSString *> *expectedDocumentIds = [[NSMutableArray alloc] init];
  1153. for (FIRDocumentReference *documentRef in createdDocuments) {
  1154. if (![deletedDocumentIds containsObject:documentRef.documentID]) {
  1155. [expectedDocumentIds addObject:documentRef.documentID];
  1156. }
  1157. }
  1158. XCTAssertEqualObjects([NSSet setWithArray:FIRQuerySnapshotGetIDs(querySnapshot2)],
  1159. [NSSet setWithArray:expectedDocumentIds],
  1160. @"querySnapshot2 has the wrong documents");
  1161. }
  1162. // Verify that Watch sent an existence filter with the correct counts when the query was
  1163. // resumed.
  1164. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1165. @"Watch should have sent exactly 1 existence filter");
  1166. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1167. existenceFilterMismatches[0];
  1168. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, 100);
  1169. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, 50);
  1170. // Verify that Watch sent a valid bloom filter.
  1171. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1172. XCTAssertNotNil(bloomFilter,
  1173. "Watch should have included a bloom filter in the existence filter");
  1174. XCTAssertGreaterThan(bloomFilter.hashCount, 0);
  1175. XCTAssertGreaterThan(bloomFilter.bitmapLength, 0);
  1176. XCTAssertGreaterThan(bloomFilter.padding, 0);
  1177. XCTAssertLessThan(bloomFilter.padding, 8);
  1178. // Verify that the bloom filter was successfully used to avert a full requery. If a false
  1179. // positive occurred then retry the entire test. Although statistically rare, false positives
  1180. // are expected to happen occasionally. When a false positive _does_ happen, just retry the test
  1181. // with a different set of documents. If that retry _also_ experiences a false positive, then
  1182. // fail the test because that is so improbable that something must have gone wrong.
  1183. if (attemptNumber == 1 && !bloomFilter.applied) {
  1184. continue;
  1185. }
  1186. XCTAssertTrue(bloomFilter.applied,
  1187. @"The bloom filter should have been successfully applied with attemptNumber=%@",
  1188. @(attemptNumber));
  1189. // Break out of the test loop now that the test passes.
  1190. break;
  1191. }
  1192. }
  1193. - (void)
  1194. testBloomFilterShouldAvertAFullRequeryWhenDocumentsWereAddedDeletedRemovedUpdatedAndUnchangedSinceTheResumeToken {
  1195. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1196. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1197. // sends.
  1198. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1199. "Skip this test when running against the Firestore emulator because the emulator does "
  1200. "not include a bloom filter when it sends existence filter messages, making it "
  1201. "impossible for this test to verify the correctness of the bloom filter.");
  1202. // Set this test to stop when the first failure occurs because some test assertion failures make
  1203. // the rest of the test not applicable or will even crash.
  1204. [self setContinueAfterFailure:NO];
  1205. // Prepare the names and contents of the 20 documents to create.
  1206. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1207. [[NSMutableDictionary alloc] init];
  1208. for (int i = 0; i < 20; i++) {
  1209. [testDocs setValue:@{@"key" : @42, @"removed" : @NO}
  1210. forKey:[NSString stringWithFormat:@"doc%@", @(1000 + i)]];
  1211. }
  1212. // Each iteration of the "while" loop below runs a single iteration of the test. The test will
  1213. // be run multiple times only if a bloom filter false positive occurs.
  1214. int attemptNumber = 0;
  1215. while (true) {
  1216. attemptNumber++;
  1217. // Create 20 documents in a new collection.
  1218. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1219. FIRQuery *query = [collRef queryWhereField:@"removed" isEqualTo:@NO];
  1220. // Run a query to populate the local cache with the 20 documents and a resume token.
  1221. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:query
  1222. source:FIRFirestoreSourceDefault];
  1223. XCTAssertEqual(querySnapshot1.count, 20u, @"querySnapshot1.count has an unexpected value");
  1224. NSArray<FIRDocumentReference *> *createdDocuments =
  1225. FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
  1226. // Out of the 20 existing documents, leave 5 docs untouched, delete 5 docs, remove 5 docs,
  1227. // update 5 docs, and add 15 new docs.
  1228. NSSet<NSString *> *deletedDocumentIds;
  1229. NSSet<NSString *> *removedDocumentIds;
  1230. NSSet<NSString *> *updatedDocumentIds;
  1231. NSMutableArray<NSString *> *addedDocumentIds = [[NSMutableArray alloc] init];
  1232. {
  1233. FIRFirestore *db2 = [self firestore];
  1234. FIRWriteBatch *batch = [db2 batch];
  1235. NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1236. for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 4) {
  1237. FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
  1238. [batch deleteDocument:documentToDelete];
  1239. [deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
  1240. }
  1241. deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
  1242. XCTAssertEqual(deletedDocumentIds.count, 5u, @"deletedDocumentIds has the wrong size");
  1243. // Update 5 documents to no longer match the query.
  1244. NSMutableArray<NSString *> *removedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1245. for (decltype(createdDocuments.count) i = 1; i < createdDocuments.count; i += 4) {
  1246. FIRDocumentReference *documentToRemove = [db2 documentWithPath:createdDocuments[i].path];
  1247. [batch updateData:@{@"removed" : @YES} forDocument:documentToRemove];
  1248. [removedDocumentIdsAccumulator addObject:documentToRemove.documentID];
  1249. }
  1250. removedDocumentIds = [NSSet setWithArray:removedDocumentIdsAccumulator];
  1251. XCTAssertEqual(removedDocumentIds.count, 5u, @"removedDocumentIds has the wrong size");
  1252. // Update 5 documents, but ensure they still match the query.
  1253. NSMutableArray<NSString *> *updatedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1254. for (decltype(createdDocuments.count) i = 2; i < createdDocuments.count; i += 4) {
  1255. FIRDocumentReference *documentToUpdate = [db2 documentWithPath:createdDocuments[i].path];
  1256. [batch updateData:@{@"key" : @43} forDocument:documentToUpdate];
  1257. [updatedDocumentIdsAccumulator addObject:documentToUpdate.documentID];
  1258. }
  1259. updatedDocumentIds = [NSSet setWithArray:updatedDocumentIdsAccumulator];
  1260. XCTAssertEqual(updatedDocumentIds.count, 5u, @"updatedDocumentIds has the wrong size");
  1261. for (int i = 0; i < 15; i += 1) {
  1262. FIRDocumentReference *documentToAdd = [db2
  1263. documentWithPath:[NSString stringWithFormat:@"%@/newDoc%@", collRef.path, @(1000 + i)]];
  1264. [batch setData:@{@"key" : @42, @"removed" : @NO} forDocument:documentToAdd];
  1265. [addedDocumentIds addObject:documentToAdd.documentID];
  1266. }
  1267. // Ensure the documentIds above are mutually exclusive.
  1268. NSMutableSet<NSString *> *mergedSet = [NSMutableSet setWithArray:addedDocumentIds];
  1269. [mergedSet unionSet:deletedDocumentIds];
  1270. [mergedSet unionSet:removedDocumentIds];
  1271. [mergedSet unionSet:updatedDocumentIds];
  1272. XCTAssertEqual(mergedSet.count, 30u, @"There are documents experienced multiple operations.");
  1273. [self commitWriteBatch:batch];
  1274. }
  1275. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1276. // existence filter rather than "delete" events when the query is resumed.
  1277. [NSThread sleepForTimeInterval:10.0f];
  1278. // Resume the query and save the resulting snapshot for verification. Use some internal testing
  1279. // hooks to "capture" the existence filter mismatches to verify that Watch sent a bloom
  1280. // filter, and it was used to avert a full requery.
  1281. __block FIRQuerySnapshot *querySnapshot2;
  1282. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1283. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1284. querySnapshot2 = [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  1285. }];
  1286. XCTAssertEqual(querySnapshot2.count, 25u, @"querySnapshot1.count has an unexpected value");
  1287. // Verify that the snapshot from the resumed query contains the expected documents; that is, 10
  1288. // existing documents that still match the query, and 15 documents that are newly added.
  1289. {
  1290. NSMutableArray<NSString *> *expectedDocumentIds = [[NSMutableArray alloc] init];
  1291. for (FIRDocumentReference *documentRef in createdDocuments) {
  1292. if (![deletedDocumentIds containsObject:documentRef.documentID] &&
  1293. ![removedDocumentIds containsObject:documentRef.documentID]) {
  1294. [expectedDocumentIds addObject:documentRef.documentID];
  1295. }
  1296. }
  1297. [expectedDocumentIds addObjectsFromArray:addedDocumentIds];
  1298. XCTAssertEqualObjects([NSSet setWithArray:FIRQuerySnapshotGetIDs(querySnapshot2)],
  1299. [NSSet setWithArray:expectedDocumentIds],
  1300. @"querySnapshot2 has the wrong documents");
  1301. }
  1302. // Verify that Watch sent an existence filter with the correct counts when the query was
  1303. // resumed.
  1304. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1305. @"Watch should have sent exactly 1 existence filter");
  1306. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1307. existenceFilterMismatches[0];
  1308. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, 35);
  1309. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, 25);
  1310. // Verify that Watch sent a valid bloom filter.
  1311. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1312. XCTAssertNotNil(bloomFilter,
  1313. "Watch should have included a bloom filter in the existence filter");
  1314. // Verify that the bloom filter was successfully used to avert a full requery. If a false
  1315. // positive occurred then retry the entire test. Although statistically rare, false positives
  1316. // are expected to happen occasionally. When a false positive _does_ happen, just retry the test
  1317. // with a different set of documents. If that retry _also_ experiences a false positive, then
  1318. // fail the test because that is so improbable that something must have gone wrong.
  1319. if (attemptNumber == 1 && !bloomFilter.applied) {
  1320. continue;
  1321. }
  1322. XCTAssertTrue(bloomFilter.applied,
  1323. @"The bloom filter should have been successfully applied with attemptNumber=%@",
  1324. @(attemptNumber));
  1325. // Break out of the test loop now that the test passes.
  1326. break;
  1327. }
  1328. }
  1329. - (void)testBloomFilterShouldCorrectlyEncodeComplexUnicodeCharacters {
  1330. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1331. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1332. // sends.
  1333. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1334. "Skip this test when running against the Firestore emulator because the emulator does "
  1335. "not include a bloom filter when it sends existence filter messages, making it "
  1336. "impossible for this test to verify the correctness of the bloom filter.");
  1337. // Set this test to stop when the first failure occurs because some test assertion failures make
  1338. // the rest of the test not applicable or will even crash.
  1339. [self setContinueAfterFailure:NO];
  1340. // Define a comparator that compares `NSString` objects in a way that orders canonically-
  1341. // equivalent, but distinct, strings in a consistent manner by using `NSForcedOrderingSearch`.
  1342. // Otherwise, the bare `[NSString compare:]` method considers canonically-equivalent, but
  1343. // distinct, strings as "equal" and orders them indeterminately.
  1344. NSComparator sortComparator = ^(NSString *string1, NSString *string2) {
  1345. return [string1 compare:string2 options:NSForcedOrderingSearch];
  1346. };
  1347. // Firestore does not do any Unicode normalization on the document IDs. Therefore, two document
  1348. // IDs that are canonically-equivalent (i.e. they visually appear identical) but are represented
  1349. // by a different sequence of Unicode code points are treated as distinct document IDs.
  1350. NSArray<NSString *> *testDocIds;
  1351. {
  1352. NSMutableArray<NSString *> *testDocIdsAccumulator = [[NSMutableArray alloc] init];
  1353. [testDocIdsAccumulator addObject:@"DocumentToDelete"];
  1354. // The next two strings both end with "e" with an accent: the first uses the dedicated Unicode
  1355. // code point for this character, while the second uses the standard lowercase "e" followed by
  1356. // the accent combining character.
  1357. [testDocIdsAccumulator addObject:@"LowercaseEWithAcuteAccent_\u00E9"];
  1358. [testDocIdsAccumulator addObject:@"LowercaseEWithAcuteAccent_\u0065\u0301"];
  1359. // The next two strings both end with an "e" with two different accents applied via the
  1360. // following two combining characters. The combining characters are specified in a different
  1361. // order and Firestore treats these document IDs as unique, despite the order of the combining
  1362. // characters being irrelevant.
  1363. [testDocIdsAccumulator addObject:@"LowercaseEWithMultipleAccents_\u0065\u0301\u0327"];
  1364. [testDocIdsAccumulator addObject:@"LowercaseEWithMultipleAccents_\u0065\u0327\u0301"];
  1365. // The next string contains a character outside the BMP (the "basic multilingual plane"); that
  1366. // is, its code point is greater than 0xFFFF. Since NSString stores text in sequences of 16-bit
  1367. // code units, using the UTF-16 encoding (according to
  1368. // https://www.objc.io/issues/9-strings/unicode) it is stored as a surrogate pair, two 16-bit
  1369. // code units U+D83D and U+DE00, to represent this character. Make sure that its presence is
  1370. // correctly tested in the bloom filter, which uses UTF-8 encoding.
  1371. [testDocIdsAccumulator addObject:@"Smiley_\U0001F600"];
  1372. testDocIds = [NSArray arrayWithArray:testDocIdsAccumulator];
  1373. }
  1374. // Verify assumptions about the equivalence of strings in `testDocIds`.
  1375. XCTAssertEqualObjects(testDocIds[1].decomposedStringWithCanonicalMapping,
  1376. testDocIds[2].decomposedStringWithCanonicalMapping);
  1377. XCTAssertEqualObjects(testDocIds[3].decomposedStringWithCanonicalMapping,
  1378. testDocIds[4].decomposedStringWithCanonicalMapping);
  1379. XCTAssertEqual([testDocIds[5] characterAtIndex:7], 0xD83D);
  1380. XCTAssertEqual([testDocIds[5] characterAtIndex:8], 0xDE00);
  1381. // Create the mapping from document ID to document data for the document IDs specified in
  1382. // `testDocIds`.
  1383. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1384. [[NSMutableDictionary alloc] init];
  1385. for (NSString *testDocId in testDocIds) {
  1386. [testDocs setValue:@{@"foo" : @42} forKey:testDocId];
  1387. }
  1388. // Create the documents whose names contain complex Unicode characters in a new collection.
  1389. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1390. // Run a query to populate the local cache with documents that have names with complex Unicode
  1391. // characters.
  1392. {
  1393. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:collRef
  1394. source:FIRFirestoreSourceDefault];
  1395. XCTAssertEqualObjects(
  1396. [FIRQuerySnapshotGetIDs(querySnapshot1) sortedArrayUsingComparator:sortComparator],
  1397. [testDocIds sortedArrayUsingComparator:sortComparator],
  1398. @"querySnapshot1 has the wrong documents");
  1399. }
  1400. // Delete one of the documents so that the next call to collection.get() will experience an
  1401. // existence filter mismatch. Use a different Firestore instance to avoid affecting the local
  1402. // cache.
  1403. FIRDocumentReference *documentToDelete = [collRef documentWithPath:@"DocumentToDelete"];
  1404. {
  1405. FIRFirestore *db2 = [self firestore];
  1406. [self deleteDocumentRef:[db2 documentWithPath:documentToDelete.path]];
  1407. }
  1408. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1409. // existence filter rather than "delete" events when the query is resumed.
  1410. [NSThread sleepForTimeInterval:10.0f];
  1411. // Resume the query and save the resulting snapshot for verification. Use some internal testing
  1412. // hooks to "capture" the existence filter mismatches.
  1413. __block FIRQuerySnapshot *querySnapshot2;
  1414. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1415. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1416. querySnapshot2 = [self readDocumentSetForRef:collRef source:FIRFirestoreSourceDefault];
  1417. }];
  1418. // Verify that the snapshot from the resumed query contains the expected documents; that is, that
  1419. // it contains the documents whose names contain complex Unicode characters and _not_ the document
  1420. // that was deleted.
  1421. {
  1422. NSMutableArray<NSString *> *querySnapshot2ExpectedDocumentIds =
  1423. [NSMutableArray arrayWithArray:testDocIds];
  1424. [querySnapshot2ExpectedDocumentIds removeObject:documentToDelete.documentID];
  1425. XCTAssertEqualObjects(
  1426. [FIRQuerySnapshotGetIDs(querySnapshot2) sortedArrayUsingComparator:sortComparator],
  1427. [querySnapshot2ExpectedDocumentIds sortedArrayUsingComparator:sortComparator],
  1428. @"querySnapshot2 has the wrong documents");
  1429. }
  1430. // Verify that Watch sent an existence filter with the correct counts.
  1431. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1432. @"Watch should have sent exactly 1 existence filter");
  1433. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1434. existenceFilterMismatches[0];
  1435. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, (int)testDocIds.count);
  1436. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, (int)testDocIds.count - 1);
  1437. // Verify that Watch sent a valid bloom filter.
  1438. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1439. XCTAssertNotNil(bloomFilter, "Watch should have included a bloom filter in the existence filter");
  1440. // The bloom filter application should statistically be successful almost every time; the _only_
  1441. // time when it would _not_ be successful is if there is a false positive when testing for
  1442. // 'DocumentToDelete' in the bloom filter. So verify that the bloom filter application is
  1443. // successful, unless there was a false positive.
  1444. BOOL isFalsePositive = [bloomFilter mightContain:documentToDelete];
  1445. XCTAssertEqual(bloomFilter.applied, !isFalsePositive);
  1446. // Verify that the bloom filter contains the document paths with complex Unicode characters.
  1447. for (FIRDocumentSnapshot *documentSnapshot in querySnapshot2.documents) {
  1448. XCTAssertTrue([bloomFilter mightContain:documentSnapshot.reference],
  1449. @"The bloom filter should contain %@", documentSnapshot.documentID);
  1450. }
  1451. }
  1452. @end