FSTLocalStoreTests.mm 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 "Firestore/Source/Local/FSTLocalStore.h"
  17. #import <FirebaseFirestore/FIRTimestamp.h>
  18. #import <XCTest/XCTest.h>
  19. #include <utility>
  20. #include <vector>
  21. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  22. #import "Firestore/Source/Local/FSTPersistence.h"
  23. #import "Firestore/Source/Local/FSTQueryData.h"
  24. #import "Firestore/Source/Model/FSTDocument.h"
  25. #import "Firestore/Source/Model/FSTMutation.h"
  26. #import "Firestore/Source/Model/FSTMutationBatch.h"
  27. #import "Firestore/Source/Util/FSTClasses.h"
  28. #import "Firestore/Example/Tests/Local/FSTLocalStoreTests.h"
  29. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  30. #import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedDictionary+Testing.h"
  31. #import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"
  32. #include "Firestore/core/include/firebase/firestore/timestamp.h"
  33. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  34. #include "Firestore/core/src/firebase/firestore/local/local_view_changes.h"
  35. #include "Firestore/core/src/firebase/firestore/local/local_write_result.h"
  36. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  37. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  38. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  39. #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
  40. #include "Firestore/core/src/firebase/firestore/util/status.h"
  41. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  42. namespace testutil = firebase::firestore::testutil;
  43. using firebase::Timestamp;
  44. using firebase::firestore::auth::User;
  45. using firebase::firestore::local::LocalViewChanges;
  46. using firebase::firestore::local::LocalWriteResult;
  47. using firebase::firestore::model::DocumentKey;
  48. using firebase::firestore::model::DocumentKeySet;
  49. using firebase::firestore::model::DocumentState;
  50. using firebase::firestore::model::FieldValue;
  51. using firebase::firestore::model::ListenSequenceNumber;
  52. using firebase::firestore::model::DocumentMap;
  53. using firebase::firestore::model::MaybeDocumentMap;
  54. using firebase::firestore::model::SnapshotVersion;
  55. using firebase::firestore::model::TargetId;
  56. using firebase::firestore::remote::RemoteEvent;
  57. using firebase::firestore::remote::TestTargetMetadataProvider;
  58. using firebase::firestore::remote::WatchChangeAggregator;
  59. using firebase::firestore::remote::WatchTargetChange;
  60. using firebase::firestore::remote::WatchTargetChangeState;
  61. using firebase::firestore::util::Status;
  62. using testutil::Query;
  63. static NSArray<FSTDocument *> *docMapToArray(const DocumentMap &docs) {
  64. NSMutableArray<FSTDocument *> *result = [NSMutableArray array];
  65. for (const auto &kv : docs.underlying_map()) {
  66. [result addObject:static_cast<FSTDocument *>(kv.second)];
  67. }
  68. return result;
  69. }
  70. NS_ASSUME_NONNULL_BEGIN
  71. @interface FSTLocalStoreTests ()
  72. @property(nonatomic, strong, readwrite) id<FSTPersistence> localStorePersistence;
  73. @property(nonatomic, strong, readwrite) FSTLocalStore *localStore;
  74. @property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *batches;
  75. @property(nonatomic, assign, readwrite) TargetId lastTargetID;
  76. @end
  77. @implementation FSTLocalStoreTests {
  78. MaybeDocumentMap _lastChanges;
  79. }
  80. - (void)setUp {
  81. [super setUp];
  82. if ([self isTestBaseClass]) {
  83. return;
  84. }
  85. id<FSTPersistence> persistence = [self persistence];
  86. self.localStorePersistence = persistence;
  87. self.localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
  88. initialUser:User::Unauthenticated()];
  89. [self.localStore start];
  90. _batches = [NSMutableArray array];
  91. _lastTargetID = 0;
  92. }
  93. - (void)tearDown {
  94. [self.localStorePersistence shutdown];
  95. [super tearDown];
  96. }
  97. - (id<FSTPersistence>)persistence {
  98. @throw FSTAbstractMethodException(); // NOLINT
  99. }
  100. - (BOOL)gcIsEager {
  101. @throw FSTAbstractMethodException(); // NOLINT
  102. }
  103. /**
  104. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  105. * FSTLocalStoreTests since it is incomplete without the implementations supplied by its
  106. * subclasses.
  107. */
  108. - (BOOL)isTestBaseClass {
  109. return [self class] == [FSTLocalStoreTests class];
  110. }
  111. - (void)writeMutation:(FSTMutation *)mutation {
  112. [self writeMutations:{mutation}];
  113. }
  114. - (void)writeMutations:(std::vector<FSTMutation *> &&)mutations {
  115. auto mutationsCopy = mutations;
  116. LocalWriteResult result = [self.localStore locallyWriteMutations:std::move(mutationsCopy)];
  117. [self.batches addObject:[[FSTMutationBatch alloc] initWithBatchID:result.batch_id()
  118. localWriteTime:Timestamp::Now()
  119. baseMutations:{}
  120. mutations:std::move(mutations)]];
  121. _lastChanges = result.changes();
  122. }
  123. - (void)applyRemoteEvent:(const RemoteEvent &)event {
  124. _lastChanges = [self.localStore applyRemoteEvent:event];
  125. }
  126. - (void)notifyLocalViewChanges:(LocalViewChanges)changes {
  127. [self.localStore notifyLocalViewChanges:std::vector<LocalViewChanges>{std::move(changes)}];
  128. }
  129. - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion
  130. transformResult:(id _Nullable)transformResult {
  131. FSTMutationBatch *batch = [self.batches firstObject];
  132. [self.batches removeObjectAtIndex:0];
  133. XCTAssertEqual(batch.mutations.size(), 1, @"Acknowledging more than one mutation not supported.");
  134. SnapshotVersion version = testutil::Version(documentVersion);
  135. absl::optional<std::vector<FieldValue>> mutationTransformResult;
  136. if (transformResult) {
  137. mutationTransformResult = std::vector<FieldValue>{FSTTestFieldValue(transformResult)};
  138. }
  139. FSTMutationResult *mutationResult =
  140. [[FSTMutationResult alloc] initWithVersion:version transformResults:mutationTransformResult];
  141. FSTMutationBatchResult *result = [FSTMutationBatchResult resultWithBatch:batch
  142. commitVersion:version
  143. mutationResults:{mutationResult}
  144. streamToken:nil];
  145. _lastChanges = [self.localStore acknowledgeBatchWithResult:result];
  146. }
  147. - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
  148. [self acknowledgeMutationWithVersion:documentVersion transformResult:nil];
  149. }
  150. - (void)rejectMutation {
  151. FSTMutationBatch *batch = [self.batches firstObject];
  152. [self.batches removeObjectAtIndex:0];
  153. _lastChanges = [self.localStore rejectBatchID:batch.batchID];
  154. }
  155. - (TargetId)allocateQuery:(core::Query)query {
  156. FSTQueryData *queryData = [self.localStore allocateQuery:std::move(query)];
  157. self.lastTargetID = queryData.targetID;
  158. return queryData.targetID;
  159. }
  160. /** Asserts that the last target ID is the given number. */
  161. #define FSTAssertTargetID(targetID) \
  162. do { \
  163. XCTAssertEqual(self.lastTargetID, targetID); \
  164. } while (0)
  165. /** Asserts that a the lastChanges contain the docs in the given array. */
  166. #define FSTAssertChanged(documents) \
  167. do { \
  168. NSArray<FSTMaybeDocument *> *expected = (documents); \
  169. XCTAssertEqual(_lastChanges.size(), expected.count); \
  170. NSEnumerator<FSTMaybeDocument *> *enumerator = expected.objectEnumerator; \
  171. for (const auto &kv : _lastChanges) { \
  172. FSTMaybeDocument *value = kv.second; \
  173. XCTAssertEqualObjects(value, [enumerator nextObject]); \
  174. } \
  175. _lastChanges = MaybeDocumentMap{}; \
  176. } while (0)
  177. /** Asserts that the given keys were removed. */
  178. #define FSTAssertRemoved(keyPaths) \
  179. do { \
  180. XCTAssertEqual(_lastChanges.size(), keyPaths.count); \
  181. NSEnumerator<NSString *> *keyPathEnumerator = keyPaths.objectEnumerator; \
  182. for (const auto &kv : _lastChanges) { \
  183. const DocumentKey &actualKey = kv.first; \
  184. FSTMaybeDocument *value = kv.second; \
  185. DocumentKey expectedKey = FSTTestDocKey([keyPathEnumerator nextObject]); \
  186. XCTAssertEqual(actualKey, expectedKey); \
  187. XCTAssertTrue([value isKindOfClass:[FSTDeletedDocument class]]); \
  188. } \
  189. _lastChanges = MaybeDocumentMap{}; \
  190. } while (0)
  191. /** Asserts that the given local store contains the given document. */
  192. #define FSTAssertContains(document) \
  193. do { \
  194. FSTMaybeDocument *expected = (document); \
  195. FSTMaybeDocument *actual = [self.localStore readDocument:expected.key]; \
  196. XCTAssertEqualObjects(actual, expected); \
  197. } while (0)
  198. /** Asserts that the given local store does not contain the given document. */
  199. #define FSTAssertNotContains(keyPathString) \
  200. do { \
  201. DocumentKey key = FSTTestDocKey(keyPathString); \
  202. FSTMaybeDocument *actual = [self.localStore readDocument:key]; \
  203. XCTAssertNil(actual); \
  204. } while (0)
  205. - (void)testMutationBatchKeys {
  206. if ([self isTestBaseClass]) return;
  207. FSTMutation *base = FSTTestSetMutation(@"foo/ignore", @{@"foo" : @"bar"});
  208. FSTMutation *set1 = FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"});
  209. FSTMutation *set2 = FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"});
  210. FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:1
  211. localWriteTime:Timestamp::Now()
  212. baseMutations:{base}
  213. mutations:{set1, set2}];
  214. DocumentKeySet keys = [batch keys];
  215. XCTAssertEqual(keys.size(), 2u);
  216. }
  217. - (void)testHandlesSetMutation {
  218. if ([self isTestBaseClass]) return;
  219. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  220. FSTAssertChanged(
  221. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  222. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  223. [self acknowledgeMutationWithVersion:0];
  224. FSTAssertChanged(
  225. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations) ]);
  226. if ([self gcIsEager]) {
  227. // Nothing is pinning this anymore, as it has been acknowledged and there are no targets active.
  228. FSTAssertNotContains(@"foo/bar");
  229. } else {
  230. FSTAssertContains(
  231. FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations));
  232. }
  233. }
  234. - (void)testHandlesSetMutationThenDocument {
  235. if ([self isTestBaseClass]) return;
  236. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  237. FSTAssertChanged(
  238. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  239. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  240. TargetId targetID = [self allocateQuery:Query("foo")];
  241. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"},
  242. DocumentState::kSynced),
  243. {targetID}, {})];
  244. FSTAssertChanged(
  245. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  246. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  247. }
  248. - (void)testHandlesAckThenRejectThenRemoteEvent {
  249. if ([self isTestBaseClass]) return;
  250. // Start a query that requires acks to be held.
  251. core::Query query = Query("foo");
  252. TargetId targetID = [self allocateQuery:query];
  253. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  254. FSTAssertChanged(
  255. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  256. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  257. // The last seen version is zero, so this ack must be held.
  258. [self acknowledgeMutationWithVersion:1];
  259. FSTAssertChanged(
  260. @[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations) ]);
  261. // Under eager GC, there is no longer a reference for the document, and it should be
  262. // deleted.
  263. if ([self gcIsEager]) {
  264. FSTAssertNotContains(@"foo/bar");
  265. } else {
  266. FSTAssertContains(
  267. FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations));
  268. }
  269. [self writeMutation:FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"})];
  270. FSTAssertChanged(
  271. @[ FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, DocumentState::kLocalMutations) ]);
  272. FSTAssertContains(FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, DocumentState::kLocalMutations));
  273. [self rejectMutation];
  274. FSTAssertRemoved(@[ @"bar/baz" ]);
  275. FSTAssertNotContains(@"bar/baz");
  276. [self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"},
  277. DocumentState::kSynced),
  278. {targetID})];
  279. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"}, DocumentState::kSynced) ]);
  280. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"}, DocumentState::kSynced));
  281. FSTAssertNotContains(@"bar/baz");
  282. }
  283. - (void)testHandlesDeletedDocumentThenSetMutationThenAck {
  284. if ([self isTestBaseClass]) return;
  285. core::Query query = Query("foo");
  286. TargetId targetID = [self allocateQuery:query];
  287. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
  288. {})];
  289. FSTAssertRemoved(@[ @"foo/bar" ]);
  290. // Under eager GC, there is no longer a reference for the document, and it should be
  291. // deleted.
  292. if (![self gcIsEager]) {
  293. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, NO));
  294. } else {
  295. FSTAssertNotContains(@"foo/bar");
  296. }
  297. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  298. FSTAssertChanged(
  299. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  300. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  301. // Can now remove the target, since we have a mutation pinning the document
  302. [self.localStore releaseQuery:query];
  303. // Verify we didn't lose anything
  304. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  305. [self acknowledgeMutationWithVersion:3];
  306. FSTAssertChanged(
  307. @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations) ]);
  308. // It has been acknowledged, and should no longer be retained as there is no target and mutation
  309. if ([self gcIsEager]) {
  310. FSTAssertNotContains(@"foo/bar");
  311. }
  312. }
  313. - (void)testHandlesSetMutationThenDeletedDocument {
  314. if ([self isTestBaseClass]) return;
  315. core::Query query = Query("foo");
  316. TargetId targetID = [self allocateQuery:query];
  317. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  318. FSTAssertChanged(
  319. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  320. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
  321. {})];
  322. FSTAssertChanged(
  323. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  324. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  325. }
  326. - (void)testHandlesDocumentThenSetMutationThenAckThenDocument {
  327. if ([self isTestBaseClass]) return;
  328. // Start a query that requires acks to be held.
  329. core::Query query = Query("foo");
  330. TargetId targetID = [self allocateQuery:query];
  331. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  332. FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, DocumentState::kSynced),
  333. {targetID})];
  334. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, DocumentState::kSynced) ]);
  335. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, DocumentState::kSynced));
  336. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  337. FSTAssertChanged(
  338. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  339. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  340. [self acknowledgeMutationWithVersion:3];
  341. // we haven't seen the remote event yet, so the write is still held.
  342. FSTAssertChanged(
  343. @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations) ]);
  344. FSTAssertContains(
  345. FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations));
  346. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"},
  347. DocumentState::kSynced),
  348. {targetID}, {})];
  349. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, DocumentState::kSynced) ]);
  350. FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, DocumentState::kSynced));
  351. }
  352. - (void)testHandlesPatchWithoutPriorDocument {
  353. if ([self isTestBaseClass]) return;
  354. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  355. FSTAssertRemoved(@[ @"foo/bar" ]);
  356. FSTAssertNotContains(@"foo/bar");
  357. [self acknowledgeMutationWithVersion:1];
  358. FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 1) ]);
  359. if ([self gcIsEager]) {
  360. FSTAssertNotContains(@"foo/bar");
  361. } else {
  362. FSTAssertContains(FSTTestUnknownDoc("foo/bar", 1));
  363. }
  364. }
  365. - (void)testHandlesPatchMutationThenDocumentThenAck {
  366. if ([self isTestBaseClass]) return;
  367. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  368. FSTAssertRemoved(@[ @"foo/bar" ]);
  369. FSTAssertNotContains(@"foo/bar");
  370. core::Query query = Query("foo");
  371. TargetId targetID = [self allocateQuery:query];
  372. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  373. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced),
  374. {targetID})];
  375. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar", @"it" : @"base"},
  376. DocumentState::kLocalMutations) ]);
  377. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar", @"it" : @"base"},
  378. DocumentState::kLocalMutations));
  379. [self acknowledgeMutationWithVersion:2];
  380. // We still haven't seen the remote events for the patch, so the local changes remain, and there
  381. // are no changes
  382. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
  383. DocumentState::kCommittedMutations) ]);
  384. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
  385. DocumentState::kCommittedMutations));
  386. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  387. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
  388. DocumentState::kSynced),
  389. {targetID}, {})];
  390. FSTAssertChanged(
  391. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"}, DocumentState::kSynced) ]);
  392. FSTAssertContains(
  393. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"}, DocumentState::kSynced));
  394. }
  395. - (void)testHandlesPatchMutationThenAckThenDocument {
  396. if ([self isTestBaseClass]) return;
  397. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  398. FSTAssertRemoved(@[ @"foo/bar" ]);
  399. FSTAssertNotContains(@"foo/bar");
  400. [self acknowledgeMutationWithVersion:1];
  401. FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 1) ]);
  402. // There's no target pinning the doc, and we've ack'd the mutation.
  403. if ([self gcIsEager]) {
  404. FSTAssertNotContains(@"foo/bar");
  405. } else {
  406. FSTAssertContains(FSTTestUnknownDoc("foo/bar", 1));
  407. }
  408. core::Query query = Query("foo");
  409. TargetId targetID = [self allocateQuery:query];
  410. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  411. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced),
  412. {targetID}, {})];
  413. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced) ]);
  414. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced));
  415. }
  416. - (void)testHandlesDeleteMutationThenAck {
  417. if ([self isTestBaseClass]) return;
  418. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  419. FSTAssertRemoved(@[ @"foo/bar" ]);
  420. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  421. [self acknowledgeMutationWithVersion:1];
  422. FSTAssertRemoved(@[ @"foo/bar" ]);
  423. // There's no target pinning the doc, and we've ack'd the mutation.
  424. if ([self gcIsEager]) {
  425. FSTAssertNotContains(@"foo/bar");
  426. }
  427. }
  428. - (void)testHandlesDocumentThenDeleteMutationThenAck {
  429. if ([self isTestBaseClass]) return;
  430. core::Query query = Query("foo");
  431. TargetId targetID = [self allocateQuery:query];
  432. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  433. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced),
  434. {targetID}, {})];
  435. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced) ]);
  436. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced));
  437. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  438. FSTAssertRemoved(@[ @"foo/bar" ]);
  439. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  440. // Remove the target so only the mutation is pinning the document
  441. [self.localStore releaseQuery:query];
  442. [self acknowledgeMutationWithVersion:2];
  443. FSTAssertRemoved(@[ @"foo/bar" ]);
  444. if ([self gcIsEager]) {
  445. // Neither the target nor the mutation pin the document, it should be gone.
  446. FSTAssertNotContains(@"foo/bar");
  447. }
  448. }
  449. - (void)testHandlesDeleteMutationThenDocumentThenAck {
  450. if ([self isTestBaseClass]) return;
  451. core::Query query = Query("foo");
  452. TargetId targetID = [self allocateQuery:query];
  453. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  454. FSTAssertRemoved(@[ @"foo/bar" ]);
  455. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  456. // Add the document to a target so it will remain in persistence even when ack'd
  457. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  458. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced),
  459. {targetID}, {})];
  460. FSTAssertRemoved(@[ @"foo/bar" ]);
  461. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  462. // Don't need to keep it pinned anymore
  463. [self.localStore releaseQuery:query];
  464. [self acknowledgeMutationWithVersion:2];
  465. FSTAssertRemoved(@[ @"foo/bar" ]);
  466. if ([self gcIsEager]) {
  467. // The doc is not pinned in a target and we've acknowledged the mutation. It shouldn't exist
  468. // anymore.
  469. FSTAssertNotContains(@"foo/bar");
  470. }
  471. }
  472. - (void)testHandlesDocumentThenDeletedDocumentThenDocument {
  473. if ([self isTestBaseClass]) return;
  474. core::Query query = Query("foo");
  475. TargetId targetID = [self allocateQuery:query];
  476. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  477. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced),
  478. {targetID}, {})];
  479. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced) ]);
  480. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced));
  481. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
  482. {})];
  483. FSTAssertRemoved(@[ @"foo/bar" ]);
  484. if (![self gcIsEager]) {
  485. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, NO));
  486. }
  487. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"},
  488. DocumentState::kSynced),
  489. {targetID}, {})];
  490. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, DocumentState::kSynced) ]);
  491. FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, DocumentState::kSynced));
  492. }
  493. - (void)testHandlesSetMutationThenPatchMutationThenDocumentThenAckThenAck {
  494. if ([self isTestBaseClass]) return;
  495. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
  496. FSTAssertChanged(
  497. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, DocumentState::kLocalMutations) ]);
  498. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, DocumentState::kLocalMutations));
  499. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  500. FSTAssertChanged(
  501. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  502. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  503. core::Query query = Query("foo");
  504. TargetId targetID = [self allocateQuery:query];
  505. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  506. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, DocumentState::kSynced),
  507. {targetID}, {})];
  508. FSTAssertChanged(
  509. @[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  510. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  511. [self.localStore releaseQuery:query];
  512. [self acknowledgeMutationWithVersion:2]; // delete mutation
  513. FSTAssertChanged(
  514. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  515. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  516. [self acknowledgeMutationWithVersion:3]; // patch mutation
  517. FSTAssertChanged(
  518. @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations) ]);
  519. if ([self gcIsEager]) {
  520. // we've ack'd all of the mutations, nothing is keeping this pinned anymore
  521. FSTAssertNotContains(@"foo/bar");
  522. } else {
  523. FSTAssertContains(
  524. FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, DocumentState::kCommittedMutations));
  525. }
  526. }
  527. - (void)testHandlesSetMutationAndPatchMutationTogether {
  528. if ([self isTestBaseClass]) return;
  529. [self writeMutations:{
  530. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
  531. FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
  532. }];
  533. FSTAssertChanged(
  534. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations) ]);
  535. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  536. }
  537. - (void)testHandlesSetMutationThenPatchMutationThenReject {
  538. if ([self isTestBaseClass]) return;
  539. if (![self gcIsEager]) return;
  540. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
  541. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, DocumentState::kLocalMutations));
  542. [self acknowledgeMutationWithVersion:1];
  543. FSTAssertNotContains(@"foo/bar");
  544. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  545. // A blind patch is not visible in the cache
  546. FSTAssertNotContains(@"foo/bar");
  547. [self rejectMutation];
  548. FSTAssertNotContains(@"foo/bar");
  549. }
  550. - (void)testHandlesSetMutationsAndPatchMutationOfJustOneTogether {
  551. if ([self isTestBaseClass]) return;
  552. [self writeMutations:{
  553. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
  554. FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"}),
  555. FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
  556. }];
  557. FSTAssertChanged((@[
  558. FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, DocumentState::kLocalMutations),
  559. FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations)
  560. ]));
  561. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  562. FSTAssertContains(FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, DocumentState::kLocalMutations));
  563. }
  564. - (void)testHandlesDeleteMutationThenPatchMutationThenAckThenAck {
  565. if ([self isTestBaseClass]) return;
  566. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  567. FSTAssertRemoved(@[ @"foo/bar" ]);
  568. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  569. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  570. FSTAssertRemoved(@[ @"foo/bar" ]);
  571. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  572. [self acknowledgeMutationWithVersion:2]; // delete mutation
  573. FSTAssertRemoved(@[ @"foo/bar" ]);
  574. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, YES));
  575. [self acknowledgeMutationWithVersion:3]; // patch mutation
  576. FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 3) ]);
  577. if ([self gcIsEager]) {
  578. // There are no more pending mutations, the doc has been dropped
  579. FSTAssertNotContains(@"foo/bar");
  580. } else {
  581. FSTAssertContains(FSTTestUnknownDoc("foo/bar", 3));
  582. }
  583. }
  584. - (void)testCollectsGarbageAfterChangeBatchWithNoTargetIDs {
  585. if ([self isTestBaseClass]) return;
  586. if (![self gcIsEager]) return;
  587. [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
  588. FSTTestDeletedDoc("foo/bar", 2, NO), {}, {}, {1})];
  589. FSTAssertNotContains(@"foo/bar");
  590. [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
  591. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kSynced),
  592. {}, {}, {1})];
  593. FSTAssertNotContains(@"foo/bar");
  594. }
  595. - (void)testCollectsGarbageAfterChangeBatch {
  596. if ([self isTestBaseClass]) return;
  597. if (![self gcIsEager]) return;
  598. core::Query query = Query("foo");
  599. TargetId targetID = [self allocateQuery:query];
  600. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  601. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kSynced),
  602. {targetID})];
  603. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, DocumentState::kSynced));
  604. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  605. FSTTestDoc("foo/bar", 2, @{@"foo" : @"baz"}, DocumentState::kSynced),
  606. {}, {targetID})];
  607. FSTAssertNotContains(@"foo/bar");
  608. }
  609. - (void)testCollectsGarbageAfterAcknowledgedMutation {
  610. if ([self isTestBaseClass]) return;
  611. if (![self gcIsEager]) return;
  612. core::Query query = Query("foo");
  613. TargetId targetID = [self allocateQuery:query];
  614. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  615. FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, DocumentState::kSynced),
  616. {targetID}, {})];
  617. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  618. // Release the query so that our target count goes back to 0 and we are considered up-to-date.
  619. [self.localStore releaseQuery:query];
  620. [self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
  621. [self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
  622. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  623. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, DocumentState::kLocalMutations));
  624. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  625. [self acknowledgeMutationWithVersion:3];
  626. FSTAssertNotContains(@"foo/bar");
  627. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, DocumentState::kLocalMutations));
  628. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  629. [self acknowledgeMutationWithVersion:4];
  630. FSTAssertNotContains(@"foo/bar");
  631. FSTAssertNotContains(@"foo/bah");
  632. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  633. [self acknowledgeMutationWithVersion:5];
  634. FSTAssertNotContains(@"foo/bar");
  635. FSTAssertNotContains(@"foo/bah");
  636. FSTAssertNotContains(@"foo/baz");
  637. }
  638. - (void)testCollectsGarbageAfterRejectedMutation {
  639. if ([self isTestBaseClass]) return;
  640. if (![self gcIsEager]) return;
  641. core::Query query = Query("foo");
  642. TargetId targetID = [self allocateQuery:query];
  643. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  644. FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, DocumentState::kSynced),
  645. {targetID}, {})];
  646. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  647. // Release the query so that our target count goes back to 0 and we are considered up-to-date.
  648. [self.localStore releaseQuery:query];
  649. [self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
  650. [self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
  651. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations));
  652. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, DocumentState::kLocalMutations));
  653. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  654. [self rejectMutation]; // patch mutation
  655. FSTAssertNotContains(@"foo/bar");
  656. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, DocumentState::kLocalMutations));
  657. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  658. [self rejectMutation]; // set mutation
  659. FSTAssertNotContains(@"foo/bar");
  660. FSTAssertNotContains(@"foo/bah");
  661. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  662. [self rejectMutation]; // delete mutation
  663. FSTAssertNotContains(@"foo/bar");
  664. FSTAssertNotContains(@"foo/bah");
  665. FSTAssertNotContains(@"foo/baz");
  666. }
  667. - (void)testPinsDocumentsInTheLocalView {
  668. if ([self isTestBaseClass]) return;
  669. if (![self gcIsEager]) return;
  670. core::Query query = Query("foo");
  671. TargetId targetID = [self allocateQuery:query];
  672. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  673. FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kSynced),
  674. {targetID})];
  675. [self writeMutation:FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"})];
  676. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kSynced));
  677. FSTAssertContains(FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, DocumentState::kLocalMutations));
  678. [self notifyLocalViewChanges:TestViewChanges(targetID, @[ @"foo/bar", @"foo/baz" ], @[])];
  679. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kSynced));
  680. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  681. FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kSynced),
  682. {}, {targetID})];
  683. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  684. FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, DocumentState::kSynced),
  685. {targetID}, {})];
  686. FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, DocumentState::kLocalMutations));
  687. [self acknowledgeMutationWithVersion:2];
  688. FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, DocumentState::kSynced));
  689. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, DocumentState::kSynced));
  690. FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, DocumentState::kSynced));
  691. [self notifyLocalViewChanges:TestViewChanges(targetID, @[], @[ @"foo/bar", @"foo/baz" ])];
  692. [self.localStore releaseQuery:query];
  693. FSTAssertNotContains(@"foo/bar");
  694. FSTAssertNotContains(@"foo/baz");
  695. }
  696. - (void)testThrowsAwayDocumentsWithUnknownTargetIDsImmediately {
  697. if ([self isTestBaseClass]) return;
  698. if (![self gcIsEager]) return;
  699. TargetId targetID = 321;
  700. [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
  701. FSTTestDoc("foo/bar", 1, @{}, DocumentState::kSynced), {}, {},
  702. {targetID})];
  703. FSTAssertNotContains(@"foo/bar");
  704. }
  705. - (void)testCanExecuteDocumentQueries {
  706. if ([self isTestBaseClass]) return;
  707. [self.localStore locallyWriteMutations:{
  708. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
  709. FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
  710. FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
  711. }];
  712. core::Query query = Query("foo/bar");
  713. DocumentMap docs = [self.localStore executeQuery:query];
  714. XCTAssertEqualObjects(docMapToArray(docs), @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"},
  715. DocumentState::kLocalMutations) ]);
  716. }
  717. - (void)testCanExecuteCollectionQueries {
  718. if ([self isTestBaseClass]) return;
  719. [self.localStore locallyWriteMutations:{
  720. FSTTestSetMutation(@"fo/bar", @{@"fo" : @"bar"}),
  721. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
  722. FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
  723. FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"}),
  724. FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
  725. }];
  726. core::Query query = Query("foo");
  727. DocumentMap docs = [self.localStore executeQuery:query];
  728. XCTAssertEqualObjects(
  729. docMapToArray(docs), (@[
  730. FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, DocumentState::kLocalMutations),
  731. FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, DocumentState::kLocalMutations)
  732. ]));
  733. }
  734. - (void)testCanExecuteMixedCollectionQueries {
  735. if ([self isTestBaseClass]) return;
  736. core::Query query = Query("foo");
  737. [self allocateQuery:query];
  738. FSTAssertTargetID(2);
  739. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  740. FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, DocumentState::kSynced), {2},
  741. {})];
  742. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  743. FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, DocumentState::kSynced), {2},
  744. {})];
  745. [self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
  746. DocumentMap docs = [self.localStore executeQuery:query];
  747. XCTAssertEqualObjects(docMapToArray(docs), (@[
  748. FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, DocumentState::kSynced),
  749. FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, DocumentState::kSynced),
  750. FSTTestDoc("foo/bonk", 0, @{@"a" : @"b"}, DocumentState::kLocalMutations)
  751. ]));
  752. }
  753. - (void)testPersistsResumeTokens {
  754. if ([self isTestBaseClass]) return;
  755. // This test only works in the absence of the FSTEagerGarbageCollector.
  756. if ([self gcIsEager]) return;
  757. core::Query query = Query("foo/bar");
  758. FSTQueryData *queryData = [self.localStore allocateQuery:query];
  759. ListenSequenceNumber initialSequenceNumber = queryData.sequenceNumber;
  760. TargetId targetID = queryData.targetID;
  761. NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(1000);
  762. WatchTargetChange watchChange{WatchTargetChangeState::Current, {targetID}, resumeToken};
  763. auto metadataProvider = TestTargetMetadataProvider::CreateSingleResultProvider(
  764. testutil::Key("foo/bar"), std::vector<TargetId>{targetID});
  765. WatchChangeAggregator aggregator{&metadataProvider};
  766. aggregator.HandleTargetChange(watchChange);
  767. RemoteEvent remoteEvent = aggregator.CreateRemoteEvent(testutil::Version(1000));
  768. [self applyRemoteEvent:remoteEvent];
  769. // Stop listening so that the query should become inactive (but persistent)
  770. [self.localStore releaseQuery:query];
  771. // Should come back with the same resume token
  772. FSTQueryData *queryData2 = [self.localStore allocateQuery:query];
  773. XCTAssertEqualObjects(queryData2.resumeToken, resumeToken);
  774. // The sequence number should have been bumped when we saved the new resume token.
  775. ListenSequenceNumber newSequenceNumber = queryData2.sequenceNumber;
  776. XCTAssertGreaterThan(newSequenceNumber, initialSequenceNumber);
  777. }
  778. - (void)testRemoteDocumentKeysForTarget {
  779. if ([self isTestBaseClass]) return;
  780. core::Query query = Query("foo");
  781. [self allocateQuery:query];
  782. FSTAssertTargetID(2);
  783. [self
  784. applyRemoteEvent:FSTTestAddedRemoteEvent(
  785. FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, DocumentState::kSynced), {2})];
  786. [self
  787. applyRemoteEvent:FSTTestAddedRemoteEvent(
  788. FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, DocumentState::kSynced), {2})];
  789. [self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
  790. DocumentKeySet keys = [self.localStore remoteDocumentKeysForTarget:2];
  791. DocumentKeySet expected{testutil::Key("foo/bar"), testutil::Key("foo/baz")};
  792. XCTAssertEqual(keys, expected);
  793. keys = [self.localStore remoteDocumentKeysForTarget:2];
  794. XCTAssertEqual(keys, (DocumentKeySet{testutil::Key("foo/bar"), testutil::Key("foo/baz")}));
  795. }
  796. // TODO(mrschmidt): The FieldValue.increment() field transform tests below would probably be
  797. // better implemented as spec tests but currently they don't support transforms.
  798. - (void)testHandlesSetMutationThenTransformMutationThenTransformMutation {
  799. if ([self isTestBaseClass]) return;
  800. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
  801. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, DocumentState::kLocalMutations));
  802. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, DocumentState::kLocalMutations) ]);
  803. [self writeMutation:FSTTestTransformMutation(
  804. @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
  805. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, DocumentState::kLocalMutations));
  806. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  807. [self writeMutation:FSTTestTransformMutation(
  808. @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
  809. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @3}, DocumentState::kLocalMutations));
  810. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @3}, DocumentState::kLocalMutations) ]);
  811. }
  812. - (void)testHandlesSetMutationThenAckThenTransformMutationThenAckThenTransformMutation {
  813. if ([self isTestBaseClass]) return;
  814. // Since this test doesn't start a listen, Eager GC removes the documents from the cache as
  815. // soon as the mutation is applied. This creates a lot of special casing in this unit test but
  816. // does not expand its test coverage.
  817. if ([self gcIsEager]) return;
  818. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
  819. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, DocumentState::kLocalMutations));
  820. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, DocumentState::kLocalMutations) ]);
  821. [self acknowledgeMutationWithVersion:1];
  822. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, DocumentState::kCommittedMutations));
  823. FSTAssertChanged(
  824. @[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, DocumentState::kCommittedMutations) ]);
  825. [self writeMutation:FSTTestTransformMutation(
  826. @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
  827. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations));
  828. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  829. [self acknowledgeMutationWithVersion:2 transformResult:@1];
  830. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, DocumentState::kCommittedMutations));
  831. FSTAssertChanged(
  832. @[ FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, DocumentState::kCommittedMutations) ]);
  833. [self writeMutation:FSTTestTransformMutation(
  834. @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
  835. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, DocumentState::kLocalMutations));
  836. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, DocumentState::kLocalMutations) ]);
  837. }
  838. - (void)testHandlesSetMutationThenTransformMutationThenRemoteEventThenTransformMutation {
  839. if ([self isTestBaseClass]) return;
  840. core::Query query = Query("foo");
  841. [self allocateQuery:query];
  842. FSTAssertTargetID(2);
  843. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
  844. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, DocumentState::kLocalMutations));
  845. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, DocumentState::kLocalMutations) ]);
  846. [self
  847. applyRemoteEvent:FSTTestAddedRemoteEvent(
  848. FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, DocumentState::kSynced), {2})];
  849. [self acknowledgeMutationWithVersion:1];
  850. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, DocumentState::kSynced));
  851. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, DocumentState::kSynced) ]);
  852. [self writeMutation:FSTTestTransformMutation(
  853. @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
  854. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations));
  855. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  856. // The value in this remote event gets ignored since we still have a pending transform mutation.
  857. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  858. FSTTestDoc("foo/bar", 2, @{@"sum" : @0}, DocumentState::kSynced), {2},
  859. {})];
  860. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, DocumentState::kLocalMutations));
  861. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  862. // Add another increment. Note that we still compute the increment based on the local value.
  863. [self writeMutation:FSTTestTransformMutation(
  864. @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
  865. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, DocumentState::kLocalMutations));
  866. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, DocumentState::kLocalMutations) ]);
  867. [self acknowledgeMutationWithVersion:3 transformResult:@1];
  868. FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"sum" : @3}, DocumentState::kLocalMutations));
  869. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"sum" : @3}, DocumentState::kLocalMutations) ]);
  870. [self acknowledgeMutationWithVersion:4 transformResult:@1339];
  871. FSTAssertContains(
  872. FSTTestDoc("foo/bar", 4, @{@"sum" : @1339}, DocumentState::kCommittedMutations));
  873. FSTAssertChanged(
  874. @[ FSTTestDoc("foo/bar", 4, @{@"sum" : @1339}, DocumentState::kCommittedMutations) ]);
  875. }
  876. - (void)testHoldsBackOnlyNonIdempotentTransforms {
  877. if ([self isTestBaseClass]) return;
  878. core::Query query = Query("foo");
  879. [self allocateQuery:query];
  880. FSTAssertTargetID(2);
  881. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0, @"array_union" : @[]})];
  882. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0, @"array_union" : @[]},
  883. DocumentState::kLocalMutations) ]);
  884. [self acknowledgeMutationWithVersion:1];
  885. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0, @"array_union" : @[]},
  886. DocumentState::kCommittedMutations) ]);
  887. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  888. FSTTestDoc("foo/bar", 1, @{@"sum" : @0, @"array_union" : @[]},
  889. DocumentState::kSynced),
  890. {2})];
  891. FSTAssertChanged(
  892. @[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0, @"array_union" : @[]}, DocumentState::kSynced) ]);
  893. [self writeMutations:{
  894. FSTTestTransformMutation(@"foo/bar",
  895. @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}),
  896. FSTTestTransformMutation(
  897. @"foo/bar",
  898. @{@"array_union" : [FIRFieldValue fieldValueForArrayUnion:@[ @"foo" ]]})
  899. }];
  900. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1, @"array_union" : @[ @"foo" ]},
  901. DocumentState::kLocalMutations) ]);
  902. // The sum transform is not idempotent and the backend's updated value is ignored. The
  903. // ArrayUnion transform is recomputed and includes the backend value.
  904. [self
  905. applyRemoteEvent:FSTTestUpdateRemoteEvent(
  906. FSTTestDoc("foo/bar", 1, @{@"sum" : @1337, @"array_union" : @[ @"bar" ]},
  907. DocumentState::kSynced),
  908. {2}, {})];
  909. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1, @"array_union" : @[ @"bar", @"foo" ]},
  910. DocumentState::kLocalMutations) ]);
  911. }
  912. - (void)testHandlesMergeMutationWithTransformThenRemoteEvent {
  913. if ([self isTestBaseClass]) return;
  914. core::Query query = Query("foo");
  915. [self allocateQuery:query];
  916. FSTAssertTargetID(2);
  917. [self writeMutations:{
  918. FSTTestPatchMutation("foo/bar", @{}, {firebase::firestore::testutil::Field("sum")}),
  919. FSTTestTransformMutation(@"foo/bar",
  920. @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})
  921. }];
  922. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, DocumentState::kLocalMutations));
  923. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  924. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  925. FSTTestDoc("foo/bar", 1, @{@"sum" : @1337}, DocumentState::kSynced),
  926. {2})];
  927. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations));
  928. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  929. }
  930. - (void)testHandlesPatchMutationWithTransformThenRemoteEvent {
  931. if ([self isTestBaseClass]) return;
  932. core::Query query = Query("foo");
  933. [self allocateQuery:query];
  934. FSTAssertTargetID(2);
  935. [self writeMutations:{
  936. FSTTestPatchMutation("foo/bar", @{}, {}),
  937. FSTTestTransformMutation(@"foo/bar",
  938. @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})
  939. }];
  940. FSTAssertNotContains(@"foo/bar");
  941. FSTAssertChanged(@[ FSTTestDeletedDoc("foo/bar", 0, NO) ]);
  942. // Note: This test reflects the current behavior, but it may be preferable to replay the
  943. // mutation once we receive the first value from the remote event.
  944. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  945. FSTTestDoc("foo/bar", 1, @{@"sum" : @1337}, DocumentState::kSynced),
  946. {2})];
  947. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations));
  948. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, DocumentState::kLocalMutations) ]);
  949. }
  950. @end
  951. NS_ASSUME_NONNULL_END