Skip to content

Commit a26088d

Browse files
committed
added tests for Date columns in migrations
1 parent 06e3ba3 commit a26088d

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

piccolo/apps/migrations/auto/serialisation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def serialise_params(params: t.Dict[str, t.Any]) -> SerialisedParams:
322322
)
323323
continue
324324

325+
# Plain class type
325326
if inspect.isclass(value) and not issubclass(value, Enum):
326327
params[key] = SerialisedCallable(callable_=value)
327328
extra_imports.append(

piccolo/columns/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ def get_sql_value(self, value: t.Any) -> t.Any:
541541
output = str(value).lower()
542542
elif isinstance(value, datetime.datetime):
543543
output = f"'{value.isoformat().replace('T', ' ')}'"
544+
elif isinstance(value, datetime.date):
545+
output = f"'{value.isoformat()}'"
544546
elif isinstance(value, datetime.time):
545547
output = f"'{value.isoformat()}'"
546548
elif isinstance(value, bytes):

piccolo/columns/column_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@ def __init__(self, default: DateArg = DateNow(), **kwargs) -> None:
668668
if isinstance(default, date):
669669
default = DateCustom.from_date(default)
670670

671+
if default == date.today:
672+
default = DateNow()
673+
671674
self.default = default
672675
kwargs.update({"default": default})
673676
super().__init__(**kwargs)

piccolo/columns/defaults/date.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def __init__(
5050

5151
@property
5252
def postgres(self):
53-
return self.date.isostring()
53+
return f"'{self.date.isoformat()}'"
5454

5555
@property
5656
def sqlite(self):
57-
return self.date.isostring()
57+
return f"'{self.date.isoformat()}'"
5858

5959
def python(self):
6060
return self.date

tests/apps/migrations/auto/integration/test_migrations.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from piccolo.conf.apps import AppConfig
1313
from piccolo.columns.column_types import (
1414
BigInt,
15+
Date,
1516
Integer,
1617
SmallInt,
1718
Text,
@@ -54,6 +55,10 @@ def time_default():
5455
return datetime.datetime.now().time()
5556

5657

58+
def date_default():
59+
return datetime.datetime.now().date()
60+
61+
5762
@postgres_only
5863
class TestMigrations(TestCase):
5964
def tearDown(self):
@@ -245,3 +250,20 @@ def test_time_column(self):
245250
]
246251
]
247252
)
253+
254+
def test_date_column(self):
255+
self._test_migrations(
256+
table_classes=[
257+
self.table(column)
258+
for column in [
259+
Date(),
260+
Date(default=datetime.date(year=2021, month=1, day=1)),
261+
Date(default=datetime.date.today),
262+
Date(default=date_default),
263+
Date(null=True, default=None),
264+
Date(null=False),
265+
Date(index=True),
266+
Date(index=False),
267+
]
268+
]
269+
)

0 commit comments

Comments
 (0)