Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Jun 10, 2024
1 parent bd3e6be commit 60346ba
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,22 @@ private TemporalAccessorTest selectTemporalAccessorTest(int id) throws SQLExcept
}

@Test
public void testJSONArray() throws SQLException {
public void testJSON() throws SQLException {
var list = listOf(1, 2, 3);

var id = insertJSONTest(list);

var jsonTest = selectJSONTest(id);
assertEquals(list, selectJSONTest(id).getValue());

assertEquals(list, jsonTest.getValue());
}

@Test
public void testJSONObject() throws SQLException {
var map = mapOf(
entry("a", 1),
entry("b", 2),
entry("c", 3)
);

var id = insertJSONTest(map);

var jsonTest = selectJSONTest(id);
updateJSONTest(id, map);

assertEquals(map, jsonTest.getValue());
assertEquals(map, selectJSONTest(id).getValue());
}

@Test
Expand All @@ -121,6 +114,17 @@ private int insertJSONTest(Object value) throws SQLException {
return queryBuilder.getGeneratedKey(0, Integer.class);
}

private void updateJSONTest(int id, Object value) throws SQLException {
var queryBuilder = QueryBuilder.update(JSONTest.class).filterByPrimaryKey("id");

try (var statement = queryBuilder.prepare(getConnection())) {
queryBuilder.executeUpdate(statement, mapOf(
entry("value", value),
entry("id", id)
));
}
}

private JSONTest selectJSONTest(int id) throws SQLException {
var queryBuilder = QueryBuilder.select(JSONTest.class).filterByPrimaryKey("id");

Expand All @@ -141,16 +145,30 @@ public void testXML() throws Exception {

var documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document document;
try (var inputStream = getClass().getResourceAsStream("test.xml")) {
document = documentBuilder.parse(inputStream);
Document document1;
try (var inputStream = getClass().getResourceAsStream("test1.xml")) {
document1 = documentBuilder.parse(inputStream);
}

var id = insertXMLTest(document1);

assertTrue(document1.isEqualNode(selectXMLTest(id).getDocument()));

Document document2;
try (var inputStream = getClass().getResourceAsStream("test2.xml")) {
document2 = documentBuilder.parse(inputStream);
}

var id = insertXMLTest(document);
updateXMLTest(id, document2);

var xmlTest = selectXMLTest(id);
assertTrue(document2.isEqualNode(selectXMLTest(id).getDocument()));
}

assertTrue(document.isEqualNode(xmlTest.getDocument()));
@Test
public void testXMLNull() {
var exception = assertThrows(SQLException.class, () -> insertXMLTest(null));

assertEquals(INTEGRITY_CONSTRAINT_VIOLATION_CODE, exception.getSQLState());
}

private int insertXMLTest(Document document) throws SQLException {
Expand All @@ -165,8 +183,19 @@ private int insertXMLTest(Document document) throws SQLException {
return queryBuilder.getGeneratedKey(0, Integer.class);
}

private void updateXMLTest(int id, Document document) throws SQLException {
var queryBuilder = QueryBuilder.updatePartial(XMLTest.class, "document");

try (var statement = queryBuilder.prepare(getConnection())) {
queryBuilder.executeUpdate(statement, mapOf(
entry("document", document),
entry("id", id)
));
}
}

private XMLTest selectXMLTest(int id) throws SQLException {
var queryBuilder = QueryBuilder.select(XMLTest.class).filterByPrimaryKey("id");
var queryBuilder = QueryBuilder.selectPartial(XMLTest.class, "document").filterByPrimaryKey("id");

try (var statement = queryBuilder.prepare(getConnection());
var results = queryBuilder.executeQuery(statement, mapOf(
Expand Down
7 changes: 7 additions & 0 deletions kilo-client/src/test/resources/org/httprpc/kilo/sql/test2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<d>D</d>
<e>E</e>
<f>F</f>
</root>

0 comments on commit 60346ba

Please sign in to comment.