forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrestoContainerTest.java
159 lines (146 loc) · 6.72 KB
/
PrestoContainerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package org.testcontainers.containers;
import org.junit.Test;
import org.testcontainers.PrestoTestImages;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author findepi
*/
public class PrestoContainerTest {
@Test
public void testSimple() throws Exception {
try (PrestoContainer<?> prestoSql = new PrestoContainer<>(PrestoTestImages.PRESTO_TEST_IMAGE)) {
prestoSql.start();
try (
Connection connection = prestoSql.createConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT node_version FROM system.runtime.nodes")
) {
assertThat(resultSet.next()).as("has result").isTrue();
assertThat(resultSet.getString("node_version"))
.as("Presto version")
.isEqualTo(PrestoContainer.DEFAULT_TAG);
assertHasCorrectExposedAndLivenessCheckPorts(prestoSql);
}
}
}
@Test
public void testSpecificVersion() throws Exception {
try (
PrestoContainer<?> prestoSql = new PrestoContainer<>(PrestoTestImages.PRESTO_PREVIOUS_VERSION_TEST_IMAGE)
) {
prestoSql.start();
try (
Connection connection = prestoSql.createConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT node_version FROM system.runtime.nodes")
) {
assertThat(resultSet.next()).as("has result").isTrue();
assertThat(resultSet.getString("node_version"))
.as("Presto version")
.isEqualTo(PrestoTestImages.PRESTO_PREVIOUS_VERSION_TEST_IMAGE.getVersionPart());
}
}
}
@Test
public void testQueryMemoryAndTpch() throws SQLException {
try (PrestoContainer<?> prestoSql = new PrestoContainer<>(PrestoTestImages.PRESTO_TEST_IMAGE)) {
prestoSql.start();
try (
Connection connection = prestoSql.createConnection();
Statement statement = connection.createStatement()
) {
// Prepare data
statement.execute(
"CREATE TABLE memory.default.table_with_array AS SELECT 1 id, ARRAY[1, 42, 2, 42, 4, 42] my_array"
);
// Query Presto using newly created table and a builtin connector
try (
ResultSet resultSet = statement.executeQuery(
"" +
"SELECT nationkey, element " +
"FROM tpch.tiny.nation " +
"JOIN memory.default.table_with_array twa ON nationkey = twa.id " +
"LEFT JOIN UNNEST(my_array) a(element) ON true " +
"ORDER BY element OFFSET 1 FETCH NEXT 3 ROWS WITH TIES "
)
) {
List<Integer> actualElements = new ArrayList<>();
while (resultSet.next()) {
actualElements.add(resultSet.getInt("element"));
}
assertThat(actualElements).isEqualTo(Arrays.asList(2, 4, 42, 42, 42));
}
}
}
}
@Test
public void testInitScript() throws Exception {
try (PrestoContainer<?> prestoSql = new PrestoContainer<>(PrestoTestImages.PRESTO_TEST_IMAGE)) {
prestoSql.withInitScript("initial.sql");
prestoSql.start();
try (
Connection connection = prestoSql.createConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT a FROM memory.default.test_table")
) {
assertThat(resultSet.next()).as("has result").isTrue();
assertThat(resultSet.getObject("a")).as("Value").isEqualTo(12345678909324L);
assertThat(resultSet.next()).as("only has one result").isFalse();
}
}
}
@Test
public void testTcJdbcUri() throws Exception {
try (
Connection connection = DriverManager.getConnection(
String.format("jdbc:tc:presto:%s://hostname/", PrestoContainer.DEFAULT_TAG)
)
) {
// Verify metadata with tc: JDBC connection URI
assertThat(Integer.parseInt(PrestoContainer.DEFAULT_TAG))
.isEqualTo(connection.getMetaData().getDatabaseMajorVersion());
// Verify transactions with tc: JDBC connection URI
assertThat(connection.getAutoCommit()).as("Is autocommit").isTrue();
connection.setAutoCommit(false);
assertThat(connection.getAutoCommit()).as("Is autocommit").isFalse();
assertThat(connection.getTransactionIsolation())
.as("Transaction isolation")
.isEqualTo(Connection.TRANSACTION_READ_UNCOMMITTED);
try (Statement statement = connection.createStatement()) {
assertThat(statement.executeUpdate("CREATE TABLE memory.default.test_tc(a bigint)"))
.as("Update result")
.isEqualTo(0);
try (
ResultSet resultSet = statement.executeQuery(
"SELECT sum(cast(node_version AS bigint)) AS v FROM system.runtime.nodes"
)
) {
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getString("v")).isEqualTo(PrestoContainer.DEFAULT_TAG);
assertThat(resultSet.next()).isFalse();
}
connection.commit();
} finally {
connection.rollback();
}
connection.setAutoCommit(true);
assertThat(connection.getAutoCommit()).as("Is autocommit").isTrue();
assertThat(connection.getTransactionIsolation())
.as("Transaction isolation should be retained")
.isEqualTo(Connection.TRANSACTION_READ_UNCOMMITTED);
}
}
private void assertHasCorrectExposedAndLivenessCheckPorts(PrestoContainer<?> prestoSql) {
assertThat(prestoSql.getExposedPorts()).containsExactly(PrestoContainer.PRESTO_PORT);
assertThat(prestoSql.getLivenessCheckPortNumbers())
.containsExactly(prestoSql.getMappedPort(PrestoContainer.PRESTO_PORT));
}
}