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 c39d8e8
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

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

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


class UTCNow(datetime):
def __new__(self):
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=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 +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=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,9 +186,9 @@ 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)
created = db.Column(db.DateTime, nullable=False, default=UTCNow)
updated = db.Column(
db.DateTime, onupdate=datetime.utcnow, default=datetime.utcnow
db.DateTime, onupdate=UTCNow, default=UTCNow
)

class Post(TimestampModel): # type: ignore[no-redef]
Expand All @@ -207,12 +212,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 +231,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,9 +244,9 @@ 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)
created = db.Column(db.DateTime, nullable=False, default=UTCNow)
updated = db.Column(
db.DateTime, onupdate=datetime.utcnow, default=datetime.utcnow
db.DateTime, onupdate=UTCNow, default=UTCNow
)

class Post(TimestampMixin, db.Model): # type: ignore[no-redef]
Expand Down

0 comments on commit c39d8e8

Please sign in to comment.