Exemple #1
0
 def test_patch_resource(self, store: FHIRStore, test_patient):
     """patch() finds a document in the database"""
     store.create(test_patient)
     store.patch("Patient", test_patient["id"], {"gender": "other"})
     assert store.read("Patient", test_patient["id"]) == {
         **test_patient, "gender": "other"
     }
Exemple #2
0
    def test_update_bad_resource_schema(self, store: FHIRStore, test_patient):
        """update() raises if json schema validation failed in mongo"""

        store.create(test_patient)
        with raises(ValidationError):
            store.update("Patient", test_patient["id"], {
                **test_patient, "gender": "elephant"
            })
Exemple #3
0
    def test_delete_missing_param(self, store: FHIRStore, test_patient):
        """delete() returns None when no matching document was found"""

        with raises(
                BadRequestError,
                match=
                "one of: 'instance_id', 'resource_id' or 'source_id' are required",
        ):
            store.delete("Patient")
Exemple #4
0
def store():
    client = MongoClient(username=MONGO_USERNAME, password=MONGO_PASSWORD)
    try:
        client.server_info()
    except ServerSelectionTimeoutError as err:
        print("MongoClient could not reach server, is it running ?")
        raise
    client_es = Elasticsearch([ES_URL], http_auth=("elastic", ES_PASSWORD))

    fhirstore = FHIRStore(client, client_es, DB_NAME)
    fhirstore.reset()
    fhirstore.bootstrap(depth=2, resource="Patient")
    fhirstore.bootstrap(depth=2, resource="Practitioner")
    fhirstore.bootstrap(depth=2, resource="MedicationRequest")

    return fhirstore
Exemple #5
0
 def test_create_resource(self, store: FHIRStore, mongo_client: MongoClient,
                          test_patient):
     """create() correctly inserts a document in the database"""
     result = store.create(test_patient)
     assert isinstance(result["_id"],
                       ObjectId), "result _id must be an objectId"
     inserted = mongo_client["Patient"].find_one({"_id": result["_id"]})
     assert inserted == test_patient
def test_search_identifier(store: FHIRStore):
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("managingOrganization:identifier", "98765")])
    )
    assert (
        result.content["entry"][0]["resource"]["managingOrganization"]["identifier"][0]["value"]
        == "98765"
    )
Exemple #7
0
    def test_delete_by_source_id(self, store: FHIRStore, test_patient):
        """delete() finds a document in the database"""
        store.create(test_patient)

        source_id = "pyrogSourceId"
        metadata = {
            "tag": [
                {
                    "system": ARKHN_CODE_SYSTEMS.source,
                    "code": source_id
                },
                {
                    "code": "some-other-tag"
                },
            ]
        }
        store.create({
            "resourceType": "Patient",
            "id": "pat2",
            "meta": metadata
        })
        store.create({
            "resourceType": "Patient",
            "id": "pat3",
            "meta": metadata
        })

        result = store.delete("Patient", source_id=source_id)
        assert result == 2
def test_search_one_param_modifier_str_not(store: FHIRStore):
    """Checks that "not" string modifier works
    """
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("name.family:not", "Donald")])
    )
    assert all(
        element["resource"]["name"][0]["family"] != "Donald" for element in result.content["entry"]
    )
def test_search_two_elements(store: FHIRStore):
    result = store.comprehensive_search("Patient", ImmutableMultiDict([("_elements", "birthDate")]))
    assert result.content["total"] == 3
    assert result.content["tag"]["code"] == "SUBSETTED"
    assert result.content["entry"] == [
        {"resource": {}, "search": {"mode": "match"}},
        {"resource": {"birthDate": "1932-09-24"}, "search": {"mode": "match"}},
        {"resource": {"birthDate": "1974-12-25"}, "search": {"mode": "match"}},
    ]
Exemple #10
0
def test_search_one_param_simple(store: FHIRStore):
    """Checks simple one parameter search
    """
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("identifier.value", "654321")])
    )
    assert len(result.content["entry"]) == 1
    assert all(
        element["resource"]["identifier"][0]["value"] == "654321"
        for element in result.content["entry"]
    )
Exemple #11
0
def test_search_summary_text(store: FHIRStore):
    result = store.comprehensive_search("Patient", ImmutableMultiDict([("_summary", "text")]))
    assert result.content["total"] == 3
    assert result.content["tag"]["code"] == "SUBSETTED"
    assert result.content["entry"] == [
        {
            "resource": {
                "meta": {
                    "tag": [
                        {
                            "system": "http://terminology.hl7.org/CodeSystem/v3-ActReason",
                            "code": "HTEST",
                            "display": "test health data",
                        }
                    ]
                },
                "id": "pat1",
                "text": {
                    "div": '<div xmlns="http://www.w3.org/1999/xhtml">\n      \n      <p>Patient Donald DUCK @ Acme Healthcare, Inc. MR = 654321</p>\n    \n    </div>',
                    "status": "generated",
                },
            },
            "search": {"mode": "match"},
        },
        {
            "resource": {
                "id": "xcda",
                "text": {
                    "div": '<div xmlns="http://www.w3.org/1999/xhtml">\n      \n      <p>Henry Levin the 7th</p>\n    \n    </div>',
                    "status": "generated",
                },
            },
            "search": {"mode": "match"},
        },
        {
            "resource": {
                "meta": {
                    "tag": [
                        {
                            "system": "http://terminology.hl7.org/CodeSystem/v3-ActReason",
                            "code": "HTEST",
                            "display": "test health data",
                        }
                    ]
                },
                "id": "example",
                "text": {
                    "div": '<div xmlns="http://www.w3.org/1999/xhtml">\n\t\t\t<table>\n\t\t\t\t<tbody>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Name</td>\n\t\t\t\t\t\t<td>Peter James \n              <b>Chalmers</b> (&quot;Jim&quot;)\n            </td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Address</td>\n\t\t\t\t\t\t<td>534 Erewhon, Pleasantville, Vic, 3999</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Contacts</td>\n\t\t\t\t\t\t<td>Home: unknown. Work: (03) 5555 6473</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Id</td>\n\t\t\t\t\t\t<td>MRN: 12345 (Acme Healthcare)</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</tbody>\n\t\t\t</table>\n\t\t</div>',
                    "status": "generated",
                },
            },
            "search": {"mode": "match"},
        },
    ]
Exemple #12
0
 def test_create_resource_with_extension(self, store: FHIRStore,
                                         mongo_client: MongoClient):
     """resources using extensions are not
     handled yet, an error should be raised"""
     with open("test/fixtures/patient-example-with-extensions.json") as f:
         patient = json.load(f)
         result = store.create(patient)
         assert isinstance(result["_id"],
                           ObjectId), "result _id must be an objectId"
         inserted = mongo_client["Patient"].find_one({"_id": result["_id"]})
         assert inserted == patient
         mongo_client["Patient"].delete_one({"_id": patient["_id"]})
Exemple #13
0
def test_search_one_param_modifier_str_exact(store: FHIRStore):
    """Checks that "exact" string modifier works
    """
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("name.family:exact", "Donald")])
    )
    assert len(result.content["entry"]) == 1
    assert all(
        element["resource"]["name"][0]["family"] == "Donald" for element in result.content["entry"]
    )
    assert all(
        element["resource"]["name"][0]["family"] != ("Chalmers" or "Levin")
        for element in result.content["entry"]
    )
Exemple #14
0
def test_search_one_param_multiple(store: FHIRStore):
    """Checks that multiple elements of one parameter are queried
    """
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("name.family", "Chalmers,Levin")])
    )
    assert len(result.content["entry"]) == 2
    assert any(
        element["resource"]["name"][0]["family"] == ("Chalmers" or "Levin")
        for element in result.content["entry"]
    )
    assert all(
        element["resource"]["name"][0]["family"] != "Donald" for element in result.content["entry"]
    )
