@Test public void deleteField() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference.update("foo", "bar", "bar.foo", FieldValue.delete()).get(); Value.Builder emptyMap = Value.newBuilder(); emptyMap.getMapValueBuilder(); Map<String, Value> fieldMap = new HashMap<>(); fieldMap.put("foo", string("bar")); CommitRequest expectedCommit = commit(update(fieldMap, Arrays.asList("foo", "bar.foo"))); assertCommitEquals(expectedCommit, commitCapture.getValue()); }
@Test public void setNestedMapWithMerge() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference.set(NESTED_CLASS_OBJECT, SetOptions.mergeFields("first", "second")).get(); Map<String, Value> nestedUpdate = new HashMap<>(); Value.Builder nestedProto = Value.newBuilder(); nestedProto.getMapValueBuilder().putAllFields(SINGLE_FIELD_PROTO); nestedUpdate.put("first", nestedProto.build()); nestedProto.getMapValueBuilder().putAllFields(ALL_SUPPORTED_TYPES_PROTO); nestedUpdate.put("second", nestedProto.build()); CommitRequest expectedCommit = commit(set(nestedUpdate, Arrays.asList("first", "second"))); assertCommitEquals(expectedCommit, commitCapture.getValue()); }
@Test public void updateNestedMap() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference.update("a.b", "foo", "a.c", FieldValue.delete()).get(); Map<String, Value> nestedUpdate = new HashMap<>(); Value.Builder valueProto = Value.newBuilder(); valueProto .getMapValueBuilder() .putFields("b", Value.newBuilder().setStringValue("foo").build()); nestedUpdate.put("a", valueProto.build()); CommitRequest expectedCommit = commit(update(nestedUpdate, Arrays.asList("a.b", "a.c"))); assertCommitEquals(expectedCommit, commitCapture.getValue()); }
nestedProto.getMapValueBuilder().putAllFields(SINGLE_FIELD_PROTO); nestedUpdate.put("first", nestedProto.build()); nestedProto.getMapValueBuilder().putAllFields(ALL_SUPPORTED_TYPES_PROTO); nestedUpdate.put("second", nestedProto.build());
@Test public void setDocumentWithNestedMerge() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference.set(NESTED_CLASS_OBJECT, SetOptions.mergeFields("first.foo")).get(); documentReference .set(NESTED_CLASS_OBJECT, SetOptions.mergeFields(Arrays.asList("first.foo"))) .get(); documentReference .set( NESTED_CLASS_OBJECT, SetOptions.mergeFieldPaths(Arrays.asList(FieldPath.of("first", "foo")))) .get(); Map<String, Value> nestedUpdate = new HashMap<>(); Value.Builder nestedProto = Value.newBuilder(); nestedProto.getMapValueBuilder().putAllFields(SINGLE_FIELD_PROTO); nestedUpdate.put("first", nestedProto.build()); CommitRequest expectedCommit = commit(set(nestedUpdate, Arrays.asList("first.foo"))); for (int i = 0; i < 3; ++i) { assertCommitEquals(expectedCommit, commitCapture.getAllValues().get(i)); } }
@Test public void setMultipleFieldsWithMerge() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference .set( NESTED_CLASS_OBJECT, SetOptions.mergeFields("first.foo", "second.foo", "second.trueValue")) .get(); Map<String, Value> nestedUpdate = new HashMap<>(); Value.Builder nestedProto = Value.newBuilder(); nestedProto.getMapValueBuilder().putAllFields(SINGLE_FIELD_PROTO); nestedUpdate.put("first", nestedProto.build()); nestedProto .getMapValueBuilder() .putFields("trueValue", Value.newBuilder().setBooleanValue(true).build()); nestedUpdate.put("second", nestedProto.build()); CommitRequest expectedCommit = commit(set(nestedUpdate, Arrays.asList("first.foo", "second.foo", "second.trueValue"))); assertCommitEquals(expectedCommit, commitCapture.getValue()); }
@Test public void updateWithDotsInFieldName() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference.update(map("a.b.c", (Object) map("d.e", "foo"))).get(); Map<String, Value> nestedUpdate = new HashMap<>(); Value.Builder valueProto = Value.newBuilder(); valueProto .getMapValueBuilder() .putFields("d.e", Value.newBuilder().setStringValue("foo").build()); Value.Builder cProto = Value.newBuilder(); cProto.getMapValueBuilder().putFields("c", valueProto.build()); Value.Builder bProto = Value.newBuilder(); bProto.getMapValueBuilder().putFields("b", cProto.build()); nestedUpdate.put("a", bProto.build()); CommitRequest expectedCommit = commit(update(nestedUpdate, Arrays.asList("a.b.c"))); assertCommitEquals(expectedCommit, commitCapture.getValue()); }
@Test public void mergeWithDotsInFieldName() throws Exception { doReturn(SINGLE_WRITE_COMMIT_RESPONSE) .when(firestoreMock) .sendRequest( commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any()); documentReference .set( map("a.b.c", map("d.e", "foo", "f.g", "bar")), SetOptions.mergeFieldPaths(Arrays.asList(FieldPath.of("a.b.c", "d.e")))) .get(); Map<String, Value> nestedUpdate = new HashMap<>(); Value.Builder nestedProto = Value.newBuilder(); nestedProto .getMapValueBuilder() .putFields("d.e", Value.newBuilder().setStringValue("foo").build()); nestedUpdate.put("a.b.c", nestedProto.build()); CommitRequest expectedCommit = commit(set(nestedUpdate, Arrays.asList("`a.b.c`.`d.e`"))); assertCommitEquals(expectedCommit, commitCapture.getValue()); }
public static Value object(String key, Value value) { Value.Builder result = Value.newBuilder(); result.getMapValueBuilder().putFields(key, value); return result.build(); }