DBZ-298 Adding test case for quoted table and column names in Postgres

This commit is contained in:
Emrul Islam 2017-07-12 23:23:41 +01:00 committed by Gunnar Morling
parent 564d90dc09
commit b9be392cd9
4 changed files with 31 additions and 4 deletions

View File

@ -62,7 +62,7 @@
*/
public abstract class AbstractRecordsProducerTest {
protected static final Pattern INSERT_TABLE_MATCHING_PATTERN = Pattern.compile("insert into (\\w+).+", Pattern.CASE_INSENSITIVE);
protected static final Pattern INSERT_TABLE_MATCHING_PATTERN = Pattern.compile("insert into \"?(\\w+)\"?.+", Pattern.CASE_INSENSITIVE);
protected static final String INSERT_CASH_TYPES_STMT = "INSERT INTO cash_table (csh) VALUES ('$1234.11')";
protected static final String INSERT_DATE_TIME_TYPES_STMT = "INSERT INTO time_table(ts, tz, date, ti, ttz, it) " +
@ -86,9 +86,13 @@ public abstract class AbstractRecordsProducerTest {
protected static final String INSERT_ARRAY_TYPES_STMT = "INSERT INTO array_table (int_array, bigint_array, text_array) " +
"VALUES ('{1,2,3}', '{1550166368505037572}', '{\"one\",\"two\",\"three\"}')";
protected static final String INSERT_QUOTED_TYPES_STMT = "INSERT INTO \"Quoted_Table\" (\"Quoted_Text_Column\") " +
"VALUES ('some text')";
protected static final Set<String> ALL_STMTS = new HashSet<>(Arrays.asList(INSERT_NUMERIC_TYPES_STMT, INSERT_DATE_TIME_TYPES_STMT,
INSERT_BIN_TYPES_STMT, INSERT_GEOM_TYPES_STMT, INSERT_TEXT_TYPES_STMT,
INSERT_CASH_TYPES_STMT, INSERT_STRING_TYPES_STMT, INSERT_ARRAY_TYPES_STMT));
INSERT_CASH_TYPES_STMT, INSERT_STRING_TYPES_STMT, INSERT_ARRAY_TYPES_STMT,
INSERT_QUOTED_TYPES_STMT));
protected List<SchemaAndValueField> schemasAndValuesForNumericType() {
return Arrays.asList(new SchemaAndValueField("si", SchemaBuilder.OPTIONAL_INT16_SCHEMA, (short) 1),
@ -179,6 +183,10 @@ protected List<SchemaAndValueField> schemasAndValuesForArrayTypes() {
);
}
protected List<SchemaAndValueField> schemasAndValuesForQuotedTypes() {
return Arrays.asList(new SchemaAndValueField("Quoted_Text_Column", Schema.OPTIONAL_STRING_SCHEMA, "some text"));
}
protected Map<String, List<SchemaAndValueField>> schemaAndValuesByTableName() {
return ALL_STMTS.stream().collect(Collectors.toMap(AbstractRecordsProducerTest::tableNameFromInsertStmt,
this::schemasAndValuesForTable));
@ -202,6 +210,8 @@ protected List<SchemaAndValueField> schemasAndValuesForTable(String insertTableS
return schemasAndValuesForTextTypes();
case INSERT_ARRAY_TYPES_STMT:
return schemasAndValuesForArrayTypes();
case INSERT_QUOTED_TYPES_STMT:
return schemasAndValuesForQuotedTypes();
default:
throw new IllegalArgumentException("unknown statement:" + insertTableStatement);
}

View File

@ -49,7 +49,7 @@ public class PostgresSchemaIT {
private static final String[] TEST_TABLES = new String[] { "public.numeric_table", "public.string_table", "public.cash_table",
"public.bitbin_table",
"public.time_table", "public.text_table", "public.geom_table", "public.tstzrange_table",
"public.array_table"
"public.array_table", "public.Quoted_Table"
};
private PostgresSchema schema;
@ -91,6 +91,8 @@ public void shouldLoadSchemaForBuiltinPostgresTypes() throws Exception {
assertTableSchema("public.array_table", "int_array, bigint_array, text_array",
SchemaBuilder.array(Schema.OPTIONAL_INT32_SCHEMA).optional().build(), SchemaBuilder.array(Schema.OPTIONAL_INT64_SCHEMA).optional().build(),
SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).optional().build());
assertTableSchema("public.Quoted_Table", "Quoted_Text_Column",
Schema.OPTIONAL_STRING_SCHEMA);
}
}

View File

@ -92,8 +92,20 @@ public void shouldReceiveChangesForInsertsWithDifferentDataTypes() throws Except
// timezone range types
consumer.expects(1);
assertInsert(INSERT_TSTZRANGE_TYPES_STMT, schemaAndValuesForTstzRangeTypes());
}
@Test
public void shouldReceiveChangesForInsertsWithQuotedNames() throws Exception {
TestHelper.executeDDL("postgres_create_tables.ddl");
consumer = testConsumer(1);
recordsProducer.start(consumer);
// Quoted column name
assertInsert(INSERT_QUOTED_TYPES_STMT, schemasAndValuesForQuotedTypes());
}
@Test
public void shouldReceiveChangesForInsertsWithArrayTypes() throws Exception {
TestHelper.executeDDL("postgres_create_tables.ddl");

View File

@ -1,3 +1,5 @@
-- noinspection SqlNoDataSourceInspectionForFile
-- Generate a number of tables to cover as many of the PG types as possible
DROP SCHEMA IF EXISTS public CASCADE;
CREATE SCHEMA public;
@ -10,3 +12,4 @@ CREATE TABLE text_table (pk SERIAL, j JSON, jb JSONB, x XML, u Uuid, PRIMARY KEY
CREATE TABLE geom_table (pk SERIAL, p POINT, PRIMARY KEY(pk));
CREATE TABLE tstzrange_table (pk serial, unbounded_exclusive_range tstzrange, bounded_inclusive_range tstzrange, PRIMARY KEY(pk));
CREATE TABLE array_table (pk SERIAL, int_array INT[], bigint_array BIGINT[], text_array TEXT[], PRIMARY KEY(pk));
CREATE TABLE "Quoted_Table" (pk SERIAL, "Quoted_Text_Column" TEXT, PRIMARY KEY(pk));