Ejemplo n.º 1
0
 def test_first_interval(self):
     three_back = datetime.utcnow() - timedelta(days=3)
     recur = Recur(days_to_start=2, days_in_cycle=10,
                   days_till_termination=35)
     result = recur.active_interval_start(start=three_back)
     # should get three back plus days_to_start
     self.assertAlmostEqual(result, three_back + timedelta(days=2))
Ejemplo n.º 2
0
 def test_expired(self):
     back_36 = datetime.utcnow() - timedelta(days=36)
     recur = Recur(days_to_start=30, days_in_cycle=2,
                   days_till_termination=35)
     result = recur.active_interval_start(start=back_36)
     # None implies expired or not started
     self.assertIsNone(result)
Ejemplo n.º 3
0
 def test_not_started(self):
     yesterday = datetime.utcnow() - timedelta(days=1)
     recur = Recur(days_to_start=2, days_in_cycle=2,
                   days_till_termination=35)
     result = recur.active_interval_start(start=yesterday)
     # None implies expired or not started
     self.assertIsNone(result)
Ejemplo n.º 4
0
 def test_third_interval(self):
     thirty_back = datetime.utcnow() - timedelta(days=30)
     recur = Recur(days_to_start=2, days_in_cycle=10,
                   days_till_termination=35)
     result = recur.active_interval_start(start=thirty_back)
     # should get back 30 back, plus 2 to start, plus 10*2
     self.assertAlmostEqual(result, thirty_back + timedelta(days=22))
Ejemplo n.º 5
0
 def test_expired(self):
     back_36 = datetime.utcnow() - timedelta(days=36)
     recur = Recur(start='{"days": 30}', cycle_length='{"days": 2}',
                   termination='{"days": 35}')
     result, _ = recur.active_interval_start(
         trigger_date=back_36, as_of_date=now)
     # None implies expired or not started
     assert result is None
Ejemplo n.º 6
0
 def test_not_started(self):
     yesterday = datetime.utcnow() - timedelta(days=1)
     recur = Recur(start='{"days": 2}', cycle_length='{"days": 2}',
                   termination='{"days": 35}')
     result, _ = recur.active_interval_start(
         trigger_date=yesterday, as_of_date=now)
     # None implies expired or not started
     self.assertIsNone(result)
Ejemplo n.º 7
0
 def test_not_started(self):
     yesterday = datetime.utcnow() - timedelta(days=1)
     recur = Recur(start='{"days": 2}', cycle_length='{"days": 2}',
                   termination='{"days": 35}')
     result, _ = recur.active_interval_start(
         trigger_date=yesterday, as_of_date=now)
     # None implies expired or not started
     assert result is None
Ejemplo n.º 8
0
 def test_first_interval(self):
     three_back = datetime.utcnow() - timedelta(days=3)
     recur = Recur(start='{"days": 2}', cycle_length='{"days": 10}',
                   termination='{"days": 35}')
     result, ic = recur.active_interval_start(
         trigger_date=three_back, as_of_date=now)
     # should get three back plus start
     self.assertAlmostEqual(result, three_back + timedelta(days=2))
     self.assertEqual(ic, 0)
Ejemplo n.º 9
0
 def test_third_interval(self):
     thirty_back = datetime.utcnow() - timedelta(days=30)
     recur = Recur(start='{"days": 2}', cycle_length='{"days": 10}',
                   termination='{"days": 35}')
     result, ic = recur.active_interval_start(
         trigger_date=thirty_back, as_of_date=now)
     # should get back 30 back, plus 2 to start, plus 10*2 (iterations)
     assert result == thirty_back + timedelta(days=22)
     assert ic == 2
Ejemplo n.º 10
0
 def test_first_interval(self):
     three_back = datetime.utcnow() - timedelta(days=3)
     recur = Recur(start='{"days": 2}', cycle_length='{"days": 10}',
                   termination='{"days": 35}')
     result, ic = recur.active_interval_start(
         trigger_date=three_back, as_of_date=now)
     # should get three back plus start
     assert result == three_back + timedelta(days=2)
     assert ic == 0
Ejemplo n.º 11
0
 def test_third_interval(self):
     thirty_back = datetime.utcnow() - timedelta(days=30)
     recur = Recur(start='{"days": 2}', cycle_length='{"days": 10}',
                   termination='{"days": 35}')
     result, ic = recur.active_interval_start(
         trigger_date=thirty_back, as_of_date=now)
     # should get back 30 back, plus 2 to start, plus 10*2
     self.assertAlmostEqual(result, thirty_back + timedelta(days=22))
     self.assertEqual(ic, 2)
Ejemplo n.º 12
0
    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
        assert not 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)
        assert trigger_date(self.test_user) == now
        assert trigger_date(self.test_user, qb) == 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)
        recur = Recur(start='{"months": 3}',
                      cycle_length='{"months": 6}',
                      termination='{"months": 24}')
        qb.recurs.append(recur)
        assert trigger_date(self.test_user, qb) == tx_date
Ejemplo n.º 13
0
    def setup_intervention_qbs(self, intervention=None):
        if not intervention:
            intervention = Intervention(name='symptom tracker',
                                        description='test')
            with SessionScope(db):
                db.session.add(intervention)
                db.session.commit()

        eq5d = self.add_questionnaire(name='eq5d')
        epic26 = self.add_questionnaire(name='epic26')
        recur6 = Recur(start='{"months": 3, "weeks": -2}',
                       cycle_length='{"months": 6}',
                       termination='{"months": 27}')
        exists = Recur.query.filter_by(start=recur6.start,
                                       cycle_length=recur6.cycle_length,
                                       termination=recur6.termination).first()
        if exists:
            recur6 = exists

        with SessionScope(db):
            db.session.add(eq5d)
            db.session.add(epic26)
            db.session.add(recur6)
            db.session.commit()
        intervention, eq5d, epic26, recur6 = map(
            db.session.merge, (intervention, eq5d, epic26, recur6))

        qb_base = QuestionnaireBank(name='Symptom Tracker Baseline',
                                    classification='baseline',
                                    intervention_id=intervention.id,
                                    start='{"days": 0}',
                                    expired='{"months": 3, "days": -14}')
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=eq5d, rank=1)
        qb_base.questionnaires.append(qbq)
        qb_base.questionnaires.append(qbq2)

        qb_r6 = QuestionnaireBank(name='Symptom Tracker Recurring',
                                  classification='recurring',
                                  intervention_id=intervention.id,
                                  start='{"days": 0}',
                                  expired='{"months": 3}',
                                  recurs=[recur6])
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=eq5d, rank=1)
        qb_r6.questionnaires.append(qbq)
        qb_r6.questionnaires.append(qbq2)

        with SessionScope(db):
            db.session.add(qb_base)
            db.session.add(qb_r6)
            db.session.commit()

        return db.session.merge(intervention)
