FSTHelpers.mm 5.9 KB

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