-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCommitResponseTest.java
More file actions
170 lines (148 loc) · 7.37 KB
/
CommitResponseTest.java
File metadata and controls
170 lines (148 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2021 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.spanner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import com.google.cloud.Timestamp;
import com.google.spanner.v1.CommitResponse.CommitStats;
import com.google.spanner.v1.TransactionOptions.IsolationLevel;
import com.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class CommitResponseTest {
@Test
public void testConstructWithTimestamp() {
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100L, 100);
CommitResponse response = new CommitResponse(timestamp);
assertEquals(timestamp, response.getCommitTimestamp());
}
@Test
public void testFromProto() {
long mutationCount = 5L;
com.google.protobuf.Timestamp timestamp =
com.google.protobuf.Timestamp.newBuilder().setSeconds(123L).setNanos(456).build();
com.google.spanner.v1.CommitResponse proto =
com.google.spanner.v1.CommitResponse.newBuilder()
.setCommitStats(
com.google.spanner.v1.CommitResponse.CommitStats.newBuilder()
.setMutationCount(mutationCount)
.build())
.setCommitTimestamp(timestamp)
.build();
CommitResponse response = new CommitResponse(proto);
assertEquals(Timestamp.ofTimeSecondsAndNanos(123L, 456), response.getCommitTimestamp());
assertEquals(mutationCount, response.getCommitStats().getMutationCount());
}
@Test
public void testEqualsAndHashCode() {
com.google.spanner.v1.CommitResponse proto1 =
com.google.spanner.v1.CommitResponse.newBuilder()
.setCommitTimestamp(com.google.protobuf.Timestamp.newBuilder().setSeconds(1L).build())
.build();
com.google.spanner.v1.CommitResponse proto2 =
com.google.spanner.v1.CommitResponse.newBuilder()
.setCommitTimestamp(com.google.protobuf.Timestamp.newBuilder().setSeconds(2L).build())
.build();
com.google.spanner.v1.CommitResponse proto3 =
com.google.spanner.v1.CommitResponse.newBuilder()
.setCommitTimestamp(com.google.protobuf.Timestamp.newBuilder().setSeconds(1L).build())
.build();
CommitResponse response1 = new CommitResponse(proto1);
CommitResponse response2 = new CommitResponse(proto2);
CommitResponse response3 = new CommitResponse(proto3);
assertEquals(response3, response1);
assertNotEquals(response2, response1);
assertNotEquals(response3, response2);
assertNotEquals(response1, null);
assertNotEquals(response1, new Object());
assertEquals(response3.hashCode(), response1.hashCode());
assertNotEquals(response2.hashCode(), response1.hashCode());
assertNotEquals(response3.hashCode(), response2.hashCode());
}
@Test
public void testHasCommitStats() {
com.google.spanner.v1.CommitResponse protoWithoutCommitStats =
com.google.spanner.v1.CommitResponse.getDefaultInstance();
CommitResponse responseWithoutCommitStats = new CommitResponse(protoWithoutCommitStats);
assertFalse(responseWithoutCommitStats.hasCommitStats());
com.google.spanner.v1.CommitResponse protoWithCommitStats =
com.google.spanner.v1.CommitResponse.newBuilder()
.setCommitStats(CommitStats.getDefaultInstance())
.build();
CommitResponse responseWithCommitStats = new CommitResponse(protoWithCommitStats);
assertTrue(responseWithCommitStats.hasCommitStats());
}
@Test
public void testGetSnapshotTimestamp() {
com.google.spanner.v1.CommitResponse protoWithoutSnapshotTimestamp =
com.google.spanner.v1.CommitResponse.getDefaultInstance();
CommitResponse responseWithoutSnapshotTimestamp =
new CommitResponse(protoWithoutSnapshotTimestamp);
assertEquals(null, responseWithoutSnapshotTimestamp.getSnapshotTimestamp());
com.google.protobuf.Timestamp timestamp =
com.google.protobuf.Timestamp.newBuilder().setSeconds(123L).setNanos(456).build();
com.google.spanner.v1.CommitResponse protoWithSnapshotTimestamp =
com.google.spanner.v1.CommitResponse.newBuilder().setSnapshotTimestamp(timestamp).build();
CommitResponse responseWithSnapshotTimestamp = new CommitResponse(protoWithSnapshotTimestamp);
assertEquals(
Timestamp.ofTimeSecondsAndNanos(123L, 456),
responseWithSnapshotTimestamp.getSnapshotTimestamp());
}
@Test
public void testGetIsolationLevel() {
com.google.spanner.v1.CommitResponse protoWithoutIsolationLevel =
com.google.spanner.v1.CommitResponse.getDefaultInstance();
CommitResponse responseWithoutIsolationLevel = new CommitResponse(protoWithoutIsolationLevel);
assertEquals(null, responseWithoutIsolationLevel.getIsolationLevel());
com.google.spanner.v1.CommitResponse protoWithUnspecifiedIsolationLevel =
com.google.spanner.v1.CommitResponse.newBuilder()
.setIsolationLevel(IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED)
.build();
CommitResponse responseWithUnspecifiedIsolationLevel =
new CommitResponse(protoWithUnspecifiedIsolationLevel);
assertEquals(null, responseWithUnspecifiedIsolationLevel.getIsolationLevel());
com.google.spanner.v1.CommitResponse protoWithIsolationLevel =
com.google.spanner.v1.CommitResponse.newBuilder()
.setIsolationLevel(IsolationLevel.REPEATABLE_READ)
.build();
CommitResponse responseWithIsolationLevel = new CommitResponse(protoWithIsolationLevel);
assertEquals(IsolationLevel.REPEATABLE_READ, responseWithIsolationLevel.getIsolationLevel());
}
@Test
public void testGetReadLockMode() {
com.google.spanner.v1.CommitResponse protoWithoutReadLockMode =
com.google.spanner.v1.CommitResponse.getDefaultInstance();
CommitResponse responseWithoutReadLockMode = new CommitResponse(protoWithoutReadLockMode);
assertEquals(null, responseWithoutReadLockMode.getReadLockMode());
com.google.spanner.v1.CommitResponse protoWithUnspecifiedReadLockMode =
com.google.spanner.v1.CommitResponse.newBuilder()
.setReadLockMode(ReadLockMode.READ_LOCK_MODE_UNSPECIFIED)
.build();
CommitResponse responseWithUnspecifiedReadLockMode =
new CommitResponse(protoWithUnspecifiedReadLockMode);
assertEquals(null, responseWithUnspecifiedReadLockMode.getReadLockMode());
com.google.spanner.v1.CommitResponse protoWithReadLockMode =
com.google.spanner.v1.CommitResponse.newBuilder()
.setReadLockMode(ReadLockMode.PESSIMISTIC)
.build();
CommitResponse responseWithReadLockMode = new CommitResponse(protoWithReadLockMode);
assertEquals(ReadLockMode.PESSIMISTIC, responseWithReadLockMode.getReadLockMode());
}
}