FSTHelpers.mm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/remote/fake_target_metadata_provider.h"
  50. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  51. #include "absl/memory/memory.h"
  52. namespace testutil = firebase::firestore::testutil;
  53. namespace util = firebase::firestore::util;
  54. using firebase::firestore::core::Direction;
  55. using firebase::firestore::core::Filter;
  56. using firebase::firestore::core::ParsedUpdateData;
  57. using firebase::firestore::core::Query;
  58. using firebase::firestore::core::View;
  59. using firebase::firestore::core::ViewChange;
  60. using firebase::firestore::core::ViewSnapshot;
  61. using firebase::firestore::local::LocalViewChanges;
  62. using firebase::firestore::local::QueryData;
  63. using firebase::firestore::local::QueryPurpose;
  64. using firebase::firestore::model::DatabaseId;
  65. using firebase::firestore::model::DeleteMutation;
  66. using firebase::firestore::model::Document;
  67. using firebase::firestore::model::DocumentComparator;
  68. using firebase::firestore::model::DocumentKey;
  69. using firebase::firestore::model::DocumentKeySet;
  70. using firebase::firestore::model::DocumentSet;
  71. using firebase::firestore::model::DocumentState;
  72. using firebase::firestore::model::FieldMask;
  73. using firebase::firestore::model::FieldPath;
  74. using firebase::firestore::model::FieldTransform;
  75. using firebase::firestore::model::FieldValue;
  76. using firebase::firestore::model::MaybeDocument;
  77. using firebase::firestore::model::MaybeDocumentMap;
  78. using firebase::firestore::model::NoDocument;
  79. using firebase::firestore::model::ObjectValue;
  80. using firebase::firestore::model::PatchMutation;
  81. using firebase::firestore::model::Precondition;
  82. using firebase::firestore::model::ResourcePath;
  83. using firebase::firestore::model::SetMutation;
  84. using firebase::firestore::model::ServerTimestampTransform;
  85. using firebase::firestore::model::SnapshotVersion;
  86. using firebase::firestore::model::TargetId;
  87. using firebase::firestore::model::TransformMutation;
  88. using firebase::firestore::model::TransformOperation;
  89. using firebase::firestore::model::UnknownDocument;
  90. using firebase::firestore::nanopb::ByteString;
  91. using firebase::firestore::remote::DocumentWatchChange;
  92. using firebase::firestore::remote::FakeTargetMetadataProvider;
  93. using firebase::firestore::remote::RemoteEvent;
  94. using firebase::firestore::remote::TargetChange;
  95. using firebase::firestore::remote::WatchChangeAggregator;
  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. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second) {
  100. NSDateComponents *comps = FSTTestDateComponents(year, month, day, hour, minute, second);
  101. return [[NSCalendar currentCalendar] dateFromComponents:comps];
  102. }
  103. NSData *FSTTestData(int bytes, ...) {
  104. va_list args;
  105. va_start(args, bytes); /* Initialize the argument list. */
  106. NSMutableData *data = [NSMutableData data];
  107. int next = bytes;
  108. while (next >= 0) {
  109. uint8_t byte = (uint8_t)next;
  110. [data appendBytes:&byte length:1];
  111. next = va_arg(args, int);
  112. }
  113. va_end(args);
  114. return [data copy];
  115. }
  116. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude) {
  117. return [[FIRGeoPoint alloc] initWithLatitude:latitude longitude:longitude];
  118. }
  119. NSDateComponents *FSTTestDateComponents(
  120. int year, int month, int day, int hour, int minute, int second) {
  121. NSDateComponents *comps = [[NSDateComponents alloc] init];
  122. comps.year = year;
  123. comps.month = month;
  124. comps.day = day;
  125. comps.hour = hour;
  126. comps.minute = minute;
  127. comps.second = second;
  128. // Force time zone to UTC to avoid these values changing due to daylight saving.
  129. comps.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  130. return comps;
  131. }
  132. FSTUserDataConverter *FSTTestUserDataConverter() {
  133. FSTUserDataConverter *converter =
  134. [[FSTUserDataConverter alloc] initWithDatabaseID:DatabaseId("project")
  135. preConverter:^id _Nullable(id _Nullable input) {
  136. return input;
  137. }];
  138. return converter;
  139. }
  140. FieldValue FSTTestFieldValue(id _Nullable value) {
  141. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  142. // HACK: We use parsedQueryValue: since it accepts scalars as well as arrays / objects, and
  143. // our tests currently use FSTTestFieldValue() pretty generically so we don't know the intent.
  144. return [converter parsedQueryValue:value];
  145. }
  146. ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data) {
  147. FieldValue wrapped = FSTTestFieldValue(data);
  148. HARD_ASSERT(wrapped.type() == FieldValue::Type::Object, "Unsupported value: %s", data);
  149. return ObjectValue(std::move(wrapped));
  150. }
  151. DocumentKey FSTTestDocKey(NSString *path) {
  152. return DocumentKey::FromPathString(util::MakeString(path));
  153. }
  154. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string database, NSString *path) {
  155. return [[FSTDocumentKeyReference alloc] initWithKey:FSTTestDocKey(path)
  156. databaseID:DatabaseId(projectID, database)];
  157. }
  158. SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values) {
  159. return SetMutation(FSTTestDocKey(path), FSTTestObjectValue(values), Precondition::None());
  160. }
  161. PatchMutation FSTTestPatchMutation(const absl::string_view path,
  162. NSDictionary<NSString *, id> *values,
  163. const std::vector<FieldPath> &updateMask) {
  164. BOOL merge = !updateMask.empty();
  165. __block ObjectValue objectValue = ObjectValue::Empty();
  166. __block std::set<FieldPath> fieldMaskPaths;
  167. [values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
  168. const FieldPath path = testutil::Field(util::MakeString(key));
  169. fieldMaskPaths.insert(path);
  170. if (![value isEqual:kDeleteSentinel]) {
  171. FieldValue parsedValue = FSTTestFieldValue(value);
  172. objectValue = objectValue.Set(path, std::move(parsedValue));
  173. }
  174. }];
  175. DocumentKey key = testutil::Key(path);
  176. Precondition precondition = merge ? Precondition::None() : Precondition::Exists(true);
  177. FieldMask mask(merge ? std::set<FieldPath>(updateMask.begin(), updateMask.end())
  178. : fieldMaskPaths);
  179. return PatchMutation(key, objectValue, mask, precondition);
  180. }
  181. TransformMutation FSTTestTransformMutation(NSString *path, NSDictionary<NSString *, id> *data) {
  182. DocumentKey key{testutil::Resource(util::MakeString(path))};
  183. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  184. ParsedUpdateData result = [converter parsedUpdateData:data];
  185. HARD_ASSERT(result.data().size() == 0,
  186. "FSTTestTransformMutation() only expects transforms; no other data");
  187. return TransformMutation(key, result.field_transforms());
  188. }
  189. DeleteMutation FSTTestDeleteMutation(NSString *path) {
  190. return DeleteMutation(FSTTestDocKey(path), Precondition::None());
  191. }
  192. NS_ASSUME_NONNULL_END