Skip to content

Commit 7c94ce3

Browse files
author
Jesse Whitehouse
committed
Add unit tests for table comments
Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent bd68ea8 commit 7c94ce3

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/databricks/sqlalchemy/test_local/test_ddl.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import pytest
22
from sqlalchemy import Column, MetaData, String, Table, create_engine
3-
from sqlalchemy.schema import CreateTable, DropColumnComment, SetColumnComment
3+
from sqlalchemy.schema import (
4+
CreateTable,
5+
DropColumnComment,
6+
DropTableComment,
7+
SetColumnComment,
8+
SetTableComment,
9+
)
10+
411

512
class DDLTestBase:
613
engine = create_engine(
@@ -12,7 +19,6 @@ def compile(self, stmt):
1219

1320

1421
class TestColumnCommentDDL(DDLTestBase):
15-
1622
@pytest.fixture
1723
def metadata(self) -> MetaData:
1824
"""Assemble a metadata object with one table containing one column."""
@@ -48,5 +54,42 @@ def test_alter_table_drop_column_comment(self, column):
4854
output = self.compile(stmt)
4955
assert output == "ALTER TABLE foobar ALTER COLUMN foo COMMENT ''"
5056

57+
5158
class TestTableCommentDDL(DDLTestBase):
52-
pass
59+
@pytest.fixture
60+
def metadata(self) -> MetaData:
61+
"""Assemble a metadata object with one table containing one column."""
62+
metadata = MetaData()
63+
64+
col1 = Column("foo", String)
65+
col2 = Column("foo", String)
66+
tbl_w_comment = Table("martin", metadata, col1, comment="foobar")
67+
tbl_wo_comment = Table("prs", metadata, col2)
68+
69+
return metadata
70+
71+
@pytest.fixture
72+
def table_with_comment(self, metadata) -> Table:
73+
return metadata.tables.get("martin")
74+
75+
@pytest.fixture
76+
def table_without_comment(self, metadata) -> Table:
77+
return metadata.tables.get("prs")
78+
79+
def test_create_table_with_comment(self, table_with_comment):
80+
stmt = CreateTable(table_with_comment)
81+
output = self.compile(stmt)
82+
assert "USING DELTA COMMENT 'foobar'" in output
83+
84+
def test_alter_table_add_comment(self, table_without_comment: Table):
85+
table_without_comment.comment = "wireless mechanical keyboard"
86+
stmt = SetTableComment(table_without_comment)
87+
output = self.compile(stmt)
88+
89+
assert output == "COMMENT ON TABLE prs IS 'wireless mechanical keyboard'"
90+
91+
def test_alter_table_drop_comment(self, table_with_comment):
92+
"""The syntax for COMMENT ON is here: https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-ddl-comment.html"""
93+
stmt = DropTableComment(table_with_comment)
94+
output = self.compile(stmt)
95+
assert output == "COMMENT ON TABLE martin IS NULL"

0 commit comments

Comments
 (0)