def test_encounter_as_fhir(self): enc = Encounter(status='planned', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=dateutil.parser.parse("2013-07-07")) data = enc.as_fhir() # confirm we can store with SessionScope(db): db.session.add(enc) db.session.commit() enc = db.session.merge(enc) self.assertEquals(enc.status, data['status']) self.assertEquals(enc.auth_method, data['auth_method'])
def testP3Pstrategy(self): # Prior to meeting conditions in strategy, user shouldn't have access # (provided we turn off public access) INTERVENTION.DECISION_SUPPORT_P3P.public_access = False INTERVENTION.SEXUAL_RECOVERY.public_access = False # part of strat. user = self.test_user assert not INTERVENTION.DECISION_SUPPORT_P3P.display_for_user( user).access # Fulfill conditions enc = Encounter(status='in-progress', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=datetime.utcnow()) with SessionScope(db): db.session.add(enc) db.session.commit() self.add_procedure(code='424313000', display='Started active surveillance') get_user(TEST_USER_ID).save_observation( codeable_concept=CC.PCaLocalized, value_quantity=CC.TRUE_VALUE, audit=Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID), status=None, issued=None) self.promote_user(user, role_name=ROLE.PATIENT.value) with SessionScope(db): db.session.commit() user = db.session.merge(user) # P3P strategy should now be in view for test user assert INTERVENTION.DECISION_SUPPORT_P3P.display_for_user(user).access
def mock_qr(user_id, instrument_id, status='completed'): today = datetime.utcnow() qr_document = { "questionnaire": { "display": "Additional questions", "reference": "https://{}/api/questionnaires/{}".format('SERVER_NAME', instrument_id) } } enc = Encounter(status='planned', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=datetime.utcnow()) with SessionScope(db): db.session.add(enc) db.session.commit() enc = db.session.merge(enc) qr = QuestionnaireResponse(subject_id=TEST_USER_ID, status=status, authored=today, document=qr_document, encounter_id=enc.id) with SessionScope(db): db.session.add(qr) db.session.commit()
def add_procedure(self, code='367336001', display='Chemotherapy', system=SNOMED, setdate=None): "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 = setdate or datetime.utcnow() procedure.end_time = datetime.utcnow() procedure.encounter = enc db.session.add(procedure) db.session.commit()
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 test_encounter_from_fhir(self): with open(os.path.join(os.path.dirname(__file__), 'encounter-example.json'), 'r') as fhir_data: data = json.load(fhir_data) enc = Encounter.from_fhir(data) self.assertEquals(enc.status, 'finished') self.assertEquals(enc.auth_method, 'password_authenticated') self.assertEquals(enc.start_time, dateutil.parser.parse("2013-05-05"))
def mock_qr(instrument_id, status='completed', timestamp=None, qb=None, doc_id=None, iteration=None): if not doc_id: doc_id = ''.join(choice(ascii_letters) for _ in range(10)) timestamp = timestamp or datetime.utcnow() qr_document = { "questionnaire": { "display": "Additional questions", "reference": "https://{}/api/questionnaires/{}".format('SERVER_NAME', instrument_id) }, "identifier": { "use": "official", "label": "cPRO survey session ID", "value": doc_id, "system": "https://stg-ae.us.truenth.org/eproms-demo" } } enc = Encounter(status='planned', auth_method='url_authenticated', user_id=TEST_USER_ID, start_time=timestamp) with SessionScope(db): db.session.add(enc) db.session.commit() enc = db.session.merge(enc) qb = qb or QuestionnaireBank.most_current_qb(get_user(TEST_USER_ID), timestamp).questionnaire_bank qr = QuestionnaireResponse(subject_id=TEST_USER_ID, status=status, authored=timestamp, document=qr_document, encounter_id=enc.id, questionnaire_bank=qb, qb_iteration=iteration) with SessionScope(db): db.session.add(qr) db.session.commit() invalidate_assessment_status_cache(TEST_USER_ID)
def create_missing_encounters(table_name): bind = op.get_bind() session = Session(bind=bind) if table_name == 'questionnaire_responses': user_col = 'subject_id' else: user_col = 'user_id' for obj_id, user_id in session.execute("SELECT id, {} FROM {} where " "encounter_id IS NULL".format( user_col, table_name)): enc = Encounter(status='finished', auth_method='staff_authenticated', start_time=datetime(2000, 0o1, 0o1, 0o1, 0o1, 0o1), user_id=user_id) session.add(enc) session.commit() enc = session.merge(enc) session.execute('UPDATE {} SET encounter_id = {} where id = {}'.format( table_name, enc.id, obj_id))
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))
def test_reporting_stats(self): user1 = self.add_user('test1') user2 = self.add_user('test2') user3 = self.add_user('test3') org = Organization(name='testorg') interv1 = INTERVENTION.COMMUNITY_OF_WELLNESS interv2 = INTERVENTION.DECISION_SUPPORT_P3P interv1.public_access = False interv2.public_access = True with SessionScope(db): db.session.add(org) db.session.add(interv1) db.session.add(interv2) user1.organizations.append(org) user2.organizations.append(org) user2.interventions.append(interv1) user3.interventions.append(interv2) map(db.session.add, (user1, user2, user3)) db.session.commit() user1, user2, user3, org = map(db.session.merge, (user1, user2, user3, org)) userid = user1.id self.promote_user(user=user1, role_name=ROLE.PATIENT.value) self.promote_user(user=user2, role_name=ROLE.PATIENT.value) self.promote_user(user=user3, role_name=ROLE.PATIENT.value) self.promote_user(user=user2, role_name=ROLE.PARTNER.value) self.promote_user(user=user3, role_name=ROLE.STAFF.value) with SessionScope(db): for i in range(5): enc = Encounter(status='finished', auth_method='password_authenticated', start_time=datetime.utcnow(), user_id=userid) db.session.add(enc) db.session.commit() stats = self.get_stats() self.assertTrue('Decision Support P3P' not in stats['interventions']) self.assertEqual(stats['interventions']['Community of Wellness'], 1) self.assertEqual(stats['organizations']['testorg'], 2) self.assertEqual(stats['organizations']['Unspecified'], 2) self.assertEqual(stats['roles']['patient'], 3) self.assertEqual(stats['roles']['staff'], 1) self.assertEqual(stats['roles']['partner'], 1) self.assertEqual(len(stats['encounters']['all']), 5) # test adding a new encounter, to confirm still using cached stats with SessionScope(db): enc = Encounter(status='finished', auth_method='password_authenticated', start_time=datetime.utcnow(), user_id=userid) db.session.add(enc) db.session.commit() stats2 = self.get_stats(invalidate=False) # shold not have changed, if still using cached values self.assertEqual(len(stats2['encounters']['all']), 5)