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

Fix tests depending on year 2024 #473

Merged
merged 4 commits into from
Jan 28, 2024
Merged

Fix tests depending on year 2024 #473

merged 4 commits into from
Jan 28, 2024

Conversation

tvedeane
Copy link
Contributor

@tvedeane tvedeane commented Jan 7, 2024

The correct way of getting time is: timezone.now().

This function is preferred over today() and utcnow().

(source: https://docs.python.org/3/library/datetime.html#datetime.datetime.now)

@crosspolar crosspolar self-requested a review January 12, 2024 17:32
Copy link
Contributor

@crosspolar crosspolar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is beacuse the mock patches django.utils but the the ShareOwner object is initiated with datetime.date.today() in line 111, which is not recommended in Django because of timezone-inawareness. Better

        def with_status(self, status: str, date=None):
            if date is None:
                date = timezone.now().today()

Maybe you can have a look if you can find more if these within models

@Theophile-Madet
Copy link
Contributor

I would rather keep the mocked time to have the tests date-independant. Even if we will never reach the new years you set, it's a bit nicer to have believable date ranges. Have you tried @crosspolar 's suggestion already?

@tvedeane
Copy link
Contributor Author

@Theophile-Madet Yes, I tried that already. Check #475.

@crosspolar crosspolar self-requested a review January 28, 2024 15:53
Copy link
Contributor

@crosspolar crosspolar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting a lot of warnings like this one:

tapir/shifts/tests/test_calendars.py::TestCalendars::test_day_printable_view_status_200
  /root/.cache/pypoetry/virtualenvs/tapir-9TtSrW0h-py3.11/lib/python3.11/site-packages/django/db/models/fields/__init__.py:1416: RuntimeWarning: DateTimeField Shift.end_time received a naive datetime (2023-06-15 16:00:00) while time zone support is active.
    warnings.warn("DateTimeField %s received a naive datetime (%s)"

@tvedeane
Copy link
Contributor Author

We're getting a lot of warnings like this one:
...
warnings.warn("DateTimeField %s received a naive datetime (%s)"

Looks like we should be avoiding constructs like: datetime.datetime(year=2023, month=6, day=15, hour=12), and instead use timezone.datetime(year=2023, month=6, day=15, hour=12, tzinfo=datetime.timezone.utc). I think this can be a new PR to clean this up.

@tvedeane tvedeane merged commit dc99830 into master Jan 28, 2024
8 checks passed
@tvedeane tvedeane deleted the fix-2024-tests branch January 28, 2024 17:18
@@ -299,7 +298,7 @@ def overlapping_with(self, obj) -> DurationModelMixinQuerySet:
def active_temporal(self, effective_date=None) -> DurationModelMixinQuerySet:
if not effective_date:
# if no effective date was given, use today as the default
effective_date = date.today()
effective_date = timezone.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this maybe be timezone.now().date()? The DurationModelMixin uses date fields, not datetime fields.
Though I'm not sure if dates can be timezone-aware

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the date class doesn't have a timezone. But do you see any problem with that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datetime.date.today() returns the local time, not UTC. I haven't thought it through, but it sounds like people could have an offset of 1 or 2 hours?!

Anyway, maybe it's parsed automatically or we ensure timezone-aware dates by timezone.now().date() (which is using UTC and not local time)

@@ -108,7 +108,7 @@ def with_name(self, search_string: str):

def with_status(self, status: str, date=None):
if date is None:
date = datetime.date.today()
date = timezone.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as in DurationModelMixinQuerySet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants