Skip to content

Commit

Permalink
fix: add support for updating an individual field with pojo in all up…
Browse files Browse the repository at this point in the history
…date method (#136)
  • Loading branch information
suraj-qlogic committed Mar 27, 2020
1 parent 495f7f9 commit 7d6c2c1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Expand Up @@ -86,7 +86,10 @@ static DocumentSnapshot fromObject(
Map<String, Value> fields = new HashMap<>();
for (Map.Entry<String, Object> entry : values.entrySet()) {
Value encodedValue =
UserDataConverter.encodeValue(FieldPath.of(entry.getKey()), entry.getValue(), options);
UserDataConverter.encodeValue(
FieldPath.of(entry.getKey()),
CustomClassMapper.convertToPlainJavaTypes(entry.getValue()),
options);
if (encodedValue != null) {
fields.put(entry.getKey(), encodedValue);
}
Expand Down
Expand Up @@ -35,6 +35,8 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_WRITE_COMMIT_RESPONSE;
import static com.google.cloud.firestore.LocalFirestoreHelper.TIMESTAMP;
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATED_POJO;
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATED_POJO_PROTO;
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_PRECONDITION;
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayRemove;
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayUnion;
Expand Down Expand Up @@ -82,6 +84,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -255,6 +258,7 @@ public void deserializeBasicTypes() throws Exception {
assertEquals(Timestamp.ofTimeSecondsAndNanos(5, 6), snapshot.getReadTime());

assertEquals(get(), getAllCapture.getValue());
assertEquals("bar", ((Map<String, Object>) snapshot.get("model")).get("foo"));
}

@Test
Expand Down Expand Up @@ -839,7 +843,8 @@ public void extractFieldMaskFromMerge() throws Exception {
"second.nullValue",
"second.objectValue.foo",
"second.timestampValue",
"second.trueValue");
"second.trueValue",
"second.model.foo");

CommitRequest expectedCommit = commit(set(nestedUpdate, updateMask));
assertCommitEquals(expectedCommit, commitCapture.getValue());
Expand Down Expand Up @@ -1028,4 +1033,20 @@ public void updateDocumentWithPreconditions() throws Exception {
assertCommitEquals(expectedCommit, request);
}
}

@Test
public void updateIndividualPojo() throws ExecutionException, InterruptedException {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
documentReference.update(UPDATED_POJO);
documentReference.update(UPDATED_POJO).get();
CommitRequest expectedCommit =
commit(update(UPDATED_POJO_PROTO, Collections.singletonList("model")));

for (CommitRequest request : commitCapture.getAllValues()) {
assertCommitEquals(expectedCommit, request);
}
}
}
Expand Up @@ -93,6 +93,7 @@ public final class LocalFirestoreHelper {
public static final Map<String, Object> UPDATED_FIELD_MAP;
public static final Map<String, Value> UPDATED_FIELD_PROTO;
public static final Map<String, Value> UPDATED_SINGLE_FIELD_PROTO;
public static final Map<String, Value> UPDATED_POJO_PROTO;

public static final Map<String, Float> SINGLE_FLOAT_MAP;
public static final Map<String, Value> SINGLE_FLOAT_PROTO;
Expand All @@ -114,6 +115,8 @@ public final class LocalFirestoreHelper {

public static final ApiFuture<CommitResponse> FIELD_TRANSFORM_COMMIT_RESPONSE;

public static final Map<String, Object> UPDATED_POJO;

public static final Date DATE;
public static final Timestamp TIMESTAMP;
public static final GeoPoint GEO_POINT;
Expand Down Expand Up @@ -660,6 +663,7 @@ public static class AllSupportedTypes {
public String nullValue = null;
public Blob bytesValue = BLOB;
public GeoPoint geoPointValue = GEO_POINT;
public Map<String, Object> model = ImmutableMap.of("foo", (Object) SINGLE_FIELD_OBJECT.foo);

@Override
public boolean equals(Object o) {
Expand All @@ -684,7 +688,8 @@ public boolean equals(Object o) {
&& Objects.equals(arrayValue, that.arrayValue)
&& Objects.equals(nullValue, that.nullValue)
&& Objects.equals(bytesValue, that.bytesValue)
&& Objects.equals(geoPointValue, that.geoPointValue);
&& Objects.equals(geoPointValue, that.geoPointValue)
&& Objects.equals(model, that.model);
}
}

Expand Down Expand Up @@ -717,6 +722,14 @@ public boolean equals(Object o) {
SINGLE_FIELD_MAP = map("foo", (Object) "bar");
SINGLE_FIELD_OBJECT = new SingleField();
SINGLE_FIELD_PROTO = map("foo", Value.newBuilder().setStringValue("bar").build());
UPDATED_POJO_PROTO =
map(
"model",
Value.newBuilder()
.setMapValue(
MapValue.newBuilder()
.putFields("foo", Value.newBuilder().setStringValue("foobar").build()))
.build());
SINGLE_FIELD_SNAPSHOT =
new DocumentSnapshot(
null,
Expand Down Expand Up @@ -777,7 +790,7 @@ public boolean equals(Object o) {
ALL_SUPPORTED_TYPES_MAP.put("nullValue", null);
ALL_SUPPORTED_TYPES_MAP.put("bytesValue", BLOB);
ALL_SUPPORTED_TYPES_MAP.put("geoPointValue", GEO_POINT);

ALL_SUPPORTED_TYPES_MAP.put("model", map("foo", SINGLE_FIELD_OBJECT.foo));
ALL_SUPPORTED_TYPES_PROTO =
ImmutableMap.<String, Value>builder()
.put("foo", Value.newBuilder().setStringValue("bar").build())
Expand Down Expand Up @@ -823,9 +836,13 @@ public boolean equals(Object o) {
.setGeoPointValue(
LatLng.newBuilder().setLatitude(50.1430847).setLongitude(-122.9477780))
.build())
.put(
"model",
Value.newBuilder()
.setMapValue(MapValue.newBuilder().putAllFields(SINGLE_FIELD_PROTO))
.build())
.build();
ALL_SUPPORTED_TYPES_OBJECT = new AllSupportedTypes();

SINGLE_WRITE_COMMIT_RESPONSE = commitResponse(/* adds= */ 1, /* deletes= */ 0);
SINGLE_DELETE_COMMIT_RESPONSE = commitResponse(/* adds= */ 0, /* deletes= */ 1);
SINGLE_CREATE_COMMIT_REQUEST = commit(create(SINGLE_FIELD_PROTO));
Expand All @@ -837,6 +854,7 @@ public boolean equals(Object o) {
CREATE_PRECONDITION = Precondition.newBuilder().setExists(false).build();

UPDATE_PRECONDITION = Precondition.newBuilder().setExists(true).build();
UPDATED_POJO = map("model", (Object) UPDATE_SINGLE_FIELD_OBJECT);
}

public static String autoId() {
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.firestore.it;

import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_SINGLE_FIELD_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.map;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -55,6 +56,7 @@
import com.google.cloud.firestore.WriteBatch;
import com.google.cloud.firestore.WriteResult;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -246,6 +248,10 @@ public void updateDocument() throws Exception {
documentSnapshot = randomDoc.get().get();
expectedResult.foo = "updated";
assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
expectedResult.model = ImmutableMap.of("foo", (Object) UPDATE_SINGLE_FIELD_OBJECT.foo);
randomDoc.update("model", UPDATE_SINGLE_FIELD_OBJECT).get();
documentSnapshot = randomDoc.get().get();
assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
}

@Test(expected = ExecutionException.class)
Expand Down

0 comments on commit 7d6c2c1

Please sign in to comment.