From f8314ef9c2852ee0326930a5edae65a6094034d3 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 3 Oct 2022 07:37:59 -0300 Subject: [PATCH] Fix timing issue with schedule tests reusing cached users (#592) Co-authored-by: Vadim Stepanov --- engine/apps/api/tests/test_schedules.py | 17 +++++++++++------ .../schedules/tests/test_on_call_schedule.py | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/engine/apps/api/tests/test_schedules.py b/engine/apps/api/tests/test_schedules.py index 7139f2e9..19537391 100644 --- a/engine/apps/api/tests/test_schedules.py +++ b/engine/apps/api/tests/test_schedules.py @@ -10,6 +10,7 @@ from rest_framework.serializers import ValidationError from rest_framework.test import APIClient from apps.alerts.models import EscalationPolicy +from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import ( CustomOnCallShift, OnCallSchedule, @@ -742,6 +743,8 @@ 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) @@ -837,7 +840,7 @@ def test_next_shifts_per_user( make_schedule, make_on_call_shift, ): - organization, user, token = make_organization_and_user_with_plugin_token() + organization, admin, token = make_organization_and_user_with_plugin_token() client = APIClient() schedule = make_schedule( @@ -848,6 +851,8 @@ def test_next_shifts_per_user( tomorrow = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) + timezone.timedelta(days=1) user_a, user_b, user_c, user_d = (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) @@ -860,16 +865,16 @@ def test_next_shifts_per_user( for user, priority, start_h, duration in shifts: data = { "start": tomorrow + timezone.timedelta(hours=start_h), - "rotation_start": tomorrow, + "rotation_start": tomorrow + timezone.timedelta(hours=start_h), "duration": timezone.timedelta(hours=duration), "priority_level": priority, "frequency": CustomOnCallShift.FREQUENCY_DAILY, "schedule": schedule, } on_call_shift = make_on_call_shift( - organization=organization, shift_type=CustomOnCallShift.TYPE_RECURRENT_EVENT, **data + organization=organization, shift_type=CustomOnCallShift.TYPE_ROLLING_USERS_EVENT, **data ) - on_call_shift.users.add(user) + on_call_shift.add_rolling_users([[user]]) # override in the past: 17-18 / D # won't be listed, but user D will still be included in the response @@ -896,10 +901,10 @@ def test_next_shifts_per_user( ) override.add_rolling_users([[user_c]]) - # final sdhedule: 7-12: B, 15-16: A, 16-17: B, 17-18: C (override), 18-20: C + # final schedule: 7-12: B, 15-16: A, 16-17: B, 17-18: C (override), 18-20: C url = reverse("api-internal:schedule-next-shifts-per-user", kwargs={"pk": schedule.public_primary_key}) - response = client.get(url, format="json", **make_user_auth_headers(user, token)) + response = client.get(url, format="json", **make_user_auth_headers(admin, token)) assert response.status_code == status.HTTP_200_OK expected = { diff --git a/engine/apps/schedules/tests/test_on_call_schedule.py b/engine/apps/schedules/tests/test_on_call_schedule.py index e8da0e67..bb9699e7 100644 --- a/engine/apps/schedules/tests/test_on_call_schedule.py +++ b/engine/apps/schedules/tests/test_on_call_schedule.py @@ -4,6 +4,7 @@ import pytest import pytz from django.utils import timezone +from apps.schedules.ical_utils import memoized_users_in_ical from apps.schedules.models import CustomOnCallShift, OnCallSchedule, OnCallScheduleCalendar, OnCallScheduleWeb from common.constants.role import Role @@ -236,6 +237,8 @@ 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"): 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)