forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandBuilderTest.java
More file actions
205 lines (160 loc) · 7.83 KB
/
CommandBuilderTest.java
File metadata and controls
205 lines (160 loc) · 7.83 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.databricks.jdbc.dbclient.impl.sqlexec;
import static com.databricks.jdbc.dbclient.impl.common.CommandConstants.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import com.databricks.jdbc.api.internal.IDatabricksSession;
import com.databricks.jdbc.common.util.WildcardUtil;
import com.databricks.jdbc.exception.DatabricksValidationException;
import java.sql.SQLException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class CommandBuilderTest {
@Mock private IDatabricksSession mockSession;
private static final String TEST_CATALOG = "test_catalog";
private static final String TEST_SCHEMA = "test_schema";
private static final String TEST_TABLE = "test_table";
private static final String TEST_SESSION_CONTEXT = "test_session_context";
@BeforeEach
void setUp() {
when(mockSession.toString()).thenReturn(TEST_SESSION_CONTEXT);
}
@Nested
@DisplayName("Tests for LIST_PRIMARY_KEYS command")
class ListPrimaryKeysTests {
@Test
@DisplayName("Should generate correct SQL for fetching primary keys")
void shouldGenerateCorrectSqlForPrimaryKeys() throws SQLException {
CommandBuilder builder =
new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE);
String sql = builder.getSQLString(CommandName.LIST_PRIMARY_KEYS);
String expectedSql =
String.format(SHOW_PRIMARY_KEYS_SQL, TEST_CATALOG, TEST_SCHEMA, TEST_TABLE);
assertEquals(expectedSql, sql);
}
@Test
@DisplayName("Should throw SQLException when catalog is null for primary keys")
void shouldThrowExceptionWhenCatalogIsNullForPrimaryKeys() {
CommandBuilder builder =
new CommandBuilder(null, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE);
assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS));
}
@Test
@DisplayName("Should throw SQLException when schema is null for primary keys")
void shouldThrowExceptionWhenSchemaIsNullForPrimaryKeys() {
CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setTable(TEST_TABLE);
assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS));
}
@Test
@DisplayName("Should throw SQLException when table is null for primary keys")
void shouldThrowExceptionWhenTableIsNullForPrimaryKeys() {
CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA);
assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS));
}
}
@Nested
@DisplayName("Tests for LIST_TABLES command")
class ListTablesTests {
@Test
@DisplayName("Should generate correct SQL for fetching tables with catalog")
void shouldGenerateCorrectSqlForTablesWithCatalog() throws SQLException {
CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession);
String sql = builder.getSQLString(CommandName.LIST_TABLES);
String expectedSql = String.format(SHOW_TABLES_SQL, TEST_CATALOG);
assertEquals(expectedSql, sql);
}
@Test
@DisplayName("Should generate correct SQL for fetching tables with catalog and schema pattern")
void shouldGenerateCorrectSqlForTablesWithCatalogAndSchemaPattern() throws SQLException {
String schemaPattern = "test_schema%";
String hiveSchemaPattern = WildcardUtil.jdbcPatternToHive(schemaPattern);
CommandBuilder builder =
new CommandBuilder(TEST_CATALOG, mockSession).setSchemaPattern(schemaPattern);
String sql = builder.getSQLString(CommandName.LIST_TABLES);
String expectedSql =
String.format(SHOW_TABLES_SQL.concat(SCHEMA_LIKE_SQL), TEST_CATALOG, hiveSchemaPattern);
assertEquals(expectedSql, sql);
}
@Test
@DisplayName(
"Should generate correct SQL for fetching tables with catalog, schema pattern, and table pattern")
void shouldGenerateCorrectSqlForTablesWithCatalogSchemaAndTablePattern() throws SQLException {
String schemaPattern = "test_schema%";
String tablePattern = "test_table%";
String hiveSchemaPattern = WildcardUtil.jdbcPatternToHive(schemaPattern);
String hiveTablePattern = WildcardUtil.jdbcPatternToHive(tablePattern);
CommandBuilder builder =
new CommandBuilder(TEST_CATALOG, mockSession)
.setSchemaPattern(schemaPattern)
.setTablePattern(tablePattern);
String sql = builder.getSQLString(CommandName.LIST_TABLES);
String expectedSql =
String.format(
SHOW_TABLES_SQL.concat(SCHEMA_LIKE_SQL).concat(LIKE_SQL),
TEST_CATALOG,
hiveSchemaPattern,
hiveTablePattern);
assertEquals(expectedSql, sql);
}
@Test
@DisplayName("Should generate correct SQL for fetching tables from all catalogs")
void shouldGenerateCorrectSqlForTablesFromAllCatalogs() throws SQLException {
CommandBuilder builder = new CommandBuilder(null, mockSession);
String sql = builder.getSQLString(CommandName.LIST_TABLES);
assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql);
}
@Test
@DisplayName("Should generate correct SQL for fetching tables with wildcard catalog")
void shouldGenerateCorrectSqlForTablesWithWildcardCatalog() throws SQLException {
// Test with '*' wildcard
CommandBuilder builder1 = new CommandBuilder("*", mockSession);
String sql1 = builder1.getSQLString(CommandName.LIST_TABLES);
assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql1);
}
}
@Nested
@DisplayName("Tests for LIST_FOREIGN_KEYS command")
class ListForeignKeysTests {
@Test
@DisplayName("Should generate correct SQL for fetching foreign keys")
void shouldGenerateCorrectSqlForForeignKeys() throws SQLException {
CommandBuilder builder =
new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE);
String sql = builder.getSQLString(CommandName.LIST_FOREIGN_KEYS);
String expectedSql =
String.format(SHOW_FOREIGN_KEYS_SQL, TEST_CATALOG, TEST_SCHEMA, TEST_TABLE);
assertEquals(expectedSql, sql);
}
@Test
@DisplayName("Should throw SQLException when catalog is null for foreign keys")
void shouldThrowExceptionWhenCatalogIsNullForForeignKeys() {
CommandBuilder builder =
new CommandBuilder(null, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE);
assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS));
}
@Test
@DisplayName("Should throw SQLException when schema is null for foreign keys")
void shouldThrowExceptionWhenSchemaIsNullForForeignKeys() {
CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setTable(TEST_TABLE);
assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS));
}
@Test
@DisplayName("Should throw SQLException when table is null for foreign keys")
void shouldThrowExceptionWhenTableIsNullForForeignKeys() {
CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA);
assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS));
}
}
@Test
@DisplayName("Should throw exception for unsupported command")
void shouldThrowExceptionForUnsupportedCommand() {
CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession);
CommandName mockCommand = mock(CommandName.class);
assertThrows(DatabricksValidationException.class, () -> builder.getSQLString(mockCommand));
}
}