Exemple #15
0
def test_handle_pipe(store: FHIRStore):
    result = store.comprehensive_search(
        "MedicationRequest",
        ImmutableMultiDict([("contained.code.coding", "http://snomed.info/sct|324252006")]),
    )
    assert result.content["entry"][0]["resource"]["id"] == "medrx0302"
    assert (
        result.content["entry"][0]["resource"]["contained"][0]["code"]["coding"][0]["system"]
        == "http://snomed.info/sct"
    )
    assert (
        result.content["entry"][0]["resource"]["contained"][0]["code"]["coding"][0]["code"]
        == "324252006"
    )
Exemple #16
0
def test_search_one_param_modifier_str_contains(store: FHIRStore):
    """Checks that "contains" string modifier works
    """
    result = store.comprehensive_search(
        "Patient",
        ImmutableMultiDict([("managingOrganization.reference:contains", "Organization")]),
    )
    assert len(result.content["entry"]) == 3
    assert any(
        element["resource"]["managingOrganization"]["reference"] == "Organization/1"
        for element in result.content["entry"]
    )
    assert any(
        element["resource"]["managingOrganization"]["reference"]
        != "Organization/2.16.840.1.113883.19.5"
        for element in result.content["entry"]
    )
Exemple #17
0
def test_search_one_params_and(store: FHIRStore):
    """Checks one parameter "and" search
    """
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("name.given", "Peter,James")])
    )
    assert len(result.content["entry"]) == 1
    assert any(
        element["resource"]["name"][0]["given"] == "Peter" or "James"
        for element in result.content["entry"]
    )
    assert all(
        element["resource"]["name"][0]["given"] != "Henry" for element in result.content["entry"]
    )
    assert all(
        element["resource"]["name"][0]["given"] != "Duck" for element in result.content["entry"]
    )
Exemple #18
0
def test_search_and_or(store: FHIRStore):
    """Checks two parameter "and,or" search
    """
    result = store.comprehensive_search(
        "Patient",
        ImmutableMultiDict([("name.family", "Levin,Chalmers"), ("identifier.value", "12345")]),
    )
    assert len(result.content["entry"]) == 2
    assert all(
        element["resource"]["identifier"][0]["value"] != "654321"
        for element in result.content["entry"]
    )
    assert all(
        element["resource"]["identifier"][0]["value"] == "12345"
        for element in result.content["entry"]
    )
    assert all(
        element["resource"]["name"][0]["family"] == "Levin" or "Chalmers"
        for element in result.content["entry"]
    )
    assert all(
        element["resource"]["name"][0]["family"] != "Donald" for element in result.content["entry"]
    )
Exemple #19
0
def test_search_no_parameters(store: FHIRStore):
    """Checks that all elements of the resource are returned
    """
    result = store.comprehensive_search("Patient", ImmutableMultiDict([]))
    assert len(result.content["entry"]) == 3
Exemple #20
0
def test_search_medicationrequest(store: FHIRStore, insert_medicationrequest):
    """Check that medicationrequest was inserted properly
    """
    result = store.comprehensive_search("MedicationRequest", {})
    assert result.content["total"] == 2
Exemple #21
0
def test_search_output_type(store: FHIRStore, insert_patient):
    """Check that the output type is correct
    """
    result = store.comprehensive_search("Patient", ImmutableMultiDict([]))
    assert result.content["resource_type"] == "Bundle"
Exemple #22
0
def test_includes(store: FHIRStore):
    result = store.comprehensive_search(
        "MedicationRequest", ImmutableMultiDict([("_include", "MedicationRequest:subject")]),
    )
    assert len(result.content["entry"]) == 4
Exemple #23
0
def test_sort_desc(store: FHIRStore):
    result = store.comprehensive_search("Patient", ImmutableMultiDict([("_sort", "-birthDate")]))
    assert (
        result.content["entry"][0]["resource"]["birthDate"]
        >= result.content["entry"][1]["resource"]["birthDate"]
    )