Ejemplo n.º 14
0
    def test_questionnaire_banks_recurs(self):
        # set up a few recurring instances
        initial_recur = Recur(days_to_start=90,
                              days_in_cycle=90,
                              days_till_termination=720)
        every_six_thereafter = Recur(days_to_start=720, days_in_cycle=180)

        metastatic_org = Organization(name='metastatic')
        questionnaire = Questionnaire(name='test_q')
        with SessionScope(db):
            db.session.add(initial_recur)
            db.session.add(every_six_thereafter)
            db.session.add(metastatic_org)
            db.session.add(questionnaire)
            db.session.commit()

        initial_recur = db.session.merge(initial_recur)
        every_six_thereafter = db.session.merge(every_six_thereafter)
        metastatic_org_id = db.session.merge(metastatic_org).id

        # with bits in place, setup a recurring QB
        mr_qb = QuestionnaireBank(name='metastatic_recurring',
                                  classification='recurring',
                                  organization_id=metastatic_org_id)
        questionnaire = db.session.merge(questionnaire)
        recurs = [initial_recur, every_six_thereafter]

        qbq = QuestionnaireBankQuestionnaire(questionnaire=questionnaire,
                                             days_till_due=1,
                                             days_till_overdue=30,
                                             rank=1,
                                             recurs=recurs)
        mr_qb.questionnaires.append(qbq)

        # confirm persistence of this questionnaire bank includes the bits
        # added above
        results = mr_qb.as_json()

        copy = QuestionnaireBank.from_json(results)
        self.assertEquals(copy.name, mr_qb.name)
        copy_q = copy.questionnaires[0]
        self.assertEquals(copy_q.recurs, [initial_recur, every_six_thereafter])

        # now, modify the persisted form, remove one recur and add another
        new_recur = Recur(days_to_start=900,
                          days_in_cycle=180,
                          days_till_termination=1800)
        results['questionnaires'][0]['recurs'] = [
            initial_recur.as_json(),
            new_recur.as_json()
        ]
        updated_copy = QuestionnaireBank.from_json(results)

        self.assertEquals(
            [r.as_json() for r in updated_copy.questionnaires[0].recurs],
            [r.as_json() for r in (initial_recur, new_recur)])
Ejemplo n.º 15
0
def mock_tnth_questionnairebanks():
    for name in (symptom_tracker_instruments):
        TestCase.add_questionnaire(name=name)

    # Symptom Tracker Baseline
    self_management = INTERVENTION.SELF_MANAGEMENT
    st_qb = QuestionnaireBank(
        name='symptom_tracker',
        classification='baseline',
        intervention_id=self_management.id,
        start='{"days": 0}',
        expired='{"months": 3}'
    )
    for rank, instrument in enumerate(symptom_tracker_instruments):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        st_qb.questionnaires.append(qbq)

    # Symptom Tracker Recurrence
    st_recur = Recur(
        start='{"months": 3}', cycle_length='{"months": 3}',
        termination='{"months": 27}')

    with SessionScope(db):
        db.session.add(st_qb)
        db.session.add(st_recur)
        db.session.commit()

    self_management = INTERVENTION.SELF_MANAGEMENT
    st_recur_qb = QuestionnaireBank(
        name='symptom_tracker_recurring',
        classification='recurring',
        intervention_id=self_management.id,
        start='{"days": 0}',
        expired='{"months": 3}',
        recurs=[st_recur]
    )
    for rank, instrument in enumerate(symptom_tracker_instruments):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        st_recur_qb.questionnaires.append(qbq)
    with SessionScope(db):
        db.session.add(st_recur_qb)
        db.session.commit()
Ejemplo n.º 16
0
    def test_questionnaire_banks_recurs(self):
        # only one recurrence per qb allowed at this time
        initial_recur = Recur(start='{"days": 90}',
                              cycle_length='{"days": 90}',
                              termination='{"days": 720}')
        every_six_thereafter = Recur(start='{"days": 720}',
                                     cycle_length='{"days": 180}')

        rp = ResearchProtocol(name='proto')
        with SessionScope(db):
            db.session.add(rp)
            db.session.commit()
        rp = db.session.merge(rp)
        rp_id = rp.id

        metastatic_org = Organization(name='metastatic')
        metastatic_org.research_protocols.append(rp)
        questionnaire = self.add_questionnaire(name='test_q')
        with SessionScope(db):
            db.session.add(initial_recur)
            db.session.add(every_six_thereafter)
            db.session.add(metastatic_org)
            db.session.add(questionnaire)
            db.session.commit()

        initial_recur = db.session.merge(initial_recur)
        every_six_thereafter = db.session.merge(every_six_thereafter)

        # with bits in place, setup a recurring QB
        recurs = [initial_recur, every_six_thereafter]
        mr_qb = QuestionnaireBank(name='metastatic_recurring',
                                  classification='recurring',
                                  research_protocol_id=rp_id,
                                  start='{"days": 0}',
                                  overdue='{"days": 1}',
                                  expired='{"days": 30}',
                                  recurs=recurs)
        questionnaire = db.session.merge(questionnaire)

        qbq = QuestionnaireBankQuestionnaire(questionnaire=questionnaire,
                                             rank=1)
        mr_qb.questionnaires.append(qbq)
        with SessionScope(db):
            db.session.add(mr_qb)
            db.session.commit()
        mr_qb, initial_recur, every_six_thereafter = map(
            db.session.merge, (mr_qb, initial_recur, every_six_thereafter))

        # confirm persistence of this questionnaire bank includes the bits
        # added above
        results = mr_qb.as_json()

        copy = QuestionnaireBank.from_json(results)
        assert copy.name == mr_qb.name
        assert copy.recurs == [initial_recur, every_six_thereafter]

        # now, modify the persisted form, remove one recur and add another
        new_recur = Recur(start='{"days": 900}',
                          cycle_length='{"days": 180}',
                          termination='{"days": 1800}')
        results['recurs'] = [initial_recur.as_json(), new_recur.as_json()]
        updated_copy = QuestionnaireBank.from_json(results)

        assert ([r.as_json() for r in updated_copy.recurs
                 ] == [r.as_json() for r in (initial_recur, new_recur)])
