Skip to content

Commit c63f9b7

Browse files
authored
Add unit tests for information_schema database (#22)
1 parent b488b04 commit c63f9b7

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2025 Qiheng He
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.linghengqian.hive.server2.jdbc.driver.thin;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.Container.ExecResult;
21+
import org.testcontainers.containers.GenericContainer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
24+
import org.testcontainers.utility.DockerImageName;
25+
26+
import java.io.IOException;
27+
import java.sql.*;
28+
import java.time.Duration;
29+
import java.time.temporal.ChronoUnit;
30+
31+
import static org.awaitility.Awaitility.await;
32+
import static org.hamcrest.MatcherAssert.assertThat;
33+
import static org.hamcrest.Matchers.is;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
36+
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
37+
@Testcontainers
38+
public class InformationSchemaTest {
39+
@Container
40+
public static final GenericContainer<?> CONTAINER = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.1"))
41+
.withEnv("SERVICE_NAME", "hiveserver2")
42+
.withExposedPorts(10000);
43+
44+
@Test
45+
void test() throws SQLException, IOException, InterruptedException {
46+
String jdbcUrlPrefix = "jdbc:hive2://" + CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(10000);
47+
await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).ignoreExceptions().until(() -> {
48+
DriverManager.getConnection(jdbcUrlPrefix).close();
49+
return true;
50+
});
51+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix);
52+
Statement statement = connection.createStatement()) {
53+
statement.execute("CREATE DATABASE demo_ds_0");
54+
}
55+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/demo_ds_0");
56+
Statement statement = connection.createStatement()) {
57+
statement.execute("CREATE TABLE IF NOT EXISTS t_order (\n" +
58+
" order_id BIGINT NOT NULL,\n" +
59+
" order_type INT,\n" +
60+
" user_id INT NOT NULL,\n" +
61+
" address_id BIGINT NOT NULL,\n" +
62+
" status string,\n" +
63+
" PRIMARY KEY (order_id) disable novalidate\n" +
64+
") STORED BY ICEBERG STORED AS ORC TBLPROPERTIES ('format-version' = '2')");
65+
statement.execute("TRUNCATE TABLE t_order");
66+
statement.executeUpdate("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')");
67+
ResultSet resultSet = statement.executeQuery("select * from t_order");
68+
assertThat(resultSet.next(), is(true));
69+
}
70+
assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrlPrefix + "/information_schema").close());
71+
ExecResult infoResult = CONTAINER.execInContainer(
72+
"/opt/hive/bin/schematool",
73+
"-info",
74+
"-dbType", "hive",
75+
"-metaDbType", "derby",
76+
"-url", "jdbc:hive2://localhost:10000/default"
77+
);
78+
assertThat(infoResult.getStdout(), is("Metastore connection URL:\t jdbc:hive2://localhost:10000/default\n" +
79+
"Metastore connection Driver :\t org.apache.hive.jdbc.HiveDriver\n" +
80+
"Metastore connection User:\t APP\n"));
81+
ExecResult initResult = CONTAINER.execInContainer(
82+
"/opt/hive/bin/schematool",
83+
"-initSchema",
84+
"-dbType", "hive",
85+
"-metaDbType", "derby",
86+
"-url", "jdbc:hive2://localhost:10000/default"
87+
);
88+
assertThat(initResult.getStdout(), is("Initializing the schema to: 4.0.0\n" +
89+
"Metastore connection URL:\t jdbc:hive2://localhost:10000/default\n" +
90+
"Metastore connection Driver :\t org.apache.hive.jdbc.HiveDriver\n" +
91+
"Metastore connection User:\t APP\n" +
92+
"Starting metastore schema initialization to 4.0.0\n" +
93+
"Initialization script hive-schema-4.0.0.hive.sql\n" +
94+
"Initialization script completed\n"));
95+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/information_schema");
96+
Statement statement = connection.createStatement()) {
97+
ResultSet resultSet = statement.executeQuery("select * from information_schema.COLUMNS limit 100");
98+
assertThat(resultSet.next(), is(true));
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2025 Qiheng He
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.linghengqian.hive.server2.jdbc.driver.uber;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.Container.ExecResult;
21+
import org.testcontainers.containers.GenericContainer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
24+
import org.testcontainers.utility.DockerImageName;
25+
26+
import java.io.IOException;
27+
import java.sql.*;
28+
import java.time.Duration;
29+
import java.time.temporal.ChronoUnit;
30+
31+
import static org.awaitility.Awaitility.await;
32+
import static org.hamcrest.MatcherAssert.assertThat;
33+
import static org.hamcrest.Matchers.is;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
36+
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
37+
@Testcontainers
38+
public class InformationSchemaTest {
39+
@Container
40+
public static final GenericContainer<?> CONTAINER = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.1"))
41+
.withEnv("SERVICE_NAME", "hiveserver2")
42+
.withExposedPorts(10000);
43+
44+
@Test
45+
void test() throws SQLException, IOException, InterruptedException {
46+
String jdbcUrlPrefix = "jdbc:hive2://" + CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(10000);
47+
await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).ignoreExceptions().until(() -> {
48+
DriverManager.getConnection(jdbcUrlPrefix).close();
49+
return true;
50+
});
51+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix);
52+
Statement statement = connection.createStatement()) {
53+
statement.execute("CREATE DATABASE demo_ds_0");
54+
}
55+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/demo_ds_0");
56+
Statement statement = connection.createStatement()) {
57+
statement.execute("CREATE TABLE IF NOT EXISTS t_order (\n" +
58+
" order_id BIGINT NOT NULL,\n" +
59+
" order_type INT,\n" +
60+
" user_id INT NOT NULL,\n" +
61+
" address_id BIGINT NOT NULL,\n" +
62+
" status string,\n" +
63+
" PRIMARY KEY (order_id) disable novalidate\n" +
64+
") STORED BY ICEBERG STORED AS ORC TBLPROPERTIES ('format-version' = '2')");
65+
statement.execute("TRUNCATE TABLE t_order");
66+
statement.executeUpdate("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')");
67+
ResultSet resultSet = statement.executeQuery("select * from t_order");
68+
assertThat(resultSet.next(), is(true));
69+
}
70+
assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrlPrefix + "/information_schema").close());
71+
ExecResult infoResult = CONTAINER.execInContainer(
72+
"/opt/hive/bin/schematool",
73+
"-info",
74+
"-dbType", "hive",
75+
"-metaDbType", "derby",
76+
"-url", "jdbc:hive2://localhost:10000/default"
77+
);
78+
assertThat(infoResult.getStdout(), is("Metastore connection URL:\t jdbc:hive2://localhost:10000/default\n" +
79+
"Metastore connection Driver :\t org.apache.hive.jdbc.HiveDriver\n" +
80+
"Metastore connection User:\t APP\n"));
81+
ExecResult initResult = CONTAINER.execInContainer(
82+
"/opt/hive/bin/schematool",
83+
"-initSchema",
84+
"-dbType", "hive",
85+
"-metaDbType", "derby",
86+
"-url", "jdbc:hive2://localhost:10000/default"
87+
);
88+
assertThat(initResult.getStdout(), is("Initializing the schema to: 4.0.0\n" +
89+
"Metastore connection URL:\t jdbc:hive2://localhost:10000/default\n" +
90+
"Metastore connection Driver :\t org.apache.hive.jdbc.HiveDriver\n" +
91+
"Metastore connection User:\t APP\n" +
92+
"Starting metastore schema initialization to 4.0.0\n" +
93+
"Initialization script hive-schema-4.0.0.hive.sql\n" +
94+
"Initialization script completed\n"));
95+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/information_schema");
96+
Statement statement = connection.createStatement()) {
97+
ResultSet resultSet = statement.executeQuery("select * from information_schema.COLUMNS limit 100");
98+
assertThat(resultSet.next(), is(true));
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)