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