Ejemplo n.º 17
0
def mock_questionnairebanks():
    # Define test Orgs and QuestionnaireBanks for each group
    localized_org = Organization(name='localized')
    metastatic_org = Organization(name='metastatic')

    # Recurring assessments every 3 months up to 24 months, then every
    # 6 months prems alternate with epic26 - start with prems
    initial_recur = Recur(days_to_start=90,
                          days_in_cycle=90,
                          days_till_termination=720)
    initial_recur_prems = Recur(days_to_start=90,
                                days_in_cycle=180,
                                days_till_termination=720)
    initial_recur_epic26 = Recur(days_to_start=180,
                                 days_in_cycle=180,
                                 days_till_termination=720)
    every_six_thereafter = Recur(days_to_start=720, days_in_cycle=180)
    every_six_thereafter_prems = Recur(days_to_start=720, days_in_cycle=360)
    every_six_thereafter_epic26 = Recur(days_to_start=900, days_in_cycle=360)

    with SessionScope(db):
        for name in (localized_instruments.union(
                *(metastatic_baseline_instruments,
                  metastatic_indefinite_instruments,
                  metastatic_recurring_instruments))):
            db.session.add(Questionnaire(name=name))
        db.session.add(localized_org)
        db.session.add(metastatic_org)
        db.session.add(initial_recur)
        db.session.add(initial_recur_prems)
        db.session.add(initial_recur_epic26)
        db.session.add(every_six_thereafter)
        db.session.add(every_six_thereafter_prems)
        db.session.add(every_six_thereafter_epic26)
        db.session.commit()
    localized_org, metastatic_org = map(db.session.merge,
                                        (localized_org, metastatic_org))
    localized_org_id = localized_org.id
    metastatic_org_id = metastatic_org.id
    initial_recur = db.session.merge(initial_recur)
    initial_recur_prems = db.session.merge(initial_recur_prems)
    initial_recur_epic26 = db.session.merge(initial_recur_epic26)
    every_six_thereafter = db.session.merge(every_six_thereafter)
    every_six_thereafter_prems = db.session.merge(every_six_thereafter_prems)
    every_six_thereafter_epic26 = db.session.merge(every_six_thereafter_epic26)

    # Localized baseline
    l_qb = QuestionnaireBank(name='localized',
                             classification='baseline',
                             organization_id=localized_org_id)
    for rank, instrument in enumerate(localized_instruments):
        q = Questionnaire.query.filter_by(name=instrument).one()
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q,
                                             days_till_due=7,
                                             days_till_overdue=90,
                                             rank=rank)
        l_qb.questionnaires.append(qbq)

    # Metastatic baseline
    mb_qb = QuestionnaireBank(name='metastatic',
                              classification='baseline',
                              organization_id=metastatic_org_id)
    for rank, instrument in enumerate(metastatic_baseline_instruments):
        q = Questionnaire.query.filter_by(name=instrument).one()
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q,
                                             days_till_due=1,
                                             days_till_overdue=30,
                                             rank=rank)
        mb_qb.questionnaires.append(qbq)

    # Metastatic indefinite
    mi_qb = QuestionnaireBank(name='metastatic_indefinite',
                              classification='indefinite',
                              organization_id=metastatic_org_id)
    for rank, instrument in enumerate(metastatic_indefinite_instruments):
        q = Questionnaire.query.filter_by(name=instrument).one()
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q,
                                             days_till_due=1,
                                             days_till_overdue=3000,
                                             rank=rank)
        mi_qb.questionnaires.append(qbq)

    # Metastatic recurring
    mr_qb = QuestionnaireBank(name='metastatic_recurring',
                              classification='recurring',
                              organization_id=metastatic_org_id)
    for rank, instrument in enumerate(metastatic_recurring_instruments):
        q = Questionnaire.query.filter_by(name=instrument).one()
        if instrument == 'prems':
            recurs = [initial_recur_prems, every_six_thereafter_prems]
        elif instrument == 'epic26':
            recurs = [initial_recur_epic26, every_six_thereafter_epic26]
        else:
            recurs = [initial_recur, every_six_thereafter]

        qbq = QuestionnaireBankQuestionnaire(questionnaire=q,
                                             days_till_due=1,
                                             days_till_overdue=30,
                                             rank=rank,
                                             recurs=recurs)
        mr_qb.questionnaires.append(qbq)

    with SessionScope(db):
        db.session.add(l_qb)
        db.session.add(mb_qb)
        db.session.add(mi_qb)
        db.session.add(mr_qb)
        db.session.commit()
