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" }
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" })
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_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"]})
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
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() stats[example] = end - start inserted.append(res) values = stats.values() print(f"insertions per second (on average): {1/statistics.mean(values):.2f}") print(f"average: {statistics.mean(values)*1000:.2f} milliseconds") print(f"median: {statistics.median(values)*1000:.2f} milliseconds") print(f"min: {min(values)*1000:.2f} milliseconds") print(f"max: {max(values)*1000:.2f} milliseconds") print(f"spread: {statistics.variance(values)}") examples = tqdm(inserted, desc="Running read benchmark") stats = {} for doc in examples:
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_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})
def test_create_bad_resource_type(self, store: FHIRStore): """create() raises if resource type is unknown""" with raises(NotFoundError, match='unsupported FHIR resource: "unknown"'): store.create({"resourceType": "unknown"})
def test_create_missing_resource_type(self, store: FHIRStore): """create() raises if resource type is not specified""" with raises(BadRequestError, match="resourceType is missing in resource"): store.create({})
def test_delete_instance(self, store: FHIRStore, test_patient): """delete() finds a document in the database""" store.create(test_patient) result = store.delete("Patient", test_patient["id"]) assert result == 1
def test_read_resource(self, store: FHIRStore, test_patient): """read() finds a document in the database""" store.create(test_patient) result = store.read("Patient", test_patient["id"]) assert result == test_patient
start = timer() store.bootstrap(depth=3) end = timer() print(end - start, "seconds") # creating document print("Inserting documents...") json_folder_path = os.path.join(os.getcwd(), "test/fixtures") json_files = [ x for x in os.listdir(json_folder_path) if x.endswith(".json") ] total = 0 for json_file in json_files: json_file_path = os.path.join(json_folder_path, json_file) with open(json_file_path, "r") as f: data = json.load(f) start = timer() print("Creating", json_file_path, "...") try: store.create(data) except Exception as e: print(e) end = timer() print(end - start, "seconds") total += (end - start) print(f"Inserted {len(json_files)} documents in {total} seconds") # srch = store.search(resource='patient',params="male") # print(srch) client.close()