@@ -29,6 +29,8 @@ public class DatastoreServiceTest {
2929 private static final Key KEY1 = PARTIAL_KEY1 .newKey ("name" );
3030 private static final Key KEY2 = Key .builder (KEY1 , KIND2 , 1 ).build ();
3131 private static final Key KEY3 = Key .builder (KEY2 ).name ("bla" ).build ();
32+ private static final Key KEY4 = KEY2 .newKey ("newName1" );
33+ private static final Key KEY5 = KEY2 .newKey ("newName2" );
3234 private static final KeyValue KEY_VALUE = new KeyValue (KEY1 );
3335 private static final ListValue LIST_VALUE1 = ListValue .builder ()
3436 .addValue (NULL_VALUE )
@@ -80,7 +82,7 @@ public void setUp() {
8082 .build ();
8183 datastore = DatastoreServiceFactory .getDefault (options );
8284 // Prepare data for testing
83- datastore .delete (KEY1 , KEY2 , KEY3 );
85+ datastore .delete (KEY1 , KEY2 , KEY3 , KEY4 , KEY5 );
8486 datastore .add (ENTITY1 , ENTITY2 );
8587 }
8688
@@ -91,13 +93,119 @@ public void testGetOptions() {
9193
9294 @ Test
9395 public void testNewTransactionCommit () {
94- // TODO (also try list value with different types)
95- fail ("Not yet implemented" );
96+ Transaction transaction = datastore .newTransaction ();
97+ transaction .add (ENTITY3 );
98+ Entity entity2 = Entity .builder (ENTITY2 )
99+ .clearProperties ()
100+ .setNullProperty ("bla" )
101+ .build ();
102+ transaction .update (entity2 );
103+ transaction .delete (KEY1 );
104+ transaction .commit ();
105+
106+ Iterator <Entity > iter = datastore .get (KEY1 , KEY2 , KEY3 );
107+ assertNull (iter .next ());
108+ assertEquals (entity2 , iter .next ());
109+ assertEquals (ENTITY3 , iter .next ());
110+ assertFalse (iter .hasNext ());
111+
112+ try {
113+ transaction .commit ();
114+ fail ("Expecting a failure" );
115+ } catch (DatastoreServiceException ex ) {
116+ // expected to fail
117+ }
118+
119+ try {
120+ transaction .rollback ();
121+ fail ("Expecting a failure" );
122+ } catch (DatastoreServiceException ex ) {
123+ // expected to fail
124+ }
125+
126+ verifyNotUsable (transaction );
127+ }
128+
129+ @ Test
130+ public void testTransactionWithRead () {
131+ Transaction transaction = datastore .newTransaction ();
132+ assertNull (transaction .get (KEY3 ));
133+ transaction .add (ENTITY3 );
134+ transaction .commit ();
135+ assertEquals (ENTITY3 , datastore .get (KEY3 ));
136+
137+ transaction = datastore .newTransaction ();
138+ assertEquals (ENTITY3 , transaction .get (KEY3 ));
139+ // update entity3 during the transaction
140+ datastore .put (Entity .builder (ENTITY3 ).clearProperties ().build ());
141+ transaction .update (ENTITY2 );
142+ try {
143+ transaction .commit ();
144+ fail ("Expecting a failure" );
145+ } catch (DatastoreServiceException expected ) {
146+ expected .printStackTrace ();
147+ assertEquals (DatastoreServiceException .Code .ABORTED , expected .code ());
148+ }
96149 }
97150
98151 @ Test
99152 public void testNewTransactionRollback () {
100- fail ("Not yet implemented" );
153+ Transaction transaction = datastore .newTransaction ();
154+ transaction .add (ENTITY3 );
155+ Entity entity2 = Entity .builder (ENTITY2 )
156+ .clearProperties ()
157+ .setNullProperty ("bla" )
158+ .setListProperty ("list3" , new StringValue ("bla" ), StringValue .builder ("bla" ).build ())
159+ .build ();
160+ transaction .update (entity2 );
161+ transaction .delete (KEY1 );
162+ transaction .rollback ();
163+ transaction .rollback (); // should be safe to repeat rollback calls
164+
165+ try {
166+ transaction .commit ();
167+ fail ("Expecting a failure" );
168+ } catch (DatastoreServiceException ex ) {
169+ // expected to fail
170+ }
171+
172+ verifyNotUsable (transaction );
173+
174+ Iterator <Entity > iter = datastore .get (KEY1 , KEY2 , KEY3 );
175+ assertEquals (ENTITY1 , iter .next ());
176+ assertEquals (ENTITY2 , iter .next ());
177+ assertNull (iter .next ());
178+ assertFalse (iter .hasNext ());
179+ }
180+
181+ private void verifyNotUsable (DatastoreWriter writer ) {
182+ try {
183+ writer .add (ENTITY3 );
184+ fail ("Expecting a failure" );
185+ } catch (DatastoreServiceException ex ) {
186+ // expected to fail
187+ }
188+
189+ try {
190+ writer .put (ENTITY3 );
191+ fail ("Expecting a failure" );
192+ } catch (DatastoreServiceException ex ) {
193+ // expected to fail
194+ }
195+
196+ try {
197+ writer .update (ENTITY3 );
198+ fail ("Expecting a failure" );
199+ } catch (DatastoreServiceException ex ) {
200+ // expected to fail
201+ }
202+
203+ try {
204+ writer .delete (ENTITY3 .key ());
205+ fail ("Expecting a failure" );
206+ } catch (DatastoreServiceException ex ) {
207+ // expected to fail
208+ }
101209 }
102210
103211 @ Test
@@ -108,10 +216,10 @@ public void testNewBatchWriter() {
108216 .clearProperties ()
109217 .setNullProperty ("bla" )
110218 .build ();
111- Entity entity4 = Entity .builder (KEY2 . newKey ( "newName1" ) )
219+ Entity entity4 = Entity .builder (KEY4 )
112220 .setProperty ("value" , new StringValue ("value" ))
113221 .build ();
114- Entity entity5 = Entity .builder (KEY2 . newKey ( "newName2" ) )
222+ Entity entity5 = Entity .builder (KEY5 )
115223 .setStringProperty ("value" , "value" )
116224 .build ();
117225 batchWriter .add (entity4 , entity5 );
@@ -127,9 +235,12 @@ public void testNewBatchWriter() {
127235
128236 try {
129237 batchWriter .submit ();
238+ fail ("Expecting a failure" );
130239 } catch (DatastoreServiceException ex ) {
131240 // expected to fail
132241 }
242+ verifyNotUsable (batchWriter );
243+
133244 batchWriter = datastore .newBatchWriter ();
134245 batchWriter .delete (entity4 .key (), entity5 .key ());
135246 batchWriter .update (ENTITY1 , ENTITY2 , ENTITY3 );
@@ -142,7 +253,11 @@ public void testNewBatchWriter() {
142253 assertNull (entities .next ());
143254 assertFalse (entities .hasNext ());
144255
145- // TODO need to cover more edge cases (ds failures, re-use of same entities,..)
256+ // TODO need to cover the cases of:
257+ // delete after put/add/update
258+ // put after delete/add/update
259+ // update after delete/add/put
260+ // add after delete/update/put
146261 }
147262
148263 @ Test
@@ -235,6 +350,7 @@ public void testGetArray() {
235350 assertFalse (entity3 .hasProperty ("bla" ));
236351 try {
237352 entity3 .stringProperty ("str" );
353+ fail ("Expecting a failure" );
238354 } catch (DatastoreServiceException expected ) {
239355 // expected - no such property
240356 }
@@ -250,6 +366,7 @@ public void testAdd() {
250366
251367 try {
252368 datastore .add (ENTITY1 );
369+ fail ("Expecting a failure" );
253370 } catch (DatastoreServiceException expected ) {
254371 // expected;
255372 }
@@ -266,6 +383,7 @@ public void testUpdate() {
266383
267384 try {
268385 datastore .update (ENTITY3 );
386+ fail ("Expecting a failure" );
269387 } catch (DatastoreServiceException expected ) {
270388 // expected;
271389 }
0 commit comments