Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save schmidt-sebastian/08b8cf9956a33989a8eedaad4533d25d to your computer and use it in GitHub Desktop.
Save schmidt-sebastian/08b8cf9956a33989a8eedaad4533d25d to your computer and use it in GitHub Desktop.
diff --git a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java
index c27e47b2c33..0366f7a0730 100644
--- a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java
+++ b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java
@@ -18,8 +18,14 @@ package com.google.cloud.firestore;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO;
+import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_VALUE;
+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;
+import static com.google.cloud.firestore.LocalFirestoreHelper.commit;
import static com.google.cloud.firestore.LocalFirestoreHelper.commitResponse;
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponse;
+import static com.google.cloud.firestore.LocalFirestoreHelper.transform;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;
@@ -33,10 +39,9 @@ import com.google.cloud.firestore.spi.v1.FirestoreRpc;
import com.google.firestore.v1.BatchGetDocumentsRequest;
import com.google.firestore.v1.CommitRequest;
import com.google.firestore.v1.CommitResponse;
-import com.google.firestore.v1.DocumentTransform;
import com.google.firestore.v1.ListCollectionIdsRequest;
-import com.google.firestore.v1.Value;
import java.util.List;
+import java.util.concurrent.ExecutionException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -181,28 +186,34 @@ public class FirestoreTest {
}
@Test
- public void arrayUnionWithPojo() {
+ public void arrayUnionWithPojo() throws ExecutionException, InterruptedException {
doReturn(commitResponse(1, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
- DocumentReference doc = firestoreMock.document("coll/doc1");
- FieldValue fieldValue = FieldValue.arrayUnion(SINGLE_FIELD_OBJECT);
- DocumentTransform.FieldTransform transform = fieldValue.toProto(FieldPath.of("foo"));
- Value value = transform.getAppendMissingElements().getValues(0);
- assertEquals("bar", value.getMapValue().getFieldsMap().get("foo").getStringValue());
+
+ DocumentReference doc = firestoreMock.document("coll/doc");
+ doc.update("array", FieldValue.arrayUnion(SINGLE_FIELD_OBJECT)).get();
+
+ CommitRequest expectedRequest =
+ commit(transform(UPDATE_PRECONDITION, "array", arrayUnion(SINGLE_FIELD_VALUE)));
+ CommitRequest actualRequest = commitCapture.getValue();
+ assertEquals(expectedRequest, actualRequest);
}
@Test
- public void arrayRemoveWithPojo() {
- doReturn(commitResponse(0, 1))
+ public void arrayRemoveWithPojo() throws ExecutionException, InterruptedException {
+ doReturn(commitResponse(1, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
- DocumentReference doc = firestoreMock.document("coll/doc1");
- FieldValue fieldValue = FieldValue.arrayRemove(SINGLE_FIELD_OBJECT);
- DocumentTransform.FieldTransform transform = fieldValue.toProto(FieldPath.of("foo"));
- Value value = transform.getRemoveAllFromArray().getValues(0);
- assertEquals("bar", value.getMapValue().getFieldsMap().get("foo").getStringValue());
+
+ DocumentReference doc = firestoreMock.document("coll/doc");
+ doc.update("array", FieldValue.arrayRemove(SINGLE_FIELD_OBJECT)).get();
+
+ CommitRequest expectedRequest =
+ commit(transform(UPDATE_PRECONDITION, "array", arrayRemove(SINGLE_FIELD_VALUE)));
+ CommitRequest actualRequest = commitCapture.getValue();
+ assertEquals(expectedRequest, actualRequest);
}
}
diff --git a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java
index f4ec28d1525..1f4165c61eb 100644
--- a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java
+++ b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java
@@ -87,6 +87,7 @@ public final class LocalFirestoreHelper {
public static final Map<String, Object> SINGLE_FIELD_MAP;
public static final SingleField SINGLE_FIELD_OBJECT;
public static final Map<String, Value> SINGLE_FIELD_PROTO;
+ public static final Value SINGLE_FIELD_VALUE;
public static final DocumentSnapshot SINGLE_FIELD_SNAPSHOT;
public static final Map<String, Object> UPDATED_FIELD_MAP;
@@ -728,6 +729,10 @@ public final class LocalFirestoreHelper {
Timestamp.ofTimeSecondsAndNanos(3, 4),
Timestamp.ofTimeSecondsAndNanos(1, 2));
+ Value.Builder singleFieldValueBuilder = Value.newBuilder();
+ singleFieldValueBuilder.getMapValueBuilder().putAllFields(SINGLE_FIELD_PROTO);
+ SINGLE_FIELD_VALUE = singleFieldValueBuilder.build();
+
UPDATED_FIELD_MAP = map("foo", (Object) "foobar");
UPDATED_FIELD_PROTO = map("foo", Value.newBuilder().setStringValue("foobar").build());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment