|
| 1 | +package com.databricks.jdbc.dbclient.impl.sqlexec; |
| 2 | + |
| 3 | +import static com.databricks.jdbc.dbclient.impl.common.CommandConstants.*; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import com.databricks.jdbc.api.internal.IDatabricksSession; |
| 8 | +import com.databricks.jdbc.common.util.WildcardUtil; |
| 9 | +import com.databricks.jdbc.exception.DatabricksSQLFeatureNotSupportedException; |
| 10 | +import java.sql.SQLException; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Nested; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | + |
| 19 | +@ExtendWith(MockitoExtension.class) |
| 20 | +class CommandBuilderTest { |
| 21 | + |
| 22 | + @Mock private IDatabricksSession mockSession; |
| 23 | + |
| 24 | + private static final String TEST_CATALOG = "test_catalog"; |
| 25 | + private static final String TEST_SCHEMA = "test_schema"; |
| 26 | + private static final String TEST_TABLE = "test_table"; |
| 27 | + private static final String TEST_SESSION_CONTEXT = "test_session_context"; |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + void setUp() { |
| 31 | + when(mockSession.toString()).thenReturn(TEST_SESSION_CONTEXT); |
| 32 | + } |
| 33 | + |
| 34 | + @Nested |
| 35 | + @DisplayName("Tests for LIST_PRIMARY_KEYS command") |
| 36 | + class ListPrimaryKeysTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("Should generate correct SQL for fetching primary keys") |
| 40 | + void shouldGenerateCorrectSqlForPrimaryKeys() throws SQLException { |
| 41 | + CommandBuilder builder = |
| 42 | + new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); |
| 43 | + |
| 44 | + String sql = builder.getSQLString(CommandName.LIST_PRIMARY_KEYS); |
| 45 | + |
| 46 | + String expectedSql = |
| 47 | + String.format(SHOW_PRIMARY_KEYS_SQL, TEST_CATALOG, TEST_SCHEMA, TEST_TABLE); |
| 48 | + assertEquals(expectedSql, sql); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @DisplayName("Should throw SQLException when catalog is null for primary keys") |
| 53 | + void shouldThrowExceptionWhenCatalogIsNullForPrimaryKeys() { |
| 54 | + CommandBuilder builder = |
| 55 | + new CommandBuilder(null, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); |
| 56 | + |
| 57 | + assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @DisplayName("Should throw SQLException when schema is null for primary keys") |
| 62 | + void shouldThrowExceptionWhenSchemaIsNullForPrimaryKeys() { |
| 63 | + CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setTable(TEST_TABLE); |
| 64 | + |
| 65 | + assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @DisplayName("Should throw SQLException when table is null for primary keys") |
| 70 | + void shouldThrowExceptionWhenTableIsNullForPrimaryKeys() { |
| 71 | + CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA); |
| 72 | + |
| 73 | + assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Nested |
| 78 | + @DisplayName("Tests for LIST_TABLES command") |
| 79 | + class ListTablesTests { |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("Should generate correct SQL for fetching tables with catalog") |
| 83 | + void shouldGenerateCorrectSqlForTablesWithCatalog() throws SQLException { |
| 84 | + CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession); |
| 85 | + |
| 86 | + String sql = builder.getSQLString(CommandName.LIST_TABLES); |
| 87 | + |
| 88 | + String expectedSql = String.format(SHOW_TABLES_SQL, TEST_CATALOG); |
| 89 | + assertEquals(expectedSql, sql); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @DisplayName("Should generate correct SQL for fetching tables with catalog and schema pattern") |
| 94 | + void shouldGenerateCorrectSqlForTablesWithCatalogAndSchemaPattern() throws SQLException { |
| 95 | + String schemaPattern = "test_schema%"; |
| 96 | + String hiveSchemaPattern = WildcardUtil.jdbcPatternToHive(schemaPattern); |
| 97 | + |
| 98 | + CommandBuilder builder = |
| 99 | + new CommandBuilder(TEST_CATALOG, mockSession).setSchemaPattern(schemaPattern); |
| 100 | + |
| 101 | + String sql = builder.getSQLString(CommandName.LIST_TABLES); |
| 102 | + |
| 103 | + String expectedSql = |
| 104 | + String.format(SHOW_TABLES_SQL.concat(SCHEMA_LIKE_SQL), TEST_CATALOG, hiveSchemaPattern); |
| 105 | + assertEquals(expectedSql, sql); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @DisplayName( |
| 110 | + "Should generate correct SQL for fetching tables with catalog, schema pattern, and table pattern") |
| 111 | + void shouldGenerateCorrectSqlForTablesWithCatalogSchemaAndTablePattern() throws SQLException { |
| 112 | + String schemaPattern = "test_schema%"; |
| 113 | + String tablePattern = "test_table%"; |
| 114 | + String hiveSchemaPattern = WildcardUtil.jdbcPatternToHive(schemaPattern); |
| 115 | + String hiveTablePattern = WildcardUtil.jdbcPatternToHive(tablePattern); |
| 116 | + |
| 117 | + CommandBuilder builder = |
| 118 | + new CommandBuilder(TEST_CATALOG, mockSession) |
| 119 | + .setSchemaPattern(schemaPattern) |
| 120 | + .setTablePattern(tablePattern); |
| 121 | + |
| 122 | + String sql = builder.getSQLString(CommandName.LIST_TABLES); |
| 123 | + |
| 124 | + String expectedSql = |
| 125 | + String.format( |
| 126 | + SHOW_TABLES_SQL.concat(SCHEMA_LIKE_SQL).concat(LIKE_SQL), |
| 127 | + TEST_CATALOG, |
| 128 | + hiveSchemaPattern, |
| 129 | + hiveTablePattern); |
| 130 | + assertEquals(expectedSql, sql); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("Should generate correct SQL for fetching tables from all catalogs") |
| 135 | + void shouldGenerateCorrectSqlForTablesFromAllCatalogs() throws SQLException { |
| 136 | + CommandBuilder builder = new CommandBuilder(null, mockSession); |
| 137 | + |
| 138 | + String sql = builder.getSQLString(CommandName.LIST_TABLES); |
| 139 | + |
| 140 | + assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + @DisplayName("Should generate correct SQL for fetching tables with wildcard catalog") |
| 145 | + void shouldGenerateCorrectSqlForTablesWithWildcardCatalog() throws SQLException { |
| 146 | + // Test with '*' wildcard |
| 147 | + CommandBuilder builder1 = new CommandBuilder("*", mockSession); |
| 148 | + String sql1 = builder1.getSQLString(CommandName.LIST_TABLES); |
| 149 | + assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql1); |
| 150 | + |
| 151 | + // Test with '%' wildcard |
| 152 | + CommandBuilder builder2 = new CommandBuilder("%", mockSession); |
| 153 | + String sql2 = builder2.getSQLString(CommandName.LIST_TABLES); |
| 154 | + assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql2); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Nested |
| 159 | + @DisplayName("Tests for LIST_FOREIGN_KEYS command") |
| 160 | + class ListForeignKeysTests { |
| 161 | + |
| 162 | + @Test |
| 163 | + @DisplayName("Should generate correct SQL for fetching foreign keys") |
| 164 | + void shouldGenerateCorrectSqlForForeignKeys() throws SQLException { |
| 165 | + CommandBuilder builder = |
| 166 | + new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); |
| 167 | + |
| 168 | + String sql = builder.getSQLString(CommandName.LIST_FOREIGN_KEYS); |
| 169 | + |
| 170 | + String expectedSql = |
| 171 | + String.format(SHOW_FOREIGN_KEYS_SQL, TEST_CATALOG, TEST_SCHEMA, TEST_TABLE); |
| 172 | + assertEquals(expectedSql, sql); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + @DisplayName("Should throw SQLException when catalog is null for foreign keys") |
| 177 | + void shouldThrowExceptionWhenCatalogIsNullForForeignKeys() { |
| 178 | + CommandBuilder builder = |
| 179 | + new CommandBuilder(null, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); |
| 180 | + |
| 181 | + assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS)); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + @DisplayName("Should throw SQLException when schema is null for foreign keys") |
| 186 | + void shouldThrowExceptionWhenSchemaIsNullForForeignKeys() { |
| 187 | + CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setTable(TEST_TABLE); |
| 188 | + |
| 189 | + assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS)); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + @DisplayName("Should throw SQLException when table is null for foreign keys") |
| 194 | + void shouldThrowExceptionWhenTableIsNullForForeignKeys() { |
| 195 | + CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA); |
| 196 | + |
| 197 | + assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS)); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + @DisplayName("Should throw exception for unsupported command") |
| 203 | + void shouldThrowExceptionForUnsupportedCommand() { |
| 204 | + CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession); |
| 205 | + |
| 206 | + CommandName mockCommand = mock(CommandName.class); |
| 207 | + |
| 208 | + assertThrows( |
| 209 | + DatabricksSQLFeatureNotSupportedException.class, () -> builder.getSQLString(mockCommand)); |
| 210 | + } |
| 211 | +} |
0 commit comments