示例#1
0
def test_field_resource_type_constraint():
    """Regarding to issue: #3 """
    fhir_field = FhirField(title="Organization resource",
                           resource_type="Organization",
                           fhir_release="R4")
    with open(str(FHIR_EXAMPLE_RESOURCES / "Patient.json"), "r") as f:
        json_dict = json.load(f)

    try:
        fhir_field.from_dict(json_dict)
    except Invalid as e:
        assert e.__class__.__name__ == "ConstraintNotSatisfied"
        assert "Fhir Resource mismatched" in str(e)
示例#2
0
def test_field_from_dict():
    """ """
    with open(str(FHIR_EXAMPLE_RESOURCES / "Organization.json"), "r") as fp:
        json_dict = json.load(fp)

    fhir_field = FhirField(
        title="Organization resource",
        resource_class="tests.fixtures.MyOrganizationResource",
        fhir_release="R4",
    )

    try:
        fhir_resource_value = fhir_field.from_dict(json_dict)
    except Invalid as exc:
        raise AssertionError(
            "Code should not come here! as "
            f"should return valid FhirResourceValue.\n{exc!s}")

    assert fhir_resource_value.resource_type == json_dict["resourceType"]

    fhir_field = FhirField(title="Organization resource",
                           resource_type="Organization",
                           fhir_release="R4")

    fhir_resource_value = fhir_field.from_dict(json_dict)
    try:
        fhir_resource_value.as_json()
    except Exception:
        raise AssertionError(
            "Code should not come here! as should be valid fhir resource")

    # Test auto discovery resource type
    fhir_field = FhirField(title="Organization resource", fhir_release="R4")
    fhir_resource_value = fhir_field.from_dict(json_dict)
    assert fhir_resource_value.resource_type == json_dict["resourceType"]

    # Test with invalid data type
    try:
        invalid_data = ("hello", "tree", "go")
        fhir_field.from_dict(invalid_data)
    except WrongType as exc:
        assert "Only dict type data is allowed" in str(exc)

    # Test with invalid fhir data
    try:
        invalid_data = dict(hello="fake", foo="bar")
        fhir_field.from_dict(invalid_data)

        raise AssertionError(
            "Code should not come here, because of invalid data")
    except Invalid as exc:
        assert "Invalid FHIR resource" in str(exc)

    # Test constraint
    fhir_field = FhirField(
        title="Organization resource",
        resource_class="tests.fixtures.MyTaskResource",
        fhir_release="R4",
    )

    try:
        fhir_field.from_dict(json_dict)
        raise AssertionError(
            "Code should not come here as required fhir model is "
            "mismatched with provided resourcetype")
    except ConstraintNotSatisfied as exc:
        assert "Fhir Resource mismatched" in str(exc)