示例#1
0
def test_field_default_value():
    """ """
    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",
        default=json_dict,
        fhir_release="R4",
    )
    assert json_dict == fhir_field.default.as_json()

    fhir_field2 = FhirField(
        title="Organization resource",
        resource_class="tests.fixtures.MyOrganizationResource",
        default=json.dumps(json_dict),
        fhir_release="R4",
    )

    assert fhir_field2.default.as_json() == fhir_field.default.as_json()

    fhir_field3 = FhirField(
        title="Organization resource",
        resource_class="tests.fixtures.MyOrganizationResource",
        default=None,
        fhir_release="R4",
    )
    assert fhir_field3.default is None
示例#2
0
def test_field_from_unicode_with_empty_str():
    """ """
    fhir_field = FhirField(
        title="Organization resource",
        resource_class="fhir.resources.organization.Organization",
        required=False,
        fhir_release="R4",
    )

    value = fhir_field.from_unicode("")
    assert value is None
示例#3
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)
示例#4
0
async def test_fhir_field_deserializer(dummy_request):
    """ """
    with open(str(FHIR_EXAMPLE_RESOURCES / "Organization.json"), "r") as fp:
        fhir_json = json.load(fp)

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

    deserialized = query_adapter(fhir_field,
                                 IJSONToValue,
                                 args=[fhir_json, None])
    assert IFhirFieldValue.providedBy(deserialized) is True
    assert deserialized.as_json() == fhir_json

    deserialized = query_adapter(fhir_field,
                                 IJSONToValue,
                                 args=[json.dumps(fhir_json), None])
    assert IFhirFieldValue.providedBy(deserialized) is True

    deserialized = query_adapter(fhir_field, IJSONToValue, args=[None, None])
    assert deserialized is None
示例#5
0
def test_field_with_wrong_default_value():
    """ """
    try:
        FhirField(
            title="Organization resource",
            resource_class="tests.fixtures.MyOrganizationResource",
            default=False,
            fhir_release="R4",
        )
    except Invalid as exc:
        assert "Only dict or string or None is accepted" in str(exc)
    try:
        FhirField(
            title="Organization resource",
            resource_class="tests.fixtures.MyOrganizationResource",
            default={"id": "I am wrong FHIR"},
            fhir_release="R4",
        )
    except Invalid as exc:
        assert "Invalid FHIR resource json is provided!" in str(exc)
示例#6
0
async def test_fhir_field_schema_serializer(dummy_request):
    """ """
    fhir_field = FhirField(title="Organization resource",
                           resource_type="Organization",
                           fhir_release="R4")

    serializer = get_multi_adapter((fhir_field, IOrganization, dummy_request),
                                   ISchemaFieldSerializeToJson)

    schema_json = await serializer()
    assert schema_json["type"] == "FhirField"
    assert schema_json["resource_type"] == "Organization"
示例#7
0
def test_field_from_unicode():
    """ """
    with open(str(FHIR_EXAMPLE_RESOURCES / "Organization.json"), "r") as fp:
        json_str = fp.read()

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

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

    # Test with invalid json string
    try:
        invalid_data = "{hekk: invalg, 2:3}"
        fhir_field.from_unicode(invalid_data)
        raise AssertionError(
            "Code should not come here! invalid json string is provided")
    except Invalid as exc:
        assert "Invalid JSON String" in str(exc)
示例#8
0
def test_field_pre_value_validate():
    """ """
    with open(str(FHIR_EXAMPLE_RESOURCES / "Organization.json"), "r") as fp:
        json_str = fp.read()

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

    try:
        fhir_field._pre_value_validate(json_str)
    except Invalid as e:
        raise AssertionError("Code should not come here!\n{0!s}".format(e))

    fhir_dict = json.loads(json_str)
    resource_type = fhir_dict.pop("resourceType")

    try:
        fhir_field._pre_value_validate(fhir_dict)
        raise AssertionError(
            "Code should not come here! As `resourceType` is not exists.")
    except Invalid:
        pass

    fhir_dict.pop("id")
    fhir_dict["resourceType"] = resource_type
    try:
        fhir_field._pre_value_validate(fhir_dict)
        raise AssertionError(
            "Code should not come here! As `id` is not exists.")
    except Invalid:
        pass

    fhir_dict.pop("resourceType")
    try:
        fhir_field._pre_value_validate(fhir_dict)
        raise AssertionError(
            "Code should not come here! As both `id` and `resourceType` are not exists."
        )
    except Invalid:
        pass
