FSTHelpers.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/FIRGeoPoint.h>
  18. #include <set>
  19. #include <utility>
  20. #import "Firestore/Source/API/FSTUserDataConverter.h"
  21. #include "Firestore/core/src/core/user_data.h"
  22. #include "Firestore/core/src/model/database_id.h"
  23. #include "Firestore/core/src/model/delete_mutation.h"
  24. #include "Firestore/core/src/model/document_key.h"
  25. #include "Firestore/core/src/model/field_mask.h"
  26. #include "Firestore/core/src/model/field_path.h"
  27. #include "Firestore/core/src/model/field_value.h"
  28. #include "Firestore/core/src/model/patch_mutation.h"
  29. #include "Firestore/core/src/model/precondition.h"
  30. #include "Firestore/core/src/model/resource_path.h"
  31. #include "Firestore/core/src/model/set_mutation.h"
  32. #include "Firestore/core/src/model/transform_mutation.h"
  33. #include "Firestore/core/src/util/string_apple.h"
  34. #include "Firestore/core/test/unit/testutil/testutil.h"
  35. namespace testutil = firebase::firestore::testutil;
  36. namespace util = firebase::firestore::util;
  37. using firebase::firestore::core::ParsedUpdateData;
  38. using firebase::firestore::model::DatabaseId;
  39. using firebase::firestore::model::DeleteMutation;
  40. using firebase::firestore::model::DocumentKey;
  41. using firebase::firestore::model::FieldMask;
  42. using firebase::firestore::model::FieldPath;
  43. using firebase::firestore::model::FieldValue;
  44. using firebase::firestore::model::ObjectValue;
  45. using firebase::firestore::model::PatchMutation;
  46. using firebase::firestore::model::Precondition;
  47. using firebase::firestore::model::SetMutation;
  48. using firebase::firestore::model::TransformMutation;
  49. NS_ASSUME_NONNULL_BEGIN
  50. /** A string sentinel that can be used with FSTTestPatchMutation() to mark a field for deletion. */
  51. static NSString *const kDeleteSentinel = @"<DELETE>";
  52. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second) {
  53. NSDateComponents *comps = FSTTestDateComponents(year, month, day, hour, minute, second);
  54. return [[NSCalendar currentCalendar] dateFromComponents:comps];
  55. }
  56. NSData *FSTTestData(int bytes, ...) {
  57. va_list args;
  58. va_start(args, bytes); /* Initialize the argument list. */
  59. NSMutableData *data = [NSMutableData data];
  60. int next = bytes;
  61. while (next >= 0) {
  62. uint8_t byte = (uint8_t)next;
  63. [data appendBytes:&byte length:1];
  64. next = va_arg(args, int);
  65. }
  66. va_end(args);
  67. return [data copy];
  68. }
  69. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude) {
  70. return [[FIRGeoPoint alloc] initWithLatitude:latitude longitude:longitude];
  71. }
  72. NSDateComponents *FSTTestDateComponents(
  73. int year, int month, int day, int hour, int minute, int second) {
  74. NSDateComponents *comps = [[NSDateComponents alloc] init];
  75. comps.year = year;
  76. comps.month = month;
  77. comps.day = day;
  78. comps.hour = hour;
  79. comps.minute = minute;
  80. comps.second = second;
  81. // Force time zone to UTC to avoid these values changing due to daylight saving.
  82. comps.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  83. return comps;
  84. }
  85. FSTUserDataConverter *FSTTestUserDataConverter() {
  86. FSTUserDataConverter *converter =
  87. [[FSTUserDataConverter alloc] initWithDatabaseID:DatabaseId("project")
  88. preConverter:^id _Nullable(id _Nullable input) {
  89. return input;
  90. }];
  91. return converter;
  92. }
  93. FieldValue FSTTestFieldValue(id _Nullable value) {
  94. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  95. // HACK: We use parsedQueryValue: since it accepts scalars as well as arrays / objects, and
  96. // our tests currently use FSTTestFieldValue() pretty generically so we don't know the intent.
  97. return [converter parsedQueryValue:value];
  98. }
  99. ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data) {
  100. FieldValue wrapped = FSTTestFieldValue(data);
  101. HARD_ASSERT(wrapped.type() == FieldValue::Type::Object, "Unsupported value: %s", data);
  102. return ObjectValue(std::move(wrapped));
  103. }
  104. DocumentKey FSTTestDocKey(NSString *path) {
  105. return DocumentKey::FromPathString(util::MakeString(path));
  106. }
  107. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string database, NSString *path) {
  108. return [[FSTDocumentKeyReference alloc] initWithKey:FSTTestDocKey(path)
  109. databaseID:DatabaseId(projectID, database)];
  110. }
  111. SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values) {
  112. return SetMutation(FSTTestDocKey(path), FSTTestObjectValue(values), Precondition::None());
  113. }
  114. PatchMutation FSTTestPatchMutation(const absl::string_view path,
  115. NSDictionary<NSString *, id> *values,
  116. const std::vector<FieldPath> &updateMask) {
  117. BOOL merge = !updateMask.empty();
  118. __block ObjectValue objectValue = ObjectValue::Empty();
  119. __block std::set<FieldPath> fieldMaskPaths;
  120. [values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *) {
  121. const FieldPath path = testutil::Field(util::MakeString(key));
  122. fieldMaskPaths.insert(path);
  123. if (![value isEqual:kDeleteSentinel]) {
  124. FieldValue parsedValue = FSTTestFieldValue(value);
  125. objectValue = objectValue.Set(path, std::move(parsedValue));
  126. }
  127. }];
  128. DocumentKey key = testutil::Key(path);
  129. Precondition precondition = merge ? Precondition::None() : Precondition::Exists(true);
  130. FieldMask mask(merge ? std::set<FieldPath>(updateMask.begin(), updateMask.end())
  131. : fieldMaskPaths);
  132. return PatchMutation(key, objectValue, mask, precondition);
  133. }
  134. TransformMutation FSTTestTransformMutation(NSString *path, NSDictionary<NSString *, id> *data) {
  135. DocumentKey key{testutil::Resource(util::MakeString(path))};
  136. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  137. ParsedUpdateData result = [converter parsedUpdateData:data];
  138. HARD_ASSERT(result.data().size() == 0,
  139. "FSTTestTransformMutation() only expects transforms; no other data");
  140. return TransformMutation(key, result.field_transforms());
  141. }
  142. DeleteMutation FSTTestDeleteMutation(NSString *path) {
  143. return DeleteMutation(FSTTestDocKey(path), Precondition::None());
  144. }
  145. NS_ASSUME_NONNULL_END