diff options
author | Clemens Fries <ormpaloompa@xenoworld.de> | 2016-05-18 18:46:27 +0200 |
---|---|---|
committer | xeno <xeno@eisberg.nacht> | 2016-05-19 00:36:52 +0200 |
commit | 62e2a4bbd5ba239fc413607b223822cf4146a673 (patch) | |
tree | dbff117e4d8fc44bfb969ebd28c9dc1a9dac586c /src/test/java |
Initial commit
Diffstat (limited to 'src/test/java')
3 files changed, 308 insertions, 0 deletions
diff --git a/src/test/java/de/xenoworld/ormpaloompa/TableTest.java b/src/test/java/de/xenoworld/ormpaloompa/TableTest.java new file mode 100644 index 0000000..79b3a0c --- /dev/null +++ b/src/test/java/de/xenoworld/ormpaloompa/TableTest.java @@ -0,0 +1,242 @@ +package de.xenoworld.ormpaloompa; + +import de.xenoworld.ormpaloompa.annotations.Field; +import de.xenoworld.ormpaloompa.annotations.TableInfo; +import de.xenoworld.ormpaloompa.testutils.DBRule; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; + +import java.sql.SQLException; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static de.xenoworld.ormpaloompa.WhereQuery.where; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + +/** + * Test object with explicit identity + */ +class Bird { + @Field(identity = true) + public Integer id; + + @Field + public String name; + + @Field + public String description; +} + +/** + * Test object with no explicit identity + */ +class Rabbit { + @Field + public Integer id; + + @Field + public String name; +} + +/** + * Test object with two identities. The first identity should be selected. + * Also, the identity here is a string. + */ +@TableInfo(tableName = "foxes") +class Fox { + @Field(identity = true) + public String name; + + @Field(identity = true) + public Integer id; +} + +/** + * A table with no id, this should throw a RuntimeException when used. + */ +@TableInfo +class Monkey { + @Field + public String name; +} + +public class TableTest { + static String[] FIXTURES = { + "CREATE TABLE birds (id INTEGER PRIMARY KEY, name TEXT, description TEXT);", + "INSERT INTO birds " + + "(id, name) " + + "VALUES " + + "(1, 'fluffy bird')," + + "(2, 'supersonic bird');", + + "CREATE TABLE rabbits (id INTEGER PRIMARY KEY, name TEXT);", + "INSERT INTO rabbits " + + "(id, name) " + + "VALUES " + + "(1, 'tiny rabbit')," + + "(2, 'giant rabbit');", + + "CREATE TABLE foxes (id INTEGER, name TEXT PRIMARY KEY);", + "INSERT INTO foxes " + + "(id, name) " + + "VALUES " + + "(1, 'red fox')," + + "(2, 'blue fox');", + + "CREATE TABLE snails (name TEXT PRIMARY KEY);", + "INSERT INTO snails " + + "(name) " + + "VALUES " + + "('slow snail')," + + "('fast snail');" + + }; + + @Rule + public DBRule dbRule = new DBRule(FIXTURES); + + /** + * The field `id` should be annotated as `@Field`, but it should not have + * property `identity` set to `true`. The field should be recognised as + * identity by virtue of being named `id`. + */ + @Test + public void testGetById_ImplicitIdFieldSelection() { + Table<Rabbit> table = new Table<>(Rabbit.class, dbRule.getConnection()); + + Rabbit rabbit = table.getById(1L).get(); + assertThat(rabbit.name, is("tiny rabbit")); + } + + /** + * This also effectively test the TableInfo annotation, declaring the table + * to be "foxes" instead of "foxs". + */ + @Test + public void testGetById_IdIsATextField() { + Table<Fox> table = new Table<>(Fox.class, dbRule.getConnection()); + + Fox fox = table.getById("red fox").get(); + + assertThat(fox.id, is(1)); + assertThat(fox.name, is("red fox")); + } + + @Test(expected = RuntimeException.class) + public void testNewTable_TableHasNoId() { + new Table<>(Monkey.class, dbRule.getConnection()); + } + + @Test + public void testCount() throws Exception { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + assertThat(table.count(), is(2)); + } + + @Test + public void testGetById() throws Exception { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + Bird bird = table.getById(1L).get(); + + assertThat(bird.name, is("fluffy bird")); + } + + @Test + public void testGetById_nonexistentId() throws Exception { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + assertThat(table.getById(0L).isPresent(), is(false)); + } + + @Test + public void testInsert() throws Exception { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + Bird newBird = new Bird(); + newBird.name = "A new bird"; + + Object id = table.insert(newBird); + + assertThat(id, is(3L)); + + Bird storedBird = table.getById(id).get(); + + assertThat(storedBird.name, is(newBird.name)); + } + + // TODO: Future: Return Identity instead of ROWID on insert() + @Ignore + @Test + public void testInsert_withStringId() throws Exception { + Table<Fox> table = new Table<>(Fox.class, dbRule.getConnection()); + + Fox newFox = new Fox(); + newFox.name = "A new fox"; + + Object id = table.insert(newFox); + + assertThat(id, is("A new fox")); + + Fox storedFox = table.getById(id).get(); + + assertThat(storedFox.name, is(newFox.name)); + } + + @Test + public void testFind() throws IllegalAccessException, SQLException, InstantiationException { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + List<Bird> list = table.find(where("id > ?") + .limit(1) + .orderBy("name", WhereQuery.Order.DESC), 0L) + .collect(Collectors.toList()); + + assertThat(list, hasSize(1)); + assertThat(list.get(0).name, is("supersonic bird")); + } + + @Test + public void testUpdate() throws Exception { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + Bird someBird = table.getById(1).get(); + assertThat(someBird.name, is("fluffy bird")); + + someBird.name = "hard bird"; + table.update(someBird); + + Bird sameBird = table.getById(1L).get(); + assertThat(sameBird.name, is("hard bird")); + } + + @Test + public void testUpdateOrInsert() throws Exception { + Table<Bird> table = new Table<>(Bird.class, dbRule.getConnection()); + + Bird newBird = new Bird(); + newBird.name = "green bird"; + + Optional<Object> insertId; + + insertId = table.updateOrInsert(newBird); + assertThat("Table grew to three entries", table.count(), is(3)); + assertThat("A new entry was created", insertId.isPresent(), is(true)); + + insertId = table.updateOrInsert(newBird); + assertThat("Table grew to four entries", table.count(), is(4)); + assertThat("A second entry was created", insertId.isPresent(), is(true)); + + Bird presentBird = table.getById(insertId.get()).get(); + presentBird.description = "This is a test"; + + insertId = table.updateOrInsert(presentBird); + assertThat("Table size did not change", table.count(), is(4)); + assertThat("No new entries were added", insertId.isPresent(), is(false)); + } +}
\ No newline at end of file diff --git a/src/test/java/de/xenoworld/ormpaloompa/WhereQueryTest.java b/src/test/java/de/xenoworld/ormpaloompa/WhereQueryTest.java new file mode 100644 index 0000000..3f3f472 --- /dev/null +++ b/src/test/java/de/xenoworld/ormpaloompa/WhereQueryTest.java @@ -0,0 +1,22 @@ +package de.xenoworld.ormpaloompa; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class WhereQueryTest { + + @Test + public void testWhereQuery() { + WhereQuery q; + + q = WhereQuery.where("foo > 0 AND bar = ?").limit(5).orderBy("bar", WhereQuery.Order.ASC); + assertEquals("foo > 0 AND bar = ? ORDER BY bar ASC LIMIT 5", q.toString()); + + q = WhereQuery.where("foo > 0 AND bar = ?").orderBy("bar", WhereQuery.Order.DESC); + assertEquals("foo > 0 AND bar = ? ORDER BY bar DESC", q.toString()); + + q = WhereQuery.where("foo > 0 AND bar = ?"); + assertEquals("foo > 0 AND bar = ?", q.toString()); + } +}
\ No newline at end of file diff --git a/src/test/java/de/xenoworld/ormpaloompa/testutils/DBRule.java b/src/test/java/de/xenoworld/ormpaloompa/testutils/DBRule.java new file mode 100644 index 0000000..0bb12f7 --- /dev/null +++ b/src/test/java/de/xenoworld/ormpaloompa/testutils/DBRule.java @@ -0,0 +1,44 @@ +package de.xenoworld.ormpaloompa.testutils; + +import org.junit.rules.ExternalResource; + +import java.sql.*; +import java.util.stream.Stream; + +public class DBRule extends ExternalResource { + protected Connection connection; + protected String[] fixtures; + + public DBRule(String... fixtures) { + this.fixtures = fixtures; + } + + @Override + protected void after() { + try { + connection.close(); + } catch (SQLException ignored) {} + } + + @Override + protected void before() throws Throwable { + Class.forName("org.sqlite.JDBC"); + connection = DriverManager.getConnection("jdbc:sqlite::memory:"); + + Statement statement = connection.createStatement(); + + Stream.of(fixtures).forEach(s -> { + try { + statement.addBatch(s); + } catch (SQLException e) { + e.printStackTrace(); + } + }); + + statement.executeBatch(); + } + + public Connection getConnection() { + return connection; + } +} |