def mock_eproms_questionnairebanks():
    # Define base ResearchProtocols
    localized_protocol = ResearchProtocol(name='localized_protocol')
    metastatic_protocol = ResearchProtocol(name='metastatic_protocol')
    with SessionScope(db):
        db.session.add(localized_protocol)
        db.session.add(metastatic_protocol)
        db.session.commit()
    localized_protocol = db.session.merge(localized_protocol)
    metastatic_protocol = db.session.merge(metastatic_protocol)
    locpro_id = localized_protocol.id
    metapro_id = metastatic_protocol.id

    # Define test Orgs and QuestionnaireBanks for each group
    localized_org = Organization(name='localized')
    localized_org.research_protocols.append(localized_protocol)
    metastatic_org = Organization(name='metastatic')
    metastatic_org.research_protocols.append(metastatic_protocol)

    # from https://docs.google.com/spreadsheets/d/\
    # 1oJ8HKfMHOdXkSshjRlr8lFXxT4aUHX5ntxnKMgf50wE/edit#gid=1339608238
    three_q_recur = Recur(start='{"months": 3}',
                          cycle_length='{"months": 6}',
                          termination='{"months": 24}')
    four_q_recur1 = Recur(start='{"months": 6}',
                          cycle_length='{"years": 1}',
                          termination='{"months": 21}')
    four_q_recur2 = Recur(start='{"months": 30}',
                          cycle_length='{"years": 1}',
                          termination='{"months": 33}')
    six_q_recur = Recur(start='{"years": 1}',
                        cycle_length='{"years": 1}',
                        termination='{"years": 3, "months": 3}')

    for name in (localized_instruments.union(
            *(metastatic_baseline_instruments,
              metastatic_indefinite_instruments, metastatic_3, metastatic_4,
              metastatic_6))):
        TestCase.add_questionnaire(name=name)

    with SessionScope(db):
        db.session.add(localized_org)
        db.session.add(metastatic_org)
        db.session.add(three_q_recur)
        db.session.add(four_q_recur1)
        db.session.add(four_q_recur2)
        db.session.add(six_q_recur)
        db.session.commit()
    localized_org, metastatic_org = map(db.session.merge,
                                        (localized_org, metastatic_org))
    three_q_recur = db.session.merge(three_q_recur)
    four_q_recur1 = db.session.merge(four_q_recur1)
    four_q_recur2 = db.session.merge(four_q_recur2)
    six_q_recur = db.session.merge(six_q_recur)

    # Localized baseline
    l_qb = QuestionnaireBank(name='localized',
                             classification='baseline',
                             research_protocol_id=locpro_id,
                             start='{"days": 0}',
                             overdue='{"days": 7}',
                             expired='{"months": 3}')
    for rank, instrument in enumerate(localized_instruments):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        l_qb.questionnaires.append(qbq)

    # Metastatic baseline
    mb_qb = QuestionnaireBank(name='metastatic',
                              classification='baseline',
                              research_protocol_id=metapro_id,
                              start='{"days": 0}',
                              overdue='{"days": 30}',
                              expired='{"months": 3}')
    for rank, instrument in enumerate(metastatic_baseline_instruments):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        mb_qb.questionnaires.append(qbq)

    # Metastatic indefinite
    mi_qb = QuestionnaireBank(name='metastatic_indefinite',
                              classification='indefinite',
                              research_protocol_id=metapro_id,
                              start='{"days": 0}',
                              expired='{"years": 50}')
    for rank, instrument in enumerate(metastatic_indefinite_instruments):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        mi_qb.questionnaires.append(qbq)

    # Metastatic recurring 3
    mr3_qb = QuestionnaireBank(name='metastatic_recurring3',
                               classification='recurring',
                               research_protocol_id=metapro_id,
                               start='{"days": 0}',
                               overdue='{"days": 30}',
                               expired='{"months": 3}',
                               recurs=[three_q_recur])
    for rank, instrument in enumerate(metastatic_3):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        mr3_qb.questionnaires.append(qbq)

    # Metastatic recurring 4
    mr4_qb = QuestionnaireBank(name='metastatic_recurring4',
                               classification='recurring',
                               research_protocol_id=metapro_id,
                               recurs=[four_q_recur1, four_q_recur2],
                               start='{"days": 0}',
                               overdue='{"days": 30}',
                               expired='{"months": 3}')
    for rank, instrument in enumerate(metastatic_4):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        mr4_qb.questionnaires.append(qbq)

    # Metastatic recurring 6
    mr6_qb = QuestionnaireBank(name='metastatic_recurring6',
                               classification='recurring',
                               research_protocol_id=metapro_id,
                               recurs=[six_q_recur],
                               start='{"days": 0}',
                               overdue='{"days": 30}',
                               expired='{"months": 3}')
    for rank, instrument in enumerate(metastatic_6):
        q = Questionnaire.find_by_name(name=instrument)
        qbq = QuestionnaireBankQuestionnaire(questionnaire=q, rank=rank)
        mr6_qb.questionnaires.append(qbq)

    with SessionScope(db):
        db.session.add(l_qb)
        db.session.add(mb_qb)
        db.session.add(mi_qb)
        db.session.add(mr3_qb)
        db.session.add(mr4_qb)
        db.session.add(mr6_qb)
        db.session.commit()
