Adding FieldValue.increment() by schmidt-sebastian · Pull Request #4018 · googleapis/google-cloud-java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private UserDataConverter() {}
* @param path path THe field path of the object to encode.
* @param sanitizedObject An Object that has been sanitized by CustomClassMapper and only contains
* valid types.
* @param options Encoding opions to use for this value.
* @param options Encoding options to use for this value.
* @return The Value proto.
*/
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.delete;
import static com.google.cloud.firestore.LocalFirestoreHelper.get;
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponse;
import static com.google.cloud.firestore.LocalFirestoreHelper.increment;
import static com.google.cloud.firestore.LocalFirestoreHelper.map;
import static com.google.cloud.firestore.LocalFirestoreHelper.object;
import static com.google.cloud.firestore.LocalFirestoreHelper.serverTimestamp;
Expand Down Expand Up @@ -408,6 +409,30 @@ public void mergeWithServerTimestamps() throws Exception {
assertCommitEquals(set, commitRequests.get(1));
}

@Test
public void setWithIncrement() throws Exception {
doReturn(FIELD_TRANSFORM_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference
.set(map("integer", FieldValue.increment(1), "double", FieldValue.increment(1.1)))
.get();

CommitRequest set =
commit(
set(Collections.<String, Value>emptyMap()),
transform(
"integer",
increment(Value.newBuilder().setIntegerValue(1).build()),
"double",
increment(Value.newBuilder().setDoubleValue(1.1).build())));

CommitRequest commitRequest = commitCapture.getValue();
assertCommitEquals(set, commitRequest);
}

@Test
public void setWithArrayUnion() throws Exception {
doReturn(FIELD_TRANSFORM_COMMIT_RESPONSE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,16 @@ public void arrayRemoveEquals() {
assertNotEquals(arrayRemove1, arrayRemove3);
assertNotEquals(arrayRemove1, arrayUnion);
}

@Test
public void incrementEquals() {
FieldValue increment1 = FieldValue.increment(42);
FieldValue increment2 = FieldValue.increment(42);
FieldValue increment3 = FieldValue.increment(42.0);
FieldValue increment4 = FieldValue.increment(42.0);
assertEquals(increment1, increment2);
assertEquals(increment3, increment4);
assertNotEquals(increment1, increment3);
assertNotEquals(increment2, increment4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public static FieldTransform serverTimestamp() {
.build();
}

public static FieldTransform increment(Value value) {
return FieldTransform.newBuilder().setIncrement(value).build();
}

public static FieldTransform arrayUnion(Value... values) {
return FieldTransform.newBuilder()
.setAppendMissingElements(ArrayValue.newBuilder().addAllValues(Arrays.asList(values)))
Expand Down