Skip to content

Commit

Permalink
Stop using datetime.utcnow() in tests
Browse files Browse the repository at this point in the history
datetime.utcnow() is deprecated for Python 3.12+, and raises a warning.
Since warnings are treated as errors, this results in test failures.
Since utcnow calls are done by the SQLAlchemy mapping machinery, we need
to use a callable.

Fixes pallets-eco#1303
  • Loading branch information
s-t-e-v-e-n-k committed Jan 31, 2024
1 parent 42a36a3 commit 90d6e72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
----------

- No longer call ``datetime.utcnow()`` in the test suite. :issue:`1303`

Version 3.1.1
-------------

Expand Down
38 changes: 20 additions & 18 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import typing as t
from datetime import datetime
from datetime import timezone

import pytest
import sqlalchemy as sa
Expand All @@ -14,6 +15,11 @@
from flask_sqlalchemy.model import Model


class UTCNow(datetime):
def __new__(cls):
return datetime.now(tz=timezone.utc)


def test_default_model_class_1x(app: Flask) -> None:
db = SQLAlchemy(app)

Expand Down Expand Up @@ -147,12 +153,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=UTCNow, init=False
)
updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
db.DateTime,
insert_default=datetime.utcnow,
onupdate=datetime.utcnow,
insert_default=UTCNow,
onupdate=UTCNow,
init=False,
)

Expand All @@ -167,10 +173,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=UTCNow
)
updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
db.DateTime, default=UTCNow, onupdate=UTCNow
)

class Post(TimestampModel): # type: ignore[no-redef]
Expand All @@ -181,10 +187,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=UTCNow)
updated = db.Column(db.DateTime, onupdate=UTCNow, default=UTCNow)

class Post(TimestampModel): # type: ignore[no-redef]
id = db.Column(db.Integer, primary_key=True)
Expand All @@ -207,12 +211,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=UTCNow, init=False
)
updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
db.DateTime,
insert_default=datetime.utcnow,
onupdate=datetime.utcnow,
insert_default=UTCNow,
onupdate=UTCNow,
init=False,
)

Expand All @@ -226,10 +230,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=UTCNow
)
updated: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
db.DateTime, default=UTCNow, onupdate=UTCNow
)

class Post(TimestampMixin, db.Model): # type: ignore[no-redef]
Expand All @@ -239,10 +243,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=UTCNow)
updated = db.Column(db.DateTime, onupdate=UTCNow, default=UTCNow)

class Post(TimestampMixin, db.Model): # type: ignore[no-redef]
id = db.Column(db.Integer, primary_key=True)
Expand Down

0 comments on commit 90d6e72

Please sign in to comment.