Skip to content

Commit

Permalink
feat: Add toString methods to classes comprising WriteBatch (#1281)
Browse files Browse the repository at this point in the history
* feat: Add toString methods to classes comprising WriteBatch

* feat: Add toString methods to classes comprising WriteBatch
take2: use dynamic class names

* feat: Add toString methods to classes comprising WriteBatch
take3:
- fix formatting
- add unit tests

* feat: Add toString methods to classes comprising WriteBatch
take4: address review comments

* feat: Add toString methods to classes comprising WriteBatch
take5: address review comment

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and cherylEnkidu committed Dec 11, 2023
1 parent aaa5318 commit 6861ca0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,11 @@ public boolean equals(Object obj) {
public int hashCode() {
return Objects.hash(rpcContext, docRef, fields);
}

@Override
public String toString() {
return String.format(
"%s{doc=%s, fields=%s, readTime=%s, updateTime=%s, createTime=%s}",
getClass().getSimpleName(), docRef, fields, readTime, updateTime, createTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ static class WriteOperation {
this.documentReference = documentReference;
this.write = write;
}

@Override
public String toString() {
return String.format("WriteOperation{write=%s, doc=%s}", write, documentReference);
}
}

final FirestoreImpl firestore;
Expand Down Expand Up @@ -646,4 +651,10 @@ List<WriteOperation> getWrites() {
public int getMutationsSize() {
return writes.size();
}

@Override
public String toString() {
return String.format(
"%s{writes=%s, committed=%s}", getClass().getSimpleName(), writes, committed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.firestore;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.Timestamp;
import com.google.cloud.firestore.spi.v1.FirestoreRpc;
import com.google.firestore.v1.Value;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

/** @author Eran Leshem */
@RunWith(MockitoJUnitRunner.class)
public class ToStringTest {

@Spy
private final FirestoreImpl firestoreMock =
new FirestoreImpl(
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
Mockito.mock(FirestoreRpc.class));

private WriteBatch batch;
private DocumentReference documentReference;

@Before
public void before() {
batch = firestoreMock.batch();
documentReference = firestoreMock.document("coll/doc");
}

@Test
public void testDocumentSnapshot() {
Map<String, Value> fields =
Collections.singletonMap(
"key123",
UserDataConverter.encodeValue(
FieldPath.of("key456"),
CustomClassMapper.convertToPlainJavaTypes("value789"),
UserDataConverter.NO_DELETES));
String toStringResult =
new DocumentSnapshot(
null,
documentReference,
fields,
Timestamp.ofTimeMicroseconds(1),
Timestamp.ofTimeMicroseconds(2),
Timestamp.ofTimeMicroseconds(3))
.toString();
assertThat(toStringResult).startsWith("DocumentSnapshot{");
assertThat(toStringResult).containsMatch("doc=DocumentReference\\{path=.*/documents/coll/doc}");
assertThat(toStringResult).containsMatch("(?s)fields=\\{key123=string_value:.*value789.*}");
assertThat(toStringResult).contains("readTime=1970-01-01T00:00:00.000001000Z");
assertThat(toStringResult).contains("updateTime=1970-01-01T00:00:00.000002000Z");
assertThat(toStringResult).contains("createTime=1970-01-01T00:00:00.000003000Z");
assertThat(toStringResult).endsWith("}");
}

@Test
public void testWriteOperation() {
String toStringResult =
new UpdateBuilder.WriteOperation(
documentReference,
DocumentSnapshot.fromObject(
null,
documentReference,
Collections.singletonMap("key", "value"),
UserDataConverter.NO_DELETES)
.toPb())
.toString();
assertThat(toStringResult).startsWith("WriteOperation{");
assertThat(toStringResult)
.containsMatch("(?s)write=update\\s*\\{\\s*name:.*/documents/coll/doc.*}");
assertThat(toStringResult).containsMatch("doc=DocumentReference\\{path=.*/documents/coll/doc}");
assertThat(toStringResult).endsWith("}");
}

@Test
public void testWriteBatch() {
batch.update(documentReference, Collections.singletonMap("key", "value"));
String toStringResult = batch.toString();
assertThat(toStringResult).startsWith("WriteBatch{");
assertThat(toStringResult)
.containsMatch("(?s)writes=\\[WriteOperation\\{write=update.*/documents/coll/doc.*}]");
assertThat(toStringResult).contains("committed=false");
assertThat(toStringResult).endsWith("}");
}
}

0 comments on commit 6861ca0

Please sign in to comment.