-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_ddl.py
More file actions
116 lines (89 loc) · 4 KB
/
test_ddl.py
File metadata and controls
116 lines (89 loc) · 4 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
import pytest
from sqlalchemy import Column, MetaData, String, Table, Numeric, Integer, create_engine
from sqlalchemy.schema import (
CreateTable,
DropColumnComment,
DropTableComment,
SetColumnComment,
SetTableComment,
)
from databricks.sqlalchemy import DatabricksArray, DatabricksMap, DatabricksVariant
class DDLTestBase:
engine = create_engine(
"databricks://token:****@****?http_path=****&catalog=****&schema=****"
)
def compile(self, stmt):
return str(stmt.compile(bind=self.engine))
class TestColumnCommentDDL(DDLTestBase):
@pytest.fixture
def metadata(self) -> MetaData:
"""Assemble a metadata object with one table containing one column."""
metadata = MetaData()
column = Column("foo", String, comment="bar")
table = Table("foobar", metadata, column)
return metadata
@pytest.fixture
def table(self, metadata) -> Table:
return metadata.tables.get("foobar")
@pytest.fixture
def column(self, table) -> Column:
return table.columns[0]
def test_create_table_with_column_comment(self, table):
stmt = CreateTable(table)
output = self.compile(stmt)
# output is a CREATE TABLE statement
assert "foo STRING COMMENT 'bar'" in output
def test_alter_table_add_column_comment(self, column):
stmt = SetColumnComment(column)
output = self.compile(stmt)
assert output == "ALTER TABLE foobar ALTER COLUMN foo COMMENT 'bar'"
def test_alter_table_drop_column_comment(self, column):
stmt = DropColumnComment(column)
output = self.compile(stmt)
assert output == "ALTER TABLE foobar ALTER COLUMN foo COMMENT ''"
class TestTableCommentDDL(DDLTestBase):
@pytest.fixture
def metadata(self) -> MetaData:
"""Assemble a metadata object with one table containing one column."""
metadata = MetaData()
col1 = Column("foo", String)
col2 = Column("foo", String)
tbl_w_comment = Table("martin", metadata, col1, comment="foobar")
tbl_wo_comment = Table("prs", metadata, col2)
return metadata
@pytest.fixture
def table_with_comment(self, metadata) -> Table:
return metadata.tables.get("martin")
@pytest.fixture
def table_without_comment(self, metadata) -> Table:
return metadata.tables.get("prs")
def test_create_table_with_comment(self, table_with_comment):
stmt = CreateTable(table_with_comment)
output = self.compile(stmt)
assert "USING DELTA" in output
assert "COMMENT 'foobar'" in output
def test_alter_table_add_comment(self, table_without_comment: Table):
table_without_comment.comment = "wireless mechanical keyboard"
stmt = SetTableComment(table_without_comment)
output = self.compile(stmt)
assert output == "COMMENT ON TABLE prs IS 'wireless mechanical keyboard'"
def test_alter_table_drop_comment(self, table_with_comment):
"""The syntax for COMMENT ON is here: https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-ddl-comment.html"""
stmt = DropTableComment(table_with_comment)
output = self.compile(stmt)
assert output == "COMMENT ON TABLE martin IS NULL"
class TestTableComplexTypeDDL(DDLTestBase):
@pytest.fixture(scope="class")
def metadata(self) -> MetaData:
metadata = MetaData()
col1 = Column("array_array_string", DatabricksArray(DatabricksArray(String)))
col2 = Column("map_string_string", DatabricksMap(String, String))
col3 = Column("variant_col", DatabricksVariant())
table = Table("complex_type", metadata, col1, col2, col3)
return metadata
def test_create_table_with_complex_type(self, metadata):
stmt = CreateTable(metadata.tables["complex_type"])
output = self.compile(stmt)
assert "array_array_string ARRAY<ARRAY<STRING>>" in output
assert "map_string_string MAP<STRING,STRING>" in output
assert "variant_col VARIANT" in output