Skip to content

Commit

Permalink
tests: UUID vs setString test
Browse files Browse the repository at this point in the history
By default, setString sends 'varchar' datatype, and PostgreSQL might result in
<<ERROR: column "id" is of type uuid but expression is of type character varying>>

There's stringType option that enables sending strings as UNSPECIFIED type to overcome that error.

fixes #1133
  • Loading branch information
vlsi committed Mar 9, 2018
1 parent af64ed2 commit 5827858
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/BaseTest4.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ public enum AutoCommit {
YES, NO
}

public enum StringType {
UNSPECIFIED, VARCHAR;
}

protected Connection con;
private BinaryMode binaryMode;
private ReWriteBatchedInserts reWriteBatchedInserts;
protected PreferQueryMode preferQueryMode;
private StringType stringType;

protected void updateProperties(Properties props) {
if (binaryMode == BinaryMode.FORCE) {
Expand All @@ -45,6 +50,9 @@ protected void updateProperties(Properties props) {
if (reWriteBatchedInserts == ReWriteBatchedInserts.YES) {
PGProperty.REWRITE_BATCHED_INSERTS.set(props, true);
}
if (stringType != null) {
PGProperty.STRING_TYPE.set(props, stringType.name().toLowerCase());
}
}

protected void forceBinary(Properties props) {
Expand All @@ -55,6 +63,14 @@ public final void setBinaryMode(BinaryMode binaryMode) {
this.binaryMode = binaryMode;
}

public StringType getStringType() {
return stringType;
}

public void setStringType(StringType stringType) {
this.stringType = stringType;
}

public void setReWriteBatchedInserts(
ReWriteBatchedInserts reWriteBatchedInserts) {
this.reWriteBatchedInserts = reWriteBatchedInserts;
Expand Down
49 changes: 49 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc4/UUIDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,43 @@
import static org.junit.Assert.assertTrue;

import org.postgresql.core.ServerVersion;
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;
import org.postgresql.util.PSQLState;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;

@RunWith(Parameterized.class)
public class UUIDTest extends BaseTest4 {

public UUIDTest(BinaryMode binaryMode, StringType stringType) {
setBinaryMode(binaryMode);
setStringType(stringType);
}

@Parameterized.Parameters(name = "binary={0}, stringType={1}")
public static Iterable<Object[]> data() {
Collection<Object[]> ids = new ArrayList<Object[]>();
for (BinaryMode binaryMode : BinaryMode.values()) {
for (StringType stringType : StringType.values()) {
ids.add(new Object[]{binaryMode, stringType});
}
}
return ids;
}

@Override
public void setUp() throws Exception {
super.setUp();
Expand Down Expand Up @@ -60,5 +84,30 @@ public void testUUID() throws SQLException {
stmt.close();
}

@Test
public void testUUIDString() throws SQLException {
String uuid = "0dcdf03a-058c-4fa3-b210-8385cb6810d5";
PreparedStatement ps = con.prepareStatement("INSERT INTO uuidtest VALUES (?)");
ps.setString(1, uuid);
try {
ps.executeUpdate();
if (getStringType() == StringType.VARCHAR) {
Assert.fail(
"setString(, uuid) should fail to insert value into UUID column when stringType=varchar."
+ " Expecting error <<column \"id\" is of type uuid but expression is of type character varying>>");
}
} catch (SQLException e) {
if (getStringType() == StringType.VARCHAR
&& PSQLState.DATATYPE_MISMATCH.getState().equals(e.getSQLState())) {
// The following error is expected in stringType=varchar mode
// ERROR: column "id" is of type uuid but expression is of type character varying
return;
}
throw e;
} finally {
TestUtil.closeQuietly(ps);
}
}

}

0 comments on commit 5827858

Please sign in to comment.