FSTHelpers.mm 5.9 KB

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