1
1
import pytest
2
2
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
+
4
11
5
12
class DDLTestBase :
6
13
engine = create_engine (
@@ -12,7 +19,6 @@ def compile(self, stmt):
12
19
13
20
14
21
class TestColumnCommentDDL (DDLTestBase ):
15
-
16
22
@pytest .fixture
17
23
def metadata (self ) -> MetaData :
18
24
"""Assemble a metadata object with one table containing one column."""
@@ -48,5 +54,42 @@ def test_alter_table_drop_column_comment(self, column):
48
54
output = self .compile (stmt )
49
55
assert output == "ALTER TABLE foobar ALTER COLUMN foo COMMENT ''"
50
56
57
+
51
58
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