FSTHelpers.mm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/Example/Tests/Util/FSTHelpers.h"
  17. #import <FirebaseFirestore/FIRFieldPath.h>
  18. #import <FirebaseFirestore/FIRGeoPoint.h>
  19. #import <FirebaseFirestore/FIRTimestamp.h>
  20. #include <cinttypes>
  21. #include <list>
  22. #include <set>
  23. #include <utility>
  24. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  25. #import "Firestore/Source/API/FSTUserDataConverter.h"
  26. #import "Firestore/Source/Core/FSTQuery.h"
  27. #import "Firestore/Source/Core/FSTView.h"
  28. #import "Firestore/Source/Local/FSTLocalViewChanges.h"
  29. #import "Firestore/Source/Local/FSTQueryData.h"
  30. #import "Firestore/Source/Model/FSTDocument.h"
  31. #import "Firestore/Source/Model/FSTDocumentSet.h"
  32. #import "Firestore/Source/Model/FSTFieldValue.h"
  33. #import "Firestore/Source/Model/FSTMutation.h"
  34. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  35. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  36. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  37. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  38. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  39. #include "Firestore/core/src/firebase/firestore/model/field_transform.h"
  40. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  41. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  42. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  43. #include "Firestore/core/src/firebase/firestore/model/transform_operations.h"
  44. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  45. #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
  46. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  47. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  48. #include "absl/memory/memory.h"
  49. namespace testutil = firebase::firestore::testutil;
  50. namespace util = firebase::firestore::util;
  51. using firebase::firestore::core::ParsedUpdateData;
  52. using firebase::firestore::core::ViewSnapshot;
  53. using firebase::firestore::model::DatabaseId;
  54. using firebase::firestore::model::DocumentKey;
  55. using firebase::firestore::model::DocumentKeySet;
  56. using firebase::firestore::model::FieldMask;
  57. using firebase::firestore::model::FieldPath;
  58. using firebase::firestore::model::FieldTransform;
  59. using firebase::firestore::model::FieldValue;
  60. using firebase::firestore::model::MaybeDocumentMap;
  61. using firebase::firestore::model::Precondition;
  62. using firebase::firestore::model::ResourcePath;
  63. using firebase::firestore::model::ServerTimestampTransform;
  64. using firebase::firestore::model::SnapshotVersion;
  65. using firebase::firestore::model::TargetId;
  66. using firebase::firestore::model::TransformOperation;
  67. using firebase::firestore::remote::DocumentWatchChange;
  68. using firebase::firestore::remote::RemoteEvent;
  69. using firebase::firestore::remote::TargetChange;
  70. using firebase::firestore::remote::WatchChangeAggregator;
  71. NS_ASSUME_NONNULL_BEGIN
  72. /** A string sentinel that can be used with FSTTestPatchMutation() to mark a field for deletion. */
  73. static NSString *const kDeleteSentinel = @"<DELETE>";
  74. FIRTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second) {
  75. NSDate *date = FSTTestDate(year, month, day, hour, minute, second);
  76. return [FIRTimestamp timestampWithDate:date];
  77. }
  78. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second) {
  79. NSDateComponents *comps = FSTTestDateComponents(year, month, day, hour, minute, second);
  80. return [[NSCalendar currentCalendar] dateFromComponents:comps];
  81. }
  82. NSData *FSTTestData(int bytes, ...) {
  83. va_list args;
  84. va_start(args, bytes); /* Initialize the argument list. */
  85. NSMutableData *data = [NSMutableData data];
  86. int next = bytes;
  87. while (next >= 0) {
  88. uint8_t byte = (uint8_t)next;
  89. [data appendBytes:&byte length:1];
  90. next = va_arg(args, int);
  91. }
  92. va_end(args);
  93. return [data copy];
  94. }
  95. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude) {
  96. return [[FIRGeoPoint alloc] initWithLatitude:latitude longitude:longitude];
  97. }
  98. NSDateComponents *FSTTestDateComponents(
  99. int year, int month, int day, int hour, int minute, int second) {
  100. NSDateComponents *comps = [[NSDateComponents alloc] init];
  101. comps.year = year;
  102. comps.month = month;
  103. comps.day = day;
  104. comps.hour = hour;
  105. comps.minute = minute;
  106. comps.second = second;
  107. // Force time zone to UTC to avoid these values changing due to daylight saving.
  108. comps.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  109. return comps;
  110. }
  111. FSTUserDataConverter *FSTTestUserDataConverter() {
  112. // This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
  113. static DatabaseId database_id{"project", DatabaseId::kDefault};
  114. FSTUserDataConverter *converter =
  115. [[FSTUserDataConverter alloc] initWithDatabaseID:&database_id
  116. preConverter:^id _Nullable(id _Nullable input) {
  117. return input;
  118. }];
  119. return converter;
  120. }
  121. FSTFieldValue *FSTTestFieldValue(id _Nullable value) {
  122. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  123. // HACK: We use parsedQueryValue: since it accepts scalars as well as arrays / objects, and
  124. // our tests currently use FSTTestFieldValue() pretty generically so we don't know the intent.
  125. return [converter parsedQueryValue:value];
  126. }
  127. FSTObjectValue *FSTTestObjectValue(NSDictionary<NSString *, id> *data) {
  128. FSTFieldValue *wrapped = FSTTestFieldValue(data);
  129. HARD_ASSERT([wrapped isKindOfClass:[FSTObjectValue class]], "Unsupported value: %s", data);
  130. return (FSTObjectValue *)wrapped;
  131. }
  132. DocumentKey FSTTestDocKey(NSString *path) {
  133. return DocumentKey::FromPathString(util::MakeString(path));
  134. }
  135. FSTDocument *FSTTestDoc(const absl::string_view path,
  136. FSTTestSnapshotVersion version,
  137. NSDictionary<NSString *, id> *data,
  138. FSTDocumentState documentState) {
  139. DocumentKey key = testutil::Key(path);
  140. return [FSTDocument documentWithData:FSTTestObjectValue(data)
  141. key:key
  142. version:testutil::Version(version)
  143. state:documentState];
  144. }
  145. FSTDeletedDocument *FSTTestDeletedDoc(const absl::string_view path,
  146. FSTTestSnapshotVersion version,
  147. BOOL hasCommittedMutations) {
  148. DocumentKey key = testutil::Key(path);
  149. return [FSTDeletedDocument documentWithKey:key
  150. version:testutil::Version(version)
  151. hasCommittedMutations:hasCommittedMutations];
  152. }
  153. FSTUnknownDocument *FSTTestUnknownDoc(const absl::string_view path,
  154. FSTTestSnapshotVersion version) {
  155. DocumentKey key = testutil::Key(path);
  156. return [FSTUnknownDocument documentWithKey:key version:testutil::Version(version)];
  157. }
  158. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string database, NSString *path) {
  159. // This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
  160. static std::list<DatabaseId> database_ids;
  161. database_ids.emplace_back(std::move(projectID), std::move(database));
  162. return [[FSTDocumentKeyReference alloc] initWithKey:FSTTestDocKey(path)
  163. databaseID:&database_ids.back()];
  164. }
  165. FSTQuery *FSTTestQuery(const absl::string_view path) {
  166. return [FSTQuery queryWithPath:testutil::Resource(path)];
  167. }
  168. FSTFilter *FSTTestFilter(const absl::string_view field, NSString *opString, id value) {
  169. const FieldPath path = testutil::Field(field);
  170. FSTRelationFilterOperator op;
  171. if ([opString isEqualToString:@"<"]) {
  172. op = FSTRelationFilterOperatorLessThan;
  173. } else if ([opString isEqualToString:@"<="]) {
  174. op = FSTRelationFilterOperatorLessThanOrEqual;
  175. } else if ([opString isEqualToString:@"=="]) {
  176. op = FSTRelationFilterOperatorEqual;
  177. } else if ([opString isEqualToString:@">="]) {
  178. op = FSTRelationFilterOperatorGreaterThanOrEqual;
  179. } else if ([opString isEqualToString:@">"]) {
  180. op = FSTRelationFilterOperatorGreaterThan;
  181. } else if ([opString isEqualToString:@"array_contains"]) {
  182. op = FSTRelationFilterOperatorArrayContains;
  183. } else {
  184. HARD_FAIL("Unsupported operator type: %s", opString);
  185. }
  186. FSTFieldValue *data = FSTTestFieldValue(value);
  187. return [FSTFilter filterWithField:path filterOperator:op value:data];
  188. }
  189. FSTSortOrder *FSTTestOrderBy(const absl::string_view field, NSString *direction) {
  190. const FieldPath path = testutil::Field(field);
  191. BOOL ascending;
  192. if ([direction isEqualToString:@"asc"]) {
  193. ascending = YES;
  194. } else if ([direction isEqualToString:@"desc"]) {
  195. ascending = NO;
  196. } else {
  197. HARD_FAIL("Unsupported direction: %s", direction);
  198. }
  199. return [FSTSortOrder sortOrderWithFieldPath:path ascending:ascending];
  200. }
  201. NSComparator FSTTestDocComparator(const absl::string_view fieldPath) {
  202. FSTQuery *query = [FSTTestQuery("docs")
  203. queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field(fieldPath)
  204. ascending:YES]];
  205. return [query comparator];
  206. }
  207. FSTDocumentSet *FSTTestDocSet(NSComparator comp, NSArray<FSTDocument *> *docs) {
  208. FSTDocumentSet *docSet = [FSTDocumentSet documentSetWithComparator:comp];
  209. for (FSTDocument *doc in docs) {
  210. docSet = [docSet documentSetByAddingDocument:doc];
  211. }
  212. return docSet;
  213. }
  214. FSTSetMutation *FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values) {
  215. return [[FSTSetMutation alloc] initWithKey:FSTTestDocKey(path)
  216. value:FSTTestObjectValue(values)
  217. precondition:Precondition::None()];
  218. }
  219. FSTPatchMutation *FSTTestPatchMutation(const absl::string_view path,
  220. NSDictionary<NSString *, id> *values,
  221. const std::vector<FieldPath> &updateMask) {
  222. BOOL merge = !updateMask.empty();
  223. __block FSTObjectValue *objectValue = [FSTObjectValue objectValue];
  224. __block std::set<FieldPath> fieldMaskPaths;
  225. [values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
  226. const FieldPath path = testutil::Field(util::MakeString(key));
  227. fieldMaskPaths.insert(path);
  228. if (![value isEqual:kDeleteSentinel]) {
  229. FSTFieldValue *parsedValue = FSTTestFieldValue(value);
  230. objectValue = [objectValue objectBySettingValue:parsedValue forPath:path];
  231. }
  232. }];
  233. DocumentKey key = testutil::Key(path);
  234. FieldMask mask(merge ? std::set<FieldPath>(updateMask.begin(), updateMask.end())
  235. : fieldMaskPaths);
  236. return [[FSTPatchMutation alloc]
  237. initWithKey:key
  238. fieldMask:mask
  239. value:objectValue
  240. precondition:merge ? Precondition::None() : Precondition::Exists(true)];
  241. }
  242. FSTTransformMutation *FSTTestTransformMutation(NSString *path, NSDictionary<NSString *, id> *data) {
  243. DocumentKey key{testutil::Resource(util::MakeString(path))};
  244. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  245. ParsedUpdateData result = [converter parsedUpdateData:data];
  246. HARD_ASSERT(result.data().value.count == 0,
  247. "FSTTestTransformMutation() only expects transforms; no other data");
  248. return [[FSTTransformMutation alloc] initWithKey:key fieldTransforms:result.field_transforms()];
  249. }
  250. FSTDeleteMutation *FSTTestDeleteMutation(NSString *path) {
  251. return [[FSTDeleteMutation alloc] initWithKey:FSTTestDocKey(path)
  252. precondition:Precondition::None()];
  253. }
  254. MaybeDocumentMap FSTTestDocUpdates(NSArray<FSTMaybeDocument *> *docs) {
  255. MaybeDocumentMap updates;
  256. for (FSTMaybeDocument *doc in docs) {
  257. updates = updates.insert(doc.key, doc);
  258. }
  259. return updates;
  260. }
  261. absl::optional<ViewSnapshot> FSTTestApplyChanges(FSTView *view,
  262. NSArray<FSTMaybeDocument *> *docs,
  263. const absl::optional<TargetChange> &targetChange) {
  264. FSTViewChange *change =
  265. [view applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(docs)]
  266. targetChange:targetChange];
  267. return std::move(change.snapshot);
  268. }
  269. namespace firebase {
  270. namespace firestore {
  271. namespace remote {
  272. TestTargetMetadataProvider TestTargetMetadataProvider::CreateSingleResultProvider(
  273. DocumentKey document_key,
  274. const std::vector<TargetId> &listen_targets,
  275. const std::vector<TargetId> &limbo_targets) {
  276. TestTargetMetadataProvider metadata_provider;
  277. FSTQuery *query = [FSTQuery queryWithPath:document_key.path()];
  278. for (TargetId target_id : listen_targets) {
  279. FSTQueryData *query_data = [[FSTQueryData alloc] initWithQuery:query
  280. targetID:target_id
  281. listenSequenceNumber:0
  282. purpose:FSTQueryPurposeListen];
  283. metadata_provider.SetSyncedKeys(DocumentKeySet{document_key}, query_data);
  284. }
  285. for (TargetId target_id : limbo_targets) {
  286. FSTQueryData *query_data = [[FSTQueryData alloc] initWithQuery:query
  287. targetID:target_id
  288. listenSequenceNumber:0
  289. purpose:FSTQueryPurposeLimboResolution];
  290. metadata_provider.SetSyncedKeys(DocumentKeySet{document_key}, query_data);
  291. }
  292. return metadata_provider;
  293. }
  294. TestTargetMetadataProvider TestTargetMetadataProvider::CreateSingleResultProvider(
  295. DocumentKey document_key, const std::vector<TargetId> &targets) {
  296. return CreateSingleResultProvider(document_key, targets, /*limbo_targets=*/{});
  297. }
  298. TestTargetMetadataProvider TestTargetMetadataProvider::CreateEmptyResultProvider(
  299. const DocumentKey &document_key, const std::vector<TargetId> &targets) {
  300. TestTargetMetadataProvider metadata_provider;
  301. FSTQuery *query = [FSTQuery queryWithPath:document_key.path()];
  302. for (TargetId target_id : targets) {
  303. FSTQueryData *query_data = [[FSTQueryData alloc] initWithQuery:query
  304. targetID:target_id
  305. listenSequenceNumber:0
  306. purpose:FSTQueryPurposeListen];
  307. metadata_provider.SetSyncedKeys(DocumentKeySet{}, query_data);
  308. }
  309. return metadata_provider;
  310. }
  311. void TestTargetMetadataProvider::SetSyncedKeys(DocumentKeySet keys, FSTQueryData *query_data) {
  312. synced_keys_[query_data.targetID] = keys;
  313. query_data_[query_data.targetID] = query_data;
  314. }
  315. DocumentKeySet TestTargetMetadataProvider::GetRemoteKeysForTarget(TargetId target_id) const {
  316. auto it = synced_keys_.find(target_id);
  317. HARD_ASSERT(it != synced_keys_.end(), "Cannot process unknown target %s", target_id);
  318. return it->second;
  319. }
  320. FSTQueryData *TestTargetMetadataProvider::GetQueryDataForTarget(TargetId target_id) const {
  321. auto it = query_data_.find(target_id);
  322. HARD_ASSERT(it != query_data_.end(), "Cannot process unknown target %s", target_id);
  323. return it->second;
  324. }
  325. } // namespace remote
  326. } // namespace firestore
  327. } // namespace firebase
  328. using firebase::firestore::remote::TestTargetMetadataProvider;
  329. RemoteEvent FSTTestAddedRemoteEvent(FSTMaybeDocument *doc,
  330. const std::vector<TargetId> &addedToTargets) {
  331. HARD_ASSERT(![doc isKindOfClass:[FSTDocument class]] || ![(FSTDocument *)doc hasLocalMutations],
  332. "Docs from remote updates shouldn't have local changes.");
  333. DocumentWatchChange change{addedToTargets, {}, doc.key, doc};
  334. auto metadataProvider =
  335. TestTargetMetadataProvider::CreateEmptyResultProvider(doc.key, addedToTargets);
  336. WatchChangeAggregator aggregator{&metadataProvider};
  337. aggregator.HandleDocumentChange(change);
  338. return aggregator.CreateRemoteEvent(doc.version);
  339. }
  340. TargetChange FSTTestTargetChangeMarkCurrent() {
  341. return {[NSData data],
  342. /*current=*/true,
  343. /*added_documents=*/DocumentKeySet{},
  344. /*modified_documents=*/DocumentKeySet{},
  345. /*removed_documents=*/DocumentKeySet{}};
  346. }
  347. TargetChange FSTTestTargetChangeAckDocuments(DocumentKeySet docs) {
  348. return {[NSData data],
  349. /*current=*/true,
  350. /*added_documents*/ std::move(docs),
  351. /*modified_documents*/ DocumentKeySet{},
  352. /*removed_documents*/ DocumentKeySet{}};
  353. }
  354. RemoteEvent FSTTestUpdateRemoteEventWithLimboTargets(
  355. FSTMaybeDocument *doc,
  356. const std::vector<TargetId> &updatedInTargets,
  357. const std::vector<TargetId> &removedFromTargets,
  358. const std::vector<TargetId> &limboTargets) {
  359. HARD_ASSERT(![doc isKindOfClass:[FSTDocument class]] || ![(FSTDocument *)doc hasLocalMutations],
  360. "Docs from remote updates shouldn't have local changes.");
  361. DocumentWatchChange change{updatedInTargets, removedFromTargets, doc.key, doc};
  362. std::vector<TargetId> listens = updatedInTargets;
  363. listens.insert(listens.end(), removedFromTargets.begin(), removedFromTargets.end());
  364. auto metadataProvider =
  365. TestTargetMetadataProvider::CreateSingleResultProvider(doc.key, listens, limboTargets);
  366. WatchChangeAggregator aggregator{&metadataProvider};
  367. aggregator.HandleDocumentChange(change);
  368. return aggregator.CreateRemoteEvent(doc.version);
  369. }
  370. RemoteEvent FSTTestUpdateRemoteEvent(FSTMaybeDocument *doc,
  371. const std::vector<TargetId> &updatedInTargets,
  372. const std::vector<TargetId> &removedFromTargets) {
  373. return FSTTestUpdateRemoteEventWithLimboTargets(doc, updatedInTargets, removedFromTargets, {});
  374. }
  375. /** Creates a resume token to match the given snapshot version. */
  376. NSData *_Nullable FSTTestResumeTokenFromSnapshotVersion(FSTTestSnapshotVersion snapshotVersion) {
  377. if (snapshotVersion == 0) {
  378. return nil;
  379. }
  380. NSString *snapshotString = [NSString stringWithFormat:@"snapshot-%" PRId64, snapshotVersion];
  381. return [snapshotString dataUsingEncoding:NSUTF8StringEncoding];
  382. }
  383. FSTLocalViewChanges *FSTTestViewChanges(TargetId targetID,
  384. NSArray<NSString *> *addedKeys,
  385. NSArray<NSString *> *removedKeys) {
  386. DocumentKeySet added;
  387. for (NSString *keyPath in addedKeys) {
  388. added = added.insert(testutil::Key(util::MakeString(keyPath)));
  389. }
  390. DocumentKeySet removed;
  391. for (NSString *keyPath in removedKeys) {
  392. removed = removed.insert(testutil::Key(util::MakeString(keyPath)));
  393. }
  394. return [FSTLocalViewChanges changesForTarget:targetID
  395. addedKeys:std::move(added)
  396. removedKeys:std::move(removed)];
  397. }
  398. NS_ASSUME_NONNULL_END