def backend_add_patient(mongo_db, patient, match_external=False): """ Insert or update a patient in patientMatcher database Args: mongo_db(pymongo.database.Database) patient(dict) : a MME patient entity Returns: inserted_id(str) : the ID of the inserted patient or None if patient couldn't be saved """ modified = None upserted = None matching_obj = None try: result = mongo_db['patients'].replace_one({'_id': patient['_id']}, patient , upsert=True) modified = result.modified_count upserted = result.upserted_id except Exception as err: LOG.fatal("Error while inserting a patient into database: {}".format(err)) # this will happen only if patient is added via POST request, # and if there is a change in patients' collections (new patient or updated patient) # Matching is not triggered by inserting demo data into database if match_external and (modified or upserted): matching_obj = external_matcher(mongo_db, patient) #save matching object to database mongo_db['matches'].insert_one(matching_obj) return modified, upserted, matching_obj
def test_external_matching(database, test_node, gpx4_patients, monkeypatch): """Testing the function that trigger patient matching across connected nodes""" patient = gpx4_patients[0] # insert test node object in database database['nodes'].insert_one(test_node) # insert patient object in database inserted_ids = backend_add_patient(mongo_db=database, patient=patient, match_external=False) assert inserted_ids class MockResponse(object): def __init__(self): self.status_code = 200 def json(self): resp = { "disclaimer": "This is a test disclaimer", "results": gpx4_patients } return resp def mock_response(*args, **kwargs): return MockResponse() monkeypatch.setattr(requests, 'request', mock_response) ext_m_result = external_matcher(database, patient, test_node['_id']) assert isinstance(ext_m_result, dict) assert ext_m_result['data']['patient']['id'] == patient['id'] assert ext_m_result['has_matches'] == True assert ext_m_result['match_type'] == 'external'
def match_external(database, query_patient, node=None): """Trigger an external patient matching for a given patient object""" # trigger the matching and save the matching id to variable matching_obj = external_matcher(database, query_patient, node) # save matching object to database only if there are results or error messages if matching_obj and (matching_obj.get("has_matches") or matching_obj.get("errors")): database["matches"].insert_one(matching_obj) return matching_obj