FSTHelpers.mm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #include "Firestore/core/src/firebase/firestore/core/filter.h"
  27. #include "Firestore/core/src/firebase/firestore/core/view.h"
  28. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  29. #include "Firestore/core/src/firebase/firestore/local/local_view_changes.h"
  30. #include "Firestore/core/src/firebase/firestore/local/query_data.h"
  31. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  32. #include "Firestore/core/src/firebase/firestore/model/delete_mutation.h"
  33. #include "Firestore/core/src/firebase/firestore/model/document.h"
  34. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  35. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  36. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  37. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  38. #include "Firestore/core/src/firebase/firestore/model/field_transform.h"
  39. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  40. #include "Firestore/core/src/firebase/firestore/model/patch_mutation.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/set_mutation.h"
  44. #include "Firestore/core/src/firebase/firestore/model/transform_mutation.h"
  45. #include "Firestore/core/src/firebase/firestore/model/transform_operation.h"
  46. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  47. #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
  48. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  49. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  50. #include "absl/memory/memory.h"
  51. namespace testutil = firebase::firestore::testutil;
  52. namespace util = firebase::firestore::util;
  53. using firebase::firestore::core::Direction;
  54. using firebase::firestore::core::Filter;
  55. using firebase::firestore::core::ParsedUpdateData;
  56. using firebase::firestore::core::Query;
  57. using firebase::firestore::core::View;
  58. using firebase::firestore::core::ViewChange;
  59. using firebase::firestore::core::ViewSnapshot;
  60. using firebase::firestore::local::LocalViewChanges;
  61. using firebase::firestore::local::QueryData;
  62. using firebase::firestore::local::QueryPurpose;
  63. using firebase::firestore::model::DatabaseId;
  64. using firebase::firestore::model::DeleteMutation;
  65. using firebase::firestore::model::Document;
  66. using firebase::firestore::model::DocumentComparator;
  67. using firebase::firestore::model::DocumentKey;
  68. using firebase::firestore::model::DocumentKeySet;
  69. using firebase::firestore::model::DocumentSet;
  70. using firebase::firestore::model::DocumentState;
  71. using firebase::firestore::model::FieldMask;
  72. using firebase::firestore::model::FieldPath;
  73. using firebase::firestore::model::FieldTransform;
  74. using firebase::firestore::model::FieldValue;
  75. using firebase::firestore::model::MaybeDocument;
  76. using firebase::firestore::model::MaybeDocumentMap;
  77. using firebase::firestore::model::NoDocument;
  78. using firebase::firestore::model::ObjectValue;
  79. using firebase::firestore::model::PatchMutation;
  80. using firebase::firestore::model::Precondition;
  81. using firebase::firestore::model::ResourcePath;
  82. using firebase::firestore::model::SetMutation;
  83. using firebase::firestore::model::ServerTimestampTransform;
  84. using firebase::firestore::model::SnapshotVersion;
  85. using firebase::firestore::model::TargetId;
  86. using firebase::firestore::model::TransformMutation;
  87. using firebase::firestore::model::TransformOperation;
  88. using firebase::firestore::model::UnknownDocument;
  89. using firebase::firestore::nanopb::ByteString;
  90. using firebase::firestore::remote::DocumentWatchChange;
  91. using firebase::firestore::remote::RemoteEvent;
  92. using firebase::firestore::remote::TargetChange;
  93. using firebase::firestore::remote::WatchChangeAggregator;
  94. using firebase::firestore::testutil::OrderBy;
  95. using firebase::firestore::testutil::Query;
  96. NS_ASSUME_NONNULL_BEGIN
  97. /** A string sentinel that can be used with FSTTestPatchMutation() to mark a field for deletion. */
  98. static NSString *const kDeleteSentinel = @"<DELETE>";
  99. FIRTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second) {
  100. NSDate *date = FSTTestDate(year, month, day, hour, minute, second);
  101. return [FIRTimestamp timestampWithDate:date];
  102. }
  103. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second) {
  104. NSDateComponents *comps = FSTTestDateComponents(year, month, day, hour, minute, second);
  105. return [[NSCalendar currentCalendar] dateFromComponents:comps];
  106. }
  107. NSData *FSTTestData(int bytes, ...) {
  108. va_list args;
  109. va_start(args, bytes); /* Initialize the argument list. */
  110. NSMutableData *data = [NSMutableData data];
  111. int next = bytes;
  112. while (next >= 0) {
  113. uint8_t byte = (uint8_t)next;
  114. [data appendBytes:&byte length:1];
  115. next = va_arg(args, int);
  116. }
  117. va_end(args);
  118. return [data copy];
  119. }
  120. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude) {
  121. return [[FIRGeoPoint alloc] initWithLatitude:latitude longitude:longitude];
  122. }
  123. NSDateComponents *FSTTestDateComponents(
  124. int year, int month, int day, int hour, int minute, int second) {
  125. NSDateComponents *comps = [[NSDateComponents alloc] init];
  126. comps.year = year;
  127. comps.month = month;
  128. comps.day = day;
  129. comps.hour = hour;
  130. comps.minute = minute;
  131. comps.second = second;
  132. // Force time zone to UTC to avoid these values changing due to daylight saving.
  133. comps.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  134. return comps;
  135. }
  136. FSTUserDataConverter *FSTTestUserDataConverter() {
  137. FSTUserDataConverter *converter =
  138. [[FSTUserDataConverter alloc] initWithDatabaseID:DatabaseId("project")
  139. preConverter:^id _Nullable(id _Nullable input) {
  140. return input;
  141. }];
  142. return converter;
  143. }
  144. FieldValue FSTTestFieldValue(id _Nullable value) {
  145. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  146. // HACK: We use parsedQueryValue: since it accepts scalars as well as arrays / objects, and
  147. // our tests currently use FSTTestFieldValue() pretty generically so we don't know the intent.
  148. return [converter parsedQueryValue:value];
  149. }
  150. ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data) {
  151. FieldValue wrapped = FSTTestFieldValue(data);
  152. HARD_ASSERT(wrapped.type() == FieldValue::Type::Object, "Unsupported value: %s", data);
  153. return ObjectValue(std::move(wrapped));
  154. }
  155. DocumentKey FSTTestDocKey(NSString *path) {
  156. return DocumentKey::FromPathString(util::MakeString(path));
  157. }
  158. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string database, NSString *path) {
  159. return [[FSTDocumentKeyReference alloc] initWithKey:FSTTestDocKey(path)
  160. databaseID:DatabaseId(projectID, database)];
  161. }
  162. SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values) {
  163. return SetMutation(FSTTestDocKey(path), FSTTestObjectValue(values), Precondition::None());
  164. }
  165. PatchMutation FSTTestPatchMutation(const absl::string_view path,
  166. NSDictionary<NSString *, id> *values,
  167. const std::vector<FieldPath> &updateMask) {
  168. BOOL merge = !updateMask.empty();
  169. __block ObjectValue objectValue = ObjectValue::Empty();
  170. __block std::set<FieldPath> fieldMaskPaths;
  171. [values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
  172. const FieldPath path = testutil::Field(util::MakeString(key));
  173. fieldMaskPaths.insert(path);
  174. if (![value isEqual:kDeleteSentinel]) {
  175. FieldValue parsedValue = FSTTestFieldValue(value);
  176. objectValue = objectValue.Set(path, std::move(parsedValue));
  177. }
  178. }];
  179. DocumentKey key = testutil::Key(path);
  180. Precondition precondition = merge ? Precondition::None() : Precondition::Exists(true);
  181. FieldMask mask(merge ? std::set<FieldPath>(updateMask.begin(), updateMask.end())
  182. : fieldMaskPaths);
  183. return PatchMutation(key, objectValue, mask, precondition);
  184. }
  185. TransformMutation FSTTestTransformMutation(NSString *path, NSDictionary<NSString *, id> *data) {
  186. DocumentKey key{testutil::Resource(util::MakeString(path))};
  187. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  188. ParsedUpdateData result = [converter parsedUpdateData:data];
  189. HARD_ASSERT(result.data().size() == 0,
  190. "FSTTestTransformMutation() only expects transforms; no other data");
  191. return TransformMutation(key, result.field_transforms());
  192. }
  193. DeleteMutation FSTTestDeleteMutation(NSString *path) {
  194. return DeleteMutation(FSTTestDocKey(path), Precondition::None());
  195. }
  196. MaybeDocumentMap FSTTestDocUpdates(const std::vector<MaybeDocument> &docs) {
  197. MaybeDocumentMap updates;
  198. for (const MaybeDocument &doc : docs) {
  199. updates = updates.insert(doc.key(), doc);
  200. }
  201. return updates;
  202. }
  203. absl::optional<ViewSnapshot> FSTTestApplyChanges(View *view,
  204. const std::vector<MaybeDocument> &docs,
  205. const absl::optional<TargetChange> &targetChange) {
  206. ViewChange change =
  207. view->ApplyChanges(view->ComputeDocumentChanges(FSTTestDocUpdates(docs)), targetChange);
  208. return change.snapshot();
  209. }
  210. namespace firebase {
  211. namespace firestore {
  212. namespace remote {
  213. TestTargetMetadataProvider TestTargetMetadataProvider::CreateSingleResultProvider(
  214. DocumentKey document_key,
  215. const std::vector<TargetId> &listen_targets,
  216. const std::vector<TargetId> &limbo_targets) {
  217. TestTargetMetadataProvider metadata_provider;
  218. core::Query query(document_key.path());
  219. for (TargetId target_id : listen_targets) {
  220. QueryData query_data(query, target_id, 0, QueryPurpose::Listen);
  221. metadata_provider.SetSyncedKeys(DocumentKeySet{document_key}, query_data);
  222. }
  223. for (TargetId target_id : limbo_targets) {
  224. QueryData query_data(query, target_id, 0, QueryPurpose::LimboResolution);
  225. metadata_provider.SetSyncedKeys(DocumentKeySet{document_key}, query_data);
  226. }
  227. return metadata_provider;
  228. }
  229. TestTargetMetadataProvider TestTargetMetadataProvider::CreateSingleResultProvider(
  230. DocumentKey document_key, const std::vector<TargetId> &targets) {
  231. return CreateSingleResultProvider(document_key, targets, /*limbo_targets=*/{});
  232. }
  233. TestTargetMetadataProvider TestTargetMetadataProvider::CreateEmptyResultProvider(
  234. const DocumentKey &document_key, const std::vector<TargetId> &targets) {
  235. TestTargetMetadataProvider metadata_provider;
  236. core::Query query(document_key.path());
  237. for (TargetId target_id : targets) {
  238. QueryData query_data(query, target_id, 0, QueryPurpose::Listen);
  239. metadata_provider.SetSyncedKeys(DocumentKeySet{}, query_data);
  240. }
  241. return metadata_provider;
  242. }
  243. void TestTargetMetadataProvider::SetSyncedKeys(DocumentKeySet keys, QueryData query_data) {
  244. synced_keys_[query_data.target_id()] = keys;
  245. query_data_[query_data.target_id()] = std::move(query_data);
  246. }
  247. DocumentKeySet TestTargetMetadataProvider::GetRemoteKeysForTarget(TargetId target_id) const {
  248. auto it = synced_keys_.find(target_id);
  249. HARD_ASSERT(it != synced_keys_.end(), "Cannot process unknown target %s", target_id);
  250. return it->second;
  251. }
  252. absl::optional<QueryData> TestTargetMetadataProvider::GetQueryDataForTarget(
  253. TargetId target_id) const {
  254. auto it = query_data_.find(target_id);
  255. HARD_ASSERT(it != query_data_.end(), "Cannot process unknown target %s", target_id);
  256. return it->second;
  257. }
  258. } // namespace remote
  259. } // namespace firestore
  260. } // namespace firebase
  261. using firebase::firestore::remote::TestTargetMetadataProvider;
  262. RemoteEvent FSTTestAddedRemoteEvent(const MaybeDocument &doc,
  263. const std::vector<TargetId> &addedToTargets) {
  264. HARD_ASSERT(!doc.is_document() || !Document(doc).has_local_mutations(),
  265. "Docs from remote updates shouldn't have local changes.");
  266. DocumentWatchChange change{addedToTargets, {}, doc.key(), doc};
  267. auto metadataProvider =
  268. TestTargetMetadataProvider::CreateEmptyResultProvider(doc.key(), addedToTargets);
  269. WatchChangeAggregator aggregator{&metadataProvider};
  270. aggregator.HandleDocumentChange(change);
  271. return aggregator.CreateRemoteEvent(doc.version());
  272. }
  273. TargetChange FSTTestTargetChangeMarkCurrent() {
  274. return {ByteString(),
  275. /*current=*/true,
  276. /*added_documents=*/DocumentKeySet{},
  277. /*modified_documents=*/DocumentKeySet{},
  278. /*removed_documents=*/DocumentKeySet{}};
  279. }
  280. TargetChange FSTTestTargetChangeAckDocuments(DocumentKeySet docs) {
  281. return {ByteString(),
  282. /*current=*/true,
  283. /*added_documents*/ std::move(docs),
  284. /*modified_documents*/ DocumentKeySet{},
  285. /*removed_documents*/ DocumentKeySet{}};
  286. }
  287. RemoteEvent FSTTestUpdateRemoteEventWithLimboTargets(
  288. const MaybeDocument &doc,
  289. const std::vector<TargetId> &updatedInTargets,
  290. const std::vector<TargetId> &removedFromTargets,
  291. const std::vector<TargetId> &limboTargets) {
  292. HARD_ASSERT(!doc.is_document() || !Document(doc).has_local_mutations(),
  293. "Docs from remote updates shouldn't have local changes.");
  294. DocumentWatchChange change{updatedInTargets, removedFromTargets, doc.key(), doc};
  295. std::vector<TargetId> listens = updatedInTargets;
  296. listens.insert(listens.end(), removedFromTargets.begin(), removedFromTargets.end());
  297. auto metadataProvider =
  298. TestTargetMetadataProvider::CreateSingleResultProvider(doc.key(), listens, limboTargets);
  299. WatchChangeAggregator aggregator{&metadataProvider};
  300. aggregator.HandleDocumentChange(change);
  301. return aggregator.CreateRemoteEvent(doc.version());
  302. }
  303. RemoteEvent FSTTestUpdateRemoteEvent(const MaybeDocument &doc,
  304. const std::vector<TargetId> &updatedInTargets,
  305. const std::vector<TargetId> &removedFromTargets) {
  306. return FSTTestUpdateRemoteEventWithLimboTargets(doc, updatedInTargets, removedFromTargets, {});
  307. }
  308. /** Creates a resume token to match the given snapshot version. */
  309. NSData *_Nullable FSTTestResumeTokenFromSnapshotVersion(FSTTestSnapshotVersion snapshotVersion) {
  310. if (snapshotVersion == 0) {
  311. return nil;
  312. }
  313. NSString *snapshotString = [NSString stringWithFormat:@"snapshot-%" PRId64, snapshotVersion];
  314. return [snapshotString dataUsingEncoding:NSUTF8StringEncoding];
  315. }
  316. LocalViewChanges TestViewChanges(TargetId targetID,
  317. NSArray<NSString *> *addedKeys,
  318. NSArray<NSString *> *removedKeys) {
  319. DocumentKeySet added;
  320. for (NSString *keyPath in addedKeys) {
  321. added = added.insert(testutil::Key(util::MakeString(keyPath)));
  322. }
  323. DocumentKeySet removed;
  324. for (NSString *keyPath in removedKeys) {
  325. removed = removed.insert(testutil::Key(util::MakeString(keyPath)));
  326. }
  327. return LocalViewChanges(targetID, std::move(added), std::move(removed));
  328. }
  329. NS_ASSUME_NONNULL_END