def test_org_trigger_date(self): # testing org-based QBs org, rp, rp_id = self.setup_org_n_rp() q = self.add_questionnaire(name='q') q, org, self.test_user = map(db.session.merge, (q, org, self.test_user)) qb = QuestionnaireBank(name='qb', research_protocol_id=rp_id, classification='baseline', start='{"days": 1}', expired='{"days": 2}') qbq = QuestionnaireBankQuestionnaire(rank=0, questionnaire=q) qb.questionnaires.append(qbq) # user without consents or TX date should return None self.assertFalse(qb.trigger_date(self.test_user)) # user with consent should return consent date self.consent_with_org(org.id, setdate=now) self.test_user = db.session.merge(self.test_user) self.assertEqual(qb.trigger_date(self.test_user), now) # user with consent and TX date should return TX date (if qb.recurs) tx_date = datetime(2017, 6, 10, 20, 00, 00, 000000) self.add_procedure(code='7', display='Focal therapy', system=ICHOM, setdate=tx_date) self.test_user = db.session.merge(self.test_user) qb.__trigger_date = None # clear out stored trigger_date recur = Recur(start='{"months": 3}', cycle_length='{"months": 6}', termination='{"months": 24}') qb.recurs.append(recur) self.assertEqual(qb.trigger_date(self.test_user), tx_date)
def test_intervention_trigger_date(self): # testing intervention-based QBs q = self.add_questionnaire('q') interv = Intervention(name='interv', description='test') with SessionScope(db): db.session.add(interv) db.session.commit() q, interv, self.test_user = map(db.session.merge, (q, interv, self.test_user)) qb = QuestionnaireBank(name='qb', intervention_id=interv.id, classification='baseline', start='{"days": 1}', expired='{"days": 2}') qbq = QuestionnaireBankQuestionnaire(rank=0, questionnaire=q) qb.questionnaires.append(qbq) # user without biopsy or TX date should return None self.assertFalse(qb.trigger_date(self.test_user)) # user with biopsy should return biopsy date self.login() self.test_user.save_observation(codeable_concept=CC.BIOPSY, value_quantity=CC.TRUE_VALUE, audit=Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID), status='', issued=None) self.test_user = db.session.merge(self.test_user) obs = self.test_user.observations.first() self.assertEqual(obs.codeable_concept.codings[0].display, 'biopsy') self.assertEqual(qb.trigger_date(self.test_user), obs.issued) # user with biopsy and TX date should return TX date tx_date = datetime.utcnow() self.add_procedure(code='7', display='Focal therapy', system=ICHOM, setdate=tx_date) self.test_user = db.session.merge(self.test_user) qb.__trigger_date = None # clear out stored trigger_date self.assertEqual(qb.trigger_date(self.test_user), tx_date)