FSTHelpers.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <map>
  18. #include <vector>
  19. #import "Firestore/Source/Core/FSTTypes.h"
  20. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  21. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  22. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  23. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  24. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  25. #include "absl/strings/string_view.h"
  26. @class FIRGeoPoint;
  27. @class FSTDeleteMutation;
  28. @class FSTDeletedDocument;
  29. @class FSTDocument;
  30. @class FSTDocumentKeyReference;
  31. @class FSTDocumentSet;
  32. @class FSTFieldValue;
  33. @class FSTLocalViewChanges;
  34. @class FSTPatchMutation;
  35. @class FSTQuery;
  36. @class FSTRemoteEvent;
  37. @class FSTSetMutation;
  38. @class FSTSnapshotVersion;
  39. @class FSTSortOrder;
  40. @class FSTTargetChange;
  41. @class FIRTimestamp;
  42. @class FSTTransformMutation;
  43. @class FSTView;
  44. @class FSTViewSnapshot;
  45. @class FSTObjectValue;
  46. @protocol FSTFilter;
  47. NS_ASSUME_NONNULL_BEGIN
  48. #if __cplusplus
  49. extern "C" {
  50. #endif
  51. #define FSTAssertIsKindOfClass(value, classType) \
  52. do { \
  53. XCTAssertEqualObjects([value class], [classType class]); \
  54. } while (0);
  55. /**
  56. * Asserts that the given NSSet of FSTDocumentKeys contains exactly the given expected keys.
  57. * This is a macro instead of a method so that the failure shows up on the right line.
  58. *
  59. * @param actualSet An NSSet of FSTDocumentKeys.
  60. * @param expectedArray A sorted array of keys that actualSet must be equal to (after converting
  61. * to an array and sorting).
  62. */
  63. #define FSTAssertEqualSets(actualSet, expectedArray) \
  64. do { \
  65. NSArray<FSTDocumentKey *> *actual = [(actualSet)allObjects]; \
  66. actual = [actual sortedArrayUsingSelector:@selector(compare:)]; \
  67. XCTAssertEqualObjects(actual, (expectedArray)); \
  68. } while (0)
  69. /**
  70. * Takes an array of "equality group" arrays and asserts that the compare: selector returns the
  71. * same as compare: on the indexes of the "equality groups" (NSOrderedSame for items in the same
  72. * group).
  73. */
  74. #define FSTAssertComparisons(values) \
  75. do { \
  76. for (NSUInteger i = 0; i < [values count]; i++) { \
  77. for (id left in values[i]) { \
  78. for (NSUInteger j = 0; j < [values count]; j++) { \
  79. for (id right in values[j]) { \
  80. NSComparisonResult expected = [@(i) compare:@(j)]; \
  81. NSComparisonResult result = [left compare:right]; \
  82. NSComparisonResult inverseResult = [right compare:left]; \
  83. XCTAssertEqual(result, expected, @"comparing %@ with %@ at (%lu, %lu)", left, right, \
  84. i, j); \
  85. XCTAssertEqual(inverseResult, -expected, @"comparing %@ with %@ at (%lu, %lu)", right, \
  86. left, j, i); \
  87. } \
  88. } \
  89. } \
  90. } \
  91. } while (0)
  92. /**
  93. * Takes an array of "equality group" arrays and asserts that the isEqual: selector returns TRUE
  94. * if-and-only-if items are in the same group.
  95. *
  96. * Additionally checks that the hash: selector returns the same value for items in the same group.
  97. */
  98. #define FSTAssertEqualityGroups(values) \
  99. do { \
  100. for (NSUInteger i = 0; i < [values count]; i++) { \
  101. for (id left in values[i]) { \
  102. for (NSUInteger j = 0; j < [values count]; j++) { \
  103. for (id right in values[j]) { \
  104. if (i == j) { \
  105. XCTAssertEqualObjects(left, right); \
  106. XCTAssertEqual([left hash], [right hash], @"comparing hash of %@ with hash of %@", \
  107. left, right); \
  108. } else { \
  109. XCTAssertNotEqualObjects(left, right); \
  110. } \
  111. } \
  112. } \
  113. } \
  114. } \
  115. } while (0)
  116. static NSString *kExceptionPrefix = @"FIRESTORE INTERNAL ASSERTION FAILED: ";
  117. // Remove possible exception-prefix.
  118. inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
  119. if ([exception hasPrefix:kExceptionPrefix]) {
  120. return [exception substringFromIndex:kExceptionPrefix.length];
  121. } else {
  122. return exception;
  123. }
  124. }
  125. // Helper for validating API exceptions.
  126. #define FSTAssertThrows(expression, exceptionReason, ...) \
  127. do { \
  128. BOOL didThrow = NO; \
  129. @try { \
  130. (void)(expression); \
  131. } @catch (NSException * exception) { \
  132. didThrow = YES; \
  133. XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
  134. FSTRemoveExceptionPrefix(exceptionReason)); \
  135. } \
  136. XCTAssertTrue(didThrow, ##__VA_ARGS__); \
  137. } while (0)
  138. /** Creates a new FIRTimestamp from components. Note that year, month, and day are all one-based. */
  139. FIRTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second);
  140. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  141. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  142. /**
  143. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  144. */
  145. NSData *FSTTestData(int bytes, ...);
  146. // Note that FIRGeoPoint is a model class in addition to an API class, so we put this helper here
  147. // instead of FSTAPIHelpers.h
  148. /** Creates a new GeoPoint from the latitude and longitude values */
  149. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  150. /**
  151. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  152. * one-based.
  153. */
  154. NSDateComponents *FSTTestDateComponents(
  155. int year, int month, int day, int hour, int minute, int second);
  156. /** Wraps a plain value into an FSTFieldValue instance. */
  157. FSTFieldValue *FSTTestFieldValue(id _Nullable value);
  158. /** Wraps a NSDictionary value into an FSTObjectValue instance. */
  159. FSTObjectValue *FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  160. /** A convenience method for creating document keys for tests. */
  161. FSTDocumentKey *FSTTestDocKey(NSString *path);
  162. /** A convenience method for creating a document key set for tests. */
  163. FSTDocumentKeySet *FSTTestDocKeySet(NSArray<FSTDocumentKey *> *keys);
  164. /** Allow tests to just use an int literal for versions. */
  165. typedef int64_t FSTTestSnapshotVersion;
  166. /** A convenience method for creating snapshot versions for tests. */
  167. FSTSnapshotVersion *FSTTestVersion(FSTTestSnapshotVersion version);
  168. /** A convenience method for creating docs for tests. */
  169. FSTDocument *FSTTestDoc(const absl::string_view path,
  170. FSTTestSnapshotVersion version,
  171. NSDictionary<NSString *, id> *data,
  172. BOOL hasMutations);
  173. /** A convenience method for creating deleted docs for tests. */
  174. FSTDeletedDocument *FSTTestDeletedDoc(const absl::string_view path, FSTTestSnapshotVersion version);
  175. /**
  176. * A convenience method for creating a document reference from a path string.
  177. */
  178. FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
  179. const absl::string_view databaseID,
  180. NSString *path);
  181. /** A convenience method for creating a query for the given path (without any other filters). */
  182. FSTQuery *FSTTestQuery(const absl::string_view path);
  183. /**
  184. * A convenience method to create a FSTFilter using a string representation for both field
  185. * and operator (<, <=, ==, >=, >, array_contains).
  186. */
  187. id<FSTFilter> FSTTestFilter(const absl::string_view field, NSString *op, id value);
  188. /** A convenience method for creating sort orders. */
  189. FSTSortOrder *FSTTestOrderBy(const absl::string_view field, NSString *direction);
  190. /**
  191. * Creates an NSComparator that will compare FSTDocuments by the given fieldPath string then by
  192. * key.
  193. */
  194. NSComparator FSTTestDocComparator(const absl::string_view fieldPath);
  195. /**
  196. * Creates a FSTDocumentSet based on the given comparator, initially containing the given
  197. * documents.
  198. */
  199. FSTDocumentSet *FSTTestDocSet(NSComparator comp, NSArray<FSTDocument *> *docs);
  200. /** Computes changes to the view with the docs and then applies them and returns the snapshot. */
  201. FSTViewSnapshot *_Nullable FSTTestApplyChanges(FSTView *view,
  202. NSArray<FSTMaybeDocument *> *docs,
  203. FSTTargetChange *_Nullable targetChange);
  204. /** Creates a set mutation for the document key at the given path. */
  205. FSTSetMutation *FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  206. /** Creates a patch mutation for the document key at the given path. */
  207. FSTPatchMutation *FSTTestPatchMutation(
  208. const absl::string_view path,
  209. NSDictionary<NSString *, id> *values,
  210. const std::vector<firebase::firestore::model::FieldPath> &updateMask);
  211. /**
  212. * Creates a FSTTransformMutation by parsing any FIRFieldValue sentinels in the provided data. The
  213. * data is expected to use dotted-notation for nested fields (i.e.
  214. * @{ @"foo.bar": [FIRFieldValue ...] } and must not contain any non-sentinel data.
  215. */
  216. FSTTransformMutation *FSTTestTransformMutation(NSString *path, NSDictionary<NSString *, id> *data);
  217. /** Creates a delete mutation for the document key at the given path. */
  218. FSTDeleteMutation *FSTTestDeleteMutation(NSString *path);
  219. /** Converts a list of documents to a sorted map. */
  220. FSTMaybeDocumentDictionary *FSTTestDocUpdates(NSArray<FSTMaybeDocument *> *docs);
  221. /** Creates a remote event with changes to a document. */
  222. FSTRemoteEvent *FSTTestUpdateRemoteEvent(FSTMaybeDocument *doc,
  223. NSArray<NSNumber *> *updatedInTargets,
  224. NSArray<NSNumber *> *removedFromTargets);
  225. /** Creates a test view changes. */
  226. FSTLocalViewChanges *FSTTestViewChanges(FSTQuery *query,
  227. NSArray<NSString *> *addedKeys,
  228. NSArray<NSString *> *removedKeys);
  229. /** Creates a resume token to match the given snapshot version. */
  230. NSData *_Nullable FSTTestResumeTokenFromSnapshotVersion(FSTTestSnapshotVersion watchSnapshot);
  231. #if __cplusplus
  232. } // extern "C"
  233. #endif
  234. NS_ASSUME_NONNULL_END