@Test public void testUsefulArgumentOutputForDebug() { expectedException.expect(StatementException.class); expectedException.expectMessage("arguments:{positional:{7:8}, named:{name:brian}, finder:[{one=two},{lazy bean property arguments \"java.lang.Object"); h.createUpdate("insert into something (id, name) values (:id, :name)") .bind("name", "brian") .bind(7, 8) .bindMap(new HandyMapThing<String>().add("one", "two")) .bindBean(new Object()) .execute(); }
@Test public void testWritesViaFluentApi() { handle.createUpdate("insert into campaigns(id, caps) values (:id, :caps)") .bind("id", 3) .bindByType("caps", caps, QualifiedType.of(STRING_MAP).with(HStore.class)) .execute(); Map<String, String> newCaps = handle.createQuery("select caps from campaigns where id=?") .bind(0, 3) .mapTo(QualifiedType.of(STRING_MAP).with(HStore.class)) .findOnly(); assertThat(newCaps).isEqualTo(caps); }
@Test public void testInsertParamBinding() { int count = h.createUpdate("insert into something (id, name) values (?, 'eric')") .bind(0, 1) .execute(); assertThat(count).isEqualTo(1); }
@Test public void testString() { handle.createUpdate(INSERT_POSITIONAL).bind(0, "herp").execute(); assertThat(positional).isEqualTo("herp"); }
@Test public void testArgumentWithoutToString() { handle.createUpdate(INSERT_POSITIONAL).bind(0, (position, statement, ctx) -> statement.setString(1, "derp")).execute(); assertThat(positional).isNotEqualTo("derp"); }
@Test public void testUuid() { UUID u = UUID.randomUUID(); h.createUpdate("INSERT INTO foo VALUES (:uuid)") .bind("uuid", u) .execute(); assertThat(h.createQuery("SELECT * FROM foo").mapTo(UUID.class).findOnly()).isEqualTo(u); }
@Test public void testUuid() { UUID u = UUID.randomUUID(); h.createUpdate("INSERT INTO foo VALUES (:uuid)") .bind("uuid", u) .execute(); assertThat(h.createQuery("SELECT * FROM foo").mapTo(UUID.class).findOnly()).isEqualTo(u); } }
@Test public void testInsertUrlSuccessful() throws MalformedURLException { String googleString = "http://www.google.com"; URL googleUrl = new URL(googleString); handle.createUpdate("INSERT INTO foo VALUES (:url)") .bind("url", googleUrl) .execute(); URL actualUrl = handle.createQuery("SELECT url FROM foo").mapTo(URL.class).findOnly(); Assertions.assertThat(actualUrl).hasToString(googleUrl.toString()); }
@Test public void testArgumentHasToString() { handle.registerArgument(new ToStringFooArgumentFactory()); handle.createUpdate(INSERT_POSITIONAL).bind(0, new Foo()).execute(); assertThat(positional).isEqualTo("this is a Foo"); }
@Test public void testObjectHasToString() { handle.registerArgument(new FooArgumentFactory()); handle.createUpdate(INSERT_POSITIONAL).bind(0, new ToStringFoo()).execute(); assertThat(positional).isEqualTo("I'm a Foo"); }
@Test public void testMapNullUrlThrowsException() { handle.createUpdate("INSERT INTO foo VALUES (:url)") .bind("url", ((URL) null)) .execute(); assertThatThrownBy(() -> handle.createQuery("SELECT url FROM foo").mapTo(URL.class).findOnly()) .isInstanceOf(ResultSetException.class); }
@Test public void testBind() { h.createUpdate("INSERT INTO values VALUES(:value)") .bind("value", EnumT.BAR) .execute(); assertThat(h.createQuery("SELECT * FROM values").mapTo(String.class).findOnly()) .isEqualTo("BAR"); }
@Test public void testBothHaveToStringAndArgumentWins() { handle.registerArgument(new ToStringFooArgumentFactory()); handle.createUpdate(INSERT_POSITIONAL).bind(0, new ToStringFoo()).execute(); assertThat(positional).isEqualTo("this is a Foo"); }
@Test public void testNeitherHasToString() { handle.registerArgument(new FooArgumentFactory()); handle.createUpdate(INSERT_POSITIONAL).bind(0, new Foo()).execute(); assertThat(positional).containsPattern("@[0-9a-f]{1,8}$"); }
@Test public void testUri() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, :uri)") .bind("uri", TEST_URI).execute(); assertThat(h.createQuery("SELECT name FROM something") .mapTo(URI.class) .findOnly()).isEqualTo(TEST_URI); } }