Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using datetime.utcnow() in tests #1308

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading