def test_from_fhir(self): with open( os.path.join(os.path.dirname(__file__), 'organization-example-f001-burgers.json'), 'r') as fhir_data: data = json.load(fhir_data) #prepopuate database with matching locale Coding.from_fhir({ 'code': 'en_AU', 'display': 'Australian English', 'system': "urn:ietf:bcp:47" }) org = Organization.from_fhir(data) self.assertEquals(org.addresses[0].line1, data['address'][0]['line'][0]) self.assertEquals(org.addresses[1].line1, data['address'][1]['line'][0]) self.assertEquals(org.name, data['name']) self.assertEquals(org.phone, "022-655 2300") self.assertTrue(org.use_specific_codings) self.assertTrue(org.race_codings) self.assertFalse(org.ethnicity_codings) self.assertEquals(org.locales.count(), 1) self.assertEquals(org.default_locale, "en_AU")
def test_codeable_concept_codings(self): "CodeableConcepts should handle coding changes" initial_coding1 = Coding(system='sys1', code='ic-1', display='i1') initial_coding2 = Coding(system='sys2', code='ic-2', display='i2') cc = CodeableConcept(text='codings change', codings=[initial_coding1, initial_coding2]) with SessionScope(db): db.session.add(initial_coding1) db.session.add(initial_coding2) db.session.add(cc) db.session.commit() initial_coding1 = db.session.merge(initial_coding1) initial_coding2 = db.session.merge(initial_coding2) cc = db.session.merge(cc) # now parse a fhir snippet containing first just a partial set data = { "test_concept1": { "coding": [ { "system": initial_coding1.system, "code": initial_coding1.code, "display": initial_coding1.display }, ], }, } cc_parsed = CodeableConcept.from_fhir(data['test_concept1']) self.assertEquals(cc_parsed.codings, cc.codings) self.assertEquals(2, len(cc.codings)) self.assertEquals(cc_parsed.text, cc.text) # and again, but now containing a new coding data = { "test_concept2": { "coding": [ { "system": initial_coding1.system, "code": initial_coding1.code, "display": initial_coding1.display }, { "system": 'local', "code": 'local-1', "display": 'si-loco' }, ], "text": "given two codings" }, } cc_parsed = CodeableConcept.from_fhir(data['test_concept2']) persisted = CodeableConcept.query.get(cc_parsed.id) self.assertEquals(cc_parsed.codings, persisted.codings) self.assertEquals(len(persisted.codings), 3) self.assertEquals(persisted.text, 'given two codings')
def test_cc_format(self): c1 = Coding(system='http://test.org', code='66.5', display='howdy') c2 = Coding(system='http://hl7.org', code='5-12', display='grade') cc = CodeableConcept(text='test text', codings=[c1, c2]) cc_str = "test format: {}".format(cc) self.assertIn(cc.text, cc_str) self.assertIn(c1.system, cc_str) self.assertIn(c1.code, cc_str) self.assertIn(c1.display, cc_str) self.assertIn(c2.system, cc_str) self.assertIn(c2.code, cc_str) self.assertIn(c2.display, cc_str)
def prep_db_for_clinical(self): # First push some clinical data into the db for the test user with SessionScope(db): audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID) observation = Observation() coding = Coding(system='SNOMED-CT', code='372278000', display='Gleason score') cc = CodeableConcept(codings=[ coding, ]) observation.codeable_concept = cc observation.value_quantity = ValueQuantity(value=2) performer = Performer(reference_txt=json.dumps( Reference.patient(TEST_USER_ID).as_fhir())) observation.performers.append(performer) db.session.add(observation) enc = Encounter(status='planned', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=datetime.utcnow()) db.session.add(enc) db.session.flush() db.session.add( UserObservation(user_id=int(TEST_USER_ID), observation_id=observation.id, encounter_id=enc.id, audit=audit)) db.session.commit()
def add_procedure(self, code='367336001', display='Chemotherapy', system=SNOMED): "Add procedure data into the db for the test user" with SessionScope(db): audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID) procedure = Procedure(audit=audit) coding = Coding(system=system, code=code, display=display).add_if_not_found(True) code = CodeableConcept(codings=[ coding, ]).add_if_not_found(True) enc = Encounter(status='planned', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=datetime.utcnow()) db.session.add(enc) db.session.commit() enc = db.session.merge(enc) procedure.code = code procedure.user = db.session.merge(self.test_user) procedure.start_time = datetime.utcnow() procedure.end_time = datetime.utcnow() procedure.encounter = enc db.session.add(procedure) db.session.commit()
def test_organization_put(self): self.promote_user(role_name=ROLE.ADMIN) self.login() with open( os.path.join(os.path.dirname(__file__), 'organization-example-f001-burgers.json'), 'r') as fhir_data: data = json.load(fhir_data) # remove the id from the file - doesn't play well with ours data.pop('id') # Shove a nearly empty org in the db and then update via the api org = Organization(name='test') with SessionScope(db): db.session.add(org) db.session.commit() org = db.session.merge(org) org_id = org.id #prepopuate database with matching locale Coding.from_fhir({ 'code': 'en_AU', 'display': 'Australian English', 'system': "urn:ietf:bcp:47" }) rv = self.client.put('/api/organization/{}'.format(org_id), content_type='application/json', data=json.dumps(data)) self.assert200(rv) # Pull the updated db entry org = Organization.query.get(org_id) self.assertEquals(org.addresses[0].line1, data['address'][0]['line'][0]) self.assertEquals(org.addresses[1].line1, data['address'][1]['line'][0]) self.assertEquals(org.name, data['name']) self.assertEquals(org.phone, "022-655 2300")
def downgrade(): bind = op.get_bind() session = Session(bind=bind) defcode = Coding(system="urn:ietf:bcp:47", display="Default", code="") session.add(defcode) session.commit() defcode = session.merge(defcode) defcc = CodeableConcept(text="") defcc.codings.append(defcode) session.add(defcc) session.commit()
def test_locale_inheritance(self): # prepopuate database with matching locale cd = Coding.from_fhir({'code': 'en_AU', 'display': 'Australian English', 'system': "urn:ietf:bcp:47"}) # create parent with locale parent_id = 101 parent = Organization(id=parent_id, name='test parent') ol = OrganizationLocale(organization_id=parent_id, coding_id=cd.id) # create child org with no locales org = Organization(id=102, name='test', partOf_id=parent_id) org.use_specific_codings = False with SessionScope(db): db.session.add(parent) db.session.commit() db.session.add(ol) db.session.add(org) db.session.commit() org = db.session.merge(org) self.assertEquals(org.partOf_id, parent_id) # add child org to user user = User.query.get(TEST_USER_ID) user.organizations.append(org) # test locale inheritance self.assertEquals(user.locale_display_options,set(['en_AU']))
def test_permanently_delete_user(self): # created acting user and to-be-deleted user actor = self.add_user('actor') deleted = self.add_user('deleted') deleted, actor = map(db.session.merge, (deleted, actor)) deleted_id, actor_id = deleted.id, actor.id deleted_email, actor_email = deleted.email, actor.email self.promote_user(user=actor, role_name=ROLE.ADMIN) with SessionScope(db): db.session.commit() deleted = db.session.merge(deleted) # create observation and user_observation audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID) observation = Observation() coding = Coding(system='SNOMED-CT', code='372278000', display='Gleason score') cc = CodeableConcept(codings=[coding, ]) observation.codeable_concept = cc observation.value_quantity = ValueQuantity(value=2) performer = Performer(reference_txt=json.dumps( Reference.patient(TEST_USER_ID).as_fhir())) observation.performers.append(performer) enc = Encounter(status='planned', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=datetime.utcnow()) with SessionScope(db): db.session.add(observation) db.session.add(enc) db.session.commit() observation, enc = map(db.session.merge, (observation, enc)) observation_id, enc_id = observation.id, enc.id user_obs = UserObservation(user_id=deleted_id, audit=audit, observation_id=observation_id, encounter_id=enc_id) with SessionScope(db): db.session.add(user_obs) db.session.commit() user_obs = db.session.merge(user_obs) user_obs_id = user_obs.id # create user and subject audits subj_audit = Audit(user_id=TEST_USER_ID, subject_id=deleted_id) user_audit = Audit(user_id=deleted_id, subject_id=TEST_USER_ID) with SessionScope(db): db.session.add(subj_audit) db.session.add(user_audit) db.session.commit() subj_audit, user_audit = map(db.session.merge, (subj_audit, user_audit)) subj_audit_id, user_audit_id = subj_audit.id, user_audit.id # create user_consent and audit consent_org = Organization(name='test org') consent_audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID) with SessionScope(db): db.session.add(consent_org) db.session.add(consent_audit) db.session.commit() db.session.add(UserConsent( user_id=deleted_id, organization_id=consent_org.id, audit=consent_audit, agreement_url='http://example.org', options=STAFF_EDITABLE_MASK)) db.session.commit() consent_org, consent_audit = map(db.session.merge, (consent_org, consent_audit)) co_id, ca_id = consent_org.id, consent_audit.id # permanently deleted user, check deletion cascades permanently_delete_user(deleted_email, actor=actor_email) self.assertFalse(User.query.get(deleted_id)) self.assertFalse(UserObservation.query.get(user_obs_id)) self.assertTrue(Observation.query.get(observation_id)) self.assertFalse(Audit.query.get(subj_audit_id)) self.assertFalse(Audit.query.get(user_audit_id)) self.assertFalse(UserConsent.query.get(co_id)) self.assertFalse(Audit.query.get(ca_id))