Skip to content

Commit 5bd1b59

Browse files
author
Jesse Whitehouse
committed
(7/x) Refactor into a CompilationTestBase to simplify reading of tests
Remove the smoke test as this will be captured in a subsequent e2e test Signed-off-by: Jesse Whitehouse <jesse.whitehouse@databricks.com>
1 parent f223041 commit 5bd1b59

1 file changed

Lines changed: 24 additions & 93 deletions

File tree

Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import enum
22

33
import pytest
4-
from sqlalchemy.schema import Column, CreateTable, MetaData, Table
54
from sqlalchemy.types import (
65
BigInteger,
76
Boolean,
@@ -29,11 +28,6 @@
2928

3029
from databricks.sqlalchemy import DatabricksDialect
3130

32-
# SchemaType is not tested because it's not used in table definitions.
33-
# It's used to extend behaviour of custom types.
34-
35-
# MatchType is not tested because it cannot appear in table definitions.
36-
3731

3832
class DatabricksDataType(enum.Enum):
3933
"""https://docs.databricks.com/en/sql/language-manual/sql-ref-datatypes.html"""
@@ -86,7 +80,28 @@ class DatabricksDataType(enum.Enum):
8680
_as_tuple_list = [(key, value) for key, value in camel_case_type_map.items()]
8781

8882

89-
class TestCamelCaseTypesCompilation:
83+
class CompilationTestBase:
84+
dialect = DatabricksDialect()
85+
86+
def _assert_compiled_value(self, type_: TypeEngine, expected: DatabricksDataType):
87+
"""Assert that when type_ is compiled for the databricks dialect, it renders the DatabricksDataType name.
88+
89+
This method initialises the type_ with no arguments.
90+
"""
91+
compiled_result = type_().compile(dialect=self.dialect)
92+
assert compiled_result == expected.name
93+
94+
def _assert_compiled_value_explicit(self, type_: TypeEngine, expected: str):
95+
"""Assert that when type_ is compiled for the databricks dialect, it renders the expected string.
96+
97+
This method expects an initialised type_ so that we can test how a TypeEngine created with arguments
98+
is compiled.
99+
"""
100+
compiled_result = type_.compile(dialect=self.dialect)
101+
assert compiled_result == expected
102+
103+
104+
class TestCamelCaseTypesCompilation(CompilationTestBase):
90105
"""Per the sqlalchemy documentation[^1] here, the camel case members of sqlalchemy.types are
91106
are expected to work across all dialects. These tests verify that the types compile into valid
92107
Databricks SQL type strings. For example, the sqlalchemy.types.Integer() should compile as "INT".
@@ -97,19 +112,11 @@ class TestCamelCaseTypesCompilation:
97112
Note that these tests have to do with type **name** compiliation. Which is separate from actually
98113
mapping values between Python and Databricks.
99114
115+
Note: SchemaType and MatchType are not tested because it's not used in table definitions
116+
100117
[1]: https://docs.sqlalchemy.org/en/20/core/type_basics.html#generic-camelcase-types
101118
"""
102119

103-
dialect = DatabricksDialect()
104-
105-
def _assert_compiled_value(self, type_: TypeEngine, expected: DatabricksDataType):
106-
compiled_result = type_().compile(dialect=self.dialect)
107-
assert compiled_result == expected.name
108-
109-
def _assert_compiled_value_explicit(self, type_: TypeEngine, expected: str):
110-
compiled_result = type_.compile(dialect=self.dialect)
111-
assert compiled_result == expected
112-
113120
@pytest.mark.parametrize("type_, expected", _as_tuple_list)
114121
def test_bare_camel_case_types_compile(self, type_, expected):
115122
self._assert_compiled_value(type_, expected)
@@ -119,79 +126,3 @@ def test_numeric_renders_as_decimal_with_precision(self):
119126

120127
def test_numeric_renders_as_decimal_with_precision_and_scale(self):
121128
self._assert_compiled_value_explicit(Numeric(10, 2), "DECIMAL(10, 2)")
122-
123-
def test_create_table_with_all_types_smoke_test(self):
124-
metadata = MetaData()
125-
test_table = Table(
126-
"abc123",
127-
metadata,
128-
Column("bigint_col", BigInteger()),
129-
Column("boolean_col", Boolean()),
130-
Column("date_col", Date()),
131-
Column("datetime_col", DateTime()),
132-
Column("double_col", Double()),
133-
Column("enum_col", Enum("Option1", "Option2", "Option3")),
134-
Column("float_col", Float()),
135-
Column("integer_col", Integer()),
136-
Column("interval_col", Interval()),
137-
Column("large_binary_col", LargeBinary()),
138-
Column("numeric_col", Numeric(precision=10, scale=2)),
139-
Column("pickletype_col", PickleType()),
140-
Column("smallinteger_col", SmallInteger()),
141-
Column("string_col", String(length=255)),
142-
Column("text_col", Text()),
143-
Column("time_col", Time()),
144-
Column("unicode_col", Unicode(length=255)),
145-
Column("unicodetext_col", UnicodeText()),
146-
Column("uuid_col", Uuid(as_uuid=True)),
147-
)
148-
149-
create_construct = CreateTable(test_table)
150-
151-
generated_sql = str(create_construct.compile(dialect=self.dialect))
152-
153-
"""
154-
The generated SQL looks like this (captured 1 October 2023):
155-
156-
CREATE TABLE abc123 (
157-
bigint_col BIGINT,
158-
boolean_col BOOLEAN,
159-
date_col DATE,
160-
datetime_col TIMESTAMP,
161-
double_col DOUBLE,
162-
enum_col STRING,
163-
float_col FLOAT,
164-
integer_col INT,
165-
interval_col TIMESTAMP,
166-
large_binary_col BINARY,
167-
numeric_col DECIMAL(10, 2),
168-
pickletype_col BINARY,
169-
smallinteger_col SMALLINT,
170-
string_col STRING,
171-
text_col STRING,
172-
time_col STRING,
173-
unicode_col STRING,
174-
unicodetext_col STRING,
175-
uuid_col STRING
176-
) USING DELTA
177-
"""
178-
179-
assert "bigint_col BIGINT" in generated_sql
180-
assert "boolean_col BOOLEAN" in generated_sql
181-
assert "date_col DATE" in generated_sql
182-
assert "datetime_col TIMESTAMP" in generated_sql
183-
assert "double_col DOUBLE" in generated_sql
184-
assert "enum_col STRING" in generated_sql
185-
assert "float_col FLOAT" in generated_sql
186-
assert "integer_col INT" in generated_sql
187-
assert "interval_col TIMESTAMP" in generated_sql
188-
assert "large_binary_col BINARY" in generated_sql
189-
assert "numeric_col DECIMAL(10, 2)" in generated_sql
190-
assert "pickletype_col BINARY" in generated_sql
191-
assert "smallinteger_col SMALLINT" in generated_sql
192-
assert "string_col STRING" in generated_sql
193-
assert "text_col STRING" in generated_sql
194-
assert "time_col STRING" in generated_sql
195-
assert "unicode_col STRING" in generated_sql
196-
assert "unicodetext_col STRING" in generated_sql
197-
assert "uuid_col STRING" in generated_sql

0 commit comments

Comments
 (0)