Skip to content
Permalink
Browse files
test: add insert .. on conflict tests for PostgreSQL 9.5
  • Loading branch information
vlsi committed Jan 7, 2016
1 parent 92011d7 commit d622a9fe928bdc2606e5399770d429c72ccfff49
@@ -112,6 +112,12 @@ public static TestSuite suite() throws Exception {
if (TestUtil.haveMinimumServerVersion(conn, "9.3")) {
suite.addTestSuite(ServerErrorTest.class);
}

if (TestUtil.haveMinimumServerVersion(conn, "9.5")) {
suite.addTestSuite(UpsertTest.class);
suite.addTestSuite(UpsertBinaryTest.class);
}

conn.close();

// That's all folks
@@ -0,0 +1,17 @@
package org.postgresql.test.jdbc2;

import java.util.Properties;

/**
* Tests {@code INSERT .. ON CONFLICT} in binary mode.
*/
public class UpsertBinaryTest extends UpsertTest {
public UpsertBinaryTest(String name) {
super(name);
}

@Override
protected void updateProperties(Properties props) {
forceBinary(props);
}
}
@@ -0,0 +1,67 @@
package org.postgresql.test.jdbc2;

import org.postgresql.test.TestUtil;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Tests {@code INSERT .. ON CONFLICT} introduced in PostgreSQL 9.5.
*/
public class UpsertTest extends BaseTest {
Statement stmt;

public UpsertTest(String name) {
super(name);
}

protected void setUp() throws Exception {
super.setUp();

TestUtil.createTempTable(con, "test_statement", "i int primary key, t varchar(5)");
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO test_statement(i, t) VALUES (42, '42')");
}

protected void tearDown() throws SQLException {
stmt.close();
TestUtil.dropTable(con, "test_statement");
super.tearDown();
}

protected int executeUpdate(String sql) throws SQLException {
PreparedStatement ps = con.prepareStatement(sql);
int count = ps.executeUpdate();
ps.close();
return count;
}

public void testUpsertDoNothingConflict() throws SQLException {
int count = executeUpdate(
"INSERT INTO test_statement(i, t) VALUES (42, '42') ON CONFLICT DO NOTHING");
assertEquals("insert on CONFLICT DO NOTHING should report 0 modified rows on CONFLICT",
0, count);
}

public void testUpsertDoNothingNoConflict() throws SQLException {
int count = executeUpdate(
"INSERT INTO test_statement(i, t) VALUES (43, '43') ON CONFLICT DO NOTHING");
assertEquals("insert on conflict DO NOTHING should report 1 modified row on plain insert",
1, count);
}

public void testUpsertDoUpdateConflict() throws SQLException {
int count = executeUpdate(
"INSERT INTO test_statement(i, t) VALUES (42, '42') ON CONFLICT(i) DO UPDATE SET t='43'");
assertEquals("insert ON CONFLICT DO UPDATE should report 1 modified row on CONFLICT",
1, count);
}

public void testUpsertDoUpdateNoConflict() throws SQLException {
int count = executeUpdate(
"INSERT INTO test_statement(i, t) VALUES (43, '43') ON CONFLICT(i) DO UPDATE SET t='43'");
assertEquals("insert on conflict do update should report 1 modified row on plain insert",
1, count);
}
}

0 comments on commit d622a9f

Please sign in to comment.