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 function.

Fixes pallets-eco#1303
  • Loading branch information
s-t-e-v-e-n-k committed Feb 2, 2024
1 parent fec440f commit ed32fa1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 19 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,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)

Expand Down Expand Up @@ -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,
)

Expand All @@ -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]
Expand All @@ -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)
Expand All @@ -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,
)

Expand All @@ -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]
Expand All @@ -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)
Expand Down

0 comments on commit ed32fa1

Please sign in to comment.