Skip to content

Commit 20555ee

Browse files
committed
add test
1 parent f92cc4b commit 20555ee

File tree

5 files changed

+115
-18
lines changed

5 files changed

+115
-18
lines changed

example/test.yml.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# The catalog_name and schema_name must be created in advance.
1+
# The catalog_name.schema_name, catalog_name.non_ascii_schema_name, non_ascii_catalog_name.non_ascii_schema_name and non_ascii_catalog_name.schema_name must be created in advance.
22

33
server_hostname:
44
http_path:
55
personal_access_token:
66
catalog_name:
77
schema_name:
8+
non_ascii_schema_name:
9+
non_ascii_catalog_name:
810
table_prefix:
911
staging_volume_name_prefix:

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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.setNonAsciiCatalogName;
5+
import static org.embulk.output.databricks.util.ConfigUtil.setNonAsciiSchemaName;
6+
import static org.embulk.output.databricks.util.ConfigUtil.setNonAsciiStagingVolumeNamePrefix;
7+
import static org.embulk.output.databricks.util.ConfigUtil.setNonAsciiTable;
48
import static org.embulk.output.databricks.util.ConnectionUtil.quotedDstTableName;
59
import static org.embulk.output.databricks.util.ConnectionUtil.run;
610
import static org.embulk.output.databricks.util.ConnectionUtil.runQuery;
@@ -14,6 +18,7 @@
1418
import java.util.stream.Collectors;
1519
import java.util.stream.IntStream;
1620
import org.embulk.config.ConfigSource;
21+
import org.embulk.exec.PartialExecutionException;
1722
import org.embulk.output.databricks.util.ConfigUtil;
1823
import org.embulk.output.jdbc.AbstractJdbcOutputPlugin.Mode;
1924
import org.junit.Assert;
@@ -73,6 +78,35 @@ public void testColumnNameMergeWithMergeRule() throws Exception {
7378
Assert.assertEquals("hogeあtest1", results.get(0).get("い"));
7479
}
7580

