FSTMutationTests.mm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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/Source/Model/FSTMutation.h"
  17. #import <FirebaseFirestore/FIRFieldValue.h>
  18. #import <FirebaseFirestore/FIRTimestamp.h>
  19. #import <XCTest/XCTest.h>
  20. #include <vector>
  21. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  22. #import "Firestore/Source/Model/FSTDocument.h"
  23. #import "Firestore/Source/Model/FSTFieldValue.h"
  24. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  25. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  26. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  27. #include "Firestore/core/src/firebase/firestore/model/field_transform.h"
  28. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  29. #include "Firestore/core/src/firebase/firestore/model/transform_operations.h"
  30. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  31. namespace testutil = firebase::firestore::testutil;
  32. using firebase::firestore::model::ArrayTransform;
  33. using firebase::firestore::model::DocumentKey;
  34. using firebase::firestore::model::FieldMask;
  35. using firebase::firestore::model::FieldPath;
  36. using firebase::firestore::model::FieldTransform;
  37. using firebase::firestore::model::Precondition;
  38. using firebase::firestore::model::TransformOperation;
  39. @interface FSTMutationTests : XCTestCase
  40. @end
  41. @implementation FSTMutationTests {
  42. FIRTimestamp *_timestamp;
  43. }
  44. - (void)setUp {
  45. _timestamp = [FIRTimestamp timestamp];
  46. }
  47. - (void)testAppliesSetsToDocuments {
  48. NSDictionary *docData = @{@"foo" : @"foo-value", @"baz" : @"baz-value"};
  49. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  50. FSTMutation *set = FSTTestSetMutation(@"collection/key", @{@"bar" : @"bar-value"});
  51. FSTMaybeDocument *setDoc = [set applyToLocalDocument:baseDoc
  52. baseDocument:baseDoc
  53. localWriteTime:_timestamp];
  54. NSDictionary *expectedData = @{@"bar" : @"bar-value"};
  55. XCTAssertEqualObjects(
  56. setDoc, FSTTestDoc("collection/key", 0, expectedData, FSTDocumentStateLocalMutations));
  57. }
  58. - (void)testAppliesPatchesToDocuments {
  59. NSDictionary *docData = @{@"foo" : @{@"bar" : @"bar-value"}, @"baz" : @"baz-value"};
  60. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  61. FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo.bar" : @"new-bar-value"}, {});
  62. FSTMaybeDocument *patchedDoc = [patch applyToLocalDocument:baseDoc
  63. baseDocument:baseDoc
  64. localWriteTime:_timestamp];
  65. NSDictionary *expectedData = @{@"foo" : @{@"bar" : @"new-bar-value"}, @"baz" : @"baz-value"};
  66. XCTAssertEqualObjects(
  67. patchedDoc, FSTTestDoc("collection/key", 0, expectedData, FSTDocumentStateLocalMutations));
  68. }
  69. - (void)testDeletesValuesFromTheFieldMask {
  70. NSDictionary *docData = @{@"foo" : @{@"bar" : @"bar-value", @"baz" : @"baz-value"}};
  71. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  72. DocumentKey key = testutil::Key("collection/key");
  73. FSTMutation *patch = [[FSTPatchMutation alloc] initWithKey:key
  74. fieldMask:{testutil::Field("foo.bar")}
  75. value:[FSTObjectValue objectValue]
  76. precondition:Precondition::None()];
  77. FSTMaybeDocument *patchedDoc = [patch applyToLocalDocument:baseDoc
  78. baseDocument:baseDoc
  79. localWriteTime:_timestamp];
  80. NSDictionary *expectedData = @{@"foo" : @{@"baz" : @"baz-value"}};
  81. XCTAssertEqualObjects(
  82. patchedDoc, FSTTestDoc("collection/key", 0, expectedData, FSTDocumentStateLocalMutations));
  83. }
  84. - (void)testPatchesPrimitiveValue {
  85. NSDictionary *docData = @{@"foo" : @"foo-value", @"baz" : @"baz-value"};
  86. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  87. FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo.bar" : @"new-bar-value"}, {});
  88. FSTMaybeDocument *patchedDoc = [patch applyToLocalDocument:baseDoc
  89. baseDocument:baseDoc
  90. localWriteTime:_timestamp];
  91. NSDictionary *expectedData = @{@"foo" : @{@"bar" : @"new-bar-value"}, @"baz" : @"baz-value"};
  92. XCTAssertEqualObjects(
  93. patchedDoc, FSTTestDoc("collection/key", 0, expectedData, FSTDocumentStateLocalMutations));
  94. }
  95. - (void)testPatchingDeletedDocumentsDoesNothing {
  96. FSTMaybeDocument *baseDoc = FSTTestDeletedDoc("collection/key", 0, NO);
  97. FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo" : @"bar"}, {});
  98. FSTMaybeDocument *patchedDoc = [patch applyToLocalDocument:baseDoc
  99. baseDocument:baseDoc
  100. localWriteTime:_timestamp];
  101. XCTAssertEqualObjects(patchedDoc, baseDoc);
  102. }
  103. - (void)testAppliesLocalServerTimestampTransformToDocuments {
  104. NSDictionary *docData = @{@"foo" : @{@"bar" : @"bar-value"}, @"baz" : @"baz-value"};
  105. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  106. FSTMutation *transform = FSTTestTransformMutation(
  107. @"collection/key", @{@"foo.bar" : [FIRFieldValue fieldValueForServerTimestamp]});
  108. FSTMaybeDocument *transformedDoc = [transform applyToLocalDocument:baseDoc
  109. baseDocument:baseDoc
  110. localWriteTime:_timestamp];
  111. // Server timestamps aren't parsed, so we manually insert it.
  112. FSTObjectValue *expectedData =
  113. FSTTestObjectValue(@{@"foo" : @{@"bar" : @"<server-timestamp>"}, @"baz" : @"baz-value"});
  114. expectedData =
  115. [expectedData objectBySettingValue:[FSTServerTimestampValue
  116. serverTimestampValueWithLocalWriteTime:_timestamp
  117. previousValue:nil]
  118. forPath:testutil::Field("foo.bar")];
  119. FSTDocument *expectedDoc = [FSTDocument documentWithData:expectedData
  120. key:FSTTestDocKey(@"collection/key")
  121. version:testutil::Version(0)
  122. state:FSTDocumentStateLocalMutations];
  123. XCTAssertEqualObjects(transformedDoc, expectedDoc);
  124. }
  125. - (void)testAppliesIncrementTransformToDocument {
  126. NSDictionary *baseDoc = @{
  127. @"longPlusLong" : @1,
  128. @"longPlusDouble" : @2,
  129. @"doublePlusLong" : @3.3,
  130. @"doublePlusDouble" : @4.0,
  131. @"longPlusNan" : @5,
  132. @"doublePlusNan" : @6.6,
  133. @"longPlusInfinity" : @7,
  134. @"doublePlusInfinity" : @8.8
  135. };
  136. NSDictionary *transform = @{
  137. @"longPlusLong" : [FIRFieldValue fieldValueForIntegerIncrement:1],
  138. @"longPlusDouble" : [FIRFieldValue fieldValueForDoubleIncrement:2.2],
  139. @"doublePlusLong" : [FIRFieldValue fieldValueForIntegerIncrement:3],
  140. @"doublePlusDouble" : [FIRFieldValue fieldValueForDoubleIncrement:4.4],
  141. @"longPlusNan" : [FIRFieldValue fieldValueForDoubleIncrement:NAN],
  142. @"doublePlusNan" : [FIRFieldValue fieldValueForDoubleIncrement:NAN],
  143. @"longPlusInfinity" : [FIRFieldValue fieldValueForDoubleIncrement:INFINITY],
  144. @"doublePlusInfinity" : [FIRFieldValue fieldValueForDoubleIncrement:INFINITY]
  145. };
  146. NSDictionary *expected = @{
  147. @"longPlusLong" : @2L,
  148. @"longPlusDouble" : @4.2,
  149. @"doublePlusLong" : @6.3,
  150. @"doublePlusDouble" : @8.4,
  151. @"longPlusNan" : @(NAN),
  152. @"doublePlusNan" : @(NAN),
  153. @"longPlusInfinity" : @(INFINITY),
  154. @"doublePlusInfinity" : @(INFINITY)
  155. };
  156. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  157. }
  158. - (void)testAppliesIncrementTransformToUnexpectedType {
  159. NSDictionary *baseDoc = @{@"string" : @"zero"};
  160. NSDictionary *transform = @{@"string" : [FIRFieldValue fieldValueForIntegerIncrement:1]};
  161. NSDictionary *expected = @{@"string" : @1};
  162. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  163. }
  164. - (void)testAppliesIncrementTransformToMissingField {
  165. NSDictionary *baseDoc = @{};
  166. NSDictionary *transform = @{@"missing" : [FIRFieldValue fieldValueForIntegerIncrement:1]};
  167. NSDictionary *expected = @{@"missing" : @1};
  168. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  169. }
  170. - (void)testAppliesIncrementTransformsConsecutively {
  171. NSDictionary *baseDoc = @{@"number" : @1};
  172. NSDictionary *transform1 = @{@"number" : [FIRFieldValue fieldValueForIntegerIncrement:2]};
  173. NSDictionary *transform2 = @{@"number" : [FIRFieldValue fieldValueForIntegerIncrement:3]};
  174. NSDictionary *transform3 = @{@"number" : [FIRFieldValue fieldValueForIntegerIncrement:4]};
  175. NSDictionary *expected = @{@"number" : @10};
  176. [self transformBaseDoc:baseDoc
  177. applyTransforms:@[ transform1, transform2, transform3 ]
  178. expecting:expected];
  179. }
  180. - (void)testAppliesIncrementWithoutOverflow {
  181. NSDictionary *baseDoc =
  182. @{@"a" : @(LONG_MAX - 1), @"b" : @(LONG_MAX - 1), @"c" : @(LONG_MAX), @"d" : @(LONG_MAX)};
  183. NSDictionary *transform = @{
  184. @"a" : [FIRFieldValue fieldValueForIntegerIncrement:1],
  185. @"b" : [FIRFieldValue fieldValueForIntegerIncrement:LONG_MAX],
  186. @"c" : [FIRFieldValue fieldValueForIntegerIncrement:1],
  187. @"d" : [FIRFieldValue fieldValueForIntegerIncrement:LONG_MAX]
  188. };
  189. NSDictionary *expected =
  190. @{@"a" : @LONG_MAX, @"b" : @LONG_MAX, @"c" : @LONG_MAX, @"d" : @LONG_MAX};
  191. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  192. }
  193. - (void)testAppliesIncrementWithoutUnderflow {
  194. NSDictionary *baseDoc =
  195. @{@"a" : @(LONG_MIN + 1), @"b" : @(LONG_MIN + 1), @"c" : @(LONG_MIN), @"d" : @(LONG_MIN)};
  196. NSDictionary *transform = @{
  197. @"a" : [FIRFieldValue fieldValueForIntegerIncrement:-1],
  198. @"b" : [FIRFieldValue fieldValueForIntegerIncrement:LONG_MIN],
  199. @"c" : [FIRFieldValue fieldValueForIntegerIncrement:-1],
  200. @"d" : [FIRFieldValue fieldValueForIntegerIncrement:LONG_MIN]
  201. };
  202. NSDictionary *expected =
  203. @{@"a" : @(LONG_MIN), @"b" : @(LONG_MIN), @"c" : @(LONG_MIN), @"d" : @(LONG_MIN)};
  204. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  205. }
  206. // NOTE: This is more a test of FSTUserDataConverter code than FSTMutation code but we don't have
  207. // unit tests for it currently. We could consider removing this test once we have integration tests.
  208. - (void)testCreateArrayUnionTransform {
  209. FSTTransformMutation *transform = FSTTestTransformMutation(@"collection/key", @{
  210. @"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @"tag" ]],
  211. @"bar.baz" :
  212. [FIRFieldValue fieldValueForArrayUnion:@[ @YES, @{@"nested" : @{@"a" : @[ @1, @2 ]}} ]]
  213. });
  214. XCTAssertEqual(transform.fieldTransforms.size(), 2);
  215. const FieldTransform &first = transform.fieldTransforms[0];
  216. XCTAssertEqual(first.path(), FieldPath({"foo"}));
  217. {
  218. std::vector<FSTFieldValue *> expectedElements{FSTTestFieldValue(@"tag")};
  219. ArrayTransform expected(TransformOperation::Type::ArrayUnion, expectedElements);
  220. XCTAssertEqual(static_cast<const ArrayTransform &>(first.transformation()), expected);
  221. }
  222. const FieldTransform &second = transform.fieldTransforms[1];
  223. XCTAssertEqual(second.path(), FieldPath({"bar", "baz"}));
  224. {
  225. std::vector<FSTFieldValue *> expectedElements {
  226. FSTTestFieldValue(@YES), FSTTestFieldValue(@{@"nested" : @{@"a" : @[ @1, @2 ]}})
  227. };
  228. ArrayTransform expected(TransformOperation::Type::ArrayUnion, expectedElements);
  229. XCTAssertEqual(static_cast<const ArrayTransform &>(second.transformation()), expected);
  230. }
  231. }
  232. // NOTE: This is more a test of FSTUserDataConverter code than FSTMutation code but we don't have
  233. // unit tests for it currently. We could consider removing this test once we have integration tests.
  234. - (void)testCreateArrayRemoveTransform {
  235. FSTTransformMutation *transform = FSTTestTransformMutation(@"collection/key", @{
  236. @"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @"tag" ]],
  237. });
  238. XCTAssertEqual(transform.fieldTransforms.size(), 1);
  239. const FieldTransform &first = transform.fieldTransforms[0];
  240. XCTAssertEqual(first.path(), FieldPath({"foo"}));
  241. {
  242. std::vector<FSTFieldValue *> expectedElements{FSTTestFieldValue(@"tag")};
  243. const ArrayTransform expected(TransformOperation::Type::ArrayRemove, expectedElements);
  244. XCTAssertEqual(static_cast<const ArrayTransform &>(first.transformation()), expected);
  245. }
  246. }
  247. - (void)testAppliesLocalArrayUnionTransformToMissingField {
  248. auto baseDoc = @{};
  249. auto transform = @{@"missing" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]};
  250. auto expected = @{@"missing" : @[ @1, @2 ]};
  251. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  252. }
  253. - (void)testAppliesLocalArrayUnionTransformToNonArrayField {
  254. auto baseDoc = @{@"non-array" : @42};
  255. auto transform = @{@"non-array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]};
  256. auto expected = @{@"non-array" : @[ @1, @2 ]};
  257. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  258. }
  259. - (void)testAppliesLocalArrayUnionTransformWithNonExistingElements {
  260. auto baseDoc = @{@"array" : @[ @1, @3 ]};
  261. auto transform = @{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @4 ]]};
  262. auto expected = @{@"array" : @[ @1, @3, @2, @4 ]};
  263. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  264. }
  265. - (void)testAppliesLocalArrayUnionTransformWithExistingElements {
  266. auto baseDoc = @{@"array" : @[ @1, @3 ]};
  267. auto transform = @{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @3 ]]};
  268. auto expected = @{@"array" : @[ @1, @3 ]};
  269. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  270. }
  271. - (void)testAppliesLocalArrayUnionTransformWithDuplicateExistingElements {
  272. // Duplicate entries in your existing array should be preserved.
  273. auto baseDoc = @{@"array" : @[ @1, @2, @2, @3 ]};
  274. auto transform = @{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2 ]]};
  275. auto expected = @{@"array" : @[ @1, @2, @2, @3 ]};
  276. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  277. }
  278. - (void)testAppliesLocalArrayUnionTransformWithDuplicateUnionElements {
  279. // Duplicate entries in your union array should only be added once.
  280. auto baseDoc = @{@"array" : @[ @1, @3 ]};
  281. auto transform = @{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @2 ]]};
  282. auto expected = @{@"array" : @[ @1, @3, @2 ]};
  283. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  284. }
  285. - (void)testAppliesLocalArrayUnionTransformWithNonPrimitiveElements {
  286. // Union nested object values (one existing, one not).
  287. auto baseDoc = @{@"array" : @[ @1, @{@"a" : @"b"} ]};
  288. auto transform =
  289. @{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @{@"a" : @"b"}, @{@"c" : @"d"} ]]};
  290. auto expected = @{@"array" : @[ @1, @{@"a" : @"b"}, @{@"c" : @"d"} ]};
  291. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  292. }
  293. - (void)testAppliesLocalArrayUnionTransformWithPartiallyOverlappingElements {
  294. // Union objects that partially overlap an existing object.
  295. auto baseDoc = @{@"array" : @[ @1, @{@"a" : @"b", @"c" : @"d"} ]};
  296. auto transform =
  297. @{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @{@"a" : @"b"}, @{@"c" : @"d"} ]]};
  298. auto expected =
  299. @{@"array" : @[ @1, @{@"a" : @"b", @"c" : @"d"}, @{@"a" : @"b"}, @{@"c" : @"d"} ]};
  300. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  301. }
  302. - (void)testAppliesLocalArrayRemoveTransformToMissingField {
  303. auto baseDoc = @{};
  304. auto transform = @{@"missing" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @2 ]]};
  305. auto expected = @{@"missing" : @[]};
  306. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  307. }
  308. - (void)testAppliesLocalArrayRemoveTransformToNonArrayField {
  309. auto baseDoc = @{@"non-array" : @42};
  310. auto transform = @{@"non-array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @2 ]]};
  311. auto expected = @{@"non-array" : @[]};
  312. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  313. }
  314. - (void)testAppliesLocalArrayRemoveTransformWithNonExistingElements {
  315. auto baseDoc = @{@"array" : @[ @1, @3 ]};
  316. auto transform = @{@"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @2, @4 ]]};
  317. auto expected = @{@"array" : @[ @1, @3 ]};
  318. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  319. }
  320. - (void)testAppliesLocalArrayRemoveTransformWithExistingElements {
  321. auto baseDoc = @{@"array" : @[ @1, @2, @3, @4 ]};
  322. auto transform = @{@"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @3 ]]};
  323. auto expected = @{@"array" : @[ @2, @4 ]};
  324. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  325. }
  326. - (void)testAppliesLocalArrayRemoveTransformWithNonPrimitiveElements {
  327. // Remove nested object values (one existing, one not).
  328. auto baseDoc = @{@"array" : @[ @1, @{@"a" : @"b"} ]};
  329. auto transform =
  330. @{@"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @{@"a" : @"b"}, @{@"c" : @"d"} ]]};
  331. auto expected = @{@"array" : @[ @1 ]};
  332. [self transformBaseDoc:baseDoc applyTransform:transform expecting:expected];
  333. }
  334. // Helper to test a particular transform scenario.
  335. - (void)transformBaseDoc:(NSDictionary<NSString *, id> *)baseData
  336. applyTransforms:(NSArray<NSDictionary<NSString *, id> *> *)transforms
  337. expecting:(NSDictionary<NSString *, id> *)expectedData {
  338. FSTMaybeDocument *currentDoc = FSTTestDoc("collection/key", 0, baseData, FSTDocumentStateSynced);
  339. for (NSDictionary<NSString *, id> *transformData in transforms) {
  340. FSTMutation *transform = FSTTestTransformMutation(@"collection/key", transformData);
  341. currentDoc = [transform applyToLocalDocument:currentDoc
  342. baseDocument:currentDoc
  343. localWriteTime:_timestamp];
  344. }
  345. FSTDocument *expectedDoc = [FSTDocument documentWithData:FSTTestObjectValue(expectedData)
  346. key:FSTTestDocKey(@"collection/key")
  347. version:testutil::Version(0)
  348. state:FSTDocumentStateLocalMutations];
  349. XCTAssertEqualObjects(currentDoc, expectedDoc);
  350. }
  351. - (void)transformBaseDoc:(NSDictionary<NSString *, id> *)baseData
  352. applyTransform:(NSDictionary<NSString *, id> *)transformData
  353. expecting:(NSDictionary<NSString *, id> *)expectedData {
  354. [self transformBaseDoc:baseData applyTransforms:@[ transformData ] expecting:expectedData];
  355. }
  356. - (void)testAppliesServerAckedIncrementTransformToDocuments {
  357. NSDictionary *docData = @{@"sum" : @1};
  358. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  359. FSTMutation *transform = FSTTestTransformMutation(
  360. @"collection/key", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]});
  361. FSTMutationResult *mutationResult =
  362. [[FSTMutationResult alloc] initWithVersion:testutil::Version(1)
  363. transformResults:@[ [FSTIntegerValue integerValue:3] ]];
  364. FSTMaybeDocument *transformedDoc = [transform applyToRemoteDocument:baseDoc
  365. mutationResult:mutationResult];
  366. NSDictionary *expectedData = @{@"sum" : @3};
  367. XCTAssertEqualObjects(transformedDoc, FSTTestDoc("collection/key", 1, expectedData,
  368. FSTDocumentStateCommittedMutations));
  369. }
  370. - (void)testAppliesServerAckedServerTimestampTransformToDocuments {
  371. NSDictionary *docData = @{@"foo" : @{@"bar" : @"bar-value"}, @"baz" : @"baz-value"};
  372. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  373. FSTMutation *transform = FSTTestTransformMutation(
  374. @"collection/key", @{@"foo.bar" : [FIRFieldValue fieldValueForServerTimestamp]});
  375. FSTMutationResult *mutationResult = [[FSTMutationResult alloc]
  376. initWithVersion:testutil::Version(1)
  377. transformResults:@[ [FSTTimestampValue timestampValue:_timestamp] ]];
  378. FSTMaybeDocument *transformedDoc = [transform applyToRemoteDocument:baseDoc
  379. mutationResult:mutationResult];
  380. NSDictionary *expectedData = @{@"foo" : @{@"bar" : _timestamp.dateValue}, @"baz" : @"baz-value"};
  381. XCTAssertEqualObjects(transformedDoc, FSTTestDoc("collection/key", 1, expectedData,
  382. FSTDocumentStateCommittedMutations));
  383. }
  384. - (void)testAppliesServerAckedArrayTransformsToDocuments {
  385. NSDictionary *docData = @{@"array_1" : @[ @1, @2 ], @"array_2" : @[ @"a", @"b" ]};
  386. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  387. FSTMutation *transform = FSTTestTransformMutation(@"collection/key", @{
  388. @"array_1" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @3 ]],
  389. @"array_2" : [FIRFieldValue fieldValueForArrayRemove:@[ @"a", @"c" ]]
  390. });
  391. // Server just sends null transform results for array operations.
  392. FSTMutationResult *mutationResult = [[FSTMutationResult alloc]
  393. initWithVersion:testutil::Version(1)
  394. transformResults:@[ [FSTNullValue nullValue], [FSTNullValue nullValue] ]];
  395. FSTMaybeDocument *transformedDoc = [transform applyToRemoteDocument:baseDoc
  396. mutationResult:mutationResult];
  397. NSDictionary *expectedData = @{@"array_1" : @[ @1, @2, @3 ], @"array_2" : @[ @"b" ]};
  398. XCTAssertEqualObjects(transformedDoc, FSTTestDoc("collection/key", 1, expectedData,
  399. FSTDocumentStateCommittedMutations));
  400. }
  401. - (void)testDeleteDeletes {
  402. NSDictionary *docData = @{@"foo" : @"bar"};
  403. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  404. FSTMutation *mutation = FSTTestDeleteMutation(@"collection/key");
  405. FSTMaybeDocument *result = [mutation applyToLocalDocument:baseDoc
  406. baseDocument:baseDoc
  407. localWriteTime:_timestamp];
  408. XCTAssertEqualObjects(result, FSTTestDeletedDoc("collection/key", 0, NO));
  409. }
  410. - (void)testSetWithMutationResult {
  411. NSDictionary *docData = @{@"foo" : @"bar"};
  412. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  413. FSTMutation *set = FSTTestSetMutation(@"collection/key", @{@"foo" : @"new-bar"});
  414. FSTMutationResult *mutationResult =
  415. [[FSTMutationResult alloc] initWithVersion:testutil::Version(4) transformResults:nil];
  416. FSTMaybeDocument *setDoc = [set applyToRemoteDocument:baseDoc mutationResult:mutationResult];
  417. NSDictionary *expectedData = @{@"foo" : @"new-bar"};
  418. XCTAssertEqualObjects(
  419. setDoc, FSTTestDoc("collection/key", 4, expectedData, FSTDocumentStateCommittedMutations));
  420. }
  421. - (void)testPatchWithMutationResult {
  422. NSDictionary *docData = @{@"foo" : @"bar"};
  423. FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, FSTDocumentStateSynced);
  424. FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo" : @"new-bar"}, {});
  425. FSTMutationResult *mutationResult =
  426. [[FSTMutationResult alloc] initWithVersion:testutil::Version(4) transformResults:nil];
  427. FSTMaybeDocument *patchedDoc = [patch applyToRemoteDocument:baseDoc
  428. mutationResult:mutationResult];
  429. NSDictionary *expectedData = @{@"foo" : @"new-bar"};
  430. XCTAssertEqualObjects(patchedDoc, FSTTestDoc("collection/key", 4, expectedData,
  431. FSTDocumentStateCommittedMutations));
  432. }
  433. #define ASSERT_VERSION_TRANSITION(mutation, base, result, expected) \
  434. do { \
  435. FSTMaybeDocument *actual = [mutation applyToRemoteDocument:base mutationResult:result]; \
  436. XCTAssertEqualObjects(actual, expected); \
  437. } while (0);
  438. /**
  439. * Tests the transition table documented in FSTMutation.h.
  440. */
  441. - (void)testTransitions {
  442. FSTDocument *docV3 = FSTTestDoc("collection/key", 3, @{}, FSTDocumentStateSynced);
  443. FSTDeletedDocument *deletedV3 = FSTTestDeletedDoc("collection/key", 3, NO);
  444. FSTMutation *setMutation = FSTTestSetMutation(@"collection/key", @{});
  445. FSTMutation *patchMutation = FSTTestPatchMutation("collection/key", @{}, {});
  446. FSTMutation *transformMutation = FSTTestTransformMutation(@"collection/key", @{});
  447. FSTMutation *deleteMutation = FSTTestDeleteMutation(@"collection/key");
  448. FSTDeletedDocument *docV7Deleted = FSTTestDeletedDoc("collection/key", 7, YES);
  449. FSTDocument *docV7Committed =
  450. FSTTestDoc("collection/key", 7, @{}, FSTDocumentStateCommittedMutations);
  451. FSTUnknownDocument *docV7Unknown = FSTTestUnknownDoc("collection/key", 7);
  452. FSTMutationResult *mutationResult =
  453. [[FSTMutationResult alloc] initWithVersion:testutil::Version(7) transformResults:nil];
  454. FSTMutationResult *transformResult =
  455. [[FSTMutationResult alloc] initWithVersion:testutil::Version(7) transformResults:@[]];
  456. ASSERT_VERSION_TRANSITION(setMutation, docV3, mutationResult, docV7Committed);
  457. ASSERT_VERSION_TRANSITION(setMutation, deletedV3, mutationResult, docV7Committed);
  458. ASSERT_VERSION_TRANSITION(setMutation, nil, mutationResult, docV7Committed);
  459. ASSERT_VERSION_TRANSITION(patchMutation, docV3, mutationResult, docV7Committed);
  460. ASSERT_VERSION_TRANSITION(patchMutation, deletedV3, mutationResult, docV7Unknown);
  461. ASSERT_VERSION_TRANSITION(patchMutation, nil, mutationResult, docV7Unknown);
  462. ASSERT_VERSION_TRANSITION(transformMutation, docV3, transformResult, docV7Committed);
  463. ASSERT_VERSION_TRANSITION(transformMutation, deletedV3, transformResult, docV7Unknown);
  464. ASSERT_VERSION_TRANSITION(transformMutation, nil, transformResult, docV7Unknown);
  465. ASSERT_VERSION_TRANSITION(deleteMutation, docV3, mutationResult, docV7Deleted);
  466. ASSERT_VERSION_TRANSITION(deleteMutation, deletedV3, mutationResult, docV7Deleted);
  467. ASSERT_VERSION_TRANSITION(deleteMutation, nil, mutationResult, docV7Deleted);
  468. }
  469. #undef ASSERT_TRANSITION
  470. @end