From cc9dc66437e04711f9a37c8673d9fcdfd366f8c2 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 6 Nov 2023 13:52:50 -0300 Subject: [PATCH] Move cache clear to fixtures, fix some deprecation notices (#3269) --- .../tasks/notify_ical_schedule_shift.py | 4 +-- .../tests/test_notify_ical_schedule_shift.py | 30 ++--------------- engine/apps/api/tests/test_schedules.py | 9 ------ .../public_api/serializers/schedules_base.py | 3 +- engine/apps/schedules/ical_utils.py | 17 +++++----- .../tests/test_custom_on_call_shift.py | 10 +++--- ...t_notify_about_empty_shifts_in_schedule.py | 7 ---- .../test_notify_about_gaps_in_schedule.py | 11 ------- .../schedules/tests/test_on_call_schedule.py | 19 ----------- .../schedules/tests/test_quality_score.py | 10 ------ .../apps/user_management/tests/test_user.py | 32 +++++++++++-------- engine/conftest.py | 7 ++++ 12 files changed, 43 insertions(+), 116 deletions(-) diff --git a/engine/apps/alerts/tasks/notify_ical_schedule_shift.py b/engine/apps/alerts/tasks/notify_ical_schedule_shift.py index 43141249..bf76d53d 100644 --- a/engine/apps/alerts/tasks/notify_ical_schedule_shift.py +++ b/engine/apps/alerts/tasks/notify_ical_schedule_shift.py @@ -3,8 +3,6 @@ import json import typing from typing import TYPE_CHECKING -from django.utils import timezone - from apps.schedules.ical_utils import calculate_shift_diff, parse_event_uid from apps.slack.client import SlackClient from apps.slack.errors import ( @@ -99,7 +97,7 @@ def notify_ical_schedule_shift(schedule_pk): prev_shift["end"] = datetime.datetime.strptime(prev_shift["end"], str_format) # get shifts in progress now - now = datetime.datetime.now(timezone.utc) + now = datetime.datetime.now(datetime.timezone.utc) current_shifts = schedule.final_events(now, now, with_empty=False, with_gap=False, ignore_untaken_swaps=True) # get days_to_lookup for next shifts (which may affect current shifts) diff --git a/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py b/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py index 31f3f731..2833a826 100644 --- a/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py +++ b/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py @@ -4,14 +4,12 @@ import textwrap from unittest.mock import Mock, patch import pytest -import pytz from django.utils import timezone from apps.alerts.tasks.notify_ical_schedule_shift import ( MIN_DAYS_TO_LOOKUP_FOR_THE_END_OF_EVENT, notify_ical_schedule_shift, ) -from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import CustomOnCallShift, OnCallScheduleCalendar, OnCallScheduleICal, OnCallScheduleWeb ICAL_DATA = """ @@ -86,8 +84,6 @@ def test_next_shift_notification_long_shifts( organization, _, _, _ = make_organization_and_user_with_slack_identities() make_user(organization=organization, username="user1") make_user(organization=organization, username="user2") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() ical_schedule = make_schedule( organization, @@ -102,7 +98,7 @@ def test_next_shift_notification_long_shifts( ) with patch("apps.alerts.tasks.notify_ical_schedule_shift.datetime", Mock(wraps=datetime)) as mock_datetime: - mock_datetime.datetime.now.return_value = datetime.datetime(2021, 9, 29, 12, 0, tzinfo=pytz.UTC) + mock_datetime.datetime.now.return_value = datetime.datetime(2021, 9, 29, 12, 0, tzinfo=datetime.timezone.utc) with patch("apps.slack.client.SlackClient.chat_postMessage") as mock_slack_api_call: notify_ical_schedule_shift(ical_schedule.pk) @@ -122,8 +118,6 @@ def test_overrides_changes_no_current_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() ical_before = textwrap.dedent( """ @@ -222,8 +216,6 @@ def test_no_changes_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -270,8 +262,6 @@ def test_current_shift_changes_trigger_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -322,8 +312,6 @@ def test_current_shift_changes_swap_split( organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -385,8 +373,6 @@ def test_current_shift_changes_end_affected_by_swap( organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -453,8 +439,6 @@ def test_next_shift_changes_no_triggering_notification( organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -524,8 +508,6 @@ def test_lower_priority_changes_no_triggering_notification( organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -589,8 +571,6 @@ def test_vtimezone_changes_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() ical_before = textwrap.dedent( """ @@ -696,7 +676,7 @@ def test_vtimezone_changes_no_triggering_notification( ) # setup current shifts before checking/triggering for notifications - now = datetime.datetime.now(timezone.utc) + now = datetime.datetime.now(datetime.timezone.utc) current_shifts = schedule.final_events(now, now, False, False) schedule.current_shifts = json.dumps(current_shifts, default=str) schedule.empty_oncall = False @@ -720,8 +700,6 @@ def test_no_changes_no_triggering_notification_from_old_to_new_task_version( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -778,8 +756,6 @@ def test_current_shift_changes_trigger_notification_from_old_to_new_task_version organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -841,8 +817,6 @@ def test_next_shift_notification_long_and_short_shifts( user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2") user3 = make_user(organization=organization, username="user3") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, diff --git a/engine/apps/api/tests/test_schedules.py b/engine/apps/api/tests/test_schedules.py index 14bbd227..0dc27e11 100644 --- a/engine/apps/api/tests/test_schedules.py +++ b/engine/apps/api/tests/test_schedules.py @@ -13,7 +13,6 @@ from rest_framework.test import APIClient from apps.alerts.models import EscalationPolicy from apps.api.permissions import LegacyAccessControlRole from apps.api.serializers.user import ScheduleUserSerializer -from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import ( CustomOnCallShift, OnCallSchedule, @@ -1197,8 +1196,6 @@ def test_filter_events_final_schedule( request_date = start_date user_a, user_b, user_c, user_d, user_e = (make_user_for_organization(organization, username=i) for i in "ABCDE") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -1308,8 +1305,6 @@ def test_filter_swap_requests( name="other_web_schedule", ) user_a, user_b, user_c = (make_user_for_organization(organization, username=i) for i in "ABC") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() today = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) start_date = today - timezone.timedelta(days=7) @@ -1408,8 +1403,6 @@ def test_next_shifts_per_user( user_a, user_b, user_c, user_d = ( make_user_for_organization(organization, username=i, _timezone=tz) for i, tz in users ) - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -1558,8 +1551,6 @@ def test_related_users( tomorrow = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) + timezone.timedelta(days=1) user_a, user_b, user_c, _ = (make_user_for_organization(organization, username=i) for i in "ABCD") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) diff --git a/engine/apps/public_api/serializers/schedules_base.py b/engine/apps/public_api/serializers/schedules_base.py index d0b5b192..03e4ae60 100644 --- a/engine/apps/public_api/serializers/schedules_base.py +++ b/engine/apps/public_api/serializers/schedules_base.py @@ -1,6 +1,5 @@ import datetime -from django.utils import timezone from rest_framework import serializers from apps.schedules.ical_utils import list_users_to_notify_from_ical @@ -21,7 +20,7 @@ class ScheduleBaseSerializer(serializers.ModelSerializer): return super().create(validated_data) def get_on_call_now(self, obj): - users_on_call = list_users_to_notify_from_ical(obj, datetime.datetime.now(timezone.utc)) + users_on_call = list_users_to_notify_from_ical(obj, datetime.datetime.now(datetime.timezone.utc)) if users_on_call is not None: return [user.public_primary_key for user in users_on_call] else: diff --git a/engine/apps/schedules/ical_utils.py b/engine/apps/schedules/ical_utils.py index 700645aa..1d4627aa 100644 --- a/engine/apps/schedules/ical_utils.py +++ b/engine/apps/schedules/ical_utils.py @@ -10,7 +10,6 @@ from typing import TYPE_CHECKING import pytz import requests from django.db.models import Q -from django.utils import timezone from icalendar import Calendar from icalendar import Event as IcalEvent @@ -251,8 +250,8 @@ def get_shifts_dict( if start < end: result_datetime.append( { - "start": start.astimezone(pytz.UTC), - "end": end.astimezone(pytz.UTC), + "start": start.astimezone(datetime.timezone.utc), + "end": end.astimezone(datetime.timezone.utc), "users": users, "missing_users": missing_users, "priority": priority, @@ -288,9 +287,9 @@ def list_of_empty_shifts_in_schedule( start_datetime = datetime.datetime.combine(start_date, datetime.time.min) + datetime.timedelta( milliseconds=1 ) - start_datetime_with_offset = (start_datetime - schedule_timezone_offset).astimezone(pytz.UTC) + start_datetime_with_offset = (start_datetime - schedule_timezone_offset).astimezone(datetime.timezone.utc) end_datetime = datetime.datetime.combine(end_date, datetime.time.max) - end_datetime_with_offset = (end_datetime - schedule_timezone_offset).astimezone(pytz.UTC) + end_datetime_with_offset = (end_datetime - schedule_timezone_offset).astimezone(datetime.timezone.utc) events = ical_events.get_events_from_ical_between( calendar, start_datetime_with_offset, end_datetime_with_offset @@ -315,8 +314,8 @@ def list_of_empty_shifts_in_schedule( start, end, all_day = event_start_end_all_day_with_respect_to_type(event, calendar_tz) if not all_day: - start = start.astimezone(pytz.UTC) - end = end.astimezone(pytz.UTC) + start = start.astimezone(datetime.timezone.utc) + end = end.astimezone(datetime.timezone.utc) empty_shifts_per_calendar.append( EmptyShift( @@ -343,7 +342,7 @@ def list_users_to_notify_from_ical( """ Retrieve on-call users for the current time """ - events_datetime = events_datetime if events_datetime else datetime.datetime.now(timezone.utc) + events_datetime = events_datetime if events_datetime else datetime.datetime.now(datetime.timezone.utc) return list_users_to_notify_from_ical_for_period( schedule, events_datetime, @@ -370,7 +369,7 @@ def get_oncall_users_for_multiple_schedules( schedules: typing.List["OnCallSchedule"], events_datetime=None ) -> SchedulesOnCallUsers: if events_datetime is None: - events_datetime = datetime.datetime.now(timezone.utc) + events_datetime = datetime.datetime.now(datetime.timezone.utc) # Exit early if there are no schedules if not schedules: diff --git a/engine/apps/schedules/tests/test_custom_on_call_shift.py b/engine/apps/schedules/tests/test_custom_on_call_shift.py index 01c490f6..6537b053 100644 --- a/engine/apps/schedules/tests/test_custom_on_call_shift.py +++ b/engine/apps/schedules/tests/test_custom_on_call_shift.py @@ -1,8 +1,8 @@ +import datetime from calendar import monthrange from unittest.mock import patch import pytest -import pytz from django.utils import timezone from apps.schedules.ical_utils import list_users_to_notify_from_ical @@ -577,7 +577,7 @@ def test_rolling_users_event_with_interval_monthly( user_2 = make_user_for_organization(organization) schedule = make_schedule(organization, schedule_class=OnCallScheduleCalendar) - start_date = timezone.datetime(year=2022, month=10, day=1, hour=10, minute=30, tzinfo=pytz.UTC) + start_date = timezone.datetime(year=2022, month=10, day=1, hour=10, minute=30, tzinfo=datetime.timezone.utc) days_for_next_month_1 = monthrange(2022, 10)[1] days_for_next_month_2 = monthrange(2022, 11)[1] + days_for_next_month_1 days_for_next_month_3 = monthrange(2022, 12)[1] + days_for_next_month_2 @@ -941,7 +941,7 @@ def test_rolling_users_with_diff_start_and_rotation_start_monthly( user_3 = make_user_for_organization(organization) schedule = make_schedule(organization, schedule_class=OnCallScheduleWeb) - start_date = timezone.datetime(year=2022, month=12, day=1, hour=10, minute=30, tzinfo=pytz.UTC) + start_date = timezone.datetime(year=2022, month=12, day=1, hour=10, minute=30, tzinfo=datetime.timezone.utc) days_in_curr_month = monthrange(2022, 12)[1] days_in_next_month = monthrange(2023, 1)[1] @@ -997,7 +997,7 @@ def test_rolling_users_with_diff_start_and_rotation_start_monthly_by_monthday( user_3 = make_user_for_organization(organization) schedule = make_schedule(organization, schedule_class=OnCallScheduleWeb) - start_date = timezone.datetime(year=2022, month=12, day=1, hour=10, minute=30, tzinfo=pytz.UTC) + start_date = timezone.datetime(year=2022, month=12, day=1, hour=10, minute=30, tzinfo=datetime.timezone.utc) days_in_curr_month = monthrange(2022, 12)[1] days_in_next_month = monthrange(2023, 1)[1] @@ -1408,7 +1408,7 @@ def test_get_oncall_users_for_multiple_schedules_emails_case_insensitive( schedule.save(update_fields=["cached_ical_file_overrides"]) # Get on-call users for 6 February 2023 11:30 UTC - events_datetime = timezone.datetime(2023, 2, 6, 11, 30, tzinfo=timezone.utc) + events_datetime = timezone.datetime(2023, 2, 6, 11, 30, tzinfo=datetime.timezone.utc) schedules = OnCallSchedule.objects.filter(pk=schedule.pk) oncall_users = schedules.get_oncall_users(events_datetime=events_datetime) diff --git a/engine/apps/schedules/tests/test_notify_about_empty_shifts_in_schedule.py b/engine/apps/schedules/tests/test_notify_about_empty_shifts_in_schedule.py index adf0bd9d..2b193fa5 100644 --- a/engine/apps/schedules/tests/test_notify_about_empty_shifts_in_schedule.py +++ b/engine/apps/schedules/tests/test_notify_about_empty_shifts_in_schedule.py @@ -5,7 +5,6 @@ import pytest from django.utils import timezone from apps.api.permissions import LegacyAccessControlRole -from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import CustomOnCallShift, OnCallScheduleWeb from apps.schedules.tasks import notify_about_empty_shifts_in_schedule @@ -19,8 +18,6 @@ def test_no_empty_shifts_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -67,8 +64,6 @@ def test_empty_shifts_trigger_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1", role=LegacyAccessControlRole.VIEWER) - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -117,8 +112,6 @@ def test_empty_non_empty_shifts_trigger_notification( organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") user2 = make_user(organization=organization, username="user2", role=LegacyAccessControlRole.VIEWER) - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, diff --git a/engine/apps/schedules/tests/test_notify_about_gaps_in_schedule.py b/engine/apps/schedules/tests/test_notify_about_gaps_in_schedule.py index 912d31b9..626eba55 100644 --- a/engine/apps/schedules/tests/test_notify_about_gaps_in_schedule.py +++ b/engine/apps/schedules/tests/test_notify_about_gaps_in_schedule.py @@ -4,7 +4,6 @@ from unittest.mock import patch import pytest from django.utils import timezone -from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import CustomOnCallShift, OnCallScheduleWeb from apps.schedules.tasks import notify_about_gaps_in_schedule @@ -18,8 +17,6 @@ def test_no_gaps_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -67,8 +64,6 @@ def test_gaps_in_the_past_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -132,8 +127,6 @@ def test_gaps_now_trigger_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -185,8 +178,6 @@ def test_gaps_near_future_trigger_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() schedule = make_schedule( organization, @@ -239,8 +230,6 @@ def test_gaps_later_than_7_days_no_triggering_notification( ): organization, _, _, _ = make_organization_and_user_with_slack_identities() user1 = make_user(organization=organization, username="user1") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() now = timezone.now().replace(microsecond=0) diff --git a/engine/apps/schedules/tests/test_on_call_schedule.py b/engine/apps/schedules/tests/test_on_call_schedule.py index 16613e82..99409143 100644 --- a/engine/apps/schedules/tests/test_on_call_schedule.py +++ b/engine/apps/schedules/tests/test_on_call_schedule.py @@ -18,7 +18,6 @@ from apps.schedules.constants import ( ICAL_STATUS_CANCELLED, ICAL_SUMMARY, ) -from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import ( CustomOnCallShift, OnCallSchedule, @@ -367,8 +366,6 @@ def test_filter_events_ical_all_day(make_organization, make_user_for_organizatio schedule.cached_ical_file_primary = calendar.to_ical() for u in ("@Bernard Desruisseaux", "@Bob", "@Alex", "@Alice"): make_user_for_organization(organization, username=u) - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() day_to_check_iso = "2021-01-27T15:27:14.448059+00:00" parsed_iso_day_to_check = datetime.datetime.fromisoformat(day_to_check_iso).replace(tzinfo=pytz.UTC) @@ -441,8 +438,6 @@ def test_final_schedule_events( start_date = now - timezone.timedelta(days=7) user_a, user_b, user_c, user_d, user_e = (make_user_for_organization(organization, username=i) for i in "ABCDE") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -561,8 +556,6 @@ def test_final_schedule_override_no_priority_shift( start_date = now - timezone.timedelta(days=7) user_a, user_b = (make_user_for_organization(organization, username=i) for i in "AB") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -642,8 +635,6 @@ def test_final_schedule_override_split( start_date = now - timezone.timedelta(days=7) user_a, user_b = (make_user_for_organization(organization, username=i) for i in "AB") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -724,8 +715,6 @@ def test_final_schedule_splitting_events( start_date = now - timezone.timedelta(days=7) user_a, user_b, user_c = (make_user_for_organization(organization, username=i) for i in "ABC") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -794,8 +783,6 @@ def test_final_schedule_splitting_same_time_events( start_date = now - timezone.timedelta(days=7) user_a, user_b, user_c = (make_user_for_organization(organization, username=i) for i in "ABC") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -1220,8 +1207,6 @@ def test_schedule_related_users(make_organization, make_user_for_organization, m start_date = now - timezone.timedelta(days=7) user_a, _, _, user_d, user_e = (make_user_for_organization(organization, username=i) for i in "ABCDE") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() shifts = ( # user, priority, start time (h), duration (hs) @@ -1281,8 +1266,6 @@ def test_schedule_related_users_usernames( # Check different usernames, including those with special characters and uppercase letters usernames = ["test", "test.test", "test.test@test.test", "TEST.TEST@TEST.TEST"] users = [make_user_for_organization(organization, username=u) for u in usernames] - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() for user in users: data = { @@ -2636,8 +2619,6 @@ def test_filter_events_ical_duplicated_uid(make_organization, make_user_for_orga schedule = make_schedule(organization, schedule_class=OnCallScheduleCalendar) schedule.cached_ical_file_primary = calendar.to_ical() make_user_for_organization(organization, username="user") - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() datetime_start = datetime.datetime(2023, 7, 17, 0, 0, tzinfo=pytz.UTC) datetime_end = datetime_start + datetime.timedelta(days=7) diff --git a/engine/apps/schedules/tests/test_quality_score.py b/engine/apps/schedules/tests/test_quality_score.py index 5926f929..d5d15ae3 100644 --- a/engine/apps/schedules/tests/test_quality_score.py +++ b/engine/apps/schedules/tests/test_quality_score.py @@ -5,7 +5,6 @@ from rest_framework import status from rest_framework.reverse import reverse from rest_framework.test import APIClient -from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import CustomOnCallShift, OnCallScheduleICal, OnCallScheduleWeb @@ -19,9 +18,6 @@ def get_schedule_quality_response( make_user_auth_headers, ): def _get_schedule_quality_response(date, days): - # clear cache - memoized_users_in_ical.cache_clear() - calendar = get_ical("quality.ics") organization = make_organization() @@ -152,8 +148,6 @@ def test_get_schedule_score_weekdays( ) users = [make_user_for_organization(organization, username=f"user-{idx}") for idx in range(8)] - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() make_on_call_shift( schedule.organization, @@ -216,8 +210,6 @@ def test_get_schedule_score_all_week( ) users = [make_user_for_organization(organization, username=f"user-{idx}") for idx in range(8)] - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() make_on_call_shift( schedule.organization, @@ -293,8 +285,6 @@ def test_get_schedule_score_all_week_imbalanced_weekends( ) users = [make_user_for_organization(organization, username=f"user-{idx}") for idx in range(8)] - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() make_on_call_shift( schedule.organization, diff --git a/engine/apps/user_management/tests/test_user.py b/engine/apps/user_management/tests/test_user.py index 5fd8baff..68edce35 100644 --- a/engine/apps/user_management/tests/test_user.py +++ b/engine/apps/user_management/tests/test_user.py @@ -1,3 +1,5 @@ +import datetime + import pytest from django.utils import timezone @@ -36,10 +38,10 @@ def test_is_in_working_hours(make_organization, make_user_for_organization): organization = make_organization() user = make_user_for_organization(organization, _timezone="Europe/London") - _7_59_utc = timezone.datetime(2023, 8, 1, 7, 59, 59, tzinfo=timezone.utc) - _8_utc = timezone.datetime(2023, 8, 1, 8, 0, 0, tzinfo=timezone.utc) - _17_utc = timezone.datetime(2023, 8, 1, 16, 0, 0, tzinfo=timezone.utc) - _17_01_utc = timezone.datetime(2023, 8, 1, 16, 0, 1, tzinfo=timezone.utc) + _7_59_utc = timezone.datetime(2023, 8, 1, 7, 59, 59, tzinfo=datetime.timezone.utc) + _8_utc = timezone.datetime(2023, 8, 1, 8, 0, 0, tzinfo=datetime.timezone.utc) + _17_utc = timezone.datetime(2023, 8, 1, 16, 0, 0, tzinfo=datetime.timezone.utc) + _17_01_utc = timezone.datetime(2023, 8, 1, 16, 0, 1, tzinfo=datetime.timezone.utc) assert user.is_in_working_hours(_7_59_utc) is False assert user.is_in_working_hours(_8_utc) is True @@ -58,15 +60,19 @@ def test_is_in_working_hours_next_day(make_organization, make_user_for_organizat }, ) - _8_59_utc = timezone.datetime(2023, 8, 1, 8, 59, 59, tzinfo=timezone.utc) # 4:59pm on Tuesday in Singapore - _9_utc = timezone.datetime(2023, 8, 1, 9, 0, 0, tzinfo=timezone.utc) # 5pm on Tuesday in Singapore - _10_utc = timezone.datetime(2023, 8, 1, 10, 0, 0, tzinfo=timezone.utc) # 6pm on Tuesday in Singapore - _10_01_utc = timezone.datetime(2023, 8, 1, 10, 0, 1, tzinfo=timezone.utc) # 6:01pm on Tuesday in Singapore + _8_59_utc = timezone.datetime(2023, 8, 1, 8, 59, 59, tzinfo=datetime.timezone.utc) # 4:59pm on Tuesday in Singapore + _9_utc = timezone.datetime(2023, 8, 1, 9, 0, 0, tzinfo=datetime.timezone.utc) # 5pm on Tuesday in Singapore + _10_utc = timezone.datetime(2023, 8, 1, 10, 0, 0, tzinfo=datetime.timezone.utc) # 6pm on Tuesday in Singapore + _10_01_utc = timezone.datetime(2023, 8, 1, 10, 0, 1, tzinfo=datetime.timezone.utc) # 6:01pm on Tuesday in Singapore - _16_59_utc = timezone.datetime(2023, 8, 1, 16, 59, 0, tzinfo=timezone.utc) # 00:59am on Wednesday in Singapore - _17_utc = timezone.datetime(2023, 8, 1, 17, 0, 0, tzinfo=timezone.utc) # 1am on Wednesday in Singapore - _18_utc = timezone.datetime(2023, 8, 1, 18, 0, 0, tzinfo=timezone.utc) # 2am on Wednesday in Singapore - _18_01_utc = timezone.datetime(2023, 8, 1, 18, 0, 1, tzinfo=timezone.utc) # 2:01am on Wednesday in Singapore + _16_59_utc = timezone.datetime( + 2023, 8, 1, 16, 59, 0, tzinfo=datetime.timezone.utc + ) # 00:59am on Wednesday in Singapore + _17_utc = timezone.datetime(2023, 8, 1, 17, 0, 0, tzinfo=datetime.timezone.utc) # 1am on Wednesday in Singapore + _18_utc = timezone.datetime(2023, 8, 1, 18, 0, 0, tzinfo=datetime.timezone.utc) # 2am on Wednesday in Singapore + _18_01_utc = timezone.datetime( + 2023, 8, 1, 18, 0, 1, tzinfo=datetime.timezone.utc + ) # 2:01am on Wednesday in Singapore tz = "Asia/Singapore" assert user.is_in_working_hours(_8_59_utc, tz=tz) is False @@ -92,5 +98,5 @@ def test_is_in_working_hours_weekend(make_organization, make_user_for_organizati organization = make_organization() user = make_user_for_organization(organization, working_hours={"saturday": []}, _timezone=None) - on_saturday = timezone.datetime(2023, 8, 5, 12, 0, 0, tzinfo=timezone.utc) + on_saturday = timezone.datetime(2023, 8, 5, 12, 0, 0, tzinfo=datetime.timezone.utc) assert user.is_in_working_hours(on_saturday, "UTC") is False diff --git a/engine/conftest.py b/engine/conftest.py index 6951c38b..200f3a0d 100644 --- a/engine/conftest.py +++ b/engine/conftest.py @@ -67,6 +67,7 @@ from apps.mobile_app.models import MobileAppAuthToken, MobileAppVerificationToke from apps.phone_notifications.phone_backend import PhoneBackend from apps.phone_notifications.tests.factories import PhoneCallRecordFactory, SMSRecordFactory from apps.phone_notifications.tests.mock_phone_provider import MockPhoneProvider +from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import OnCallScheduleWeb from apps.schedules.tests.factories import ( CustomOnCallShiftFactory, @@ -188,6 +189,12 @@ def mock_is_labels_feature_enabled(settings): setattr(settings, "FEATURE_LABELS_ENABLED_FOR_ALL", True) +@pytest.fixture(autouse=True) +def clear_ical_users_cache(): + # clear users pks <-> organization cache (persisting between tests) + memoized_users_in_ical.cache_clear() + + @pytest.fixture def mock_is_labels_feature_enabled_for_org(settings): def _mock_is_labels_feature_enabled_for_org(org_id):