|
4 | 4 |
|
5 | 5 | from nrlf.core.constants import ( |
6 | 6 | CATEGORY_ATTRIBUTES, |
| 7 | + CONTENT_RETRIEVAL_CODE_MAP, |
7 | 8 | ODS_SYSTEM, |
8 | 9 | TYPE_ATTRIBUTES, |
9 | 10 | TYPE_CATEGORIES, |
|
16 | 17 | validate_type, |
17 | 18 | ) |
18 | 19 | from nrlf.producer.fhir.r4.model import ( |
| 20 | + ContentStabilityExtension, |
| 21 | + ContentStabilityExtensionCoding, |
| 22 | + ContentStabilityExtensionValueCodeableConcept, |
19 | 23 | DocumentReference, |
20 | 24 | OperationOutcomeIssue, |
21 | 25 | RequestQueryType, |
| 26 | + RetrievalMechanismExtension, |
| 27 | + RetrievalMechanismExtensionCoding, |
| 28 | + RetrievalMechanismExtensionValueCodeableConcept, |
22 | 29 | ) |
23 | 30 | from nrlf.tests.data import load_document_reference_json |
24 | 31 |
|
@@ -1780,3 +1787,127 @@ def test_validate_content_retrieval_lowercase_urls(): |
1780 | 1787 | "diagnostics": "Invalid content retrieval extension (content[0].extension[0].url: Input should be 'https://fhir.nhs.uk/England/StructureDefinition/Extension-England-RetrievalMechanism', see: https://fhir.nhs.uk/England/ValueSet/England-RetrievalMechanism)", |
1781 | 1788 | "expression": ["content[0].extension[0].url"], |
1782 | 1789 | } |
| 1790 | + |
| 1791 | + |
| 1792 | +def make_content_stability_extension(code, display): |
| 1793 | + return ContentStabilityExtension( |
| 1794 | + url="https://fhir.nhs.uk/England/StructureDefinition/Extension-England-ContentStability", |
| 1795 | + valueCodeableConcept=ContentStabilityExtensionValueCodeableConcept( |
| 1796 | + coding=[ |
| 1797 | + ContentStabilityExtensionCoding( |
| 1798 | + system="https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability", |
| 1799 | + code=code, |
| 1800 | + display=display, |
| 1801 | + ) |
| 1802 | + ] |
| 1803 | + ), |
| 1804 | + ) |
| 1805 | + |
| 1806 | + |
| 1807 | +def make_retrieval_mechanism_extension(code, display): |
| 1808 | + return RetrievalMechanismExtension( |
| 1809 | + url="https://fhir.nhs.uk/England/StructureDefinition/Extension-England-RetrievalMechanism", |
| 1810 | + valueCodeableConcept=RetrievalMechanismExtensionValueCodeableConcept( |
| 1811 | + coding=[ |
| 1812 | + RetrievalMechanismExtensionCoding( |
| 1813 | + system="https://fhir.nhs.uk/England/CodeSystem/England-RetrievalMechanism", |
| 1814 | + code=code, |
| 1815 | + display=display, |
| 1816 | + ) |
| 1817 | + ] |
| 1818 | + ), |
| 1819 | + ) |
| 1820 | + |
| 1821 | + |
| 1822 | +def test_has_valid_extensions(): |
| 1823 | + validator = DocumentReferenceValidator() |
| 1824 | + extensions = [ |
| 1825 | + make_content_stability_extension("static", "Static"), |
| 1826 | + make_retrieval_mechanism_extension("Direct", "Direct"), |
| 1827 | + ] |
| 1828 | + assert validator._has_valid_extensions(extensions, 0) is True |
| 1829 | + |
| 1830 | + |
| 1831 | +def test_has_valid_extensions_multiple_retrieval_mechanism(): |
| 1832 | + validator = DocumentReferenceValidator() |
| 1833 | + extensions = [ |
| 1834 | + make_content_stability_extension("static", "Static"), |
| 1835 | + make_retrieval_mechanism_extension("Direct", "Direct"), |
| 1836 | + make_retrieval_mechanism_extension("SSP", "Spine Secure Proxy"), |
| 1837 | + ] |
| 1838 | + assert validator._has_valid_extensions(extensions, 0) is False |
| 1839 | + assert any( |
| 1840 | + "Invalid content retrieval extension: Extension must have one content retrieval extension, see: ('https://fhir.nhs.uk/England/ValueSet/England-RetrievalMechanism')" |
| 1841 | + in issue.diagnostics |
| 1842 | + for issue in validator.result.issues |
| 1843 | + ) |
| 1844 | + |
| 1845 | + |
| 1846 | +def test_no_content_extensions(): |
| 1847 | + validator = DocumentReferenceValidator() |
| 1848 | + extensions = [] |
| 1849 | + assert validator._has_valid_extensions(extensions, 0) is False |
| 1850 | + assert any( |
| 1851 | + "Invalid content extension: Extension must have one content stability extension, see: ('https://fhir.nhs.uk/England/ValueSet/England-NRLContentStability')" |
| 1852 | + in issue.diagnostics |
| 1853 | + for issue in validator.result.issues |
| 1854 | + ) |
| 1855 | + |
| 1856 | + |
| 1857 | +@pytest.mark.parametrize( |
| 1858 | + "code, display", [("static", "Static"), ("dynamic", "Dynamic")] |
| 1859 | +) |
| 1860 | +def test_validate_content_stability_extension_valid(code, display): |
| 1861 | + validator = DocumentReferenceValidator() |
| 1862 | + ext = make_content_stability_extension("static", "Static") |
| 1863 | + assert validator._validate_content_stability_extension(ext, 0, 0) is True |
| 1864 | + assert validator.result.issues == [] |
| 1865 | + |
| 1866 | + |
| 1867 | +@pytest.mark.parametrize( |
| 1868 | + "code, display", [("dynamic", "Static"), ("static", "Dynamic")] |
| 1869 | +) |
| 1870 | +def test_validate_content_stability_extension_display_mismatch(code, display): |
| 1871 | + validator = DocumentReferenceValidator() |
| 1872 | + ext = make_content_stability_extension(code, display) |
| 1873 | + assert validator._validate_content_stability_extension(ext, 0, 0) is False |
| 1874 | + assert any( |
| 1875 | + f"Invalid content extension display: {display} Extension display must be the same as code either 'Static' or 'Dynamic'" |
| 1876 | + in issue.diagnostics |
| 1877 | + for issue in validator.result.issues |
| 1878 | + ) |
| 1879 | + |
| 1880 | + |
| 1881 | +@pytest.mark.parametrize( |
| 1882 | + "code, display", |
| 1883 | + [ |
| 1884 | + ("SSP", "Spine Secure Proxy"), |
| 1885 | + ("Direct", "Direct"), |
| 1886 | + ("LDR", "Large Document Retrieval"), |
| 1887 | + ], |
| 1888 | +) |
| 1889 | +def test_validate_retrieval_mechanism_extension_valid(code, display): |
| 1890 | + validator = DocumentReferenceValidator() |
| 1891 | + ext = make_retrieval_mechanism_extension(code, display) |
| 1892 | + assert validator._validate_retrieval_mechanism_extension(ext, 0, 0) is True |
| 1893 | + assert validator.result.issues == [] |
| 1894 | + |
| 1895 | + |
| 1896 | +@pytest.mark.parametrize( |
| 1897 | + "code, display", |
| 1898 | + [ |
| 1899 | + (code, wrong_display) |
| 1900 | + for code, correct_display in CONTENT_RETRIEVAL_CODE_MAP.items() |
| 1901 | + for wrong_display in CONTENT_RETRIEVAL_CODE_MAP.values() |
| 1902 | + if wrong_display != correct_display |
| 1903 | + ], |
| 1904 | +) |
| 1905 | +def test_validate_retrieval_mechanism_extension_display_mismatch(code, display): |
| 1906 | + validator = DocumentReferenceValidator() |
| 1907 | + ext = make_retrieval_mechanism_extension(code, display) |
| 1908 | + assert validator._validate_retrieval_mechanism_extension(ext, 0, 0) is False |
| 1909 | + assert any( |
| 1910 | + f"Invalid content extension display: {display} Expected display is '{CONTENT_RETRIEVAL_CODE_MAP.get(code)}'" |
| 1911 | + in issue.diagnostics |
| 1912 | + for issue in validator.result.issues |
| 1913 | + ) |
0 commit comments