Skip to content

Commit 21842f2

Browse files
committed
fix ‘falsey’ default value generation
1 parent b1aab48 commit 21842f2

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

pydbml/renderer/dbml/default/column.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def render_options(model: Column) -> str:
2424
options.append('pk')
2525
if model.autoinc:
2626
options.append('increment')
27-
if model.default:
27+
if model.default is not None:
2828
options.append(f'default: {default_to_str(model.default)}')
2929
if model.unique:
3030
options.append('unique')

test/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ def string_column_with_table(db: Database, table1: Table, string_column: Column)
8787
db.add(table1)
8888
return string_column
8989

90+
@pytest.fixture
91+
def boolean_column() -> Column:
92+
return Column(
93+
name='enabled',
94+
type='boolean',
95+
pk=False,
96+
autoinc=False,
97+
unique=False,
98+
not_null=True,
99+
default=False,
100+
comment='This is a defaulted boolean column',
101+
note=Note('This is a note for the column'),
102+
properties={'foo': 'bar', 'baz': "qux\nqux"}
103+
)
104+
105+
106+
@pytest.fixture
107+
def boolean_column_with_table(db: Database, table1: Table, boolean_column: Column) -> Column:
108+
table1.add_column(boolean_column)
109+
db.add(table1)
110+
return boolean_column
111+
90112

91113
@pytest.fixture
92114
def table1() -> Table:

test/test_data/integration1.dbml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Enum "level" {
1212
Table "Employees" as "emp" {
1313
"id" integer [pk, increment]
1414
"name" varchar [note: 'Full employee name']
15-
"age" number
15+
"age" number [default: 0]
1616
"level" level
1717
"favorite_book_id" integer
1818
}

test/test_renderer/test_dbml/test_column.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_enum(simple_column_with_table: Column, enum1: Enum) -> None:
133133
def test_complex(complex_column_with_table: Column) -> None:
134134
expected = (
135135
"// This is a counter column\n"
136-
'"counter" "product status" [pk, increment, unique, not null, note: \'This is '
136+
'"counter" "product status" [pk, increment, default: 0, unique, not null, note: \'This is '
137137
"a note for the column']"
138138
)
139139
assert render_column(complex_column_with_table) == expected
@@ -147,3 +147,11 @@ def test_string(string_column_with_table: Column) -> None:
147147
)
148148
assert render_column(string_column_with_table) == expected
149149

150+
@staticmethod
151+
def test_boolean(boolean_column_with_table: Column) -> None:
152+
expected = (
153+
"// This is a defaulted boolean column\n"
154+
'"enabled" boolean [default: False, not null, note: \'This is '
155+
"a note for the column']"
156+
)
157+
assert render_column(boolean_column_with_table) == expected

test/test_renderer/test_sql/test_default/test_column.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ def test_string(string_column: Column) -> None:
2626
"'value''s'"
2727
)
2828
assert render_column(string_column) == expected
29+
30+
@staticmethod
31+
def test_string(boolean_column: Column) -> None:
32+
expected = (
33+
"-- This is a defaulted boolean column\n"
34+
'"enabled" boolean NOT NULL DEFAULT '
35+
"False"
36+
)
37+
assert render_column(boolean_column) == expected

0 commit comments

Comments
 (0)