diff --git a/CHANGES.rst b/CHANGES.rst index 724c5848..29c3fe01 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,7 @@ Version 3.1.2 ------------- - Fix issue with calling ``repr()`` on ``SQLAlchemy`` instance with no default engine. :issue:`1295` +- No longer call ``datetime.utcnow()`` in the test suite. :issue:`1303` Version 3.1.1 diff --git a/tests/test_model.py b/tests/test_model.py index 0968a1e2..fc56d28d 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -2,6 +2,7 @@ import typing as t from datetime import datetime +from datetime import timezone import pytest import sqlalchemy as sa @@ -14,6 +15,10 @@ from flask_sqlalchemy.model import Model +def utc_now() -> datetime: + return datetime.now(tz=timezone.utc) + + def test_default_model_class_1x(app: Flask) -> None: db = SQLAlchemy(app) @@ -147,12 +152,12 @@ def test_abstractmodel(app: Flask, model_class: t.Any) -> None: class TimestampModel(db.Model): __abstract__ = True created: sa_orm.Mapped[datetime] = sa_orm.mapped_column( - db.DateTime, nullable=False, insert_default=datetime.utcnow, init=False + db.DateTime, nullable=False, insert_default=utc_now, init=False ) updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column( db.DateTime, - insert_default=datetime.utcnow, - onupdate=datetime.utcnow, + insert_default=utc_now, + onupdate=utc_now, init=False, ) @@ -167,10 +172,10 @@ class Post(TimestampModel): class TimestampModel(db.Model): # type: ignore[no-redef] __abstract__ = True created: sa_orm.Mapped[datetime] = sa_orm.mapped_column( - db.DateTime, nullable=False, default=datetime.utcnow + db.DateTime, nullable=False, default=utc_now ) updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow + db.DateTime, default=utc_now, onupdate=utc_now ) class Post(TimestampModel): # type: ignore[no-redef] @@ -181,10 +186,8 @@ class Post(TimestampModel): # type: ignore[no-redef] class TimestampModel(db.Model): # type: ignore[no-redef] __abstract__ = True - created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) - updated = db.Column( - db.DateTime, onupdate=datetime.utcnow, default=datetime.utcnow - ) + created = db.Column(db.DateTime, nullable=False, default=utc_now) + updated = db.Column(db.DateTime, onupdate=utc_now, default=utc_now) class Post(TimestampModel): # type: ignore[no-redef] id = db.Column(db.Integer, primary_key=True) @@ -207,12 +210,12 @@ def test_mixinmodel(app: Flask, model_class: t.Any) -> None: class TimestampMixin(sa_orm.MappedAsDataclass): created: sa_orm.Mapped[datetime] = sa_orm.mapped_column( - db.DateTime, nullable=False, insert_default=datetime.utcnow, init=False + db.DateTime, nullable=False, insert_default=utc_now, init=False ) updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column( db.DateTime, - insert_default=datetime.utcnow, - onupdate=datetime.utcnow, + insert_default=utc_now, + onupdate=utc_now, init=False, ) @@ -226,10 +229,10 @@ class Post(TimestampMixin, db.Model): class TimestampMixin: # type: ignore[no-redef] created: sa_orm.Mapped[datetime] = sa_orm.mapped_column( - db.DateTime, nullable=False, default=datetime.utcnow + db.DateTime, nullable=False, default=utc_now ) updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow + db.DateTime, default=utc_now, onupdate=utc_now ) class Post(TimestampMixin, db.Model): # type: ignore[no-redef] @@ -239,10 +242,8 @@ class Post(TimestampMixin, db.Model): # type: ignore[no-redef] else: class TimestampMixin: # type: ignore[no-redef] - created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) - updated = db.Column( - db.DateTime, onupdate=datetime.utcnow, default=datetime.utcnow - ) + created = db.Column(db.DateTime, nullable=False, default=utc_now) + updated = db.Column(db.DateTime, onupdate=utc_now, default=utc_now) class Post(TimestampMixin, db.Model): # type: ignore[no-redef] id = db.Column(db.Integer, primary_key=True)