FIRNumericTransformTests.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2019 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 <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  19. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  20. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  21. double DOUBLE_EPSILON = 0.000001;
  22. @interface FIRNumericTransformTests : FSTIntegrationTestCase
  23. @end
  24. @implementation FIRNumericTransformTests {
  25. // A document reference to read and write to.
  26. FIRDocumentReference *_docRef;
  27. // Accumulator used to capture events during the test.
  28. FSTEventAccumulator<FIRDocumentSnapshot *> *_accumulator;
  29. // Listener registration for a listener maintained during the course of the test.
  30. id<FIRListenerRegistration> _listenerRegistration;
  31. }
  32. - (void)invokeTest {
  33. // None of these tests work in the emulator as of 1.4.5.
  34. if ([FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  35. [super invokeTest];
  36. }
  37. - (void)setUp {
  38. [super setUp];
  39. _docRef = [self documentRef];
  40. _accumulator = [FSTEventAccumulator accumulatorForTest:self];
  41. _listenerRegistration =
  42. [_docRef addSnapshotListenerWithIncludeMetadataChanges:YES
  43. listener:_accumulator.valueEventHandler];
  44. // Wait for initial nil snapshot to avoid potential races.
  45. FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"];
  46. XCTAssertFalse(initialSnapshot.exists);
  47. }
  48. - (void)tearDown {
  49. [_listenerRegistration remove];
  50. [super tearDown];
  51. }
  52. #pragma mark - Test Helpers
  53. /** Writes some initial data and consumes the events generated. */
  54. - (void)writeInitialData:(NSDictionary<NSString *, id> *)data {
  55. [self writeDocumentRef:_docRef data:data];
  56. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, data);
  57. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, data);
  58. }
  59. - (void)expectLocalAndRemoteValue:(int64_t)expectedSum {
  60. FIRDocumentSnapshot *snap = [_accumulator awaitLocalEvent];
  61. XCTAssertEqualObjects(@(expectedSum), snap[@"sum"]);
  62. snap = [_accumulator awaitRemoteEvent];
  63. XCTAssertEqualObjects(@(expectedSum), snap[@"sum"]);
  64. }
  65. - (void)expectApproximateLocalAndRemoteValue:(double)expectedSum {
  66. FIRDocumentSnapshot *snap = [_accumulator awaitLocalEvent];
  67. XCTAssertEqualWithAccuracy(expectedSum, [snap[@"sum"] doubleValue], DOUBLE_EPSILON);
  68. snap = [_accumulator awaitRemoteEvent];
  69. XCTAssertEqualWithAccuracy(expectedSum, [snap[@"sum"] doubleValue], DOUBLE_EPSILON);
  70. }
  71. #pragma mark - Test Cases
  72. - (void)testCreateDocumentWithIncrement {
  73. [self writeDocumentRef:_docRef
  74. data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1337]}];
  75. [self expectLocalAndRemoteValue:1337];
  76. }
  77. - (void)testMergeOnNonExistingDocumentWithIncrement {
  78. [self mergeDocumentRef:_docRef
  79. data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1337]}];
  80. [self expectLocalAndRemoteValue:1337];
  81. }
  82. - (void)testIntegerIncrementWithExistingInteger {
  83. [self writeInitialData:@{@"sum" : @1337}];
  84. [self updateDocumentRef:_docRef data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}];
  85. [self expectLocalAndRemoteValue:1338];
  86. }
  87. - (void)testDoubleIncrementWithExistingDouble {
  88. [self writeInitialData:@{@"sum" : @13.37}];
  89. [self updateDocumentRef:_docRef
  90. data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.1]}];
  91. [self expectApproximateLocalAndRemoteValue:13.47];
  92. }
  93. - (void)testIntegerIncrementWithExistingDouble {
  94. [self writeInitialData:@{@"sum" : @13.37}];
  95. [self updateDocumentRef:_docRef data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}];
  96. [self expectApproximateLocalAndRemoteValue:14.37];
  97. }
  98. - (void)testDoubleIncrementWithExistingInteger {
  99. [self writeInitialData:@{@"sum" : @1337}];
  100. [self updateDocumentRef:_docRef
  101. data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.1]}];
  102. [self expectApproximateLocalAndRemoteValue:1337.1];
  103. }
  104. - (void)testIntegerIncrementWithExistingString {
  105. [self writeInitialData:@{@"sum" : @"overwrite"}];
  106. [self updateDocumentRef:_docRef
  107. data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1337]}];
  108. [self expectLocalAndRemoteValue:1337];
  109. }
  110. - (void)testDoubleIncrementWithExistingString {
  111. [self writeInitialData:@{@"sum" : @"overwrite"}];
  112. [self updateDocumentRef:_docRef
  113. data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:13.37]}];
  114. [self expectApproximateLocalAndRemoteValue:13.37];
  115. }
  116. - (void)testMultipleDoubleIncrements {
  117. [self writeInitialData:@{@"sum" : @"0.0"}];
  118. [self disableNetwork];
  119. [_docRef updateData:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.1]}];
  120. [_docRef updateData:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.01]}];
  121. [_docRef updateData:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.001]}];
  122. FIRDocumentSnapshot *snap = [_accumulator awaitLocalEvent];
  123. XCTAssertEqualWithAccuracy(0.1, [snap[@"sum"] doubleValue], DOUBLE_EPSILON);
  124. snap = [_accumulator awaitLocalEvent];
  125. XCTAssertEqualWithAccuracy(0.11, [snap[@"sum"] doubleValue], DOUBLE_EPSILON);
  126. snap = [_accumulator awaitLocalEvent];
  127. XCTAssertEqualWithAccuracy(0.111, [snap[@"sum"] doubleValue], DOUBLE_EPSILON);
  128. [self enableNetwork];
  129. snap = [_accumulator awaitRemoteEvent];
  130. XCTAssertEqualWithAccuracy(0.111, [snap[@"sum"] doubleValue], DOUBLE_EPSILON);
  131. }
  132. @end