Ejemplo n.º 19
0
    def setup_qbs(self, org=None, rp_name='v2', retired_as_of=None):
        org, rp, rp_id = self.setup_org_n_rp(org=org,
                                             org_name='CRV',
                                             rp_name=rp_name,
                                             retired_as_of=retired_as_of)
        epic26 = self.add_questionnaire(name='epic26_{}'.format(rp_name))
        recur3 = Recur(start='{"months": 3}',
                       cycle_length='{"months": 6}',
                       termination='{"months": 24}')
        exists = Recur.query.filter_by(start=recur3.start,
                                       cycle_length=recur3.cycle_length,
                                       termination=recur3.termination).first()
        if exists:
            recur3 = exists

        recur6 = Recur(start='{"months": 6}',
                       cycle_length='{"years": 1}',
                       termination='{"years": 3, "months": 3}')
        exists = Recur.query.filter_by(start=recur6.start,
                                       cycle_length=recur6.cycle_length,
                                       termination=recur6.termination).first()
        if exists:
            recur6 = exists

        with SessionScope(db):
            db.session.add(epic26)
            db.session.add(recur3)
            db.session.add(recur6)
            db.session.commit()
        org, epic26, recur3, recur6 = map(db.session.merge,
                                          (org, epic26, recur3, recur6))

        qb_base = QuestionnaireBank(name='CRV Baseline {}'.format(rp_name),
                                    classification='baseline',
                                    research_protocol_id=rp_id,
                                    start='{"days": 0}',
                                    overdue='{"days": 30}',
                                    expired='{"months": 3}')
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qb_base.questionnaires.append(qbq)

        qb_m3 = QuestionnaireBank(
            name='CRV_recurring_3mo_period {}'.format(rp_name),
            classification='recurring',
            research_protocol_id=rp_id,
            start='{"days": 0}',
            overdue='{"days": 30}',
            expired='{"months": 3}',
            recurs=[recur3])
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qb_m3.questionnaires.append(qbq)

        qb_m6 = QuestionnaireBank(
            name='CRV_recurring_6mo_period {}'.format(rp_name),
            classification='recurring',
            research_protocol_id=rp_id,
            start='{"days": 0}',
            overdue='{"days": 30}',
            expired='{"months": 3}',
            recurs=[recur6])
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qb_m6.questionnaires.append(qbq)

        with SessionScope(db):
            db.session.add(qb_base)
            db.session.add(qb_m3)
            db.session.add(qb_m6)
            db.session.commit()
        qb_base, qb_m3, qb_m6 = map(db.session.merge, (qb_base, qb_m3, qb_m6))

        return db.session.merge(org)
Ejemplo n.º 20
0
    def setup_org_qbs(self,
                      org=None,
                      rp_name='v2',
                      retired_as_of=None,
                      include_indef=False):
        org_name = org.name if org else 'CRV'
        org, rp, rp_id = self.setup_org_n_rp(org=org,
                                             org_name=org_name,
                                             rp_name=rp_name,
                                             retired_as_of=retired_as_of)
        # enable re-entrance - if first q exists, probably a second
        # org is being setup - return as the rest is done
        q_name = 'eortc_{}'.format(rp_name)
        for q in Questionnaire.query.all():
            if q.name == q_name:
                return

        eortc = self.add_questionnaire(name=q_name)
        epic26 = self.add_questionnaire(name='epic26_{}'.format(rp_name))
        recur3 = Recur(start='{"months": 3}',
                       cycle_length='{"months": 6}',
                       termination='{"months": 24}')
        exists = Recur.query.filter_by(start=recur3.start,
                                       cycle_length=recur3.cycle_length,
                                       termination=recur3.termination).first()
        if exists:
            recur3 = exists

        recur6 = Recur(start='{"months": 6}',
                       cycle_length='{"years": 1}',
                       termination='{"years": 3, "months": 3}')
        exists = Recur.query.filter_by(start=recur6.start,
                                       cycle_length=recur6.cycle_length,
                                       termination=recur6.termination).first()
        if exists:
            recur6 = exists

        with SessionScope(db):
            db.session.add(eortc)
            db.session.add(epic26)
            db.session.add(recur3)
            db.session.add(recur6)
            db.session.commit()
        org, eortc, epic26, recur3, recur6 = map(
            db.session.merge, (org, eortc, epic26, recur3, recur6))

        qb_base = QuestionnaireBank(name='{} Baseline {}'.format(
            org_name, rp_name),
                                    classification='baseline',
                                    research_protocol_id=rp_id,
                                    start='{"days": 0}',
                                    overdue='{"days": 30}',
                                    expired='{"months": 3}')
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=eortc, rank=1)
        qb_base.questionnaires.append(qbq)
        qb_base.questionnaires.append(qbq2)

        qb_m3 = QuestionnaireBank(name='{}_recurring_3mo_period {}'.format(
            org_name, rp_name),
                                  classification='recurring',
                                  research_protocol_id=rp_id,
                                  start='{"days": 0}',
                                  overdue='{"days": 30}',
                                  expired='{"months": 3}',
                                  recurs=[recur3])
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=eortc, rank=1)
        qb_m3.questionnaires.append(qbq)
        qb_m3.questionnaires.append(qbq2)

        qb_m6 = QuestionnaireBank(name='{}_recurring_6mo_period {}'.format(
            org_name, rp_name),
                                  classification='recurring',
                                  research_protocol_id=rp_id,
                                  start='{"days": 0}',
                                  overdue='{"days": 30}',
                                  expired='{"months": 3}',
                                  recurs=[recur6])
        qbq = QuestionnaireBankQuestionnaire(questionnaire=epic26, rank=0)
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=eortc, rank=1)
        qb_m6.questionnaires.append(qbq)
        qb_m6.questionnaires.append(qbq2)

        with SessionScope(db):
            db.session.add(qb_base)
            db.session.add(qb_m3)
            db.session.add(qb_m6)
            db.session.commit()

        if include_indef:
            self.setup_qb(questionnaire_name='irondemog_{}'.format(rp_name),
                          qb_name='indef_{}'.format(rp_name),
                          classification='indefinite',
                          rp_id=rp_id,
                          expired="{\"years\": 50}")

        return db.session.merge(org)
    def test_questionnaire_banks_recurs(self):
        # only one recurrence per qb allowed at this time
        initial_recur = Recur(
            start='{"days": 90}', cycle_length='{"days": 90}',
            termination='{"days": 720}')
        every_six_thereafter = Recur(
            start='{"days": 720}', cycle_length='{"days": 180}')

        rp = ResearchProtocol(name='proto')
        with SessionScope(db):
            db.session.add(rp)
            db.session.commit()
        rp = db.session.merge(rp)
        rp_id = rp.id

        metastatic_org = Organization(name='metastatic')
        metastatic_org.research_protocols.append(rp)
        questionnaire = self.add_questionnaire(name='test_q')
        with SessionScope(db):
            db.session.add(initial_recur)
            db.session.add(every_six_thereafter)
            db.session.add(metastatic_org)
            db.session.add(questionnaire)
            db.session.commit()

        initial_recur = db.session.merge(initial_recur)
        every_six_thereafter = db.session.merge(every_six_thereafter)

        # with bits in place, setup a recurring QB
        recurs = [initial_recur, every_six_thereafter]
        mr_qb = QuestionnaireBank(
            name='metastatic_recurring',
            classification='recurring',
            research_protocol_id=rp_id,
            start='{"days": 0}', overdue='{"days": 1}',
            expired='{"days": 30}',
            recurs=recurs)
        questionnaire = db.session.merge(questionnaire)

        qbq = QuestionnaireBankQuestionnaire(
            questionnaire=questionnaire, rank=1)
        mr_qb.questionnaires.append(qbq)
        with SessionScope(db):
            db.session.add(mr_qb)
            db.session.commit()
        mr_qb, initial_recur, every_six_thereafter = map(
            db.session.merge, (mr_qb, initial_recur, every_six_thereafter))

        # confirm persistence of this questionnaire bank includes the bits
        # added above
        results = mr_qb.as_json()

        copy = QuestionnaireBank.from_json(results)
        assert copy.name == mr_qb.name
        assert copy.recurs == [initial_recur, every_six_thereafter]

        # now, modify the persisted form, remove one recur and add another
        new_recur = Recur(
            start='{"days": 900}',
            cycle_length='{"days": 180}',
            termination='{"days": 1800}')
        results['recurs'] = [
            initial_recur.as_json(), new_recur.as_json()]
        updated_copy = QuestionnaireBank.from_json(results)

        assert ([r.as_json() for r in updated_copy.recurs]
                == [r.as_json() for r in (initial_recur, new_recur)])