示例#9
0
class IObservation(IFhirContent, IContentIndex):

    index_field(
        "observation_resource",
        type="object",
        field_mapping=fhir_resource_mapping("Observation"),
        fhirpath_enabled=True,
        resource_type="Observation",
        fhir_release=FHIR_VERSION.R4,
    )
    observation_resource = FhirField(title="Observation Resource",
                                     resource_type="Observation",
                                     fhir_release="R4")
示例#10
0
class IEncounter(IFhirContent, IContentIndex):

    index_field(
        "encounter_resource",
        type="object",
        field_mapping=fhir_resource_mapping("Encounter"),
        fhirpath_enabled=True,
        resource_type="Encounter",
        fhir_release=FHIR_VERSION.R4,
    )
    encounter_resource = FhirField(title="Encounter Resource",
                                   resource_type="Encounter",
                                   fhir_release="R4")
示例#11
0
def test_field_init_validation_with_noninterface():
    """ """
    # Wrong interface class
    try:
        FhirField(
            title="Organization resource",
            resource_interface="tests.fixtures.NoneInterfaceClass",
            fhir_release="R4",
        )
        raise AssertionError(
            "Code should not come here! as wrong interface class is provided")
    except WrongType as exc:
        assert "An interface is required" in str(exc)
示例#12
0
def test_field_init_validation_with_wrong_dotted_path():
    """ """
    # Wrong module path
    try:
        FhirField(
            title="Organization resource",
            resource_interface="fake.tests.NoneInterfaceClass",
            fhir_release="R4",
        )
        raise AssertionError(
            "Code should not come here! as wrong interface class is provided")
    except Invalid as exc:
        assert "Invalid FHIR Resource Interface" in str(exc)
示例#13
0
class ITask(IFhirContent, IContentIndex):

    index_field(
        "task_resource",
        type="object",
        field_mapping=fhir_resource_mapping("Task"),
        fhirpath_enabled=True,
        resource_type="Task",
        fhir_release=FHIR_VERSION.R4,
    )

    task_resource = FhirField(title="Task Item Resource",
                              resource_type="Task",
                              fhir_release="R4")
示例#14
0
class IChargeItem(IFhirContent, IContentIndex):

    index_field(
        "chargeitem_resource",
        type="object",
        field_mapping=fhir_resource_mapping("ChargeItem"),
        fhirpath_enabled=True,
        resource_type="ChargeItem",
        fhir_release=FHIR_VERSION.R4,
    )

    chargeitem_resource = FhirField(title="Charge Item Resource",
                                    resource_type="ChargeItem",
                                    fhir_release="R4")
示例#15
0
class IPatient(IFhirContent, IContentIndex):

    index_field(
        "patient_resource",
        type="object",
        field_mapping=fhir_resource_mapping("Patient"),
        fhirpath_enabled=True,
        resource_type="Patient",
        fhir_release=FHIR_VERSION.R4,
    )
    index_field("p_type", type="keyword")
    p_type = TextLine(title="Patient Type", required=False)
    patient_resource = FhirField(title="Patient Resource",
                                 resource_type="Patient",
                                 fhir_release="R4")
示例#16
0
class IOrganization(IFhirContent, IContentIndex):

    index_field(
        "organization_resource",
        type="object",
        field_mapping=fhir_resource_mapping("Organization"),
        fhirpath_enabled=True,
        resource_type="Organization",
        fhir_release=FHIR_VERSION.R4,
    )
    index_field("org_type", type="keyword")
    org_type = TextLine(title="Organization Type", required=False)
    organization_resource = FhirField(title="Organization Resource",
                                      resource_type="Organization",
                                      fhir_release="R4")
示例#17
0
class IMedicationRequest(IFhirContent, IContentIndex):

    index_field(
        "medicationrequest_resource",
        type="object",
        field_mapping=fhir_resource_mapping("MedicationRequest"),
        fhirpath_enabled=True,
        resource_type="MedicationRequest",
        fhir_release=FHIR_VERSION.R4,
    )

    medicationrequest_resource = FhirField(
        title="Medication Request Resource",
        resource_type="MedicationRequest",
        fhir_release="R4",
    )
