Skip to content

Commit

Permalink
Added feature to accept Firestore Value. (#4709)
Browse files Browse the repository at this point in the history
* Fix firestore value

* Added IT test
  • Loading branch information
Praful Makani authored and sduskis committed Mar 25, 2019
1 parent 95a9cdf commit 554c109
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.firestore.annotation.PropertyName;
import com.google.cloud.firestore.annotation.ServerTimestamp;
import com.google.cloud.firestore.annotation.ThrowOnExtraProperties;
import com.google.firestore.v1.Value;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -166,7 +167,8 @@ private static <T> Object serialize(T o, ErrorPath path) {
|| o instanceof GeoPoint
|| o instanceof Blob
|| o instanceof DocumentReference
|| o instanceof FieldValue) {
|| o instanceof FieldValue
|| o instanceof Value) {
return o;
} else {
Class<T> clazz = (Class<T>) o.getClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ static Value encodeValue(
} else if (sanitizedObject instanceof Blob) {
Blob blob = (Blob) sanitizedObject;
return Value.newBuilder().setBytesValue(blob.toByteString()).build();
} else if (sanitizedObject instanceof Value) {
return (Value) sanitizedObject;
} else if (sanitizedObject instanceof DocumentReference) {
DocumentReference docRef = (DocumentReference) sanitizedObject;
return Value.newBuilder().setReferenceValue(docRef.getName()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ public void setDocument() throws Exception {
assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
}

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

batch
.set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_PROTO)
.set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_OBJECT)
.set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_PROTO, SetOptions.merge())
.set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_OBJECT, SetOptions.merge());

List<Write> writes = new ArrayList<>();
writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO));
writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO));
writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO, Arrays.asList("foo")));
writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO, Arrays.asList("foo")));

assertEquals(4, batch.getMutationsSize());

List<WriteResult> writeResults = batch.commit().get();
for (int i = 0; i < writeResults.size(); ++i) {
assertEquals(Timestamp.ofTimeSecondsAndNanos(i, i), writeResults.get(i).getUpdateTime());
}

CommitRequest commitRequest = commitCapture.getValue();
assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
}

@Test
public void omitWriteResultForDocumentTransforms() throws Exception {
doReturn(commitResponse(2, 0))
Expand Down Expand Up @@ -179,6 +209,31 @@ public void createDocument() throws Exception {
assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
}

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

batch
.create(documentReference, LocalFirestoreHelper.SINGLE_FIELD_PROTO)
.create(documentReference, LocalFirestoreHelper.SINGLE_FIELD_OBJECT);

assertEquals(2, batch.getMutationsSize());

List<WriteResult> writeResults = batch.commit().get();
List<Write> writes = new ArrayList<>();

for (int i = 0; i < writeResults.size(); ++i) {
assertEquals(Timestamp.ofTimeSecondsAndNanos(i, i), writeResults.get(i).getUpdateTime());
writes.add(create(LocalFirestoreHelper.SINGLE_FIELD_PROTO));
}

CommitRequest commitRequest = commitCapture.getValue();
assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
}

@Test
public void deleteDocument() throws Exception {
doReturn(commitResponse(2, 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public void createDocument() throws Exception {
assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class));
}

@Test
public void createDocumentWithValue() throws Exception {
assertEquals(20, randomDoc.getId().length());
randomDoc.create(LocalFirestoreHelper.SINGLE_FIELD_PROTO).get();
DocumentSnapshot documentSnapshot = randomDoc.get().get();
assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class));
}

@Test
public void setDocument() throws Exception {
Map<String, Object> nanNullMap = new HashMap<>();
Expand All @@ -160,6 +168,14 @@ public void setDocument() throws Exception {
assertEquals(null, documentSnapshot.get("null"));
}

@Test
public void setDocumentWithValue() throws Exception {
assertEquals(20, randomDoc.getId().length());
randomDoc.set(LocalFirestoreHelper.SINGLE_FIELD_PROTO).get();
DocumentSnapshot documentSnapshot = randomDoc.get().get();
assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class));
}

@Test
public void setDocumentWithMerge() throws Exception {
Map<String, Object> originalMap =
Expand Down

0 comments on commit 554c109

Please sign in to comment.