Skip to content

Commit 9ef89ba

Browse files
test: add DatastoreException assertions in integration tests (googleapis#1396)
* test: add DatastoreException assertions in integration tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix nested assertions --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 2fce0f3 commit 9ef89ba

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,14 @@ public void testNewTransactionCommit() {
463463
transaction.commit();
464464
fail("Expecting a failure");
465465
} catch (DatastoreException expected) {
466-
assertEquals("FAILED_PRECONDITION", expected.getReason());
466+
assertDatastoreException(expected, "FAILED_PRECONDITION", 0);
467467
}
468468

469469
try {
470470
transaction.rollback();
471471
fail("Expecting a failure");
472472
} catch (DatastoreException expected) {
473-
assertEquals("FAILED_PRECONDITION", expected.getReason());
473+
assertDatastoreException(expected, "FAILED_PRECONDITION", 0);
474474
}
475475
}
476476

@@ -666,7 +666,7 @@ public void testNewTransactionRollback() {
666666
transaction.commit();
667667
fail("Expecting a failure");
668668
} catch (DatastoreException expected) {
669-
assertEquals("FAILED_PRECONDITION", expected.getReason());
669+
assertDatastoreException(expected, "FAILED_PRECONDITION", 0);
670670
}
671671

672672
List<Entity> list = datastore.fetch(KEY1, KEY2, KEY3);
@@ -718,7 +718,7 @@ public void testNewBatch() {
718718
batch.submit();
719719
fail("Expecting a failure");
720720
} catch (DatastoreException expected) {
721-
assertEquals("FAILED_PRECONDITION", expected.getReason());
721+
assertDatastoreException(expected, "FAILED_PRECONDITION", 0);
722722
}
723723

724724
batch = datastore.newBatch();
@@ -1430,7 +1430,7 @@ public void testGetArrayNoDeferredResults() {
14301430
entity3.getString("str");
14311431
fail("Expecting a failure");
14321432
} catch (DatastoreException expected) {
1433-
// expected - no such property
1433+
assertDatastoreException(expected, "FAILED_PRECONDITION", 0);
14341434
}
14351435
assertFalse(result.hasNext());
14361436
datastore.delete(ENTITY3.getKey());
@@ -1447,7 +1447,7 @@ public void testAddEntity() {
14471447
datastore.add(ENTITY1);
14481448
fail("Expecting a failure");
14491449
} catch (DatastoreException expected) {
1450-
// expected;
1450+
assertDatastoreException(expected, "ALREADY_EXISTS", 6);
14511451
}
14521452

14531453
List<Entity> entities = datastore.add(ENTITY3, PARTIAL_ENTITY1, PARTIAL_ENTITY2);
@@ -1475,7 +1475,7 @@ public void testUpdate() {
14751475
datastore.update(ENTITY3);
14761476
fail("Expecting a failure");
14771477
} catch (DatastoreException expected) {
1478-
// expected;
1478+
assertDatastoreException(expected, "NOT_FOUND", 5);
14791479
}
14801480
datastore.add(ENTITY3);
14811481
assertEquals(ENTITY3, datastore.get(ENTITY3.getKey()));
@@ -1566,7 +1566,7 @@ public Integer run(DatastoreReaderWriter transaction) {
15661566
datastore.runInTransaction(callable2);
15671567
fail("Expecting a failure");
15681568
} catch (DatastoreException expected) {
1569-
assertEquals(4, ((DatastoreException) expected.getCause()).getCode());
1569+
assertDatastoreException((DatastoreException) expected.getCause(), "DEADLINE_EXCEEDED", 4);
15701570
}
15711571
}
15721572

@@ -1620,7 +1620,7 @@ public Integer run(DatastoreReaderWriter transaction) {
16201620
datastore.runInTransaction(callable2, readOnlyOptions);
16211621
fail("Expecting a failure");
16221622
} catch (DatastoreException expected) {
1623-
assertEquals(3, ((DatastoreException) expected.getCause()).getCode());
1623+
assertDatastoreException((DatastoreException) expected.getCause(), "INVALID_ARGUMENT", 3);
16241624
}
16251625
}
16261626

@@ -1836,4 +1836,10 @@ private void testCountAggregationReadTimeWith(Consumer<AggregationQuery.Builder>
18361836
datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
18371837
}
18381838
}
1839+
1840+
private void assertDatastoreException(
1841+
DatastoreException expected, String reason, int statusCode) {
1842+
assertEquals(reason, expected.getReason());
1843+
assertEquals(statusCode, expected.getCode());
1844+
}
18391845
}

0 commit comments

Comments
 (0)