22
33import typing as t
44from datetime import datetime
5+ from datetime import timezone
56
67import pytest
78import sqlalchemy as sa
1415from flask_sqlalchemy .model import Model
1516
1617
18+ class UTCNow (datetime ):
19+ def __new__ (cls ): # type: ignore[no-untyped-def]
20+ return datetime .now (tz = timezone .utc )
21+
22+
1723def test_default_model_class_1x (app : Flask ) -> None :
1824 db = SQLAlchemy (app )
1925
@@ -147,12 +153,12 @@ def test_abstractmodel(app: Flask, model_class: t.Any) -> None:
147153 class TimestampModel (db .Model ):
148154 __abstract__ = True
149155 created : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
150- db .DateTime , nullable = False , insert_default = datetime . utcnow , init = False
156+ db .DateTime , nullable = False , insert_default = UTCNow , init = False
151157 )
152158 updated : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
153159 db .DateTime ,
154- insert_default = datetime . utcnow ,
155- onupdate = datetime . utcnow ,
160+ insert_default = UTCNow ,
161+ onupdate = UTCNow ,
156162 init = False ,
157163 )
158164
@@ -167,10 +173,10 @@ class Post(TimestampModel):
167173 class TimestampModel (db .Model ): # type: ignore[no-redef]
168174 __abstract__ = True
169175 created : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
170- db .DateTime , nullable = False , default = datetime . utcnow
176+ db .DateTime , nullable = False , default = UTCNow
171177 )
172178 updated : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
173- db .DateTime , default = datetime . utcnow , onupdate = datetime . utcnow
179+ db .DateTime , default = UTCNow , onupdate = UTCNow
174180 )
175181
176182 class Post (TimestampModel ): # type: ignore[no-redef]
@@ -181,10 +187,8 @@ class Post(TimestampModel): # type: ignore[no-redef]
181187
182188 class TimestampModel (db .Model ): # type: ignore[no-redef]
183189 __abstract__ = True
184- created = db .Column (db .DateTime , nullable = False , default = datetime .utcnow )
185- updated = db .Column (
186- db .DateTime , onupdate = datetime .utcnow , default = datetime .utcnow
187- )
190+ created = db .Column (db .DateTime , nullable = False , default = UTCNow )
191+ updated = db .Column (db .DateTime , onupdate = UTCNow , default = UTCNow )
188192
189193 class Post (TimestampModel ): # type: ignore[no-redef]
190194 id = db .Column (db .Integer , primary_key = True )
@@ -207,12 +211,12 @@ def test_mixinmodel(app: Flask, model_class: t.Any) -> None:
207211
208212 class TimestampMixin (sa_orm .MappedAsDataclass ):
209213 created : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
210- db .DateTime , nullable = False , insert_default = datetime . utcnow , init = False
214+ db .DateTime , nullable = False , insert_default = UTCNow , init = False
211215 )
212216 updated : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
213217 db .DateTime ,
214- insert_default = datetime . utcnow ,
215- onupdate = datetime . utcnow ,
218+ insert_default = UTCNow ,
219+ onupdate = UTCNow ,
216220 init = False ,
217221 )
218222
@@ -226,10 +230,10 @@ class Post(TimestampMixin, db.Model):
226230
227231 class TimestampMixin : # type: ignore[no-redef]
228232 created : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
229- db .DateTime , nullable = False , default = datetime . utcnow
233+ db .DateTime , nullable = False , default = UTCNow
230234 )
231235 updated : sa_orm .Mapped [datetime ] = sa_orm .mapped_column (
232- db .DateTime , default = datetime . utcnow , onupdate = datetime . utcnow
236+ db .DateTime , default = UTCNow , onupdate = UTCNow
233237 )
234238
235239 class Post (TimestampMixin , db .Model ): # type: ignore[no-redef]
@@ -239,10 +243,8 @@ class Post(TimestampMixin, db.Model): # type: ignore[no-redef]
239243 else :
240244
241245 class TimestampMixin : # type: ignore[no-redef]
242- created = db .Column (db .DateTime , nullable = False , default = datetime .utcnow )
243- updated = db .Column (
244- db .DateTime , onupdate = datetime .utcnow , default = datetime .utcnow
245- )
246+ created = db .Column (db .DateTime , nullable = False , default = UTCNow )
247+ updated = db .Column (db .DateTime , onupdate = UTCNow , default = UTCNow )
246248
247249 class Post (TimestampMixin , db .Model ): # type: ignore[no-redef]
248250 id = db .Column (db .Integer , primary_key = True )
0 commit comments