Skip to content

Commit

Permalink
postgres integration test suite, fix for default object mapping for b…
Browse files Browse the repository at this point in the history
…yte array
  • Loading branch information
zsoltherpai committed May 15, 2020
1 parent 67f4fd6 commit 70054fb
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,8 @@ private static void basicTypes(Map<Class, ObjectMapperRsExtractor<?>> exs) {
}

private static void binaryTypes(Map<Class, ObjectMapperRsExtractor<?>> exs) {
reg(exs, byte[].class, (rs, i) -> {
Blob blob = rs.getBlob(i);
if (blob == null) {
return null;
}
byte[] data = blob.getBytes(1, (int) blob.length());
freeBlob(blob);
return data;
});
reg(exs, byte[].class, (rs, i) -> rs.getBytes(i));

reg(exs, ByteBuffer.class, (rs, i) -> {
Blob blob = rs.getBlob(i);
if (blob == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ abstract class IntegrationTestRoutine extends Specification {
["DUMMY", "dummy"].any { tables.contains(it) }
}

protected static void createTestTable(Connection connection) {
protected static void createTestTable(Connection connection, String binaryColumn) {
try {
new FluentJdbcBuilder().build().queryOn(connection).update(dropDummyTable).run()
} catch(Exception e) {
// ignorable
}
new FluentJdbcBuilder().build().queryOn(connection).update(createDummyTable).run()
new FluentJdbcBuilder().build().queryOn(connection).update(createDummyTable.replace("%BINCOLUMN%", binaryColumn)).run()
}

void removeContentAndVerify() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import java.time.LocalDate;
import java.time.Month;

class Dummies {
static final def dummy1 = new Dummy("idValue1", "stringValue1", LocalDate.of(2014, Month.MARCH, 12), new java.sql.Date(System.currentTimeMillis()));
static final def dummy2 = new Dummy("idValue2", "stringValue2", LocalDate.of(2014, Month.JANUARY, 2), new java.sql.Date(System.currentTimeMillis()));
static final def dummy1 = new Dummy("idValue1", "stringValue1", LocalDate.of(2014, Month.MARCH, 12), new java.sql.Date(System.currentTimeMillis()), "foo".getBytes());
static final def dummy2 = new Dummy("idValue2", "stringValue2", LocalDate.of(2014, Month.JANUARY, 2), new java.sql.Date(System.currentTimeMillis()), "foo".getBytes());

static Iterator<Map<String, Object>> namedBatchParams(Dummy... dummies) {
List<Map<String, Object>> allParams = []
Expand All @@ -31,6 +31,7 @@ class Dummies {
assert actual.dateLocalDate.equals(expected.dateLocalDate)
assert actual.dateSqlDate.toLocalDate().equals(expected.dateSqlDate.toLocalDate())
assert actual.nullString == null && expected.nullString == null
assert Arrays.equals(actual.bytearray, expected.bytearray)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class Dummy {
LocalDate dateLocalDate
java.sql.Date dateSqlDate
String nullString
byte[] byteArray
byte[] bytearray

Dummy(String id, String string, LocalDate dateLocalDate, java.sql.Date dateSqlDate) {
Dummy(String id, String string, LocalDate dateLocalDate, java.sql.Date dateSqlDate, byte[] bytearray) {
this.id = id
this.string = string
this.dateLocalDate = dateLocalDate
this.dateSqlDate = dateSqlDate
this.nullString = null
this.bytearray = bytearray
}

private Dummy() {
Expand All @@ -27,7 +28,8 @@ class Dummy {
string,
dateLocalDate,
dateSqlDate,
nullString
nullString,
bytearray
]
}

Expand All @@ -37,7 +39,8 @@ class Dummy {
"string" : string,
"dateLocalDate": dateLocalDate,
"dateSqlDate" : dateSqlDate,
"nullString" : nullString
"nullString" : nullString,
"bytearray" : bytearray
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.codejargon.fluentjdbc.integration.testdata;

class TestQuery {
public static final String insertSqlPositional = "INSERT INTO DUMMY(id, string, dateLocalDate, dateSqlDate, nullString) VALUES(?, ?, ?, ?, ?)";
public static final String insertSqlNamed = "INSERT INTO DUMMY(id, string, dateLocalDate, dateSqlDate, nullString) VALUES(:id, :string, :dateLocalDate, :dateSqlDate, :nullString)";
public static final String insertSqlPositional = "INSERT INTO DUMMY(id, string, dateLocalDate, dateSqlDate, nullString, bytearray) VALUES(?, ?, ?, ?, ?, ?)";
public static final String insertSqlNamed = "INSERT INTO DUMMY(id, string, dateLocalDate, dateSqlDate, nullString, bytearray) VALUES(:id, :string, :dateLocalDate, :dateSqlDate, :nullString, :bytearray)";
public static final String selectAllSql = "SELECT * FROM DUMMY";
public static final String selectIds = "SELECT * FROM DUMMY where id in (:ids)";
public static final String createDummyTable = "CREATE TABLE DUMMY (id VARCHAR(255) PRIMARY KEY, string VARCHAR(255), dateLocalDate DATE, dateSqlDate DATE, nullString VARCHAR(255))";
public static final String createDummyTable = "CREATE TABLE DUMMY (id VARCHAR(255) PRIMARY KEY, string VARCHAR(255), dateLocalDate DATE, dateSqlDate DATE, nullString VARCHAR(255), bytearray %BINCOLUMN%)";
public static final String dropDummyTable = "DROP TABLE DUMMY"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DerbyIntegrationTest extends IntegrationTestRoutine {
@BeforeClass
static void initH2() {
initDerbyDataSource()
createTestTable(sentry)
createTestTable(sentry, "BLOB")
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class H2IntegrationTest extends IntegrationTestRoutine {
@BeforeClass
static void initH2() {
initH2DataSource()
createTestTable(sentry)
createTestTable(sentry, "VARBINARY")
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HSQLIntegrationTest extends IntegrationTestRoutine {
@BeforeClass
static void initHsql() {
initH2DataSource()
createTestTable(sentry)
createTestTable(sentry, "VARBINARY(100)")
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PostgresIntegrationTest extends IntegrationTestRoutine {
def con = null
try {
con = ds.getConnection()
createTestTable(con)
createTestTable(con, "bytea")
} finally {
con.close()
}
Expand All @@ -34,4 +34,7 @@ class PostgresIntegrationTest extends IntegrationTestRoutine {
protected DataSource dataSource() {
return ds
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ObjectMapperTest extends Specification {
meta.getColumnLabel(12) >> "OPTIONAL_NON_EMPTY_COLUMN"
resultSet.getTimestamp(13) >> null
meta.getColumnLabel(13) >> "OPTIONAL_EMPTY_COLUMN"
resultSet.getBlob(14) >> new SerialBlob(expectedDummy.byteArrayColumn)
resultSet.getBytes(14) >> expectedDummy.byteArrayColumn
meta.getColumnLabel(14) >> "BYTE_ARRAY_COLUMN"
resultSet.getMetaData() >> meta
meta.getColumnCount() >> 14
Expand Down

0 comments on commit 70054fb

Please sign in to comment.