Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created December 18, 2022 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/f4fcf86f80d480aa4680a43a9446aa54 to your computer and use it in GitHub Desktop.
Save sminnee/f4fcf86f80d480aa4680a43a9446aa54 to your computer and use it in GitHub Desktop.
diff --git a/server/app_util/tests/data.py b/server/app_util/tests/data.py
index 7d8c03ebd..bd868f291 100644
--- a/server/app_util/tests/data.py
+++ b/server/app_util/tests/data.py
@@ -173,6 +173,11 @@ def utc(dt: datetime):
return dt.astimezone(pytz.utc)
+def utc_date(dt: datetime):
+ """Convert a timezone-aware datetime to UTC and then return the date only. Test helper."""
+ return dt.astimezone(pytz.utc).date()
+
+
def nzt(y: int, m: int, d: int, *args):
"""Create a datetime in Pacific/Auckland timezone, midnight of the given day. Test helper."""
tz = pytz.timezone("Pacific/Auckland")
diff --git a/server/facilities/tests/views/work_orders/__init__.py b/server/facilities/tests/views/work_orders/__init__.py
index f9badc5b4..acf339591 100644
--- a/server/facilities/tests/views/work_orders/__init__.py
+++ b/server/facilities/tests/views/work_orders/__init__.py
@@ -10,7 +10,14 @@ from rest_framework.test import APIClient
from app_util.collections import find_item
from app_util.tests.constants import URL_DATE_FORMAT
-from app_util.tests.data import create_test_contact, create_test_work_order, create_test_org_and_property, nzt, utc
+from app_util.tests.data import (
+ create_test_contact,
+ create_test_work_order,
+ create_test_org_and_property,
+ nzt,
+ utc,
+ utc_date,
+)
from app_util.tests.mixins import GenericModelTestCase
from app_util.tests.util import assert_contains, assert_some, first_due_date, send_request
from facilities.models import (
@@ -629,6 +636,65 @@ class WorkOrderTestCase(GenericModelTestCase):
work_order_date.status,
)
+ def test_simple_update_to_repeating_date_range(self):
+ """
+ If the date (but not the repeat time) of a repeating date is updated, then shift all dates without recreating.
+ """
+
+ task = create_test_work_order(nzt(2022, 1, 31), repeat_time=WorkOrderRepeat.MONTH)
+ wo = task.work_order_content.work_order
+
+ def _single_update(wo, due_date, updates):
+ update_work_order(
+ wo, due_date, updates, disable_notifications=True, edit_type=WorkOrderRepeatEditType.THIS_EVENT
+ )
+
+ _single_update(wo, nzt(2022, 2, 28), {"status": WorkOrderStatus.ON_HOLD})
+ _single_update(wo, nzt(2022, 6, 30), {"status": WorkOrderStatus.SCHEDULED})
+
+ wod_pks = wo.dates.order_by("due_date").values_list("id", flat=True)
+ print(wod_pks)
+
+ # Check updated data - new WODs are created for the updated items, and the values that follow them
+ self.assertListEqual(
+ [
+ (utc(nzt(2022, 1, 31)), utc_date(nzt(2022, 2, 27))),
+ (utc(nzt(2022, 2, 28)), utc_date(nzt(2022, 3, 30))),
+ (utc(nzt(2022, 3, 31)), utc_date(nzt(2022, 6, 29))),
+ (utc(nzt(2022, 6, 30)), utc_date(nzt(2022, 7, 30))),
+ (utc(nzt(2022, 7, 31)), None),
+ ],
+ [(dt.due_date, dt.repeat_end_date) for dt in wo.dates.order_by("due_date")],
+ )
+
+ # From the last of the month to the 15th of the month, updating in the middle of the month
+ update_work_order(
+ wo,
+ nzt(2022, 3, 31),
+ {"due_date": nzt(2022, 3, 15), "start_date": nzt(2022, 2, 16)},
+ disable_notifications=True,
+ edit_type=WorkOrderRepeatEditType.ALL_EVENTS,
+ )
+
+ # Refresh
+ wo = WorkOrder.objects.get(pk=wo.pk)
+
+ # Check updated data - new WODs are created for the updated items, and the values that follow them
+ self.assertListEqual(
+ [
+ (utc(nzt(2022, 1, 15)), utc_date(nzt(2022, 2, 14))),
+ (utc(nzt(2022, 2, 15)), utc_date(nzt(2022, 3, 14))),
+ (utc(nzt(2022, 3, 15)), utc_date(nzt(2022, 6, 14))),
+ (utc(nzt(2022, 6, 15)), utc_date(nzt(2022, 7, 14))),
+ (utc(nzt(2022, 7, 15)), None),
+ ],
+ [(dt.due_date, dt.repeat_end_date) for dt in wo.dates.order_by("due_date")],
+ )
+
+ # The WOD records should have been updated, not recreated
+ updated_pks = wo.dates.order_by("due_date").values_list("id", flat=True)
+ self.assertEqual(wod_pks, updated_pks)
+
def test_update_content_of_single_repeat_instance(self):
"""Update the content of a single repeat instance ."""
client, user = self.create_client()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment