Skip to content

Commit 2e976a9

Browse files
committed
add non-ascii test
1 parent c60c40e commit 2e976a9

File tree

3 files changed

+200
-18
lines changed

3 files changed

+200
-18
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package org.embulk.output.databricks;
2+
3+
import static java.lang.String.format;
4+
import static org.embulk.output.databricks.util.ConnectionUtil.*;
5+
import static org.junit.Assert.assertEquals;
6+
7+
import java.sql.Connection;
8+
import java.sql.DatabaseMetaData;
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
import org.embulk.output.databricks.util.ConfigUtil;
12+
import org.embulk.output.jdbc.JdbcUtils;
13+
import org.junit.After;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
// The purpose of this class is to understand the behavior of DatabaseMetadata,
18+
// so if this test fails due to a library update, please change the test result.
19+
public class TestDatabaseMetadata {
20+
private DatabaseMetaData dbm;
21+
private Connection conn;
22+
23+
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
24+
String catalog = t.getCatalogName();
25+
String schema = t.getSchemaName();
26+
String table = t.getTablePrefix() + "_test";
27+
String nonAsciiCatalog = t.getNonAsciiCatalogName();
28+
String nonAsciiSchema = t.getNonAsciiSchemaName();
29+
String nonAsciiTable = t.getTablePrefix() + "_テスト";
30+
31+
@Before
32+
public void setup() throws SQLException, ClassNotFoundException {
33+
conn = connectByTestTask();
34+
dbm = conn.getMetaData();
35+
run(conn, "USE CATALOG " + catalog);
36+
run(conn, "USE SCHEMA " + schema);
37+
createTables();
38+
}
39+
40+
@After
41+
public void cleanup() {
42+
try {
43+
conn.close();
44+
} catch (SQLException ignored) {
45+
46+
}
47+
dropAllTemporaryTables();
48+
}
49+
50+
@Test
51+
public void testGetPrimaryKeys() throws SQLException {
52+
assertEquals(1, countPrimaryKeys(catalog, schema, table, "a0"));
53+
assertEquals(1, countPrimaryKeys(null, schema, table, "a0"));
54+
assertEquals(1, countPrimaryKeys(nonAsciiCatalog, nonAsciiSchema, nonAsciiTable, "h0"));
55+
assertEquals(1, countPrimaryKeys(null, nonAsciiSchema, nonAsciiTable, "d0"));
56+
}
57+
58+
@Test
59+
public void testGetTables() throws SQLException {
60+
assertEquals(1, countTablesResult(catalog, schema, table));
61+
assertEquals(2, countTablesResult(null, schema, table));
62+
assertEquals(1, countTablesResult(nonAsciiCatalog, nonAsciiSchema, nonAsciiTable));
63+
assertEquals(0, countTablesResult(null, nonAsciiSchema, nonAsciiTable)); // expected 2
64+
}
65+
66+
@Test
67+
public void testGetColumns() throws SQLException {
68+
assertEquals(2, countColumnsResult(catalog, schema, table));
69+
assertEquals(4, countColumnsResult(null, schema, table));
70+
assertEquals(2, countColumnsResult(nonAsciiCatalog, nonAsciiSchema, nonAsciiTable));
71+
assertEquals(0, countColumnsResult(null, nonAsciiSchema, nonAsciiTable)); // expected 2
72+
}
73+
74+
private void createTables() {
75+
String queryFormat =
76+
"CREATE TABLE IF NOT EXISTS `%s`.`%s`.`%s` (%s String PRIMARY KEY, %s INTEGER)";
77+
run(conn, format(queryFormat, catalog, schema, table, "a0", "a1"));
78+
run(conn, format(queryFormat, catalog, schema, nonAsciiTable, "b0", "b1"));
79+
run(conn, format(queryFormat, catalog, nonAsciiSchema, table, "c0", "c1"));
80+
run(conn, format(queryFormat, catalog, nonAsciiSchema, nonAsciiTable, "d0", "d1"));
81+
run(conn, format(queryFormat, nonAsciiCatalog, schema, table, "e0", "e1"));
82+
run(conn, format(queryFormat, nonAsciiCatalog, schema, nonAsciiTable, "f0", "f1"));
83+
run(conn, format(queryFormat, nonAsciiCatalog, nonAsciiSchema, table, "g0", "g1"));
84+
run(conn, format(queryFormat, nonAsciiCatalog, nonAsciiSchema, nonAsciiTable, "h0", "h1"));
85+
}
86+
87+
private int countPrimaryKeys(
88+
String catalogName, String schemaName, String tableName, String primaryKey)
89+
throws SQLException {
90+
try (ResultSet rs = dbm.getPrimaryKeys(catalogName, schemaName, tableName)) {
91+
int count = 0;
92+
while (rs.next()) {
93+
String columnName = rs.getString("COLUMN_NAME");
94+
assertEquals(primaryKey, columnName);
95+
count += 1;
96+
}
97+
return count;
98+
}
99+
}
100+
101+
private int countTablesResult(String catalogName, String schemaName, String tableName)
102+
throws SQLException {
103+
String e = dbm.getSearchStringEscape();
104+
String c = JdbcUtils.escapeSearchString(catalogName, e);
105+
String s = JdbcUtils.escapeSearchString(schemaName, e);
106+
String t = JdbcUtils.escapeSearchString(tableName, e);
107+
try (ResultSet rs = dbm.getTables(c, s, t, null)) {
108+
return countResultSet(rs);
109+
}
110+
}
111+
112+
private int countColumnsResult(String catalogName, String schemaName, String tableName)
113+
throws SQLException {
114+
String e = dbm.getSearchStringEscape();
115+
String c = JdbcUtils.escapeSearchString(catalogName, e);
116+
String s = JdbcUtils.escapeSearchString(schemaName, e);
117+
String t = JdbcUtils.escapeSearchString(tableName, e);
118+
try (ResultSet rs = dbm.getColumns(c, s, t, null)) {
119+
return countResultSet(rs);
120+
}
121+
}
122+
123+
private int countResultSet(ResultSet rs) throws SQLException {
124+
int count = 0;
125+
while (rs.next()) {
126+
count += 1;
127+
}
128+
return count;
129+
}
130+
}

