From 8fa81b2885d2597cb90e31294595a09ce62ffa44 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 25 Mar 2018 18:10:14 -0700 Subject: Fix load by alternate key with Automatic primary key Storables ignore @Automatic properties when checking if the primary key is initialized. This makes sense for insert, because the Repository is responsible for populating the @Automatic property. However, this doesn't work for load--when the @Automatic property is also the primary key-- because it won't attempt to load by alternate key when it must. --- .../java/com/amazon/carbonado/TestStorables.java | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/test/java/com/amazon/carbonado/TestStorables.java') diff --git a/src/test/java/com/amazon/carbonado/TestStorables.java b/src/test/java/com/amazon/carbonado/TestStorables.java index b5b4fa8..e08e2ee 100644 --- a/src/test/java/com/amazon/carbonado/TestStorables.java +++ b/src/test/java/com/amazon/carbonado/TestStorables.java @@ -3513,6 +3513,33 @@ public class TestStorables extends TestCase { } } + public void test_loadByAltKeyWithAutomatic() throws Exception { + Storage storage = + getRepository().storageFor(AutomaticAndAltKey.class); + + // Insert a couple of records, starting with id=1. + // Note: manually set the Automatic ID property because not every + // Repository type will set it automatically. + for (int i=1; i <= 2; i++) { + AutomaticAndAltKey record = storage.prepare(); + record.setID(i); + record.setName("row" + i); + record.insert(); + } + + // Load an existing record. + AutomaticAndAltKey record = storage.prepare(); + record.setName("row2"); + assertTrue(record.tryLoad()); + assertEquals(2, record.getID()); + assertEquals("row2", record.getName()); + + // Load a non-existent record. + record = storage.prepare(); + record.setName("row3"); + assertFalse(record.tryLoad()); + } + private void assertUninitialized(boolean expected, Storable storable, String... properties) { for (String property : properties) { assertEquals(expected, storable.isPropertyUninitialized(property)); -- cgit v1.2.3