From b6ce63e2a96d646427b1b29eabb33a36b9544b90 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Thu, 23 Feb 2023 15:20:51 -0300 Subject: [PATCH] Fix/rewrite flaky schedule tests (#1397) --- engine/apps/api/tests/test_schedules.py | 50 ++++++------------- .../public_api/tests/test_schedule_export.py | 43 ++++++++++++++-- .../apps/public_api/tests/test_schedules.py | 2 +- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/engine/apps/api/tests/test_schedules.py b/engine/apps/api/tests/test_schedules.py index 73cada78..e3a9d633 100644 --- a/engine/apps/api/tests/test_schedules.py +++ b/engine/apps/api/tests/test_schedules.py @@ -1096,20 +1096,15 @@ def test_merging_same_shift_events( organization, user, token = make_organization_and_user_with_plugin_token() client = APIClient() - schedule = make_schedule( - organization, - schedule_class=OnCallScheduleWeb, - name="test_web_schedule", - ) + schedule = make_schedule(organization, schedule_class=OnCallScheduleWeb) now = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) start_date = now - timezone.timedelta(days=7) - request_date = start_date + timezone.timedelta(days=1) + # tomorrow + request_date = now + timezone.timedelta(days=1) user_a = make_user_for_organization(organization) user_b = make_user_for_organization(organization) user_c = make_user_for_organization(organization, role=LegacyAccessControlRole.VIEWER) - # clear users pks <-> organization cache (persisting between tests) - memoized_users_in_ical.cache_clear() data = { "start": start_date + timezone.timedelta(hours=10), @@ -1124,58 +1119,43 @@ def test_merging_same_shift_events( ) on_call_shift.add_rolling_users([[user_a, user_c, user_b]]) - expected_events = [ - { - "calendar_type": 0, - "end": request_date + timezone.timedelta(hours=12), - "is_gap": False, - "priority_level": 1, - "start": request_date + timezone.timedelta(hours=10), - "users": sorted([user_a.username, user_b.username]), - "missing_users": [user_c.username], - } - ] + expected_users = { + "users": sorted([user_a.username, user_b.username]), + "missing_users": [user_c.username], + } # final schedule url = reverse("api-internal:schedule-filter-events", kwargs={"pk": schedule.public_primary_key}) - url += "?date={}&days=1".format(request_date.strftime("%Y-%m-%d")) + url += "?date={}&days=3".format(request_date.strftime("%Y-%m-%d")) response = client.get(url, format="json", **make_user_auth_headers(user, token)) assert response.status_code == status.HTTP_200_OK - returned_events = [ + returned_users = [ { - "calendar_type": e["calendar_type"], - "end": e["end"], - "is_gap": e["is_gap"], - "priority_level": e["priority_level"], - "start": e["start"], "users": sorted([u["display_name"] for u in e["users"]]) if e["users"] else None, "missing_users": e["missing_users"], } for e in response.data["events"] if not e["is_gap"] ] - assert returned_events == expected_events + for users in returned_users: + assert users == expected_users # rotations url = reverse("api-internal:schedule-filter-events", kwargs={"pk": schedule.public_primary_key}) - url += "?date={}&days=1&type=rotation".format(request_date.strftime("%Y-%m-%d")) + url += "?date={}&days=3&type=rotation".format(request_date.strftime("%Y-%m-%d")) response = client.get(url, format="json", **make_user_auth_headers(user, token)) assert response.status_code == status.HTTP_200_OK - returned_events = [ + returned_users = [ { - "calendar_type": e["calendar_type"], - "end": e["end"], - "is_gap": e["is_gap"], - "priority_level": e["priority_level"], - "start": e["start"], "users": sorted([u["display_name"] for u in e["users"]]) if e["users"] else None, "missing_users": e["missing_users"], } for e in response.data["events"] if not e["is_gap"] ] - assert returned_events == expected_events + for users in returned_users: + assert users == expected_users @pytest.mark.django_db diff --git a/engine/apps/public_api/tests/test_schedule_export.py b/engine/apps/public_api/tests/test_schedule_export.py index 4d311d98..f24b25a5 100644 --- a/engine/apps/public_api/tests/test_schedule_export.py +++ b/engine/apps/public_api/tests/test_schedule_export.py @@ -7,7 +7,44 @@ from rest_framework.test import APIClient from apps.auth_token.models import ScheduleExportAuthToken, UserScheduleExportAuthToken from apps.schedules.models import OnCallScheduleICal -ICAL_URL = "https://calendar.google.com/calendar/ical/c_6i1aprpgaqu89hqeelv7mrj264%40group.calendar.google.com/private-6a995cea6e74dd2cdc5d8c75bee06a2f/basic.ics" # noqa +ICAL_DATA = """ +BEGIN:VCALENDAR +PRODID://Grafana Labs//Grafana On-Call// +CALSCALE:GREGORIAN +X-WR-CALNAME:test_ical_schedule +X-WR-TIMEZONE:UTC +BEGIN:VEVENT +SUMMARY:justin.hunthrop@grafana.com +DTSTART;TZID=America/Chicago:20211015T000000 +DTEND;TZID=America/Chicago:20211015T120000 +DTSTAMP:20230223T144743Z +UID:03vjiku070po61a9t8t7ln9q4o@google.com +SEQUENCE:1 +RRULE:FREQ=DAILY +CREATED:20211015T013834Z +DESCRIPTION: +LAST-MODIFIED:20211015T142118Z +LOCATION: +STATUS:CONFIRMED +TRANSP:TRANSPARENT +END:VEVENT +BEGIN:VEVENT +SUMMARY:amixr +DTSTART;TZID=America/Chicago:20211015T120000 +DTEND;TZID=America/Chicago:20211016T000000 +DTSTAMP:20230223T144743Z +UID:0g1cuqi56qtaqvgb38crsh0mpa@google.com +SEQUENCE:1 +RRULE:FREQ=DAILY +CREATED:20211015T020105Z +DESCRIPTION: +LAST-MODIFIED:20211015T140758Z +LOCATION: +STATUS:CONFIRMED +TRANSP:OPAQUE +END:VEVENT +END:VCALENDAR +""" @pytest.mark.django_db @@ -19,7 +56,7 @@ def test_export_calendar(make_organization_and_user_with_token, make_schedule): organization, schedule_class=OnCallScheduleICal, name="test_ical_schedule", - ical_url_primary=ICAL_URL, + cached_ical_file_primary=ICAL_DATA, ) _, schedule_token = ScheduleExportAuthToken.create_auth_token( user=user, organization=organization, schedule=schedule @@ -51,7 +88,7 @@ def test_export_user_calendar(make_organization_and_user_with_token, make_schedu organization, schedule_class=OnCallScheduleICal, name="test_ical_schedule", - ical_url_primary=ICAL_URL, + cached_ical_file_primary=ICAL_DATA, ) _, schedule_token = UserScheduleExportAuthToken.create_auth_token(user=user, organization=organization) diff --git a/engine/apps/public_api/tests/test_schedules.py b/engine/apps/public_api/tests/test_schedules.py index d98e54e1..22d1fc1e 100644 --- a/engine/apps/public_api/tests/test_schedules.py +++ b/engine/apps/public_api/tests/test_schedules.py @@ -14,7 +14,7 @@ from apps.schedules.models import ( OnCallScheduleWeb, ) -ICAL_URL = "https://calendar.google.com/calendar/ical/amixr.io_37gttuakhrtr75ano72p69rt78%40group.calendar.google.com/private-1d00a680ba5be7426c3eb3ef1616e26d/basic.ics" +ICAL_URL = "https://some.calendar.url" @pytest.mark.django_db