-
Notifications
You must be signed in to change notification settings - Fork 666
Adding FieldValue.numericAdd() #105
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
20152fe
Adding FieldValue.numericAdd()
schmidt-sebastian 602d03f
Adding NaN and Infinity test
schmidt-sebastian ef1862b
Use addExact in comment
schmidt-sebastian 6e386c4
Adding more Edge case testing
schmidt-sebastian 0df4ee8
Review feedback
schmidt-sebastian e74e03c
More Feedback
schmidt-sebastian 1f98e0a
Porting changes back from Web
schmidt-sebastian 720d708
Last round of feedback, mostly reverting rename
schmidt-sebastian 2a4807f
Adding comment
schmidt-sebastian 3bd91ec
Master merge
schmidt-sebastian f4cf039
Enable tests
schmidt-sebastian 3f2e09e
Running Google Java Format
schmidt-sebastian 261c7c8
Update Proto with wording from iOS
schmidt-sebastian bb10ecb
Rename numericAdd to increment
schmidt-sebastian 5b4f0a2
Fix wording
schmidt-sebastian 29758d1
Merge
schmidt-sebastian 991fb7c
Update Protobuf to use increment()
schmidt-sebastian 94493b8
Merge
schmidt-sebastian 468c282
v1 Update
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
...e-firestore/src/androidTest/java/com/google/firebase/firestore/NumericTransformsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Copyright 2018 Google LLC | ||
| // | ||
| // 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. | ||
|
|
||
| package com.google.firebase.firestore; | ||
|
|
||
| import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testDocument; | ||
| import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor; | ||
| import static com.google.firebase.firestore.testutil.TestUtil.map; | ||
| import static junit.framework.Assert.assertEquals; | ||
| import static junit.framework.Assert.assertFalse; | ||
|
|
||
| import com.google.android.gms.tasks.Tasks; | ||
| import com.google.firebase.firestore.testutil.EventAccumulator; | ||
| import com.google.firebase.firestore.testutil.IntegrationTestUtil; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class NumericTransformsTest { | ||
| private static final double DOUBLE_EPSILON = 0.000001; | ||
|
|
||
| // A document reference to read and write to. | ||
| private DocumentReference docRef; | ||
|
|
||
| // Accumulator used to capture events during the test. | ||
| private EventAccumulator<DocumentSnapshot> accumulator; | ||
|
|
||
| // Listener registration for a listener maintained during the course of the test. | ||
| private ListenerRegistration listenerRegistration; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| docRef = testDocument(); | ||
| accumulator = new EventAccumulator<>(); | ||
| listenerRegistration = | ||
| docRef.addSnapshotListener(MetadataChanges.INCLUDE, accumulator.listener()); | ||
|
|
||
| // Wait for initial null snapshot to avoid potential races. | ||
| DocumentSnapshot initialSnapshot = accumulator.await(); | ||
| assertFalse(initialSnapshot.exists()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| listenerRegistration.remove(); | ||
| IntegrationTestUtil.tearDown(); | ||
| } | ||
|
|
||
| /** Writes some initialData and consumes the events generated. */ | ||
| private void writeInitialData(Map<String, Object> initialData) { | ||
| waitFor(docRef.set(initialData)); | ||
| accumulator.awaitRemoteEvent(); | ||
| } | ||
|
|
||
| private void expectLocalAndRemoteValue(double expectedSum) { | ||
| DocumentSnapshot snap = accumulator.awaitLocalEvent(); | ||
| assertEquals(expectedSum, snap.getDouble("sum"), DOUBLE_EPSILON); | ||
| snap = accumulator.awaitRemoteEvent(); | ||
| assertEquals(expectedSum, snap.getDouble("sum"), DOUBLE_EPSILON); | ||
| } | ||
|
|
||
| private void expectLocalAndRemoteValue(long expectedSum) { | ||
| DocumentSnapshot snap = accumulator.awaitLocalEvent(); | ||
| assertEquals(expectedSum, (long) snap.getLong("sum")); | ||
| snap = accumulator.awaitRemoteEvent(); | ||
| assertEquals(expectedSum, (long) snap.getLong("sum")); | ||
| } | ||
|
|
||
| @Test | ||
| public void createDocumentWithIncrement() { | ||
| waitFor(docRef.set(map("sum", FieldValue.numericAdd(1337)))); | ||
| expectLocalAndRemoteValue(1337L); | ||
| } | ||
|
|
||
| @Test | ||
| public void mergeOnNonExistingDocumentWithIncrement() { | ||
| waitFor(docRef.set(map("sum", FieldValue.numericAdd(1337)), SetOptions.merge())); | ||
| expectLocalAndRemoteValue(1337L); | ||
| } | ||
|
|
||
| @Test | ||
| public void integerIncrementWithExistingInteger() { | ||
| writeInitialData(map("sum", 1337L)); | ||
| waitFor(docRef.update("sum", FieldValue.numericAdd(1))); | ||
| expectLocalAndRemoteValue(1338L); | ||
| } | ||
|
|
||
| @Test | ||
| public void doubleIncrementWithExistingDouble() { | ||
mikelehen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| writeInitialData(map("sum", 13.37D)); | ||
| waitFor(docRef.update("sum", FieldValue.numericAdd(0.1))); | ||
| expectLocalAndRemoteValue(13.47D); | ||
| } | ||
|
|
||
| @Test | ||
| public void integerIncrementWithExistingDouble() { | ||
| writeInitialData(map("sum", 13.37D)); | ||
| waitFor(docRef.update("sum", FieldValue.numericAdd(1))); | ||
| expectLocalAndRemoteValue(14.37D); | ||
| } | ||
|
|
||
| @Test | ||
| public void doubleIncrementWithExistingInteger() { | ||
| writeInitialData(map("sum", 1337L)); | ||
| waitFor(docRef.update("sum", FieldValue.numericAdd(0.1))); | ||
| expectLocalAndRemoteValue(1337.1D); | ||
| } | ||
|
|
||
| @Test | ||
| public void integerIncrementWithExistingString() { | ||
| writeInitialData(map("sum", "overwrite")); | ||
| waitFor(docRef.update("sum", FieldValue.numericAdd(1337))); | ||
| expectLocalAndRemoteValue(1337L); | ||
| } | ||
|
|
||
| @Test | ||
| public void doubleIncrementWithExistingString() { | ||
| writeInitialData(map("sum", "overwrite")); | ||
| waitFor(docRef.update("sum", FieldValue.numericAdd(13.37))); | ||
| expectLocalAndRemoteValue(13.37D); | ||
| } | ||
|
|
||
| @Test | ||
| public void multipleDoubleIncrements() throws ExecutionException, InterruptedException { | ||
| writeInitialData(map("sum", 0.0D)); | ||
|
|
||
| Tasks.await(docRef.getFirestore().disableNetwork()); | ||
|
|
||
| docRef.update("sum", FieldValue.numericAdd(0.1D)); | ||
| docRef.update("sum", FieldValue.numericAdd(0.01D)); | ||
| docRef.update("sum", FieldValue.numericAdd(0.001D)); | ||
|
|
||
| DocumentSnapshot snap = accumulator.awaitLocalEvent(); | ||
| assertEquals(0.1D, snap.getDouble("sum"), DOUBLE_EPSILON); | ||
| snap = accumulator.awaitLocalEvent(); | ||
| assertEquals(0.11D, snap.getDouble("sum"), DOUBLE_EPSILON); | ||
| snap = accumulator.awaitLocalEvent(); | ||
| assertEquals(0.111D, snap.getDouble("sum"), DOUBLE_EPSILON); | ||
|
|
||
| Tasks.await(docRef.getFirestore().enableNetwork()); | ||
|
|
||
| snap = accumulator.awaitRemoteEvent(); | ||
| assertEquals(0.111D, snap.getDouble("sum"), DOUBLE_EPSILON); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.