Skip to content
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

Added feature to accept Firestore Value. #4709

Merged
merged 2 commits into from
Mar 25, 2019
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 @@ -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