|
1 | 1 | import json |
2 | | -from unittest.mock import patch |
| 2 | +from unittest.mock import Mock, patch |
3 | 3 |
|
4 | 4 | from freeze_uuid import freeze_uuid |
5 | 5 | from freezegun import freeze_time |
@@ -1734,3 +1734,144 @@ def test__set_create_time_fields_when_no_date_but_perms(): |
1734 | 1734 | }, |
1735 | 1735 | "date": test_time, |
1736 | 1736 | } |
| 1737 | + |
| 1738 | + |
| 1739 | +@mock_aws |
| 1740 | +@mock_repository |
| 1741 | +@freeze_uuid("00000000-0000-0000-0000-000000000001") |
| 1742 | +@patch("api.producer.createDocumentReference.create_document_reference.logger") |
| 1743 | +def test_create_logs_for_unexpected_multi_pointer( |
| 1744 | + mock_logger: Mock, |
| 1745 | + repository: DocumentPointerRepository, |
| 1746 | +): |
| 1747 | + doc_ref = load_document_reference("Y05868-736253002-Valid-with-master-id") |
| 1748 | + doc_pointer = DocumentPointer.from_document_reference(doc_ref) |
| 1749 | + repository.create(doc_pointer) |
| 1750 | + |
| 1751 | + event = create_test_api_gateway_event( |
| 1752 | + headers=create_headers(), |
| 1753 | + body=doc_ref.model_dump_json(exclude_none=True), |
| 1754 | + ) |
| 1755 | + |
| 1756 | + result = handler(event, create_mock_context()) |
| 1757 | + body = result.pop("body") |
| 1758 | + |
| 1759 | + assert result == { |
| 1760 | + "statusCode": "201", |
| 1761 | + "headers": { |
| 1762 | + "Location": "/DocumentReference/Y05868-00000000-0000-0000-0000-000000000001", |
| 1763 | + **default_response_headers(), |
| 1764 | + }, |
| 1765 | + "isBase64Encoded": False, |
| 1766 | + } |
| 1767 | + |
| 1768 | + parsed_body = json.loads(body) |
| 1769 | + |
| 1770 | + assert parsed_body == { |
| 1771 | + "resourceType": "OperationOutcome", |
| 1772 | + "issue": [ |
| 1773 | + { |
| 1774 | + "severity": "information", |
| 1775 | + "code": "informational", |
| 1776 | + "details": { |
| 1777 | + "coding": [ |
| 1778 | + { |
| 1779 | + "code": "RESOURCE_CREATED", |
| 1780 | + "display": "Resource created", |
| 1781 | + "system": "https://fhir.nhs.uk/ValueSet/NRL-ResponseCode", |
| 1782 | + } |
| 1783 | + ] |
| 1784 | + }, |
| 1785 | + "diagnostics": "The document has been created", |
| 1786 | + } |
| 1787 | + ], |
| 1788 | + } |
| 1789 | + |
| 1790 | + assert any( |
| 1791 | + call[0][0].name == "PROCREATE012" for call in mock_logger.log.call_args_list |
| 1792 | + ) |
| 1793 | + |
| 1794 | + assert { |
| 1795 | + "existing_pointers_count": 1, |
| 1796 | + "nhs_number": ( |
| 1797 | + doc_ref.subject.identifier.value |
| 1798 | + if doc_ref.subject and doc_ref.subject.identifier |
| 1799 | + else None |
| 1800 | + ), |
| 1801 | + "pointer_type": ( |
| 1802 | + f"{doc_ref.type.coding[0].system}|{doc_ref.type.coding[0].code}" |
| 1803 | + if doc_ref.type and doc_ref.type.coding |
| 1804 | + else None |
| 1805 | + ), |
| 1806 | + "custodian": ( |
| 1807 | + doc_ref.custodian.identifier.value |
| 1808 | + if doc_ref.custodian and doc_ref.custodian.identifier |
| 1809 | + else None |
| 1810 | + ), |
| 1811 | + "new_pointer_id": "Y05868-00000000-0000-0000-0000-000000000001", |
| 1812 | + "new_pointer_master_id": ( |
| 1813 | + doc_ref.masterIdentifier.value if doc_ref.masterIdentifier else None |
| 1814 | + ), |
| 1815 | + } == [ |
| 1816 | + call[1:][0] |
| 1817 | + for call in mock_logger.log.call_args_list |
| 1818 | + if call[0][0].name == "PROCREATE012" |
| 1819 | + ][ |
| 1820 | + 0 |
| 1821 | + ] |
| 1822 | + |
| 1823 | + |
| 1824 | +@mock_aws |
| 1825 | +@mock_repository |
| 1826 | +@freeze_uuid("00000000-0000-0000-0000-000000000001") |
| 1827 | +@patch("api.producer.createDocumentReference.create_document_reference.logger") |
| 1828 | +def test_create_logs_for_expected_multi_pointer( |
| 1829 | + mock_logger: Mock, |
| 1830 | + repository: DocumentPointerRepository, |
| 1831 | +): |
| 1832 | + doc_ref = load_document_reference("Y05868-Appointment-Valid") |
| 1833 | + doc_pointer = DocumentPointer.from_document_reference(doc_ref) |
| 1834 | + repository.create(doc_pointer) |
| 1835 | + |
| 1836 | + event = create_test_api_gateway_event( |
| 1837 | + headers=create_headers(), |
| 1838 | + body=doc_ref.model_dump_json(exclude_none=True), |
| 1839 | + ) |
| 1840 | + |
| 1841 | + result = handler(event, create_mock_context()) |
| 1842 | + body = result.pop("body") |
| 1843 | + |
| 1844 | + assert result == { |
| 1845 | + "statusCode": "201", |
| 1846 | + "headers": { |
| 1847 | + "Location": "/DocumentReference/Y05868-00000000-0000-0000-0000-000000000001", |
| 1848 | + **default_response_headers(), |
| 1849 | + }, |
| 1850 | + "isBase64Encoded": False, |
| 1851 | + } |
| 1852 | + |
| 1853 | + parsed_body = json.loads(body) |
| 1854 | + |
| 1855 | + assert parsed_body == { |
| 1856 | + "resourceType": "OperationOutcome", |
| 1857 | + "issue": [ |
| 1858 | + { |
| 1859 | + "severity": "information", |
| 1860 | + "code": "informational", |
| 1861 | + "details": { |
| 1862 | + "coding": [ |
| 1863 | + { |
| 1864 | + "code": "RESOURCE_CREATED", |
| 1865 | + "display": "Resource created", |
| 1866 | + "system": "https://fhir.nhs.uk/ValueSet/NRL-ResponseCode", |
| 1867 | + } |
| 1868 | + ] |
| 1869 | + }, |
| 1870 | + "diagnostics": "The document has been created", |
| 1871 | + } |
| 1872 | + ], |
| 1873 | + } |
| 1874 | + |
| 1875 | + assert not any( |
| 1876 | + call[0][0].name == "PROCREATE012" for call in mock_logger.log.call_args_list |
| 1877 | + ) |
0 commit comments