Skip to content

Commit 44b313f

Browse files
committed
Add tests for MySQL BIT and PostgreSQL BYTEA columns
Signed-off-by: Joern Bernhardt <[email protected]>
1 parent 2564ecb commit 44b313f

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

src/test/java/io/vertx/ext/asyncsql/MySQLClientTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import io.vertx.core.Handler;
2121
import io.vertx.core.json.JsonArray;
2222
import io.vertx.core.json.JsonObject;
23+
import io.vertx.ext.sql.ResultSet;
2324
import io.vertx.ext.sql.SQLConnection;
2425
import io.vertx.ext.sql.UpdateResult;
2526
import io.vertx.ext.unit.Async;
2627
import io.vertx.ext.unit.TestContext;
2728
import org.junit.Before;
2829
import org.junit.Test;
2930

31+
import java.util.Arrays;
32+
3033
public class MySQLClientTest extends SQLTestBase {
3134

3235

@@ -93,6 +96,36 @@ public void testInsertedIds(TestContext context) {
9396
});
9497
}
9598

99+
@Override
100+
protected String createByteArray1TableColumn() {
101+
return "BIT(1)";
102+
}
103+
104+
@Override
105+
protected String createByteArray2TableColumn() {
106+
return "BIT(2)";
107+
}
108+
109+
@Override
110+
protected String createByteArray3TableColumn() {
111+
return "BIT(9)";
112+
}
113+
114+
@Override
115+
protected String[] insertByteArray1Values() {
116+
return new String[]{"B'1'", "B'0'", "B'1'"};
117+
}
118+
119+
@Override
120+
protected String[] insertByteArray2Values() {
121+
return new String[]{"B'10'", "B'01'", "B'11'"};
122+
}
123+
124+
@Override
125+
protected String[] insertByteArray3Values() {
126+
return new String[]{"B'100000000'", "B'000000001'", "B'100000001'"};
127+
}
128+
96129
private void setupAutoIncrementTable(SQLConnection conn, Handler<AsyncResult<Void>> handler) {
97130
conn.execute("BEGIN",
98131
ar -> conn.execute("DROP TABLE IF EXISTS test_table",

src/test/java/io/vertx/ext/asyncsql/PostgreSQLClientTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.Before;
2828
import org.junit.Test;
2929

30+
import java.util.Arrays;
3031
import java.util.UUID;
3132

3233
public class PostgreSQLClientTest extends SQLTestBase {
@@ -111,6 +112,36 @@ public void testUsingUUIDsInTables(TestContext context) {
111112
});
112113
}
113114

115+
@Override
116+
protected String createByteArray1TableColumn() {
117+
return "BYTEA";
118+
}
119+
120+
@Override
121+
protected String createByteArray2TableColumn() {
122+
return "BYTEA";
123+
}
124+
125+
@Override
126+
protected String createByteArray3TableColumn() {
127+
return "BYTEA";
128+
}
129+
130+
@Override
131+
protected String[] insertByteArray1Values() {
132+
return new String[]{"E'\\x01'", "E'\\\\x00'", "E'\\\\x01'"};
133+
}
134+
135+
@Override
136+
protected String[] insertByteArray2Values() {
137+
return new String[]{"E'\\\\x02'", "E'\\\\x01'", "E'\\\\x03'"};
138+
}
139+
140+
@Override
141+
protected String[] insertByteArray3Values() {
142+
return new String[]{"E'\\\\x0100'", "E'\\\\x0001'", "E'\\\\x0101'"};
143+
}
144+
114145
private void setupAutoIncrementTable(SQLConnection conn, Handler<AsyncResult<Void>> handler) {
115146
conn.execute("BEGIN",
116147
ar -> conn.execute("DROP TABLE IF EXISTS test_table",

src/test/java/io/vertx/ext/asyncsql/SQLTestBase.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,94 @@ public void testDecimalFields(TestContext context) {
492492
});
493493
}
494494

495+
protected abstract String createByteArray1TableColumn();
496+
protected abstract String createByteArray2TableColumn();
497+
protected abstract String createByteArray3TableColumn();
498+
protected abstract String[] insertByteArray1Values();
499+
protected abstract String[] insertByteArray2Values();
500+
protected abstract String[] insertByteArray3Values();
501+
502+
@Test
503+
public void testByteA1Fields(TestContext context) {
504+
Async async = context.async();
505+
client.getConnection(arConn -> {
506+
ensureSuccess(context, arConn);
507+
conn = arConn.result();
508+
conn.execute("DROP TABLE IF EXISTS test_table", arDrop -> {
509+
ensureSuccess(context, arDrop);
510+
conn.execute("CREATE TABLE test_table (id INT, some_bit " + createByteArray1TableColumn() + ")", arCreate -> {
511+
ensureSuccess(context, arCreate);
512+
String[] s = insertByteArray1Values();
513+
conn.execute("INSERT INTO test_table (id, some_bit) VALUES (1, " + s[0] + "),(2, " + s[1] + "),(3, " + s[2] + ")", arInsert -> {
514+
ensureSuccess(context, arInsert);
515+
conn.query("SELECT some_bit FROM test_table ORDER BY id", arQuery -> {
516+
ensureSuccess(context, arQuery);
517+
ResultSet res = arQuery.result();
518+
context.assertTrue(Arrays.equals(new byte[]{0b1}, res.getRows().get(0).getBinary("some_bit")));
519+
context.assertTrue(Arrays.equals(new byte[]{0b0}, res.getResults().get(1).getBinary(0)));
520+
context.assertTrue(Arrays.equals(new byte[]{0b1}, res.getRows().get(2).getBinary("some_bit")));
521+
async.complete();
522+
});
523+
});
524+
});
525+
});
526+
});
527+
}
528+
529+
@Test
530+
public void testByteA2Fields(TestContext context) {
531+
Async async = context.async();
532+
client.getConnection(arConn -> {
533+
ensureSuccess(context, arConn);
534+
conn = arConn.result();
535+
conn.execute("DROP TABLE IF EXISTS test_table", arDrop -> {
536+
ensureSuccess(context, arDrop);
537+
conn.execute("CREATE TABLE test_table (id INT, some_bit " + createByteArray2TableColumn() + ")", arCreate -> {
538+
ensureSuccess(context, arCreate);
539+
String[] s = insertByteArray2Values();
540+
conn.execute("INSERT INTO test_table (id, some_bit) VALUES (1, " + s[0] + "),(2, " + s[1] + "),(3, " + s[2] + ")", arInsert -> {
541+
ensureSuccess(context, arInsert);
542+
conn.query("SELECT some_bit FROM test_table ORDER BY id", arQuery -> {
543+
ensureSuccess(context, arQuery);
544+
ResultSet res = arQuery.result();
545+
context.assertTrue(Arrays.equals(new byte[]{0b10}, res.getRows().get(0).getBinary("some_bit")));
546+
context.assertTrue(Arrays.equals(new byte[]{0b01}, res.getResults().get(1).getBinary(0)));
547+
context.assertTrue(Arrays.equals(new byte[]{0b11}, res.getRows().get(2).getBinary("some_bit")));
548+
async.complete();
549+
});
550+
});
551+
});
552+
});
553+
});
554+
}
555+
556+
@Test
557+
public void testByteA3Fields(TestContext context) {
558+
Async async = context.async();
559+
client.getConnection(arConn -> {
560+
ensureSuccess(context, arConn);
561+
conn = arConn.result();
562+
conn.execute("DROP TABLE IF EXISTS test_table", arDrop -> {
563+
ensureSuccess(context, arDrop);
564+
conn.execute("CREATE TABLE test_table (id INT, some_bit " + createByteArray3TableColumn() + ")", arCreate -> {
565+
ensureSuccess(context, arCreate);
566+
String[] s = insertByteArray3Values();
567+
conn.execute("INSERT INTO test_table (id, some_bit) VALUES (1, " + s[0] + "),(2, " + s[1] + "),(3, " + s[2] + ")", arInsert -> {
568+
ensureSuccess(context, arInsert);
569+
conn.query("SELECT some_bit FROM test_table ORDER BY id", arQuery -> {
570+
ensureSuccess(context, arQuery);
571+
ResultSet res = arQuery.result();
572+
context.assertTrue(Arrays.equals(new byte[]{0b1, 0b0}, res.getRows().get(0).getBinary("some_bit")));
573+
context.assertTrue(Arrays.equals(new byte[]{0b0, 0b1}, res.getResults().get(1).getBinary(0)));
574+
context.assertTrue(Arrays.equals(new byte[]{0b1, 0b1}, res.getRows().get(2).getBinary("some_bit")));
575+
async.complete();
576+
});
577+
});
578+
});
579+
});
580+
});
581+
}
582+
495583
protected void setSqlModeIfPossible(Handler<Void> handler) {
496584
handler.handle(null);
497585
}

0 commit comments

Comments
 (0)