FSTHelpers.mm 6.0 KB

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