|
1 | 1 | import os |
2 | 2 | from contextlib import contextmanager |
3 | 3 | from datetime import datetime, timezone |
4 | | -from unittest.mock import Mock, PropertyMock, patch |
| 4 | +from unittest.mock import Mock, patch |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from azure.storage.blob import BlobProperties |
|
10 | 10 | from manage_breast_screening.notifications.management.commands.create_appointments import ( |
11 | 11 | Command, |
12 | 12 | ) |
13 | | -from manage_breast_screening.notifications.models import ZONE_INFO, Appointment, Clinic |
| 13 | +from manage_breast_screening.notifications.models import ( |
| 14 | + ZONE_INFO, |
| 15 | + Appointment, |
| 16 | + Clinic, |
| 17 | + Extract, |
| 18 | +) |
14 | 19 | from manage_breast_screening.notifications.tests.factories import ( |
15 | 20 | AppointmentFactory, |
16 | 21 | ClinicFactory, |
@@ -44,7 +49,7 @@ def stored_blob_data(prefix_dir: str, filenames: list[str]): |
44 | 49 | mock_blob_contents = [] |
45 | 50 | for filename in filenames: |
46 | 51 | mock_blob = Mock(spec=BlobProperties) |
47 | | - mock_blob.name = PropertyMock(return_value=f"{prefix_dir}/{filename}") |
| 52 | + mock_blob.name = f"{prefix_dir}/{filename}" |
48 | 53 | mock_blobs.append(mock_blob) |
49 | 54 | mock_blob_contents.append(open(fixture_file_path(filename)).read()) |
50 | 55 |
|
@@ -118,6 +123,9 @@ def test_handle_creates_records(self): |
118 | 123 |
|
119 | 124 | assert appointments[1].assessment is True |
120 | 125 |
|
| 126 | + assert Extract.objects.count() == 1 |
| 127 | + assert Extract.objects.first().appointments.count() == 2 |
| 128 | + |
121 | 129 | def test_handles_holding_clinics(self): |
122 | 130 | """Test does not create appointments for valid NBSS data marked as a Holding Clinic""" |
123 | 131 | today_dirname = datetime.today().strftime("%Y-%m-%d") |
@@ -318,3 +326,44 @@ def test_calls_command_handler( |
318 | 326 | Command().handle(**{"date_str": "2000-01-01"}) |
319 | 327 |
|
320 | 328 | mock_command_handler.assert_called_with("CreateAppointments") |
| 329 | + |
| 330 | + def test_create_extract_and_cancel(self): |
| 331 | + """Test Extract creation for new booked appointments in NBSS data, stored in Azure storage blob""" |
| 332 | + today_dirname = datetime.now().strftime("%Y-%m-%d") |
| 333 | + |
| 334 | + with stored_blob_data(today_dirname, [VALID_DATA_FILE]): |
| 335 | + Command().handle(**{"date_str": today_dirname}) |
| 336 | + |
| 337 | + assert Extract.objects.count() == 1 |
| 338 | + first_extract = Extract.objects.all()[0] |
| 339 | + assert first_extract.appointments.count() == 2 |
| 340 | + assert first_extract.sequence_number == 13 |
| 341 | + assert first_extract.bso_code == "KMK" |
| 342 | + assert first_extract.filename == f"{today_dirname}/{VALID_DATA_FILE}" |
| 343 | + assert first_extract.record_count == 3 |
| 344 | + |
| 345 | + with stored_blob_data(today_dirname, [UPDATED_APPOINTMENT_FILE]): |
| 346 | + Command().handle(**{"date_str": today_dirname}) |
| 347 | + |
| 348 | + assert Extract.objects.count() == 2 |
| 349 | + second_extract = Extract.objects.all()[1] |
| 350 | + |
| 351 | + assert ( |
| 352 | + first_extract.appointments.first().id |
| 353 | + == second_extract.appointments.get().id |
| 354 | + ) |
| 355 | + |
| 356 | + @pytest.mark.django_db(transaction=True) |
| 357 | + def test_errors_when_same_extract(self): |
| 358 | + today_dirname = datetime.now().strftime("%Y-%m-%d") |
| 359 | + |
| 360 | + with stored_blob_data(today_dirname, [VALID_DATA_FILE]): |
| 361 | + Command().handle(**{"date_str": today_dirname}) |
| 362 | + |
| 363 | + assert Appointment.objects.count() == 2 |
| 364 | + |
| 365 | + with stored_blob_data(today_dirname, [VALID_DATA_FILE]): |
| 366 | + with pytest.raises(CommandError): |
| 367 | + Command().handle(**{"date_str": today_dirname}) |
| 368 | + |
| 369 | + assert Appointment.objects.count() == 2 |
0 commit comments