src/test/java/org/embulk/output/databricks/TestDatabricksOutputConnection.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package org.embulk.output.databricks;
22

3+
import static org.embulk.output.databricks.util.ConnectionUtil.*;
4+
import static org.junit.Assert.assertTrue;
5+
36
import java.sql.*;
47
import java.util.*;
58
import java.util.concurrent.Executor;
9+
import org.embulk.output.databricks.util.ConfigUtil;
10+
import org.embulk.output.databricks.util.ConnectionUtil;
611
import org.embulk.output.jdbc.JdbcColumn;
712
import org.embulk.output.jdbc.JdbcSchema;
813
import org.embulk.output.jdbc.MergeConfig;
@@ -11,10 +16,35 @@
1116
import org.junit.Test;
1217

1318
public class TestDatabricksOutputConnection {
19+
@Test
20+
public void testTableExists() throws SQLException, ClassNotFoundException {
21+
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
22+
String asciiTableName = t.getTablePrefix() + "_test";
23+
String nonAsciiTableName = t.getTablePrefix() + "_テスト";
24+
testTableExists(t.getCatalogName(), t.getSchemaName(), asciiTableName);
25+
testTableExists(t.getNonAsciiCatalogName(), t.getSchemaName(), asciiTableName);
26+
testTableExists(t.getCatalogName(), t.getNonAsciiSchemaName(), asciiTableName);
27+
testTableExists(t.getCatalogName(), t.getSchemaName(), nonAsciiTableName);
28+
testTableExists(t.getNonAsciiCatalogName(), t.getNonAsciiSchemaName(), nonAsciiTableName);
29+
}
30+
31+
private void testTableExists(String catalogName, String schemaName, String tableName)
32+
throws SQLException, ClassNotFoundException {
33+
String fullTableName = String.format("`%s`.`%s`.`%s`", catalogName, schemaName, tableName);
34+
try (Connection conn = ConnectionUtil.connectByTestTask()) {
35+
run(conn, "CREATE TABLE IF NOT EXISTS " + fullTableName);
36+
try (DatabricksOutputConnection outputConn =
37+
buildOutputConnection(conn, catalogName, schemaName)) {
38+
assertTrue(outputConn.tableExists(new TableIdentifier(null, null, tableName)));
39+
}
40+
} finally {
41+
run("DROP TABLE IF EXISTS " + fullTableName);
42+
}
43+
}
1444

1545
@Test
16-
public void TestBuildCopySQL() throws SQLException {
17-
try (DatabricksOutputConnection conn = buildOutputConnection()) {
46+
public void testBuildCopySQL() throws SQLException {
47+
try (DatabricksOutputConnection conn = buildDummyOutputConnection()) {
1848
TableIdentifier tableIdentifier = new TableIdentifier("database", "schemaName", "tableName");
1949
String actual = conn.buildCopySQL(tableIdentifier, "filePath", buildJdbcSchema());
2050
String expected =
@@ -24,8 +54,8 @@ public void TestBuildCopySQL() throws SQLException {
2454
}
2555

2656
@Test
27-
public void TestBuildAggregateSQL() throws SQLException {
28-
try (DatabricksOutputConnection conn = buildOutputConnection()) {
57+
public void testBuildAggregateSQL() throws SQLException {
58+
try (DatabricksOutputConnection conn = buildDummyOutputConnection()) {
2959
List<TableIdentifier> fromTableIdentifiers = new ArrayList<>();
3060
fromTableIdentifiers.add(new TableIdentifier("database", "schemaName", "tableName0"));
3161
fromTableIdentifiers.add(new TableIdentifier("database", "schemaName", "tableName1"));
@@ -39,7 +69,7 @@ public void TestBuildAggregateSQL() throws SQLException {
3969
}
4070

4171
@Test
42-
public void TestMergeConfigSQLWithMergeRules() throws SQLException {
72+
public void testMergeConfigSQLWithMergeRules() throws SQLException {
4373
List<String> mergeKeys = buildMergeKeys("col0", "col1");
4474
Optional<List<String>> mergeRules =
4575
buildMergeRules("col0 = CONCAT(T.col0, 'test')", "col1 = T.col1 + S.col1");
@@ -50,7 +80,7 @@ public void TestMergeConfigSQLWithMergeRules() throws SQLException {
5080
}
5181

5282
@Test
53-
public void TestMergeConfigSQLWithNoMergeRules() throws SQLException {
83+
public void testMergeConfigSQLWithNoMergeRules() throws SQLException {
5484
List<String> mergeKeys = buildMergeKeys("col0", "col1");
5585
Optional<List<String>> mergeRules = Optional.empty();
5686
String actual = mergeConfigSQL(new MergeConfig(mergeKeys, mergeRules));
@@ -60,7 +90,7 @@ public void TestMergeConfigSQLWithNoMergeRules() throws SQLException {
6090
}
6191

6292
private String mergeConfigSQL(MergeConfig mergeConfig) throws SQLException {
63-
try (DatabricksOutputConnection conn = buildOutputConnection()) {
93+
try (DatabricksOutputConnection conn = buildDummyOutputConnection()) {
6494
TableIdentifier aggregateToTable =
6595
new TableIdentifier("database", "schemaName", "tableName9");
6696
TableIdentifier toTable = new TableIdentifier("database", "schemaName", "tableName100");
@@ -76,7 +106,13 @@ private Optional<List<String>> buildMergeRules(String... keys) {
76106
return keys.length > 0 ? Optional.of(Arrays.asList(keys)) : Optional.empty();
77107
}
78108

79-
private DatabricksOutputConnection buildOutputConnection() throws SQLException {
109+
private DatabricksOutputConnection buildOutputConnection(
110+
Connection conn, String catalogName, String schemaName)
111+
throws SQLException, ClassNotFoundException {
112+
return new DatabricksOutputConnection(conn, catalogName, schemaName);
113+
}
114+
115+
private DatabricksOutputConnection buildDummyOutputConnection() throws SQLException {
80116
return new DatabricksOutputConnection(
81117
buildDummyConnection(), "defaultCatalogName", "defaultSchemaName");
82118
}

src/test/java/org/embulk/output/databricks/TestDatabricksOutputPluginByNonAscii.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.embulk.output.databricks;
22

33
import static org.embulk.output.databricks.util.ConfigUtil.createPluginConfigSource;
4+
import static org.embulk.output.databricks.util.ConfigUtil.setMergeRule;
45
import static org.embulk.output.databricks.util.ConfigUtil.setNonAsciiCatalogName;
56
import static org.embulk.output.databricks.util.ConfigUtil.setNonAsciiSchemaName;
67
import static org.embulk.output.databricks.util.ConfigUtil.setNonAsciiStagingVolumeNamePrefix;
@@ -18,7 +19,6 @@
1819
import java.util.stream.Collectors;
1920
import java.util.stream.IntStream;
2021
import org.embulk.config.ConfigSource;
21-
import org.embulk.exec.PartialExecutionException;
2222
import org.embulk.output.databricks.util.ConfigUtil;
2323
import org.embulk.output.jdbc.AbstractJdbcOutputPlugin.Mode;
2424
import org.junit.Assert;
@@ -68,14 +68,9 @@ public void testColumnNameWithoutMergeKeys() throws Exception {
6868
@Test
6969
public void testColumnNameMergeWithMergeRule() throws Exception {
7070
ConfigSource configSource = createPluginConfigSource(Mode.MERGE);
71-
ConfigUtil.setMergeRule(configSource, "`い` = CONCAT(T.`い`, 'あ', S.`い`)");
71+
setMergeRule(configSource, "`い` = CONCAT(T.`い`, 'あ', S.`い`)");
7272
setupForMerge(configSource, true);
73-
runOutput(configSource, "あ", "い");
74-
List<Map<String, Object>> results =
75-
runQuery("SELECT * FROM " + quotedDstTableName(configSource));
76-
Assert.assertEquals(1, results.size());
77-
Assert.assertEquals("test0", results.get(0).get("あ"));
78-
Assert.assertEquals("hogeあtest1", results.get(0).get("い"));
73+
runOutputAndAssertMergeWithMergeRule(configSource);
7974
}
8075

8176
@Test
@@ -89,8 +84,7 @@ public void testCatalogName() throws Exception {
8984
public void testSchemaName() throws Exception {
9085
ConfigSource configSource = createPluginConfigSource(Mode.INSERT);
9186
setNonAsciiSchemaName(configSource);
92-
Assert.assertThrows(
93-
PartialExecutionException.class, () -> runOutputAndAssertResult(configSource));
87+
runOutputAndAssertResult(configSource);
9488
}
9589

9690
@Test
@@ -107,13 +101,35 @@ public void testStagingVolumeNamePrefix() throws Exception {
107101
runOutputAndAssertResult(configSource);
108102
}
109103

104+
@Test
105+
public void testAllAttributes() throws Exception {
106+
ConfigSource configSource = createPluginConfigSource(Mode.MERGE);
107+
setNonAsciiCatalogName(configSource);
108+
setNonAsciiSchemaName(configSource);
109+
setNonAsciiTable(configSource);
110+
setNonAsciiStagingVolumeNamePrefix(configSource);
111+
setMergeRule(configSource, "`い` = CONCAT(T.`い`, 'あ', S.`い`)");
112+
setupForMerge(configSource, true);
113+
runOutputAndAssertMergeWithMergeRule(configSource);
114+
}
115+
110116
private void setupForMerge(ConfigSource configSource, boolean hasPrimaryKey) {
111117
String quotedDstTableName = quotedDstTableName(configSource);
112118
String primaryKey = hasPrimaryKey ? "PRIMARY KEY" : "";
113119
run("CREATE TABLE " + quotedDstTableName + " (`あ` STRING " + primaryKey + ", `い` STRING)");
114120
run("INSERT INTO " + quotedDstTableName + "(`あ`, `い`) VALUES ('test0', 'hoge')");
115121
}
116122

123+
private void runOutputAndAssertMergeWithMergeRule(ConfigSource configSource) throws IOException {
124+
runOutput(configSource, "あ", "い");
125+
126+
List<Map<String, Object>> results =
127+
runQuery("SELECT * FROM " + quotedDstTableName(configSource));
128+
Assert.assertEquals(1, results.size());
129+
Assert.assertEquals("test0", results.get(0).get("あ"));
130+
Assert.assertEquals("hogeあtest1", results.get(0).get("い"));
131+
}
132+
117133
private void runOutputAndAssertResult(ConfigSource configSource) throws IOException {
118134
runOutputAndAssertResult(configSource, "あ", "`", "\"", "'");
119135
}

0 commit comments

Comments
 (0)