-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Adding FieldValue.increment() #2075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9b42925
8213eeb
2f7e7e0
29a55da
b74b388
c096fe1
4f58e41
67c0a05
55a525d
8b43738
c84f263
34e45f1
a1c5d12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright 2018 Google | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import <FirebaseFirestore/FirebaseFirestore.h> | ||
|
|
||
| #import <XCTest/XCTest.h> | ||
|
|
||
| #import "Firestore/Source/API/FIRFieldValue+Internal.h" | ||
|
|
||
| #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h" | ||
| #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h" | ||
|
|
||
| @interface FIRNumericTransformTests : FSTIntegrationTestCase | ||
| @end | ||
|
|
||
| @implementation FIRNumericTransformTests { | ||
| // A document reference to read and write to. | ||
| FIRDocumentReference *_docRef; | ||
|
|
||
| // Accumulator used to capture events during the test. | ||
| FSTEventAccumulator<FIRDocumentSnapshot *> *_accumulator; | ||
|
|
||
| // Listener registration for a listener maintained during the course of the test. | ||
| id<FIRListenerRegistration> _listenerRegistration; | ||
| } | ||
|
|
||
| - (void)setUp { | ||
| [super setUp]; | ||
|
|
||
| _docRef = [self documentRef]; | ||
| _accumulator = [FSTEventAccumulator accumulatorForTest:self]; | ||
| _listenerRegistration = | ||
| [_docRef addSnapshotListenerWithIncludeMetadataChanges:YES | ||
| listener:_accumulator.valueEventHandler]; | ||
|
|
||
| // Wait for initial nil snapshot to avoid potential races. | ||
| FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"]; | ||
| XCTAssertFalse(initialSnapshot.exists); | ||
| } | ||
|
|
||
| - (void)tearDown { | ||
| [_listenerRegistration remove]; | ||
|
|
||
| [super tearDown]; | ||
| } | ||
|
|
||
| #pragma mark - Test Helpers | ||
|
|
||
| /** Writes some initial data and consumes the events generated. */ | ||
| - (void)writeInitialData:(NSDictionary<NSString *, id> *)data { | ||
| [self writeDocumentRef:_docRef data:data]; | ||
| XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, data); | ||
| XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, data); | ||
| } | ||
|
|
||
| - (void)expectLocalAndRemoteValue:(int64_t)expectedSum { | ||
| FIRDocumentSnapshot *snap = [_accumulator awaitLocalEvent]; | ||
| XCTAssertEqual(@(expectedSum), snap[@"sum"]); | ||
| snap = [_accumulator awaitRemoteEvent]; | ||
| XCTAssertEqual(@(expectedSum), snap[@"sum"]); | ||
| } | ||
|
|
||
| - (void)expectApproximateLocalAndRemoteValue:(double)expectedSum { | ||
| FIRDocumentSnapshot *snap = [_accumulator awaitLocalEvent]; | ||
| XCTAssertEqual(@(expectedSum), snap[@"sum"]); | ||
| snap = [_accumulator awaitRemoteEvent]; | ||
| XCTAssertEqual(@(expectedSum), snap[@"sum"]); | ||
| } | ||
|
|
||
| #pragma mark - Test Cases | ||
|
|
||
| - (void)testCreateDocumentWithIncrement { | ||
| [self writeDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1337]}]; | ||
| [self expectLocalAndRemoteValue:1337]; | ||
| } | ||
|
|
||
| - (void)testMergeOnNonExistingDocumentWithIncrement { | ||
| [self mergeDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1337]}]; | ||
| [self expectLocalAndRemoteValue:1337]; | ||
| } | ||
|
|
||
| - (void)testIntegerIncrementWithExistingInteger { | ||
| [self writeInitialData:@{@"sum" : @1337}]; | ||
| [self updateDocumentRef:_docRef data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}]; | ||
| [self expectLocalAndRemoteValue:13378]; | ||
|
||
| } | ||
|
|
||
| - (void)testDoubleIncrementWithExistingDouble { | ||
| [self writeInitialData:@{@"sum" : @13.37}]; | ||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.1]}]; | ||
| [self expectApproximateLocalAndRemoteValue:13.47]; | ||
| } | ||
|
|
||
| - (void)testIntegerIncrementWithExistingDouble { | ||
| [self writeInitialData:@{@"sum" : @13.37}]; | ||
| [self updateDocumentRef:_docRef data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}]; | ||
| [self expectApproximateLocalAndRemoteValue:14.37]; | ||
| } | ||
|
|
||
| - (void)testDoubleIncrementWithExistingInteger { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: maybe replace "double" in test name with "float", "floating point", or "real"? "DoubleIncrement" reads like "IncrementTwice", not "IncrementByADouble".
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to push back on this suggestions purely out of laziness. I want these tests names to match across all platforms, and changing one name here requires me to port this change to Android and Web. Thanks for flagging this though. |
||
| [self writeInitialData:@{@"sum" : @1337}]; | ||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.1]}]; | ||
| [self expectApproximateLocalAndRemoteValue:1337.1]; | ||
| } | ||
|
|
||
| - (void)testIntegerIncrementWithExistingString { | ||
| [self writeInitialData:@{@"sum" : @"overwrite"}]; | ||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1337]}]; | ||
| [self expectLocalAndRemoteValue:1337]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR: why was it decided to overwrite instead of reject in this case?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The backend semantics for NUMERIC_ADD follow those of ARRA_UNION and coerce to the expected type first. This ensures consistency between all document transforms. |
||
| } | ||
|
|
||
| - (void)testDoubleIncrementWithExistingString { | ||
| [self writeInitialData:@{@"sum" : @"overwrite"}]; | ||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:13.37]}]; | ||
| [self expectApproximateLocalAndRemoteValue:13.37]; | ||
| } | ||
|
|
||
| - (void)testMultipleDoubleIncrements { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: test for overflow/interesting floating point values (NaN/infinity/etc.)?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests here test the integration with the backend, and as such am I inclined to not add too much edge case testing. I am testing the internal logic that deals with NaN and Infinity in the unit tests. |
||
| [self writeInitialData:@{@"sum" : @"0.0"}]; | ||
|
|
||
| [self disableNetwork]; | ||
|
|
||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.1]}]; | ||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.01]}]; | ||
| [self updateDocumentRef:_docRef | ||
| data:@{@"sum" : [FIRFieldValue fieldValueForDoubleIncrement:0.01]}]; | ||
|
||
|
|
||
| FIRDocumentSnapshot *snap = [_accumulator awaitLocalEvent]; | ||
| XCTAssertEqual(@(0.1), snap[@"sum"]); | ||
| snap = [_accumulator awaitLocalEvent]; | ||
| XCTAssertEqual(@(0.11), snap[@"sum"]); | ||
| snap = [_accumulator awaitLocalEvent]; | ||
| XCTAssertEqual(@(0.111), snap[@"sum"]); | ||
|
|
||
| [self enableNetwork]; | ||
| snap = [_accumulator awaitRemoteEvent]; | ||
| XCTAssertEqual(@(0.111), snap[@"sum"]); | ||
| } | ||
|
|
||
| @end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
XCTAssertEqualWithAccuracy?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Fixed.
I should have probably asked you not to review these tests until I was able to run them.