Skip to content

Commit 9df8028

Browse files
feat: Add toString methods to classes comprising WriteBatch (#1281)
* 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>
1 parent e59362d commit 9df8028

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,11 @@ public boolean equals(Object obj) {
448448
public int hashCode() {
449449
return Objects.hash(rpcContext, docRef, fields);
450450
}
451+
452+
@Override
453+
public String toString() {
454+
return String.format(
455+
"%s{doc=%s, fields=%s, readTime=%s, updateTime=%s, createTime=%s}",
456+
getClass().getSimpleName(), docRef, fields, readTime, updateTime, createTime);
457+
}
451458
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/UpdateBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ static class WriteOperation {
5353
this.documentReference = documentReference;
5454
this.write = write;
5555
}
56+
57+
@Override
58+
public String toString() {
59+
return String.format("WriteOperation{write=%s, doc=%s}", write, documentReference);
60+
}
5661
}
5762

5863
final FirestoreImpl firestore;
@@ -646,4 +651,10 @@ List<WriteOperation> getWrites() {
646651
public int getMutationsSize() {
647652
return writes.size();
648653
}
654+
655+
@Override
656+
public String toString() {
657+
return String.format(
658+
"%s{writes=%s, committed=%s}", getClass().getSimpleName(), writes, committed);
659+
}
649660
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.Timestamp;
22+
import com.google.cloud.firestore.spi.v1.FirestoreRpc;
23+
import com.google.firestore.v1.Value;
24+
import java.util.Collections;
25+
import java.util.Map;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.mockito.Mockito;
30+
import org.mockito.Spy;
31+
import org.mockito.runners.MockitoJUnitRunner;
32+
33+
/** @author Eran Leshem */
34+
@RunWith(MockitoJUnitRunner.class)
35+
public class ToStringTest {
36+
37+
@Spy
38+
private final FirestoreImpl firestoreMock =
39+
new FirestoreImpl(
40+
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
41+
Mockito.mock(FirestoreRpc.class));
42+
43+
private WriteBatch batch;
44+
private DocumentReference documentReference;
45+
46+
@Before
47+
public void before() {
48+
batch = firestoreMock.batch();
49+
documentReference = firestoreMock.document("coll/doc");
50+
}
51+
52+
@Test
53+
public void testDocumentSnapshot() {
54+
Map<String, Value> fields =
55+
Collections.singletonMap(
56+
"key123",
57+
UserDataConverter.encodeValue(
58+
FieldPath.of("key456"),
59+
CustomClassMapper.convertToPlainJavaTypes("value789"),
60+
UserDataConverter.NO_DELETES));
61+
String toStringResult =
62+
new DocumentSnapshot(
63+
null,
64+
documentReference,
65+
fields,
66+
Timestamp.ofTimeMicroseconds(1),
67+
Timestamp.ofTimeMicroseconds(2),
68+
Timestamp.ofTimeMicroseconds(3))
69+
.toString();
70+
assertThat(toStringResult).startsWith("DocumentSnapshot{");
71+
assertThat(toStringResult).containsMatch("doc=DocumentReference\\{path=.*/documents/coll/doc}");
72+
assertThat(toStringResult).containsMatch("(?s)fields=\\{key123=string_value:.*value789.*}");
73+
assertThat(toStringResult).contains("readTime=1970-01-01T00:00:00.000001000Z");
74+
assertThat(toStringResult).contains("updateTime=1970-01-01T00:00:00.000002000Z");
75+
assertThat(toStringResult).contains("createTime=1970-01-01T00:00:00.000003000Z");
76+
assertThat(toStringResult).endsWith("}");
77+
}
78+
79+
@Test
80+
public void testWriteOperation() {
81+
String toStringResult =
82+
new UpdateBuilder.WriteOperation(
83+
documentReference,
84+
DocumentSnapshot.fromObject(
85+
null,
86+
documentReference,
87+
Collections.singletonMap("key", "value"),
88+
UserDataConverter.NO_DELETES)
89+
.toPb())
90+
.toString();
91+
assertThat(toStringResult).startsWith("WriteOperation{");
92+
assertThat(toStringResult)
93+
.containsMatch("(?s)write=update\\s*\\{\\s*name:.*/documents/coll/doc.*}");
94+
assertThat(toStringResult).containsMatch("doc=DocumentReference\\{path=.*/documents/coll/doc}");
95+
assertThat(toStringResult).endsWith("}");
96+
}
97+
98+
@Test
99+
public void testWriteBatch() {
100+
batch.update(documentReference, Collections.singletonMap("key", "value"));
101+
String toStringResult = batch.toString();
102+
assertThat(toStringResult).startsWith("WriteBatch{");
103+
assertThat(toStringResult)
104+
.containsMatch("(?s)writes=\\[WriteOperation\\{write=update.*/documents/coll/doc.*}]");
105+
assertThat(toStringResult).contains("committed=false");
106+
assertThat(toStringResult).endsWith("}");
107+
}
108+
}

0 commit comments

Comments
 (0)