Esempio n. 1
0
    def test_composite_type(self):
        """ """
        results = self.load_contents()

        self.admin_browser.open(results[3] + "/++add++Observation")

        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Carbon dioxide in blood"

        with open(os.path.join(FHIR_FIXTURE_PATH, "Observation.json"),
                  "rb") as f:
            fhir_json = json_loads(f.read())

        self.admin_browser.getControl(name="form.widgets.observation_resource"
                                      ).value = json_dumps(fhir_json)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)

        # Let's flush
        self.es.connection.indices.flush()

        context = self.get_context("Observation", True)
        # Test simple composite
        params = (("code-value-quantity", "http://loinc.org|11557-6$6.2"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)
Esempio n. 2
0
    def test_catalogsearch_missing_modifier(self):
        """ """
        results = self.load_contents()
        org1_url = results[0]
        # add another patient
        self.admin_browser.open(org1_url + "/++add++Patient")
        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Patient"

        with open(str(FHIR_FIXTURE_PATH / "Patient.json"), "rb") as f:
            data = json_loads(f.read())
            data["id"] = "20c5245f-89a8-49f8-b244-666b32adb92e"
            data["gender"] = None
            self.admin_browser.getControl(
                name="form.widgets.patient_resource").value = json_dumps(data)

        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()

        # Let's test
        context = self.get_context("Patient", False)

        params = (("gender:missing", "true"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))

        self.assertEqual(1, len(brains))
        self.assertIsNone(brains[0].getObject().patient_resource.gender)

        params = (("gender:missing", "false"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(1, len(brains))
        self.assertIsNotNone(brains[0].getObject().patient_resource.gender)
Esempio n. 3
0
    def test_IN_OR(self):
        """ """
        results = self.load_contents()
        new_id = str(uuid.uuid4())
        new_patient_id = str(uuid.uuid4())
        new_procedure_request_id = str(uuid.uuid4())
        self.admin_browser.open(results[3] + "/++add++Task")

        with open(os.path.join(FHIR_FIXTURE_PATH, "SubTask_HAQ.json"),
                  "rb") as f:
            json_value = json_loads(f.read())
            json_value["id"] = new_id
            json_value["status"] = "completed"
            json_value["for"]["reference"] = "Patient/" + new_patient_id
            json_value["basedOn"][0]["reference"] = ("ProcedureRequest/" +
                                                     new_procedure_request_id)

            self.admin_browser.getControl(name="form.widgets.task_resource"
                                          ).value = json_dumps(json_value)

            self.admin_browser.getControl(name="form.widgets.IBasic.title"
                                          ).value = json_value["description"]

        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)

        # Let's flush
        self.es.connection.indices.flush()

        context = self.get_context("Task", True)
        params = (("status", "ready,draft"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        # should All three tasks
        self.assertEqual(len(brains), 3)

        params = ((
            "patient",
            "Patient/19c5245f-89a8-49f8-b244-666b32adb92e,Patient/" +
            new_patient_id,
        ), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        # should All three tasks + one
        self.assertEqual(len(brains), 4)

        params = ((
            "based-on",
            ("ProcedureRequest/0c57a6c9-c275-4a0a-"
             "bd96-701daf7bd7ce,ProcedureRequest/") + new_procedure_request_id,
        ), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        # should two tasks
        self.assertEqual(len(brains), 2)
Esempio n. 4
0
def _get_mapping(fhir_release: str, resource_type: str):
    """
    :param fhir_release:
    :param resource_type:
    :return:
    """
    filename = (ELASTICSEARCH_STATIC_MAPPINGS / fhir_release /
                f"{resource_type}.mapping.json")
    try:
        with open(str(filename), "rb") as fp:
            data = json_loads(fp.read())
            return data["mapping"]
    except FileNotFoundError:
        raise LookupError(
            f"No mapping has been found for {fhir_release}.{resource_type}")
Esempio n. 5
0
async def test_create_patient(hyperfhir_site, es_setup):
    """ """
    client = hyperfhir_site
    with open(str(FHIR_EXAMPLE_RESOURCES / "Patient.json"), "rb") as fp:
        json_data = json_loads(fp.read())

    res = await client.post(
        "/api/v1/fhir/Patient",
        json=json_data,
        headers={"Accept": "application/json"},
    )
    assert res.status_code == 201
    db = get_db()
    result = await db.fetch_all(
        f"SELECT * FROM {ResourceHistoryModel.__tablename__} ORDER BY id ASC")
    assert len(result) == 1
Esempio n. 6
0
async def setup_elasticsearch(
    release_name: str,
    es_conn: AsyncElasticsearchConnection = None,
    create: bool = False,
):
    """
    :param release_name:
    :param es_conn:
    :param create:
    :return:
    """
    conn = es_conn.raw_connection
    real_index_name = _get_index_name(release_name)
    exist = await conn.indices.exists(real_index_name)
    if create is True:
        if exist:
            # xxx: es_conn.indices.delete_alias(real_index_name, alias_name="")
            await conn.indices.delete(real_index_name)
    else:
        if exist:
            return

    body = {
        "settings": {
            "analysis": {
                "analyzer": {
                    "fhir_reference_analyzer": {
                        "tokenizer": "keyword",
                        "filter": ["fhir_reference_filter"],
                    },
                },
                "filter": {
                    "fhir_reference_filter": {
                        "type": "pattern_capture",
                        "preserve_original": True,
                        "patterns":
                        [r"(?:\w+\/)?(https?\:\/\/.*|[a-zA-Z0-9_-]+)"],
                    },
                },
                "char_filter": {},
                "tokenizer": {},
            },
            "index": {
                "mapping": {
                    "total_fields": {
                        "limit":
                        int(
                            os.environ.get(
                                "ELASTICSEARCH_INDEX_MAPPING_TOTAL_FIELDS",
                                "10000"))
                    },
                    "depth": {
                        "limit":
                        int(
                            os.environ.get("ELASTICSEARCH_INDEX_MAPPING_DEPTH",
                                           "50"))
                    },
                    "nested_fields": {
                        "limit":
                        int(
                            os.environ.get(
                                "ELASTICSEARCH_INDEX_MAPPING_NESTED_FIELDS",
                                "1200"))
                    },
                }
            },
        },
        "mappings": {
            "dynamic": False,
            "properties": {
                "access_scopes": {
                    "index": True,
                    "store": True,
                    "type": "keyword"
                },
                "access_users": {
                    "index": True,
                    "store": True,
                    "type": "keyword"
                },
            },
        },
    }

    for jsonfile in (ELASTICSEARCH_STATIC_MAPPINGS /
                     release_name).glob("*.mapping.json"):

        with open(str(jsonfile), "rb") as fp:
            data = json_loads(fp.read())
            body["mappings"]["properties"][
                data["resourceType"]] = data["mapping"]

    await conn.indices.create(real_index_name, body=body)
    # xxx: create alias
    await conn.indices.refresh(index=real_index_name)
Esempio n. 7
0
async def deserialize_json_request_body(
    request: Request,
) -> typing.Union[typing.Dict[str, typing.Any], typing.List[typing.Any]]:
    """ """
    raw = await request.body()
    return json_loads(raw)
Esempio n. 8
0
    def test_reference_with_below_above(self):
        """ """
        results = self.load_contents()
        context = self.get_context("Task", True)
        # Should Get All Tasks
        params = (("patient:below", "Patient"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 3)

        self.admin_browser.open(results[3] + "/++add++Observation")
        with open(os.path.join(FHIR_FIXTURE_PATH, "Observation.json"),
                  "rb") as f:
            json_value1 = json_loads(f.read())
            self.admin_browser.getControl(
                name="form.widgets.observation_resource").value = json_dumps(
                    json_value1)

            self.admin_browser.getControl(
                name="form.widgets.IBasic.title").value = (
                    json_value1["resourceType"] + json_value1["id"])

        self.admin_browser.getControl(name="form.buttons.save").click()
        with open("output.html", "w") as fp:
            fp.write(self.admin_browser.contents)
        self.assertIn("Item created", self.admin_browser.contents)

        device_id = str(uuid.uuid4())
        self.admin_browser.open(results[3] + "/++add++Observation")
        with open(os.path.join(FHIR_FIXTURE_PATH, "Observation.json"),
                  "rb") as f:
            json_value = json_loads(f.read())
            json_value["id"] = str(uuid.uuid4())
            json_value["subject"] = {"reference": "Device/" + device_id}
            self.admin_browser.getControl(
                name="form.widgets.observation_resource").value = json_dumps(
                    json_value)

            self.admin_browser.getControl(
                name="form.widgets.IBasic.title").value = (
                    json_value["resourceType"] + json_value["id"])

        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        self.es.connection.indices.flush()

        context = self.get_context("Observation", True)
        # Should One
        params = (("subject:below", "Device"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # Little bit complex
        params = (("subject:below", "Device,Patient"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 2)

        # Search By Multiple Ids
        # no need above
        params = (("subject",
                   device_id + "," + json_value1["subject"]["reference"]), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 2)

        params = (("subject", device_id), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)
Esempio n. 9
0
    def test_issue_21_code_and_coding(self):
        """Add Support for IN/OR query for token and other if possible search type"""
        results = self.load_contents()
        with open(os.path.join(FHIR_FIXTURE_PATH, "ChargeItem.json"),
                  "rb") as f:
            fhir_json = json_loads(f.read())

        fhir_json_copy = copy.deepcopy(fhir_json)
        fhir_json_copy["id"] = str(uuid.uuid4())
        fhir_json_copy["code"]["coding"] = [
            {
                "code": "387517004",
                "display": "Paracetamol",
                "system": "http://snomed.info/387517004",
            },
            {
                "code": "91107009",
                "display": "Caffeine",
                "system": "http://snomed.info/91107009",
            },
        ]
        fhir_json_copy["code"]["text"] = "Paracetamol (substance)"

        self.admin_browser.open(results[3] + "/++add++ChargeItem")
        self.admin_browser.getControl(name="form.widgets.IBasic.title"
                                      ).value = "Test Clinical Bill (USD)"
        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json_copy)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)

        fhir_json_copy = copy.deepcopy(fhir_json)
        fhir_json_copy["id"] = str(uuid.uuid4())
        fhir_json_copy["code"]["coding"] = [{
            "code":
            "387137007",
            "display":
            "Omeprazole",
            "system":
            "http://snomed.info/387137007",
        }]
        fhir_json_copy["code"]["text"] = "Omeprazole (substance)"

        self.admin_browser.open(results[3] + "/++add++ChargeItem")
        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill(BDT)"
        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json_copy)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()

        context = self.get_context("ChargeItem", True)

        params = (("code", "387517004,387137007"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 2)

        # Test with system+code with negetive
        params = ((
            "code:not",
            ("http://snomed.info/387517004|FF009,"
             "http://snomed.info/387137007|387137007"),
        ), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)
Esempio n. 10
0
    def test_code_datatype(self):
        """ """
        results = self.load_contents()

        self.admin_browser.open(results[3] + "/++add++ChargeItem")

        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill"

        with open(os.path.join(FHIR_FIXTURE_PATH, "ChargeItem.json"),
                  "rb") as f:
            fhir_json = json_loads(f.read())

        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()

        # Test code (Coding)
        context = self.get_context("ChargeItem", True)

        params = (("code", "F01510"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # Test with system+code
        params = (("code", "http://snomed.info/sct|F01510"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # test with code only
        params = (("code", "|F01510"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # test with system only
        params = (("code", "http://snomed.info/sct|"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # test with text
        params = (("code:text", "Nice Code"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)
        # test with .as(
        self.admin_browser.open(results[3] + "/++add++MedicationRequest")

        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill"

        with open(os.path.join(FHIR_FIXTURE_PATH, "MedicationRequest.json"),
                  "rb") as f:
            fhir_json = json_loads(f.read())

        self.admin_browser.getControl(
            name="form.widgets.medicationrequest_resource").value = json_dumps(
                fhir_json)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()

        # test with only code
        context = self.get_context("MedicationRequest", True)
        params = (("code", "322254008"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # test with system and code
        params = (("code", "http://snomed.info/sct|"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)
Esempio n. 11
0
    def test_number_type_search(self):
        """ """
        results = self.load_contents()

        self.admin_browser.open(results[3] + "/++add++ChargeItem")

        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill"

        with open(os.path.join(FHIR_FIXTURE_PATH, "ChargeItem.json"),
                  "rb") as f:
            fhir_json_charge_item = json_loads(f.read())

        self.admin_browser.getControl(
            name="form.widgets.chargeitem_resource").value = json_dumps(
                fhir_json_charge_item)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)

        # Let's flush
        self.es.connection.indices.flush()
        # Test so normal
        context = self.get_context("ChargeItem", True)

        # Test normal float value order
        params = (("factor-override", "0.8"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        params = (("factor-override", "gt0.79"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # Test for Encounter
        self.admin_browser.open(results[3] + "/++add++Encounter")

        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Encounter"

        with open(os.path.join(FHIR_FIXTURE_PATH, "Encounter.json"),
                  "rb") as f:
            fhir_json = json_loads(f.read())

        self.admin_browser.getControl(name="form.widgets.encounter_resource"
                                      ).value = json_dumps(fhir_json)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()

        context = self.get_context("Encounter", True)

        params = (("length", "gt139"), )

        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # Test Issue#21
        fhir_json_copy = copy.deepcopy(fhir_json_charge_item)
        fhir_json_copy["id"] = str(uuid.uuid4())
        fhir_json_copy["priceOverride"].update({
            "value": 12,
            "unit": "USD",
            "code": "USD"
        })
        fhir_json_copy["quantity"]["value"] = 3
        fhir_json_copy["factorOverride"] = 0.54

        self.admin_browser.open(results[3] + "/++add++ChargeItem")
        self.admin_browser.getControl(name="form.widgets.IBasic.title"
                                      ).value = "Test Clinical Bill (USD)"
        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json_copy)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)

        fhir_json_copy = copy.deepcopy(fhir_json_charge_item)
        fhir_json_copy["id"] = str(uuid.uuid4())
        fhir_json_copy["priceOverride"].update({
            "value": 850,
            "unit": "BDT",
            "code": "BDT"
        })
        fhir_json_copy["quantity"]["value"] = 8
        fhir_json_copy["factorOverride"] = 0.21

        self.admin_browser.open(results[3] + "/++add++ChargeItem")
        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill(BDT)"
        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json_copy)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()
        # Test with multiple equal values
        context = self.get_context("ChargeItem", True)
        params = (("factor-override", "0.8,0.21"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 2)

        params = (("factor-override", "gt0.8,lt0.54"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))

        self.assertEqual(len(brains), 1)
Esempio n. 12
0
    def test_quantity_type_search(self):
        """ """
        results = self.load_contents()

        self.admin_browser.open(results[3] + "/++add++ChargeItem")

        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill"

        with open(os.path.join(FHIR_FIXTURE_PATH, "ChargeItem.json"),
                  "rb") as f:
            fhir_json = json_loads(f.read())

        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        self.assertIn("chargeitem/view", self.admin_browser.url)
        # Let's flush
        self.es.connection.indices.flush()
        # Test so normal
        context = self.get_context("ChargeItem", True)

        # Test ascending order
        params = (("quantity", "5"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        params = (("quantity", "lt5.1"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # Test with value code/unit and system
        params = (("price-override", "gt39.99|urn:iso:std:iso:4217|EUR"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        self.assertEqual(len(brains), 1)

        # Test with code/unit and system
        params = (("price-override", "40||EUR"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))

        self.assertEqual(len(brains), 1)
        # Test Issue#21
        fhir_json_copy = copy.deepcopy(fhir_json)
        fhir_json_copy["id"] = str(uuid.uuid4())
        fhir_json_copy["priceOverride"].update({
            "value": 12,
            "unit": "USD",
            "code": "USD"
        })
        fhir_json_copy["quantity"]["value"] = 3
        fhir_json_copy["factorOverride"] = 0.54

        self.admin_browser.open(results[3] + "/++add++ChargeItem")
        self.admin_browser.getControl(name="form.widgets.IBasic.title"
                                      ).value = "Test Clinical Bill (USD)"
        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json_copy)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)

        fhir_json_copy = copy.deepcopy(fhir_json)
        fhir_json_copy["id"] = str(uuid.uuid4())
        fhir_json_copy["priceOverride"].update({
            "value": 850,
            "unit": "BDT",
            "code": "BDT"
        })
        fhir_json_copy["quantity"]["value"] = 8
        fhir_json_copy["factorOverride"] = 0.21

        self.admin_browser.open(results[3] + "/++add++ChargeItem")
        self.admin_browser.getControl(
            name="form.widgets.IBasic.title").value = "Test Clinical Bill(BDT)"
        self.admin_browser.getControl(name="form.widgets.chargeitem_resource"
                                      ).value = json_dumps(fhir_json_copy)
        self.admin_browser.getControl(name="form.buttons.save").click()
        self.assertIn("Item created", self.admin_browser.contents)
        # Let's flush
        self.es.connection.indices.flush()

        params = ((
            "price-override",
            "gt39.99|urn:iso:std:iso:4217|EUR,le850|urn:iso:std:iso:4217|BDT",
        ), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))

        self.assertEqual(len(brains), 2)

        params = (("price-override", "ge12,le850"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        # should be all three now
        self.assertEqual(len(brains), 3)
        # serach by only system and code
        params = ((
            "price-override",
            ("|urn:iso:std:iso:4217|USD,"
             "|urn:iso:std:iso:4217|BDT,"
             "|urn:iso:std:iso:4217|DKK"),
        ), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        # should be 2
        self.assertEqual(len(brains), 2)

        # serach by unit only
        params = (("price-override", "|BDT,|DKK"), )
        brains = zcatalog_fhir_search(context, query_string=urlencode(params))
        # should be one
        self.assertEqual(len(brains), 1)