81+
@Test
82+
public void testCatalogName() throws Exception {
83+
ConfigSource configSource = createPluginConfigSource(Mode.INSERT);
84+
setNonAsciiCatalogName(configSource);
85+
runOutputAndAssertResult(configSource);
86+
}
87+
88+
@Test
89+
public void testSchemaName() throws Exception {
90+
ConfigSource configSource = createPluginConfigSource(Mode.INSERT);
91+
setNonAsciiSchemaName(configSource);
92+
Assert.assertThrows(
93+
PartialExecutionException.class, () -> runOutputAndAssertResult(configSource));
94+
}
95+
96+
@Test
97+
public void testTableName() throws Exception {
98+
ConfigSource configSource = createPluginConfigSource(Mode.INSERT);
99+
setNonAsciiTable(configSource);
100+
runOutputAndAssertResult(configSource);
101+
}
102+
103+
@Test
104+
public void testStagingVolumeNamePrefix() throws Exception {
105+
ConfigSource configSource = createPluginConfigSource(Mode.INSERT);
106+
setNonAsciiStagingVolumeNamePrefix(configSource);
107+
runOutputAndAssertResult(configSource);
108+
}
109+
76110
private void setupForMerge(ConfigSource configSource, boolean hasPrimaryKey) {
77111
String quotedDstTableName = quotedDstTableName(configSource);
78112
String primaryKey = hasPrimaryKey ? "PRIMARY KEY" : "";

src/test/java/org/embulk/output/databricks/util/ConfigUtil.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public interface TestTask extends Task {
5454

5555
@Config("staging_volume_name_prefix")
5656
public String getStagingVolumeNamePrefix();
57+
58+
@Config("non_ascii_catalog_name")
59+
public String getNonAsciiCatalogName();
60+
61+
@Config("non_ascii_schema_name")
62+
public String getNonAsciiSchemaName();
5763
}
5864

5965
public static TestTask createTestTask() {
@@ -89,14 +95,29 @@ public static DatabricksOutputPlugin.DatabricksPluginTask createPluginTask(
8995
return CONFIG_MAPPER.map(configSource, DatabricksOutputPlugin.DatabricksPluginTask.class);
9096
}
9197

98+
public static ConfigSource setNonAsciiCatalogName(ConfigSource configSource) {
99+
return configSource.set("catalog_name", createTestTask().getNonAsciiCatalogName());
100+
}
101+
102+
public static ConfigSource setNonAsciiSchemaName(ConfigSource configSource) {
103+
return configSource.set("schema_name", createTestTask().getNonAsciiSchemaName());
104+
}
105+
106+
public static ConfigSource setNonAsciiTable(ConfigSource configSource) {
107+
return configSource.set("table", createTestTask().getTablePrefix() + "_マルチバイトテスト");
108+
}
109+
110+
public static ConfigSource setNonAsciiStagingVolumeNamePrefix(ConfigSource configSource) {
111+
String s = createTestTask().getStagingVolumeNamePrefix() + "_マルチバイトテスト";
112+
return configSource.set("staging_volume_name_prefix", s);
113+
}
114+
92115
public static ConfigSource setMergeKeys(ConfigSource configSource, String... mergeKeys) {
93-
configSource.set("merge_keys", mergeKeys);
94-
return configSource;
116+
return configSource.set("merge_keys", mergeKeys);
95117
}
96118

97119
public static ConfigSource setMergeRule(ConfigSource configSource, String... mergeRule) {
98-
configSource.set("merge_rule", mergeRule);
99-
return configSource;
120+
return configSource.set("merge_rule", mergeRule);
100121
}
101122

102123
public static ConfigSource setColumnOption(

src/test/java/org/embulk/output/databricks/util/ConnectionUtil.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,36 @@ public static String quotedDstTableName(ConfigSource configSource) {
3333

3434
public static void dropAllTemporaryTables() {
3535
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
36+
try (Connection conn = connectByTestTask()) {
37+
dropAllTemporaryTables(conn, t.getCatalogName(), t.getSchemaName());
38+
dropAllTemporaryTables(conn, t.getCatalogName(), t.getNonAsciiSchemaName());
39+
dropAllTemporaryTables(conn, t.getNonAsciiCatalogName(), t.getSchemaName());
40+
dropAllTemporaryTables(conn, t.getNonAsciiCatalogName(), t.getNonAsciiSchemaName());
41+
} catch (SQLException | ClassNotFoundException e) {
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
46+
private static void dropAllTemporaryTables(
47+
Connection conn, String catalogName, String schemaName) {
48+
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
3649
String tableNamesSQL =
3750
String.format(
3851
"select table_name from system.information_schema.tables where table_catalog = '%s' AND table_schema = '%s' AND table_name LIKE '%s%%'",
39-
t.getCatalogName(), t.getSchemaName(), t.getTablePrefix());
40-
runQuery(tableNamesSQL)
52+
catalogName, schemaName, t.getTablePrefix());
53+
runQuery(conn, tableNamesSQL)
4154
.forEach(
4255
x -> {
4356
String tableName = (String) x.get("table_name");
4457
String dropSql =
4558
String.format(
46-
"drop table if exists `%s`.`%s`.`%s`",
47-
t.getCatalogName(), t.getSchemaName(), tableName);
48-
run(dropSql);
59+
"drop table if exists `%s`.`%s`.`%s`", catalogName, schemaName, tableName);
60+
run(conn, dropSql);
4961
});
5062
}
5163

52-
public static List<Map<String, Object>> runQuery(String query) {
53-
try (Connection conn = connectByTestTask();
54-
Statement stmt = conn.createStatement();
64+
public static List<Map<String, Object>> runQuery(Connection conn, String query) {
65+
try (Statement stmt = conn.createStatement();
5566
ResultSet rs = stmt.executeQuery(query)) {
5667
List<Map<String, Object>> result = new ArrayList<>();
5768
while (rs.next()) {
@@ -62,15 +73,30 @@ public static List<Map<String, Object>> runQuery(String query) {
6273
result.add(resMap);
6374
}
6475
return result;
76+
} catch (SQLException e) {
77+
throw new RuntimeException(e);
78+
}
79+
}
80+
81+
public static List<Map<String, Object>> runQuery(String query) {
82+
try (Connection conn = connectByTestTask()) {
83+
return runQuery(conn, query);
6584
} catch (SQLException | ClassNotFoundException e) {
6685
throw new RuntimeException(e);
6786
}
6887
}
6988

70-
public static Boolean run(String query) {
71-
try (Connection conn = connectByTestTask();
72-
Statement stmt = conn.createStatement()) {
89+
public static Boolean run(Connection conn, String query) {
90+
try (Statement stmt = conn.createStatement()) {
7391
return stmt.execute(query);
92+
} catch (SQLException e) {
93+
throw new RuntimeException(e);
94+
}
95+
}
96+
97+
public static Boolean run(String query) {
98+
try (Connection conn = connectByTestTask()) {
99+
return run(conn, query);
74100
} catch (SQLException | ClassNotFoundException e) {
75101
throw new RuntimeException(e);
76102
}

src/test/java/org/embulk/output/databricks/util/DatabricksApiClientUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88

99
public class DatabricksApiClientUtil {
1010
public static void deleteAllTemporaryStagingVolumes() {
11+
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
12+
deleteAllTemporaryStagingVolumes(t.getCatalogName(), t.getSchemaName());
13+
deleteAllTemporaryStagingVolumes(t.getCatalogName(), t.getNonAsciiSchemaName());
14+
deleteAllTemporaryStagingVolumes(t.getNonAsciiCatalogName(), t.getSchemaName());
15+
deleteAllTemporaryStagingVolumes(t.getNonAsciiCatalogName(), t.getNonAsciiSchemaName());
16+
}
17+
18+
public static void deleteAllTemporaryStagingVolumes(String catalogName, String schemaName) {
1119
WorkspaceClient client = createWorkspaceClient();
12-
fetchAllTemporaryStagingVolumes()
20+
fetchAllTemporaryStagingVolumes(catalogName, schemaName)
1321
.forEach(
1422
x -> {
1523
String name =
@@ -20,11 +28,17 @@ public static void deleteAllTemporaryStagingVolumes() {
2028

2129
public static List<VolumeInfo> fetchAllTemporaryStagingVolumes() {
2230
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
31+
return fetchAllTemporaryStagingVolumes(t.getCatalogName(), t.getSchemaName());
32+
}
33+
34+
private static List<VolumeInfo> fetchAllTemporaryStagingVolumes(
35+
String catalogName, String schemaName) {
36+
ConfigUtil.TestTask t = ConfigUtil.createTestTask();
2337
WorkspaceClient client = createWorkspaceClient();
2438
List<VolumeInfo> results = new ArrayList<>();
2539
client
2640
.volumes()
27-
.list(t.getCatalogName(), t.getSchemaName())
41+
.list(catalogName, schemaName)
2842
.forEach(
2943
x -> {
3044
if (x.getName().startsWith(t.getStagingVolumeNamePrefix())) {

0 commit comments

Comments
 (0)