FSTHelpers.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <Foundation/Foundation.h>
  17. #include <string>
  18. #include <unordered_map>
  19. #include <vector>
  20. #include "Firestore/core/src/firebase/firestore/core/filter.h"
  21. #include "Firestore/core/src/firebase/firestore/core/view.h"
  22. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  23. #include "Firestore/core/src/firebase/firestore/local/local_view_changes.h"
  24. #include "Firestore/core/src/firebase/firestore/local/query_data.h"
  25. #include "Firestore/core/src/firebase/firestore/model/delete_mutation.h"
  26. #include "Firestore/core/src/firebase/firestore/model/document.h"
  27. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  28. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  29. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  30. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  31. #include "Firestore/core/src/firebase/firestore/model/maybe_document.h"
  32. #include "Firestore/core/src/firebase/firestore/model/mutation.h"
  33. #include "Firestore/core/src/firebase/firestore/model/no_document.h"
  34. #include "Firestore/core/src/firebase/firestore/model/patch_mutation.h"
  35. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  36. #include "Firestore/core/src/firebase/firestore/model/set_mutation.h"
  37. #include "Firestore/core/src/firebase/firestore/model/transform_mutation.h"
  38. #include "Firestore/core/src/firebase/firestore/model/types.h"
  39. #include "Firestore/core/src/firebase/firestore/model/unknown_document.h"
  40. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  41. #include "absl/strings/string_view.h"
  42. #include "absl/types/optional.h"
  43. @class FIRGeoPoint;
  44. @class FIRTimestamp;
  45. @class FSTDocumentKeyReference;
  46. @class FSTUserDataConverter;
  47. namespace firebase {
  48. namespace firestore {
  49. namespace remote {
  50. class RemoteEvent;
  51. } // namespace remote
  52. } // namespace firestore
  53. } // namespace firebase
  54. namespace core = firebase::firestore::core;
  55. namespace local = firebase::firestore::local;
  56. namespace model = firebase::firestore::model;
  57. NS_ASSUME_NONNULL_BEGIN
  58. #define FSTAssertIsKindOfClass(value, classType) \
  59. do { \
  60. XCTAssertEqualObjects([value class], [classType class]); \
  61. } while (0);
  62. /**
  63. * Takes an array of "equality group" arrays and asserts that the compare: selector returns the
  64. * same as compare: on the indexes of the "equality groups" (NSOrderedSame for items in the same
  65. * group).
  66. */
  67. #define FSTAssertComparisons(values) \
  68. do { \
  69. for (NSUInteger i = 0; i < [values count]; i++) { \
  70. for (id left in values[i]) { \
  71. for (NSUInteger j = 0; j < [values count]; j++) { \
  72. for (id right in values[j]) { \
  73. NSComparisonResult expected = [@(i) compare:@(j)]; \
  74. NSComparisonResult result = [left compare:right]; \
  75. NSComparisonResult inverseResult = [right compare:left]; \
  76. XCTAssertEqual(result, expected, @"comparing %@ with %@ at (%lu, %lu)", left, right, \
  77. (unsigned long)i, (unsigned long)j); \
  78. XCTAssertEqual(inverseResult, -expected, @"comparing %@ with %@ at (%lu, %lu)", right, \
  79. left, (unsigned long)j, (unsigned long)i); \
  80. } \
  81. } \
  82. } \
  83. } \
  84. } while (0)
  85. /**
  86. * Takes an array of "equality group" arrays and asserts that the isEqual: selector returns TRUE
  87. * if-and-only-if items are in the same group.
  88. *
  89. * Additionally checks that the hash: selector returns the same value for items in the same group.
  90. */
  91. #define FSTAssertEqualityGroups(values) \
  92. do { \
  93. for (NSUInteger i = 0; i < [values count]; i++) { \
  94. for (id left in values[i]) { \
  95. for (NSUInteger j = 0; j < [values count]; j++) { \
  96. for (id right in values[j]) { \
  97. if (i == j) { \
  98. XCTAssertEqualObjects(left, right); \
  99. XCTAssertEqual([left hash], [right hash], @"comparing hash of %@ with hash of %@", \
  100. left, right); \
  101. } else { \
  102. XCTAssertNotEqualObjects(left, right); \
  103. } \
  104. } \
  105. } \
  106. } \
  107. } \
  108. } while (0)
  109. static NSString *kExceptionPrefix = @"FIRESTORE INTERNAL ASSERTION FAILED: ";
  110. // Remove possible exception-prefix.
  111. inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
  112. if ([exception hasPrefix:kExceptionPrefix]) {
  113. return [exception substringFromIndex:kExceptionPrefix.length];
  114. } else {
  115. return exception;
  116. }
  117. }
  118. // Helper for validating API exceptions.
  119. #define FSTAssertThrows(expression, exceptionReason, ...) \
  120. do { \
  121. BOOL didThrow = NO; \
  122. @try { \
  123. (void)(expression); \
  124. } @catch (NSException * exception) { \
  125. didThrow = YES; \
  126. XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
  127. FSTRemoveExceptionPrefix(exceptionReason)); \
  128. } \
  129. XCTAssertTrue(didThrow, ##__VA_ARGS__); \
  130. } while (0)
  131. // Helper to compare vectors containing Objective-C objects.
  132. #define FSTAssertEqualVectors(v1, v2) \
  133. do { \
  134. XCTAssertEqual(v1.size(), v2.size(), @"Vector length mismatch"); \
  135. for (size_t i = 0; i < v1.size(); i++) { \
  136. XCTAssertEqualObjects(v1[i], v2[i]); \
  137. } \
  138. } while (0)
  139. /** Creates a new FIRTimestamp from components. Note that year, month, and day are all one-based. */
  140. FIRTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second);
  141. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  142. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  143. /**
  144. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  145. */
  146. NSData *FSTTestData(int bytes, ...);
  147. // Note that FIRGeoPoint is a model class in addition to an API class, so we put this helper here
  148. // instead of FSTAPIHelpers.h
  149. /** Creates a new GeoPoint from the latitude and longitude values */
  150. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  151. /** Creates a user data converter set up for a generic project. */
  152. FSTUserDataConverter *FSTTestUserDataConverter();
  153. /**
  154. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  155. * one-based.
  156. */
  157. NSDateComponents *FSTTestDateComponents(
  158. int year, int month, int day, int hour, int minute, int second);
  159. /** Wraps a plain value into an FieldValue instance. */
  160. model::FieldValue FSTTestFieldValue(id _Nullable value);
  161. /** Wraps a NSDictionary value into an ObjectValue instance. */
  162. model::ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  163. /** A convenience method for creating document keys for tests. */
  164. firebase::firestore::model::DocumentKey FSTTestDocKey(NSString *path);
  165. /** Allow tests to just use an int literal for versions. */
  166. typedef int64_t FSTTestSnapshotVersion;
  167. /**
  168. * A convenience method for creating a document reference from a path string.
  169. */
  170. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string databaseID, NSString *path);
  171. /** Computes changes to the view with the docs and then applies them and returns the snapshot. */
  172. absl::optional<core::ViewSnapshot> FSTTestApplyChanges(
  173. core::View *view,
  174. const std::vector<model::MaybeDocument> &docs,
  175. const absl::optional<firebase::firestore::remote::TargetChange> &targetChange);
  176. /** Creates a set mutation for the document key at the given path. */
  177. model::SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  178. /** Creates a patch mutation for the document key at the given path. */
  179. model::PatchMutation FSTTestPatchMutation(
  180. absl::string_view path,
  181. NSDictionary<NSString *, id> *values,
  182. const std::vector<firebase::firestore::model::FieldPath> &updateMask);
  183. /**
  184. * Creates a TransformMutation by parsing any FIRFieldValue sentinels in the provided data. The
  185. * data is expected to use dotted-notation for nested fields (i.e.
  186. * @{ @"foo.bar": [FIRFieldValue ...] } and must not contain any non-sentinel data.
  187. */
  188. model::TransformMutation FSTTestTransformMutation(NSString *path,
  189. NSDictionary<NSString *, id> *data);
  190. /** Creates a delete mutation for the document key at the given path. */
  191. model::DeleteMutation FSTTestDeleteMutation(NSString *path);
  192. /** Converts a list of documents to a sorted map. */
  193. firebase::firestore::model::MaybeDocumentMap FSTTestDocUpdates(
  194. const std::vector<model::MaybeDocument> &docs);
  195. /** Creates a remote event that inserts a new document. */
  196. firebase::firestore::remote::RemoteEvent FSTTestAddedRemoteEvent(
  197. const model::MaybeDocument &doc,
  198. const std::vector<firebase::firestore::model::TargetId> &addedToTargets);
  199. /** Creates a remote event that inserts a list of documents. */
  200. firebase::firestore::remote::RemoteEvent FSTTestAddedRemoteEvent(
  201. const std::vector<model::MaybeDocument> &doc,
  202. const std::vector<firebase::firestore::model::TargetId> &addedToTargets);
  203. /** Creates a remote event with changes to a document. */
  204. firebase::firestore::remote::RemoteEvent FSTTestUpdateRemoteEvent(
  205. const model::MaybeDocument &doc,
  206. const std::vector<firebase::firestore::model::TargetId> &updatedInTargets,
  207. const std::vector<firebase::firestore::model::TargetId> &removedFromTargets);
  208. /** Creates a remote event with changes to a document. Allows for identifying limbo targets */
  209. firebase::firestore::remote::RemoteEvent FSTTestUpdateRemoteEventWithLimboTargets(
  210. const model::MaybeDocument &doc,
  211. const std::vector<firebase::firestore::model::TargetId> &updatedInTargets,
  212. const std::vector<firebase::firestore::model::TargetId> &removedFromTargets,
  213. const std::vector<firebase::firestore::model::TargetId> &limboTargets);
  214. /** Creates a test view changes. */
  215. local::LocalViewChanges TestViewChanges(firebase::firestore::model::TargetId targetID,
  216. NSArray<NSString *> *addedKeys,
  217. NSArray<NSString *> *removedKeys);
  218. /** Creates a test target change that acks all 'docs' and marks the target as CURRENT */
  219. firebase::firestore::remote::TargetChange FSTTestTargetChangeAckDocuments(
  220. firebase::firestore::model::DocumentKeySet docs);
  221. /** Creates a test target change that marks the target as CURRENT */
  222. firebase::firestore::remote::TargetChange FSTTestTargetChangeMarkCurrent();
  223. /** Creates a resume token to match the given snapshot version. */
  224. NSData *_Nullable FSTTestResumeTokenFromSnapshotVersion(FSTTestSnapshotVersion watchSnapshot);
  225. NS_ASSUME_NONNULL_END