示例#18
0
def test_field_validate():
    """ """
    with open(str(FHIR_EXAMPLE_RESOURCES / "Organization.json"), "r") as f:
        json_dict = json.load(f)

    organization = implementer(IFhirResource)(
        lookup_fhir_class("Organization"))(json_dict)
    fhir_resource_value = FhirFieldValue(obj=organization)

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

    try:
        fhir_field._validate(fhir_resource_value)
    except Invalid as exc:
        raise AssertionError("Code should not come here!\n{0!s}".format(exc))

    # Test wrong type value!
    try:
        fhir_field._validate(dict(hello="wrong"))
        raise AssertionError(
            "Code should not come here! wrong data type is provide")
    except WrongType as exc:
        assert "fhirpath_guillotina.field.FhirFieldValue" in str(exc)

    type_, address_ = fhir_resource_value.type, fhir_resource_value.address
    fhir_resource_value.type = 390
    fhir_resource_value.address = "i am wrong type"

    try:
        fhir_field._validate(fhir_resource_value)
        raise AssertionError(
            "Code should not come here! wrong element data type is provided")
    except Invalid as exc:
        assert "invalid element inside fhir model object" in str(exc)

    # Restore
    fhir_resource_value.type = type_
    fhir_resource_value.address = address_
    # Test model constraint
    fhir_field = FhirField(
        title="Organization resource",
        resource_class="tests.fixtures.MyTaskResource",
        fhir_release="R4",
    )

    try:
        fhir_field._validate(fhir_resource_value)
        raise AssertionError("Code should not come here! model mismatched!")
    except WrongContainedType as exc:
        assert "Wrong fhir resource value" in str(exc)

    # Test resource type constraint!
    fhir_field = FhirField(title="Organization resource",
                           resource_type="Task",
                           fhir_release="R4")

    try:
        fhir_field._validate(fhir_resource_value)
        raise AssertionError("Code should not come here! model mismatched!")
    except ConstraintNotSatisfied as exc:
        assert "Resource type must be `Task`" in str(exc)

    # Wrong interface attributes
    fhir_field = FhirField(
        title="Organization resource",
        resource_interface="tests.fixtures.IWrongInterface",
        fhir_release="R4",
    )

    try:
        fhir_field._validate(fhir_resource_value)
        raise AssertionError(
            "Code should not come here! interface and object mismatched!")
    except Invalid as exc:

        assert "has failed to implement interface" in str(exc)
示例#19
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)
示例#20
0
def test_field_init_validate():  # noqa: C901,E999
    """ """
    # Test with minimal params
    try:
        FhirField(title="Organization resource", fhir_release="R4")
    except Invalid as exc:
        raise AssertionError(
            "Code should not come here, as everything should goes fine.\n{0!s}"
            .format(exc))
    # Test with fhir field specific params
    try:
        FhirField(
            title="Organization resource",
            resource_class="tests.fixtures.MyOrganizationResource",
            resource_interface=(
                "fhirpath_guillotina.interfaces.IFhirResource"),
            fhir_release="R4",
        )
    except Invalid as exc:
        raise AssertionError(
            "Code should not come here, as everything should goes fine.\n{0!s}"
            .format(exc))

    try:
        FhirField(
            title="Organization resource",
            resource_type="Organization",
            fhir_release="R4",
        )
    except Invalid as exc:
        raise AssertionError(
            "Code should not come here, as everything should goes fine.\n{0!s}"
            .format(exc))

    # resource_type and model are not allowed combinely
    try:
        FhirField(
            title="Organization resource",
            resource_type="Organization",
            resource_class="fhir.resources.organization.Organization",
            fhir_release="R4",
        )
        raise AssertionError(
            "Code should not come here! as should be invalid error")
    except Invalid:
        pass

    # test with invalid python style dotted path (fake module)
    try:
        FhirField(
            title="Organization resource",
            resource_class="fake.fake.models.organization.Organization",
            fhir_release="R4",
        )
        raise AssertionError(
            "Code should not come here! as should be invalid error")
    except Invalid:
        pass

    # test with invalid fhir model
    try:
        FhirField(
            title="Organization resource",
            resource_class="guillotina.content.Resource",
            fhir_release="R4",
        )
        raise AssertionError(
            "Code should not come here! as should be invalid error")
    except Invalid as exc:
        assert "has not been derrived from FHIRAbstractBase class" in str(exc)

    # test with invalid ResourceType
    try:
        FhirField(
            title="Organization resource",
            resource_type="FakeResource",
            fhir_release="R4",
        )
        raise AssertionError(
            "Code should not come here! as should be invalid error")
    except Invalid as exc:
        assert "FakeResource is not valid fhir resource type" in str(exc)