FIRQueryTests.mm 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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. - (void)testResumingAQueryShouldUseBloomFilterToAvoidFullRequery {
  905. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  906. // the emulator is improved to include a bloom filter in the existence filter messages that it
  907. // sends.
  908. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  909. "Skip this test when running against the Firestore emulator because the emulator does "
  910. "not include a bloom filter when it sends existence filter messages, making it "
  911. "impossible for this test to verify the correctness of the bloom filter.");
  912. // Set this test to stop when the first failure occurs because some test assertion failures make
  913. // the rest of the test not applicable or will even crash.
  914. [self setContinueAfterFailure:NO];
  915. // Prepare the names and contents of the 100 documents to create.
  916. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  917. [[NSMutableDictionary alloc] init];
  918. for (int i = 0; i < 100; i++) {
  919. [testDocs setValue:@{@"key" : @42} forKey:[NSString stringWithFormat:@"doc%@", @(1000 + i)]];
  920. }
  921. // Each iteration of the "while" loop below runs a single iteration of the test. The test will
  922. // be run multiple times only if a bloom filter false positive occurs.
  923. int attemptNumber = 0;
  924. while (true) {
  925. attemptNumber++;
  926. // Create 100 documents in a new collection.
  927. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  928. // Run a query to populate the local cache with the 100 documents and a resume token.
  929. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:collRef
  930. source:FIRFirestoreSourceDefault];
  931. XCTAssertEqual(querySnapshot1.count, 100, @"querySnapshot1.count has an unexpected value");
  932. NSArray<FIRDocumentReference *> *createdDocuments =
  933. FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
  934. // Delete 50 of the 100 documents. Use a different Firestore instance to avoid affecting the
  935. // local cache.
  936. NSSet<NSString *> *deletedDocumentIds;
  937. {
  938. FIRFirestore *db2 = [self firestore];
  939. FIRWriteBatch *batch = [db2 batch];
  940. NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  941. for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
  942. FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
  943. [batch deleteDocument:documentToDelete];
  944. [deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
  945. }
  946. [self commitWriteBatch:batch];
  947. deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
  948. }
  949. XCTAssertEqual(deletedDocumentIds.count, 50u, @"deletedDocumentIds has the wrong size");
  950. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  951. // existence filter rather than "delete" events when the query is resumed.
  952. [NSThread sleepForTimeInterval:10.0f];
  953. // Resume the query and save the resulting snapshot for verification.
  954. // Use some internal testing hooks to "capture" the existence filter mismatches to verify that
  955. // Watch sent a bloom filter, and it was used to avert a full requery.
  956. __block FIRQuerySnapshot *querySnapshot2;
  957. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  958. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  959. querySnapshot2 = [self readDocumentSetForRef:collRef source:FIRFirestoreSourceDefault];
  960. }];
  961. // Verify that the snapshot from the resumed query contains the expected documents; that is,
  962. // that it contains the 50 documents that were _not_ deleted.
  963. {
  964. NSMutableArray<NSString *> *expectedDocumentIds = [[NSMutableArray alloc] init];
  965. for (FIRDocumentReference *documentRef in createdDocuments) {
  966. if (![deletedDocumentIds containsObject:documentRef.documentID]) {
  967. [expectedDocumentIds addObject:documentRef.documentID];
  968. }
  969. }
  970. XCTAssertEqualObjects([NSSet setWithArray:FIRQuerySnapshotGetIDs(querySnapshot2)],
  971. [NSSet setWithArray:expectedDocumentIds],
  972. @"querySnapshot2 has the wrong documents");
  973. }
  974. // Verify that Watch sent an existence filter with the correct counts when the query was
  975. // resumed.
  976. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  977. @"Watch should have sent exactly 1 existence filter");
  978. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  979. existenceFilterMismatches[0];
  980. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, 100);
  981. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, 50);
  982. // Verify that Watch sent a valid bloom filter.
  983. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  984. XCTAssertNotNil(bloomFilter,
  985. "Watch should have included a bloom filter in the existence filter");
  986. XCTAssertGreaterThan(bloomFilter.hashCount, 0);
  987. XCTAssertGreaterThan(bloomFilter.bitmapLength, 0);
  988. XCTAssertGreaterThan(bloomFilter.padding, 0);
  989. XCTAssertLessThan(bloomFilter.padding, 8);
  990. // Verify that the bloom filter was successfully used to avert a full requery. If a false
  991. // positive occurred then retry the entire test. Although statistically rare, false positives
  992. // are expected to happen occasionally. When a false positive _does_ happen, just retry the test
  993. // with a different set of documents. If that retry _also_ experiences a false positive, then
  994. // fail the test because that is so improbable that something must have gone wrong.
  995. if (attemptNumber == 1 && !bloomFilter.applied) {
  996. continue;
  997. }
  998. XCTAssertTrue(bloomFilter.applied,
  999. @"The bloom filter should have been successfully applied with attemptNumber=%@",
  1000. @(attemptNumber));
  1001. // Break out of the test loop now that the test passes.
  1002. break;
  1003. }
  1004. }
  1005. - (void)
  1006. testBloomFilterShouldAvertAFullRequeryWhenDocumentsWereAddedDeletedRemovedUpdatedAndUnchangedSinceTheResumeToken {
  1007. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1008. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1009. // sends.
  1010. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1011. "Skip this test when running against the Firestore emulator because the emulator does "
  1012. "not include a bloom filter when it sends existence filter messages, making it "
  1013. "impossible for this test to verify the correctness of the bloom filter.");
  1014. // Set this test to stop when the first failure occurs because some test assertion failures make
  1015. // the rest of the test not applicable or will even crash.
  1016. [self setContinueAfterFailure:NO];
  1017. // Prepare the names and contents of the 20 documents to create.
  1018. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1019. [[NSMutableDictionary alloc] init];
  1020. for (int i = 0; i < 20; i++) {
  1021. [testDocs setValue:@{@"key" : @42, @"removed" : @NO}
  1022. forKey:[NSString stringWithFormat:@"doc%@", @(1000 + i)]];
  1023. }
  1024. // Each iteration of the "while" loop below runs a single iteration of the test. The test will
  1025. // be run multiple times only if a bloom filter false positive occurs.
  1026. int attemptNumber = 0;
  1027. while (true) {
  1028. attemptNumber++;
  1029. // Create 20 documents in a new collection.
  1030. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1031. FIRQuery *query = [collRef queryWhereField:@"removed" isEqualTo:@NO];
  1032. // Run a query to populate the local cache with the 20 documents and a resume token.
  1033. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:query
  1034. source:FIRFirestoreSourceDefault];
  1035. XCTAssertEqual(querySnapshot1.count, 20u, @"querySnapshot1.count has an unexpected value");
  1036. NSArray<FIRDocumentReference *> *createdDocuments =
  1037. FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
  1038. // Out of the 20 existing documents, leave 5 docs untouched, delete 5 docs, remove 5 docs,
  1039. // update 5 docs, and add 15 new docs.
  1040. NSSet<NSString *> *deletedDocumentIds;
  1041. NSSet<NSString *> *removedDocumentIds;
  1042. NSSet<NSString *> *updatedDocumentIds;
  1043. NSMutableArray<NSString *> *addedDocumentIds = [[NSMutableArray alloc] init];
  1044. {
  1045. FIRFirestore *db2 = [self firestore];
  1046. FIRWriteBatch *batch = [db2 batch];
  1047. NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1048. for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 4) {
  1049. FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
  1050. [batch deleteDocument:documentToDelete];
  1051. [deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
  1052. }
  1053. deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
  1054. XCTAssertEqual(deletedDocumentIds.count, 5u, @"deletedDocumentIds has the wrong size");
  1055. // Update 5 documents to no longer match the query.
  1056. NSMutableArray<NSString *> *removedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1057. for (decltype(createdDocuments.count) i = 1; i < createdDocuments.count; i += 4) {
  1058. FIRDocumentReference *documentToRemove = [db2 documentWithPath:createdDocuments[i].path];
  1059. [batch updateData:@{@"removed" : @YES} forDocument:documentToRemove];
  1060. [removedDocumentIdsAccumulator addObject:documentToRemove.documentID];
  1061. }
  1062. removedDocumentIds = [NSSet setWithArray:removedDocumentIdsAccumulator];
  1063. XCTAssertEqual(removedDocumentIds.count, 5u, @"removedDocumentIds has the wrong size");
  1064. // Update 5 documents, but ensure they still match the query.
  1065. NSMutableArray<NSString *> *updatedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
  1066. for (decltype(createdDocuments.count) i = 2; i < createdDocuments.count; i += 4) {
  1067. FIRDocumentReference *documentToUpdate = [db2 documentWithPath:createdDocuments[i].path];
  1068. [batch updateData:@{@"key" : @43} forDocument:documentToUpdate];
  1069. [updatedDocumentIdsAccumulator addObject:documentToUpdate.documentID];
  1070. }
  1071. updatedDocumentIds = [NSSet setWithArray:updatedDocumentIdsAccumulator];
  1072. XCTAssertEqual(updatedDocumentIds.count, 5u, @"updatedDocumentIds has the wrong size");
  1073. for (int i = 0; i < 15; i += 1) {
  1074. FIRDocumentReference *documentToAdd = [db2
  1075. documentWithPath:[NSString stringWithFormat:@"%@/newDoc%@", collRef.path, @(1000 + i)]];
  1076. [batch setData:@{@"key" : @42, @"removed" : @NO} forDocument:documentToAdd];
  1077. [addedDocumentIds addObject:documentToAdd.documentID];
  1078. }
  1079. // Ensure the documentIds above are mutually exclusive.
  1080. NSMutableSet<NSString *> *mergedSet = [NSMutableSet setWithArray:addedDocumentIds];
  1081. [mergedSet unionSet:deletedDocumentIds];
  1082. [mergedSet unionSet:removedDocumentIds];
  1083. [mergedSet unionSet:updatedDocumentIds];
  1084. XCTAssertEqual(mergedSet.count, 30u, @"There are documents experienced multiple operations.");
  1085. [self commitWriteBatch:batch];
  1086. }
  1087. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1088. // existence filter rather than "delete" events when the query is resumed.
  1089. [NSThread sleepForTimeInterval:10.0f];
  1090. // Resume the query and save the resulting snapshot for verification. Use some internal testing
  1091. // hooks to "capture" the existence filter mismatches to verify that Watch sent a bloom
  1092. // filter, and it was used to avert a full requery.
  1093. __block FIRQuerySnapshot *querySnapshot2;
  1094. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1095. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1096. querySnapshot2 = [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  1097. }];
  1098. XCTAssertEqual(querySnapshot2.count, 25u, @"querySnapshot1.count has an unexpected value");
  1099. // Verify that the snapshot from the resumed query contains the expected documents; that is, 10
  1100. // existing documents that still match the query, and 15 documents that are newly added.
  1101. {
  1102. NSMutableArray<NSString *> *expectedDocumentIds = [[NSMutableArray alloc] init];
  1103. for (FIRDocumentReference *documentRef in createdDocuments) {
  1104. if (![deletedDocumentIds containsObject:documentRef.documentID] &&
  1105. ![removedDocumentIds containsObject:documentRef.documentID]) {
  1106. [expectedDocumentIds addObject:documentRef.documentID];
  1107. }
  1108. }
  1109. [expectedDocumentIds addObjectsFromArray:addedDocumentIds];
  1110. XCTAssertEqualObjects([NSSet setWithArray:FIRQuerySnapshotGetIDs(querySnapshot2)],
  1111. [NSSet setWithArray:expectedDocumentIds],
  1112. @"querySnapshot2 has the wrong documents");
  1113. }
  1114. // Verify that Watch sent an existence filter with the correct counts when the query was
  1115. // resumed.
  1116. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1117. @"Watch should have sent exactly 1 existence filter");
  1118. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1119. existenceFilterMismatches[0];
  1120. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, 35);
  1121. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, 25);
  1122. // Verify that Watch sent a valid bloom filter.
  1123. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1124. XCTAssertNotNil(bloomFilter,
  1125. "Watch should have included a bloom filter in the existence filter");
  1126. // Verify that the bloom filter was successfully used to avert a full requery. If a false
  1127. // positive occurred then retry the entire test. Although statistically rare, false positives
  1128. // are expected to happen occasionally. When a false positive _does_ happen, just retry the test
  1129. // with a different set of documents. If that retry _also_ experiences a false positive, then
  1130. // fail the test because that is so improbable that something must have gone wrong.
  1131. if (attemptNumber == 1 && !bloomFilter.applied) {
  1132. continue;
  1133. }
  1134. XCTAssertTrue(bloomFilter.applied,
  1135. @"The bloom filter should have been successfully applied with attemptNumber=%@",
  1136. @(attemptNumber));
  1137. // Break out of the test loop now that the test passes.
  1138. break;
  1139. }
  1140. }
  1141. - (void)testBloomFilterShouldCorrectlyEncodeComplexUnicodeCharacters {
  1142. // TODO(b/291365820): Stop skipping this test when running against the Firestore emulator once
  1143. // the emulator is improved to include a bloom filter in the existence filter messages that it
  1144. // sends.
  1145. XCTSkipIf([FSTIntegrationTestCase isRunningAgainstEmulator],
  1146. "Skip this test when running against the Firestore emulator because the emulator does "
  1147. "not include a bloom filter when it sends existence filter messages, making it "
  1148. "impossible for this test to verify the correctness of the bloom filter.");
  1149. // Set this test to stop when the first failure occurs because some test assertion failures make
  1150. // the rest of the test not applicable or will even crash.
  1151. [self setContinueAfterFailure:NO];
  1152. // Define a comparator that compares `NSString` objects in a way that orders canonically-
  1153. // equivalent, but distinct, strings in a consistent manner by using `NSForcedOrderingSearch`.
  1154. // Otherwise, the bare `[NSString compare:]` method considers canonically-equivalent, but
  1155. // distinct, strings as "equal" and orders them indeterminately.
  1156. NSComparator sortComparator = ^(NSString *string1, NSString *string2) {
  1157. return [string1 compare:string2 options:NSForcedOrderingSearch];
  1158. };
  1159. // Firestore does not do any Unicode normalization on the document IDs. Therefore, two document
  1160. // IDs that are canonically-equivalent (i.e. they visually appear identical) but are represented
  1161. // by a different sequence of Unicode code points are treated as distinct document IDs.
  1162. NSArray<NSString *> *testDocIds;
  1163. {
  1164. NSMutableArray<NSString *> *testDocIdsAccumulator = [[NSMutableArray alloc] init];
  1165. [testDocIdsAccumulator addObject:@"DocumentToDelete"];
  1166. // The next two strings both end with "e" with an accent: the first uses the dedicated Unicode
  1167. // code point for this character, while the second uses the standard lowercase "e" followed by
  1168. // the accent combining character.
  1169. [testDocIdsAccumulator addObject:@"LowercaseEWithAcuteAccent_\u00E9"];
  1170. [testDocIdsAccumulator addObject:@"LowercaseEWithAcuteAccent_\u0065\u0301"];
  1171. // The next two strings both end with an "e" with two different accents applied via the
  1172. // following two combining characters. The combining characters are specified in a different
  1173. // order and Firestore treats these document IDs as unique, despite the order of the combining
  1174. // characters being irrelevant.
  1175. [testDocIdsAccumulator addObject:@"LowercaseEWithMultipleAccents_\u0065\u0301\u0327"];
  1176. [testDocIdsAccumulator addObject:@"LowercaseEWithMultipleAccents_\u0065\u0327\u0301"];
  1177. // The next string contains a character outside the BMP (the "basic multilingual plane"); that
  1178. // is, its code point is greater than 0xFFFF. Since NSString stores text in sequences of 16-bit
  1179. // code units, using the UTF-16 encoding (according to
  1180. // https://www.objc.io/issues/9-strings/unicode) it is stored as a surrogate pair, two 16-bit
  1181. // code units U+D83D and U+DE00, to represent this character. Make sure that its presence is
  1182. // correctly tested in the bloom filter, which uses UTF-8 encoding.
  1183. [testDocIdsAccumulator addObject:@"Smiley_\U0001F600"];
  1184. testDocIds = [NSArray arrayWithArray:testDocIdsAccumulator];
  1185. }
  1186. // Verify assumptions about the equivalence of strings in `testDocIds`.
  1187. XCTAssertEqualObjects(testDocIds[1].decomposedStringWithCanonicalMapping,
  1188. testDocIds[2].decomposedStringWithCanonicalMapping);
  1189. XCTAssertEqualObjects(testDocIds[3].decomposedStringWithCanonicalMapping,
  1190. testDocIds[4].decomposedStringWithCanonicalMapping);
  1191. XCTAssertEqual([testDocIds[5] characterAtIndex:7], 0xD83D);
  1192. XCTAssertEqual([testDocIds[5] characterAtIndex:8], 0xDE00);
  1193. // Create the mapping from document ID to document data for the document IDs specified in
  1194. // `testDocIds`.
  1195. NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *testDocs =
  1196. [[NSMutableDictionary alloc] init];
  1197. for (NSString *testDocId in testDocIds) {
  1198. [testDocs setValue:@{@"foo" : @42} forKey:testDocId];
  1199. }
  1200. // Create the documents whose names contain complex Unicode characters in a new collection.
  1201. FIRCollectionReference *collRef = [self collectionRefWithDocuments:testDocs];
  1202. // Run a query to populate the local cache with documents that have names with complex Unicode
  1203. // characters.
  1204. {
  1205. FIRQuerySnapshot *querySnapshot1 = [self readDocumentSetForRef:collRef
  1206. source:FIRFirestoreSourceDefault];
  1207. XCTAssertEqualObjects(
  1208. [FIRQuerySnapshotGetIDs(querySnapshot1) sortedArrayUsingComparator:sortComparator],
  1209. [testDocIds sortedArrayUsingComparator:sortComparator],
  1210. @"querySnapshot1 has the wrong documents");
  1211. }
  1212. // Delete one of the documents so that the next call to collection.get() will experience an
  1213. // existence filter mismatch. Use a different Firestore instance to avoid affecting the local
  1214. // cache.
  1215. FIRDocumentReference *documentToDelete = [collRef documentWithPath:@"DocumentToDelete"];
  1216. {
  1217. FIRFirestore *db2 = [self firestore];
  1218. [self deleteDocumentRef:[db2 documentWithPath:documentToDelete.path]];
  1219. }
  1220. // Wait for 10 seconds, during which Watch will stop tracking the query and will send an
  1221. // existence filter rather than "delete" events when the query is resumed.
  1222. [NSThread sleepForTimeInterval:10.0f];
  1223. // Resume the query and save the resulting snapshot for verification. Use some internal testing
  1224. // hooks to "capture" the existence filter mismatches.
  1225. __block FIRQuerySnapshot *querySnapshot2;
  1226. NSArray<FSTTestingHooksExistenceFilterMismatchInfo *> *existenceFilterMismatches =
  1227. [FSTTestingHooks captureExistenceFilterMismatchesDuringBlock:^{
  1228. querySnapshot2 = [self readDocumentSetForRef:collRef source:FIRFirestoreSourceDefault];
  1229. }];
  1230. // Verify that the snapshot from the resumed query contains the expected documents; that is, that
  1231. // it contains the documents whose names contain complex Unicode characters and _not_ the document
  1232. // that was deleted.
  1233. {
  1234. NSMutableArray<NSString *> *querySnapshot2ExpectedDocumentIds =
  1235. [NSMutableArray arrayWithArray:testDocIds];
  1236. [querySnapshot2ExpectedDocumentIds removeObject:documentToDelete.documentID];
  1237. XCTAssertEqualObjects(
  1238. [FIRQuerySnapshotGetIDs(querySnapshot2) sortedArrayUsingComparator:sortComparator],
  1239. [querySnapshot2ExpectedDocumentIds sortedArrayUsingComparator:sortComparator],
  1240. @"querySnapshot2 has the wrong documents");
  1241. }
  1242. // Verify that Watch sent an existence filter with the correct counts.
  1243. XCTAssertEqual(existenceFilterMismatches.count, 1u,
  1244. @"Watch should have sent exactly 1 existence filter");
  1245. FSTTestingHooksExistenceFilterMismatchInfo *existenceFilterMismatchInfo =
  1246. existenceFilterMismatches[0];
  1247. XCTAssertEqual(existenceFilterMismatchInfo.localCacheCount, (int)testDocIds.count);
  1248. XCTAssertEqual(existenceFilterMismatchInfo.existenceFilterCount, (int)testDocIds.count - 1);
  1249. // Verify that Watch sent a valid bloom filter.
  1250. FSTTestingHooksBloomFilter *bloomFilter = existenceFilterMismatchInfo.bloomFilter;
  1251. XCTAssertNotNil(bloomFilter, "Watch should have included a bloom filter in the existence filter");
  1252. // The bloom filter application should statistically be successful almost every time; the _only_
  1253. // time when it would _not_ be successful is if there is a false positive when testing for
  1254. // 'DocumentToDelete' in the bloom filter. So verify that the bloom filter application is
  1255. // successful, unless there was a false positive.
  1256. BOOL isFalsePositive = [bloomFilter mightContain:documentToDelete];
  1257. XCTAssertEqual(bloomFilter.applied, !isFalsePositive);
  1258. // Verify that the bloom filter contains the document paths with complex Unicode characters.
  1259. for (FIRDocumentSnapshot *documentSnapshot in querySnapshot2.documents) {
  1260. XCTAssertTrue([bloomFilter mightContain:documentSnapshot.reference],
  1261. @"The bloom filter should contain %@", documentSnapshot.documentID);
  1262. }
  1263. }
  1264. @end