|
1 | 1 | from datetime import datetime, timedelta |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +import time_machine |
4 | 5 |
|
5 | | -from manage_breast_screening.notifications.models import ZONE_INFO |
| 6 | +from manage_breast_screening.notifications.models import ZONE_INFO, Clinic |
6 | 7 | from manage_breast_screening.notifications.queries.helper import Helper |
7 | 8 | from manage_breast_screening.notifications.tests.factories import ( |
8 | 9 | AppointmentFactory, |
| 10 | + ChannelStatusFactory, |
9 | 11 | ClinicFactory, |
| 12 | + MessageFactory, |
10 | 13 | ) |
11 | 14 |
|
12 | 15 |
|
| 16 | +def df(dt: datetime): |
| 17 | + return dt.strftime("%Y-%m-%d %H:%M") |
| 18 | + |
| 19 | + |
| 20 | +def create_appointment_set( |
| 21 | + nhs_number: str, |
| 22 | + date: datetime, |
| 23 | + clinic: Clinic, |
| 24 | + appointment_status: str, |
| 25 | + episode_type: str, |
| 26 | + message_status: str | None = None, |
| 27 | + channel_statuses: dict[str, str] | None = None, |
| 28 | +): |
| 29 | + appt = AppointmentFactory( |
| 30 | + clinic=clinic, |
| 31 | + status=appointment_status, |
| 32 | + episode_type=episode_type, |
| 33 | + nhs_number=nhs_number, |
| 34 | + starts_at=date, |
| 35 | + ) |
| 36 | + if appointment_status == "C": |
| 37 | + appt.cancelled_at = datetime.now(tz=ZONE_INFO) |
| 38 | + appt.save() |
| 39 | + |
| 40 | + if message_status: |
| 41 | + sent_at = datetime.now(tz=ZONE_INFO) - timedelta(days=3) |
| 42 | + message = MessageFactory( |
| 43 | + appointment=appt, status=message_status, sent_at=sent_at |
| 44 | + ) |
| 45 | + for channel, status in channel_statuses.items(): |
| 46 | + status_updated_at = sent_at |
| 47 | + if channel != "nhsapp": |
| 48 | + status_updated_at = sent_at + timedelta(days=3) |
| 49 | + |
| 50 | + ChannelStatusFactory( |
| 51 | + message=message, |
| 52 | + channel=channel, |
| 53 | + status=status, |
| 54 | + status_updated_at=status_updated_at, |
| 55 | + ) |
| 56 | + return appt |
| 57 | + |
| 58 | + |
13 | 59 | @pytest.mark.django_db |
14 | 60 | class TestReconciliationQuery: |
| 61 | + @time_machine.travel(datetime.now(tz=ZONE_INFO), tick=False) |
15 | 62 | def test_appointments_for_today_by_episode_type(self): |
16 | | - clinic1 = ClinicFactory(bso_code="MDB", code="BU001") |
17 | | - clinic2 = ClinicFactory(bso_code="MDB", code="BU002") |
18 | | - clinic3 = ClinicFactory(bso_code="MDB", code="BU003") |
19 | | - |
20 | | - AppointmentFactory.create_batch(size=7, clinic=clinic1) |
21 | | - AppointmentFactory.create_batch(size=8, clinic=clinic1, episode_type="R") |
22 | | - AppointmentFactory.create_batch(size=2, clinic=clinic1, episode_type="G") |
23 | | - AppointmentFactory.create_batch( |
24 | | - size=2, clinic=clinic1, episode_type="G", status="C" |
25 | | - ) |
26 | | - AppointmentFactory.create_batch(size=3, clinic=clinic1, episode_type="F") |
27 | | - AppointmentFactory.create_batch(size=5, clinic=clinic1, episode_type="H") |
28 | | - AppointmentFactory.create_batch(size=2, clinic=clinic2, episode_type="F") |
29 | | - AppointmentFactory.create_batch( |
30 | | - size=3, clinic=clinic2, episode_type="F", status="A" |
31 | | - ) |
32 | | - AppointmentFactory.create_batch(size=9, clinic=clinic2, episode_type="H") |
33 | | - AppointmentFactory.create_batch(size=5, clinic=clinic2, episode_type="N") |
34 | | - AppointmentFactory.create_batch(size=2, clinic=clinic3) |
35 | | - AppointmentFactory.create_batch(size=5, clinic=clinic3, episode_type="F") |
36 | | - AppointmentFactory.create_batch(size=4, clinic=clinic3, episode_type="G") |
37 | | - AppointmentFactory.create_batch(size=6, clinic=clinic3, episode_type="R") |
| 63 | + now = datetime.now(tz=ZONE_INFO) |
| 64 | + breakpoint() |
38 | 65 |
|
39 | | - tomorrow_appt = AppointmentFactory(clinic=clinic1) |
40 | | - tomorrow_appt.created_at = datetime.now(tz=ZONE_INFO) + timedelta(days=1) |
41 | | - tomorrow_appt.save() |
| 66 | + clinic1 = ClinicFactory(code="BU001", bso_code="BSO1", name="BSU 1") |
| 67 | + clinic2 = ClinicFactory(code="BU002", bso_code="BSO1", name="BSU 2") |
42 | 68 |
|
43 | | - results = Helper.fetchall( |
44 | | - "reconciliation", |
45 | | - ( |
46 | | - datetime.now(tz=ZONE_INFO).date(), |
47 | | - "MDB", |
48 | | - ), |
49 | | - ) |
| 69 | + date1 = now - timedelta(days=4) |
| 70 | + date2 = now - timedelta(days=5) |
| 71 | + date3 = now - timedelta(days=6) |
| 72 | + |
| 73 | + nhsapp_read = {"nhsapp": "read"} |
| 74 | + sms_delivered = {"nhsapp": "unnotified", "sms": "delivered"} |
| 75 | + letter_sent = { |
| 76 | + "nhsapp": "unnotified", |
| 77 | + "sms": "permanent_failure", |
| 78 | + "letter": "received", |
| 79 | + } |
| 80 | + failed = { |
| 81 | + "nhsapp": "unnotified", |
| 82 | + "sms": "permanent_failure", |
| 83 | + "letter": "validation_failed", |
| 84 | + } |
| 85 | + |
| 86 | + test_data = [ |
| 87 | + ["9991112211", date1, clinic1, "B", "R"], |
| 88 | + ["9991112212", date1, clinic2, "B", "S"], |
| 89 | + ["9991112213", date2, clinic1, "C", "S"], |
| 90 | + ["9991112214", date3, clinic2, "C", "G"], |
| 91 | + ["9991112221", date1, clinic1, "B", "R", "failed", failed], |
| 92 | + ["9991112222", date3, clinic1, "B", "S", "delivered", nhsapp_read], |
| 93 | + ["9991112223", date2, clinic1, "B", "S", "delivered", sms_delivered], |
| 94 | + ["9991112225", date3, clinic2, "B", "F", "delivered", nhsapp_read], |
| 95 | + ["9991112226", date1, clinic2, "B", "F", "delivered", sms_delivered], |
| 96 | + ["9991112227", date1, clinic2, "C", "S", "delivered", nhsapp_read], |
| 97 | + ["9991112228", date3, clinic1, "B", "F", "failed", failed], |
| 98 | + ["9991112229", date2, clinic1, "B", "R", "delivered", letter_sent], |
| 99 | + ["9991112231", date3, clinic2, "B", "F", "delivered", nhsapp_read], |
| 100 | + ] |
50 | 101 |
|
51 | | - assert len(results) == 14 |
| 102 | + expectations = [ |
| 103 | + [ |
| 104 | + "9991112211", |
| 105 | + "BSU 1 (BU001)", |
| 106 | + "Routine recall", |
| 107 | + "Booked", |
| 108 | + "Pending", |
| 109 | + df(now), |
| 110 | + df(date1), |
| 111 | + None, |
| 112 | + None, |
| 113 | + None, |
| 114 | + None, |
| 115 | + None, |
| 116 | + ], |
| 117 | + [ |
| 118 | + "9991112212", |
| 119 | + "BSU 2 (BU002)", |
| 120 | + "Self referal", |
| 121 | + "Booked", |
| 122 | + "Pending", |
| 123 | + df(now), |
| 124 | + df(date1), |
| 125 | + None, |
| 126 | + None, |
| 127 | + None, |
| 128 | + None, |
| 129 | + None, |
| 130 | + ], |
| 131 | + [ |
| 132 | + "9991112213", |
| 133 | + "BSU 1 (BU001)", |
| 134 | + "Self referal", |
| 135 | + "Cancelled", |
| 136 | + "Pending", |
| 137 | + df(now), |
| 138 | + df(date2), |
| 139 | + df(now), |
| 140 | + None, |
| 141 | + None, |
| 142 | + None, |
| 143 | + None, |
| 144 | + ], |
| 145 | + [ |
| 146 | + "9991112214", |
| 147 | + "BSU 1 (BU001)", |
| 148 | + "Self referal", |
| 149 | + "Cancelled", |
| 150 | + "Pending", |
| 151 | + df(now), |
| 152 | + df(date3), |
| 153 | + df(now), |
| 154 | + None, |
| 155 | + None, |
| 156 | + None, |
| 157 | + None, |
| 158 | + ], |
| 159 | + ] |
52 | 160 |
|
53 | | - assert results[0] == ("BU001", "F", "B", 3) |
54 | | - assert results[1] == ("BU001", "G", "B", 2) |
55 | | - assert results[2] == ("BU001", "G", "C", 2) |
56 | | - assert results[3] == ("BU001", "H", "B", 5) |
57 | | - assert results[4] == ("BU001", "R", "B", 8) |
58 | | - assert results[5] == ("BU001", "S", "B", 7) |
| 161 | + for d in test_data: |
| 162 | + create_appointment_set(*d) |
59 | 163 |
|
60 | | - assert results[6] == ("BU002", "F", "A", 3) |
61 | | - assert results[7] == ("BU002", "F", "B", 2) |
62 | | - assert results[8] == ("BU002", "H", "B", 9) |
63 | | - assert results[9] == ("BU002", "N", "B", 5) |
| 164 | + results = Helper.fetchall("reconciliation", ["1 week", "BSO1"]) |
64 | 165 |
|
65 | | - assert results[10] == ("BU003", "F", "B", 5) |
66 | | - assert results[11] == ("BU003", "G", "B", 4) |
67 | | - assert results[12] == ("BU003", "R", "B", 6) |
68 | | - assert results[13] == ("BU003", "S", "B", 2) |
| 166 | + for expectation in expectations: |
| 167 | + for idx, res in enumerate(results): |
| 168 | + assert expectation == list(res) |
69 | 169 |
|
70 | 170 | def test_appointments_filtered_for_specified_bso(self): |
71 | 171 | clinic1 = ClinicFactory(bso_code="MDB", code="BU001") |
|
0 commit comments