Exemple #24
0
    def test_unique_indices(self, store: FHIRStore, mongo_client: MongoClient):
        """create() raises if index is already present"""
        store.create({
            "identifier": [
                {
                    "value": "val",
                    "system": "sys"
                },
                {
                    "value": "just_value"
                },
                {
                    "value": "value",
                    "system": "system",
                    "type": {
                        "coding": [{
                            "system": "type_system",
                            "code": "type_code"
                        }]
                    },
                },
            ],
            "resourceType":
            "Patient",
            "id":
            "pat1",
        })

        # index on id
        with raises(DuplicateKeyError, match='dup key: { id: "pat1" }'):
            store.create({"resourceType": "Patient", "id": "pat1"})

        # index on (identifier.value, identifier.system)
        with raises(
                DuplicateKeyError,
                match=
                'dup key: { identifier.system: "sys", identifier.value: "val", \
identifier.type.coding.system: null, identifier.type.coding.code: null }',
        ):
            store.create({
                "identifier": [{
                    "value": "val",
                    "system": "sys"
                }],
                "resourceType": "Patient",
                "id": "pat2",
            })
        with raises(
                DuplicateKeyError,
                match=
                'dup key: { identifier.system: null, identifier.value: "just_value", \
identifier.type.coding.system: null, identifier.type.coding.code: null }',
        ):
            store.create({
                "identifier": [{
                    "value": "just_value"
                }],
                "resourceType": "Patient",
                "id": "pat2",
            })
        with raises(
                DuplicateKeyError,
                match=
                'dup key: { identifier.system: "system", identifier.value: "value", \
identifier.type.coding.system: "type_system", identifier.type.coding.code: "type_code" }',
        ):
            store.create({
                "identifier": [
                    {
                        "value": "new_val"
                    },
                    {
                        "value": "value",
                        "system": "system",
                        "type": {
                            "coding": [{
                                "system": "type_system",
                                "code": "type_code"
                            }]
                        },
                    },
                ],
                "resourceType":
                "Patient",
                "id":
                "pat2",
            })
def test_search_bad_resource_type(store: FHIRStore):
    """search() raises error if resource type is unknown"""

    with raises(NotFoundError, match='unsupported FHIR resource: "unknown"'):
        store.comprehensive_search(resource_type="unknown", args={})
Exemple #26
0
        t = tqdm(total=total_size,
                 unit='B',
                 unit_scale=True,
                 desc="Downloading example resources")
        with open('benchmark/examples.zip', 'wb') as f:
            for data in r.iter_content(block_size):
                t.update(len(data))
                f.write(data)
        t.close()
    else:
        print("Using cached resources")


download_resources()
client = MongoClient()
store = FHIRStore(client, "benchmark")
store.resume()
# store.reset()
# store.bootstrap(depth=3)

examples = tqdm(iter_examples(),
                total=count_examples(),
                desc="Running write benchmark")
stats = {}
inserted = []
for example, data in examples:
    if not data.get('id'):
        data["id"] = str(uuid4())
    start = timer()
    res = store.create(data)
    end = timer()
Exemple #27
0
def test_count_medicationrequest(store: FHIRStore):
    result = store.comprehensive_search(
        "MedicationRequest", ImmutableMultiDict([("_summary", "count")])
    )
    assert result.content["total"] == 2
Exemple #28
0
def test_count_some(store: FHIRStore):
    result = store.comprehensive_search(
        "Patient", ImmutableMultiDict([("_summary", "count"), ("identifier.value", "ne12345")]),
    )
    assert result.content["total"] == 1
    assert result.content["tag"]["code"] == "SUBSETTED"
Exemple #29
0
def test_count_all(store: FHIRStore):
    result = store.comprehensive_search("Patient", ImmutableMultiDict([("_summary", "count")]))
    assert result.content["total"] == 3
    assert result.content["tag"]["code"] == "SUBSETTED"
Exemple #30
0
    def test_create_bad_resource_schema(self, store: FHIRStore):
        """create() raises if json schema validation failed in mongo"""

        with raises(ValidationError):
            store.create({"resourceType": "Patient", "id": 42})