3838import com .google .cloud .spanner .TransactionRunner ;
3939import com .google .cloud .spanner .TransactionRunner .TransactionCallable ;
4040import java .util .Arrays ;
41+ import org .junit .Before ;
4142import org .junit .BeforeClass ;
4243import org .junit .ClassRule ;
4344import org .junit .Test ;
@@ -55,10 +56,13 @@ public final class ITDMLTest {
5556 /** Sequence for assigning unique keys to test cases. */
5657 private static int seq ;
5758
59+ /** Id prefix per test case. */
60+ private static int id ;
61+
5862 private static final String INSERT_DML =
59- "INSERT INTO T (k, v) VALUES ('boo1', 1), ('boo2', 2), ('boo3', 3), ('boo4', 4);" ;
60- private static final String UPDATE_DML = "UPDATE T SET T.V = 100 WHERE T.K LIKE 'boo%';" ;
61- private static final String DELETE_DML = "DELETE FROM T WHERE T.K like 'boo%';" ;
63+ "INSERT INTO T (k, v) VALUES ('%d- boo1', 1), ('%d- boo2', 2), ('%d- boo3', 3), ('%d- boo4', 4);" ;
64+ private static final String UPDATE_DML = "UPDATE T SET T.V = 100 WHERE T.K LIKE '%d- boo% %';" ;
65+ private static final String DELETE_DML = "DELETE FROM T WHERE T.K like '%d- boo% %';" ;
6266 private static final long DML_COUNT = 4 ;
6367
6468 private static boolean throwAbortOnce = false ;
@@ -75,10 +79,27 @@ public static void setUpDatabase() {
7579 client = env .getTestHelper ().getDatabaseClient (db );
7680 }
7781
82+ @ Before
83+ public void increaseTestId () {
84+ id ++;
85+ }
86+
7887 private static String uniqueKey () {
7988 return "k" + seq ++;
8089 }
8190
91+ private String insertDml () {
92+ return String .format (INSERT_DML , id , id , id , id );
93+ }
94+
95+ private String updateDml () {
96+ return String .format (UPDATE_DML , id );
97+ }
98+
99+ private String deleteDml () {
100+ return String .format (DELETE_DML , id );
101+ }
102+
82103 private void executeUpdate (long expectedCount , final String ... stmts ) {
83104 final TransactionCallable <Long > callable =
84105 new TransactionCallable <Long >() {
@@ -106,7 +127,7 @@ public Long run(TransactionContext transaction) {
106127 public void abortOnceShouldSucceedAfterRetry () {
107128 try {
108129 throwAbortOnce = true ;
109- executeUpdate (DML_COUNT , INSERT_DML );
130+ executeUpdate (DML_COUNT , insertDml () );
110131 assertThat (throwAbortOnce ).isFalse ();
111132 } catch (AbortedException e ) {
112133 fail ("Abort Exception not caught and retried" );
@@ -115,55 +136,55 @@ public void abortOnceShouldSucceedAfterRetry() {
115136
116137 @ Test
117138 public void partitionedDML () {
118- executeUpdate (DML_COUNT , INSERT_DML );
139+ executeUpdate (DML_COUNT , insertDml () );
119140 assertThat (
120141 client
121142 .singleUse (TimestampBound .strong ())
122- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" ))
143+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" ))
123144 .getLong (0 ))
124145 .isEqualTo (1 );
125146
126- long rowCount = client .executePartitionedUpdate (Statement .of (UPDATE_DML ));
147+ long rowCount = client .executePartitionedUpdate (Statement .of (updateDml () ));
127148 // Note: With PDML there is a possibility of network replay or partial update to occur, causing
128149 // this assert to fail. We should remove this assert if it is a recurring failure in IT tests.
129150 assertThat (rowCount ).isEqualTo (DML_COUNT );
130151 assertThat (
131152 client
132153 .singleUse (TimestampBound .strong ())
133- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" ))
154+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" ))
134155 .getLong (0 ))
135156 .isEqualTo (100 );
136157
137- rowCount = client .executePartitionedUpdate (Statement .of (DELETE_DML ));
158+ rowCount = client .executePartitionedUpdate (Statement .of (deleteDml () ));
138159 assertThat (rowCount ).isEqualTo (DML_COUNT );
139160 assertThat (
140161 client
141162 .singleUse (TimestampBound .strong ())
142- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" )))
163+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" )))
143164 .isNull ();
144165 }
145166
146167 @ Test
147168 public void standardDML () {
148- executeUpdate (DML_COUNT , INSERT_DML );
169+ executeUpdate (DML_COUNT , insertDml () );
149170 assertThat (
150171 client
151172 .singleUse (TimestampBound .strong ())
152- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" ))
173+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" ))
153174 .getLong (0 ))
154175 .isEqualTo (1 );
155- executeUpdate (DML_COUNT , UPDATE_DML );
176+ executeUpdate (DML_COUNT , updateDml () );
156177 assertThat (
157178 client
158179 .singleUse (TimestampBound .strong ())
159- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" ))
180+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" ))
160181 .getLong (0 ))
161182 .isEqualTo (100 );
162- executeUpdate (DML_COUNT , DELETE_DML );
183+ executeUpdate (DML_COUNT , deleteDml () );
163184 assertThat (
164185 client
165186 .singleUse (TimestampBound .strong ())
166- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" )))
187+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" )))
167188 .isNull ();
168189 }
169190
@@ -182,44 +203,48 @@ public void standardDMLWithError() {
182203
183204 @ Test
184205 public void standardDMLWithDuplicates () {
185- executeUpdate (DML_COUNT , INSERT_DML );
206+ executeUpdate (DML_COUNT , insertDml () );
186207
187208 executeUpdate (
188209 4 ,
189- "UPDATE T SET v = 200 WHERE k = 'boo1';" ,
190- "UPDATE T SET v = 300 WHERE k = 'boo1';" ,
191- "UPDATE T SET v = 400 WHERE k = 'boo1';" ,
192- "UPDATE T SET v = 500 WHERE k = 'boo1';" );
210+ String . format ( "UPDATE T SET v = 200 WHERE k = '%d- boo1';" , id ) ,
211+ String . format ( "UPDATE T SET v = 300 WHERE k = '%d- boo1';" , id ) ,
212+ String . format ( "UPDATE T SET v = 400 WHERE k = '%d- boo1';" , id ) ,
213+ String . format ( "UPDATE T SET v = 500 WHERE k = '%d- boo1';" , id ) );
193214 assertThat (
194215 client
195216 .singleUse (TimestampBound .strong ())
196- .readRow ("T" , Key .of (" boo1" ), Arrays .asList ("V" ))
217+ .readRow ("T" , Key .of (String . format ( "%d- boo1", id ) ), Arrays .asList ("V" ))
197218 .getLong (0 ))
198219 .isEqualTo (500 );
199220
200- executeUpdate (DML_COUNT , DELETE_DML , DELETE_DML );
221+ executeUpdate (DML_COUNT , deleteDml (), deleteDml () );
201222 }
202223
203224 @ Test
204225 public void standardDMLReadYourWrites () {
205- executeUpdate (DML_COUNT , INSERT_DML );
226+ executeUpdate (DML_COUNT , insertDml () );
206227
207228 final TransactionCallable <Void > callable =
208229 new TransactionCallable <Void >() {
209230 @ Override
210231 public Void run (TransactionContext transaction ) {
211232 long rowCount =
212- transaction .executeUpdate (Statement .of ("UPDATE T SET v = v * 2 WHERE k = 'boo2';" ));
233+ transaction .executeUpdate (
234+ Statement .of (String .format ("UPDATE T SET v = v * 2 WHERE k = '%d-boo2';" , id )));
213235 assertThat (rowCount ).isEqualTo (1 );
214- assertThat (transaction .readRow ("T" , Key .of ("boo2" ), Arrays .asList ("v" )).getLong (0 ))
236+ assertThat (
237+ transaction
238+ .readRow ("T" , Key .of (String .format ("%d-boo2" , id )), Arrays .asList ("v" ))
239+ .getLong (0 ))
215240 .isEqualTo (2 * 2 );
216241 return null ;
217242 }
218243 };
219244 TransactionRunner runner = client .readWriteTransaction ();
220245 runner .run (callable );
221246
222- executeUpdate (DML_COUNT , DELETE_DML );
247+ executeUpdate (DML_COUNT , deleteDml () );
223248 }
224249
225250 @ Test
@@ -233,7 +258,7 @@ class UserException extends Exception {
233258 new TransactionCallable <Void >() {
234259 @ Override
235260 public Void run (TransactionContext transaction ) throws UserException {
236- long rowCount = transaction .executeUpdate (Statement .of (INSERT_DML ));
261+ long rowCount = transaction .executeUpdate (Statement .of (insertDml () ));
237262 assertThat (rowCount ).isEqualTo (DML_COUNT );
238263 throw new UserException ("failing to commit" );
239264 }
@@ -252,7 +277,10 @@ public Void run(TransactionContext transaction) throws UserException {
252277 ResultSet resultSet =
253278 client
254279 .singleUse (TimestampBound .strong ())
255- .read ("T" , KeySet .range (KeyRange .prefix (Key .of ("boo" ))), Arrays .asList ("K" ));
280+ .read (
281+ "T" ,
282+ KeySet .range (KeyRange .prefix (Key .of (String .format ("%d-boo" , id )))),
283+ Arrays .asList ("K" ));
256284 assertThat (resultSet .next ()).isFalse ();
257285 }
258286
@@ -312,8 +340,8 @@ public Long run(TransactionContext transaction) {
312340
313341 @ Test
314342 public void standardDMLWithExecuteSQL () {
315- executeQuery (DML_COUNT , INSERT_DML );
343+ executeQuery (DML_COUNT , insertDml () );
316344 // checks for multi-stmts within a txn, therefore also verifying seqNo.
317- executeQuery (DML_COUNT * 2 , UPDATE_DML , DELETE_DML );
345+ executeQuery (DML_COUNT * 2 , updateDml (), deleteDml () );
318346 }
319347}
0 commit comments