Ejemplo n.º 1
0
    def test_lookup_with_intervention(self):
        intv = Intervention(name='TEST', description='Test Intervention')
        epic26 = self.add_questionnaire(name='epic26')
        eproms_add = self.add_questionnaire(name='eproms_add')
        with SessionScope(db):
            db.session.add(intv)
            db.session.commit()
        intv, epic26, eproms_add = map(db.session.merge,
                                       (intv, epic26, eproms_add))

        bank = QuestionnaireBank(name='CRV',
                                 intervention_id=intv.id,
                                 start='{"days": 7}',
                                 expired='{"days": 90}')
        for rank, q in enumerate((epic26, eproms_add)):
            qbq = QuestionnaireBankQuestionnaire(questionnaire_id=q.id,
                                                 rank=rank)
            bank.questionnaires.append(qbq)

        self.test_user.interventions.append(intv)
        self.login()
        self.add_required_clinical_data()
        with SessionScope(db):
            db.session.add(bank)
            db.session.commit()

        # Patient associated with INTV intervention should generate appropriate
        # questionnaires as of start date
        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        qb_status = QB_Status(self.test_user,
                              as_of_date=now + relativedelta(days=8))
        qb = qb_status.current_qbd().questionnaire_bank
        results = list(qb.questionnaires)
        assert len(results) == 2
    def test_intervention_in_progress(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": 0}', expired='{"days": 2}')
        qbq = QuestionnaireBankQuestionnaire(rank=0, questionnaire=q)
        qb.questionnaires.append(qbq)

        # 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=now)
        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        obs = self.test_user.observations.first()
        assert obs.codeable_concept.codings[0].display == 'biopsy'
        assert trigger_date(self.test_user, qb) == obs.issued

        # add mock in-process QB - confirm current qb is still correct
        mock_qr('q', 'in-progress', qb=qb)
        self.test_user, qb = map(db.session.merge, (self.test_user, qb))
        qb_stat = QB_Status(user=self.test_user, as_of_date=now)
        assert qb_stat.current_qbd().questionnaire_bank == qb
    def test_visit_baseline(self):
        crv = self.setup_org_qbs()
        self.bless_with_basics(setdate=now)  # pick up a consent, etc.
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(crv)

        qstats = QB_Status(self.test_user, now)
        qbd = qstats.current_qbd()
        assert visit_name(qbd) == "Baseline"
Ejemplo n.º 4
0
    def test_visit_baseline(self):
        crv = self.setup_org_qbs()
        self.bless_with_basics(setdate=now)  # pick up a consent, etc.
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(crv)

        qstats = QB_Status(self.test_user, now)
        qbd = qstats.current_qbd()
        assert visit_name(qbd) == "Baseline"
    def test_lookup_for_user(self):
        crv, rp, rp_id = self.setup_org_n_rp(org_name='CRV')
        epic26 = self.add_questionnaire(name='epic26')
        eproms_add = self.add_questionnaire(name='eproms_add')
        comorb = self.add_questionnaire(name='comorb')
        crv, epic26, eproms_add, comorb = map(
            db.session.merge, (crv, epic26, eproms_add, comorb))

        bank = QuestionnaireBank(
            name='CRV', research_protocol_id=rp_id,
            start='{"days": 7}',
            expired='{"days": 90}')
        for rank, q in enumerate((epic26, eproms_add, comorb)):
            qbq = QuestionnaireBankQuestionnaire(
                questionnaire_id=q.id,
                rank=rank)
            bank.questionnaires.append(qbq)

        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(crv)
        self.consent_with_org(org_id=crv.id, setdate=now)
        self.promote_user(role_name=ROLE.PATIENT.value)
        with SessionScope(db):
            db.session.add(bank)
            db.session.commit()

        # User associated with CRV org should generate appropriate
        # questionnaires
        self.test_user = db.session.merge(self.test_user)

        # Doesn't start for 7 days, initially shouldn't get any
        qb_stat = QB_Status(user=self.test_user, as_of_date=now)
        assert qb_stat.current_qbd() is None

        qb_stat = QB_Status(
            user=self.test_user, as_of_date=now + relativedelta(days=7))
        qb = qb_stat.current_qbd().questionnaire_bank
        results = list(qb.questionnaires)
        assert len(results) == 3
        # confirm rank sticks
        assert results[0].name == 'epic26'
        assert results[2].name == 'comorb'
Ejemplo n.º 6
0
    def test_visit_9mo(self):
        crv = self.setup_org_qbs()
        backdate, nowish = associative_backdate(
            now=now, backdate=relativedelta(months=9))
        self.bless_with_basics(setdate=backdate)
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(crv)

        qstats = QB_Status(self.test_user, nowish + timedelta(hours=1))
        qbd = qstats.current_qbd()
        assert visit_name(qbd) == "Month 9"
    def test_visit_9mo(self):
        crv = self.setup_org_qbs()
        backdate, nowish = associative_backdate(
            now=now, backdate=relativedelta(months=9))
        self.bless_with_basics(setdate=backdate)
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(crv)

        qstats = QB_Status(self.test_user, nowish + timedelta(hours=1))
        qbd = qstats.current_qbd()
        assert visit_name(qbd) == "Month 9"
Ejemplo n.º 8
0
    def test_lookup_for_user(self):
        crv, rp, rp_id = self.setup_org_n_rp(org_name='CRV')
        epic26 = self.add_questionnaire(name='epic26')
        eproms_add = self.add_questionnaire(name='eproms_add')
        comorb = self.add_questionnaire(name='comorb')
        crv, epic26, eproms_add, comorb = map(
            db.session.merge, (crv, epic26, eproms_add, comorb))

        bank = QuestionnaireBank(name='CRV',
                                 research_protocol_id=rp_id,
                                 start='{"days": 7}',
                                 expired='{"days": 90}')
        for rank, q in enumerate((epic26, eproms_add, comorb)):
            qbq = QuestionnaireBankQuestionnaire(questionnaire_id=q.id,
                                                 rank=rank)
            bank.questionnaires.append(qbq)

        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(crv)
        self.consent_with_org(org_id=crv.id, setdate=now)
        self.promote_user(role_name=ROLE.PATIENT.value)
        with SessionScope(db):
            db.session.add(bank)
            db.session.commit()

        # User associated with CRV org should generate appropriate
        # questionnaires
        self.test_user = db.session.merge(self.test_user)

        # Doesn't start for 7 days, initially shouldn't get any
        qb_stat = QB_Status(user=self.test_user, as_of_date=now)
        assert qb_stat.current_qbd() is None

        qb_stat = QB_Status(user=self.test_user,
                            as_of_date=now + relativedelta(days=7))
        qb = qb_stat.current_qbd().questionnaire_bank
        results = list(qb.questionnaires)
        assert len(results) == 3
        # confirm rank sticks
        assert results[0].name == 'epic26'
        assert results[2].name == 'comorb'
 def test_no_start_date(self):
     # W/O a biopsy (i.e. event start date), no questionnaries
     self.promote_user(role_name=ROLE.PATIENT.value)
     # toggle default setup - set biopsy false for test user
     self.login()
     self.test_user = db.session.merge(self.test_user)
     self.test_user.save_observation(
         codeable_concept=CC.BIOPSY, value_quantity=CC.FALSE_VALUE,
         audit=Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID),
         status='final', issued=now)
     qstats = QB_Status(self.test_user, now)
     assert not qstats.current_qbd()
     assert not qstats.enrolled_in_classification("baseline")
Ejemplo n.º 10
0
 def test_no_start_date(self):
     # W/O a biopsy (i.e. event start date), no questionnaries
     self.promote_user(role_name=ROLE.PATIENT.value)
     # toggle default setup - set biopsy false for test user
     self.login()
     self.test_user = db.session.merge(self.test_user)
     self.test_user.save_observation(
         codeable_concept=CC.BIOPSY, value_quantity=CC.FALSE_VALUE,
         audit=Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID),
         status='final', issued=now)
     qstats = QB_Status(self.test_user, now)
     assert not qstats.current_qbd()
     assert not qstats.enrolled_in_classification("baseline")
Ejemplo n.º 11
0
    def test_completed_older_rp(self):
        """If current qb completed on older rp, should show as done"""
        fourmonthsago = now - timedelta(days=120)
        weekago = now - timedelta(weeks=1)
        twoweeksago = now - timedelta(weeks=2)
        org = self.setup_org_qbs(rp_name='v2', retired_as_of=weekago)
        org_id = org.id
        self.setup_org_qbs(org=org, rp_name='v3')

        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(org)
        audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        uc = UserConsent(user_id=TEST_USER_ID,
                         organization_id=org_id,
                         audit=audit,
                         agreement_url='http://no.com',
                         acceptance_date=fourmonthsago)
        with SessionScope(db):
            db.session.add(audit)
            db.session.add(uc)
            db.session.commit()
        user = db.session.merge(self.test_user)

        # Now, should still be rp v3, 3mo recurrence
        a_s = QB_Status(user=user, as_of_date=now)
        assert (a_s.current_qbd().questionnaire_bank.name ==
                'CRV_recurring_3mo_period v3')
        assert a_s.instruments_needing_full_assessment() == [
            'epic26_v3', 'eortc_v3'
        ]

        # Complete the questionnaire from the 3mo v2 QB
        v2qb = QuestionnaireBank.query.filter(
            QuestionnaireBank.name == 'CRV_recurring_3mo_period v2').one()
        mock_qr('epic26_v2', timestamp=twoweeksago, qb=v2qb, iteration=0)
        mock_qr('eortc_v2', timestamp=twoweeksago, qb=v2qb, iteration=0)

        # Two weeks ago, should be completed
        user = db.session.merge(user)
        a_s = QB_Status(user=user, as_of_date=twoweeksago)
        assert a_s.overall_status == OverallStatus.completed

        # Current should also be completed, even tho protocol changed
        a_s = QB_Status(user=user, as_of_date=now)
        assert a_s.overall_status == OverallStatus.completed
def mock_qr(
        instrument_id, status='completed', timestamp=None, qb=None,
        doc_id=None, iteration=None, user_id=TEST_USER_ID, entry_method=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=user_id,
        start_time=timestamp)
    if entry_method:
        enc.type.append(entry_method.codings[0])
    with SessionScope(db):
        db.session.add(enc)
        db.session.commit()
    enc = db.session.merge(enc)
    if not qb:
        qstats = QB_Status(get_user(user_id), timestamp)
        qbd = qstats.current_qbd()
        qb, iteration = qbd.questionnaire_bank, qbd.iteration

    qr = QuestionnaireResponse(
        subject_id=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_users_QBT(user_id=user_id)
    def test_completed_older_rp(self):
        """If current qb completed on older rp, should show as done"""
        fourmonthsago = now - timedelta(days=120)
        weekago = now - timedelta(weeks=1)
        twoweeksago = now - timedelta(weeks=2)
        org = self.setup_org_qbs(rp_name='v2', retired_as_of=weekago)
        org_id = org.id
        self.setup_org_qbs(org=org, rp_name='v3')

        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(org)
        audit = Audit(
            user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        uc = UserConsent(
            user_id=TEST_USER_ID, organization_id=org_id,
            audit=audit, agreement_url='http://no.com',
            acceptance_date=fourmonthsago)
        with SessionScope(db):
            db.session.add(audit)
            db.session.add(uc)
            db.session.commit()
        user = db.session.merge(self.test_user)

        # Now, should still be rp v3, 3mo recurrence
        a_s = QB_Status(user=user, as_of_date=now)
        assert (a_s.current_qbd().questionnaire_bank.name
                == 'CRV_recurring_3mo_period v3')
        assert a_s.instruments_needing_full_assessment() == [
            'epic26_v3', 'eortc_v3']

        # Complete the questionnaire from the 3mo v2 QB
        v2qb = QuestionnaireBank.query.filter(
            QuestionnaireBank.name == 'CRV_recurring_3mo_period v2').one()
        mock_qr('epic26_v2', timestamp=twoweeksago, qb=v2qb, iteration=0)
        mock_qr('eortc_v2', timestamp=twoweeksago, qb=v2qb, iteration=0)

        # Two weeks ago, should be completed
        user = db.session.merge(user)
        a_s = QB_Status(user=user, as_of_date=twoweeksago)
        assert a_s.overall_status == OverallStatus.completed

        # Current should also be completed, even tho protocol changed
        a_s = QB_Status(user=user, as_of_date=now)
        assert a_s.overall_status == OverallStatus.completed
Ejemplo n.º 14
0
    def test_2nd_recur_due(self):

        # backdate so baseline q's have expired, and we within the 2nd
        # recurrence window
        backdate, nowish = associative_backdate(
            now=now, backdate=relativedelta(months=9, hours=1))
        self.bless_with_basics(
            setdate=backdate, local_metastatic='metastatic')
        self.test_user = db.session.merge(self.test_user)
        a_s = QB_Status(user=self.test_user, as_of_date=nowish)
        assert a_s.overall_status == OverallStatus.due

        # in the initial window w/ no questionnaires submitted
        # should include all from initial recur
        assert set(a_s.instruments_needing_full_assessment()) == metastatic_3

        # however, we should be looking at iteration 2 (zero index)!
        assert a_s.current_qbd().iteration == 1
    def test_2nd_recur_due(self):

        # backdate so baseline q's have expired, and we within the 2nd
        # recurrence window
        backdate, nowish = associative_backdate(
            now=now, backdate=relativedelta(months=9, hours=1))
        self.bless_with_basics(
            setdate=backdate, local_metastatic='metastatic')
        self.test_user = db.session.merge(self.test_user)
        a_s = QB_Status(user=self.test_user, as_of_date=nowish)
        assert a_s.overall_status == OverallStatus.due

        # in the initial window w/ no questionnaires submitted
        # should include all from initial recur
        assert set(a_s.instruments_needing_full_assessment()) == metastatic_3

        # however, we should be looking at iteration 2 (zero index)!
        assert a_s.current_qbd().iteration == 1
Ejemplo n.º 16
0
def mock_qr(
        instrument_id, status='completed', timestamp=None, qb=None,
        doc_id=None, iteration=None, user_id=TEST_USER_ID):
    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=user_id,
        start_time=timestamp)
    with SessionScope(db):
        db.session.add(enc)
        db.session.commit()
    enc = db.session.merge(enc)
    if not qb:
        qstats = QB_Status(get_user(user_id), timestamp)
        qbd = qstats.current_qbd()
        qb, iteration = qbd.questionnaire_bank, qbd.iteration

    qr = QuestionnaireResponse(
        subject_id=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_users_QBT(user_id=user_id)
    def test_lookup_with_intervention(self):
        intv = Intervention(name='TEST', description='Test Intervention')
        epic26 = self.add_questionnaire(name='epic26')
        eproms_add = self.add_questionnaire(name='eproms_add')
        with SessionScope(db):
            db.session.add(intv)
            db.session.commit()
        intv, epic26, eproms_add = map(
            db.session.merge, (intv, epic26, eproms_add))

        bank = QuestionnaireBank(
            name='CRV', intervention_id=intv.id,
            start='{"days": 7}',
            expired='{"days": 90}')
        for rank, q in enumerate((epic26, eproms_add)):
            qbq = QuestionnaireBankQuestionnaire(
                questionnaire_id=q.id,
                rank=rank)
            bank.questionnaires.append(qbq)

        self.test_user.interventions.append(intv)
        self.login()
        self.add_required_clinical_data()
        with SessionScope(db):
            db.session.add(bank)
            db.session.commit()

        # Patient associated with INTV intervention should generate appropriate
        # questionnaires as of start date
        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        qb_status = QB_Status(
            self.test_user,
            as_of_date=now+relativedelta(days=8))
        qb = qb_status.current_qbd().questionnaire_bank
        results = list(qb.questionnaires)
        assert len(results) == 2
Ejemplo n.º 18
0
    def test_intervention_in_progress(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": 0}',
                               expired='{"days": 2}')
        qbq = QuestionnaireBankQuestionnaire(rank=0, questionnaire=q)
        qb.questionnaires.append(qbq)

        # 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=now)
        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        obs = self.test_user.observations.first()
        assert obs.codeable_concept.codings[0].display == 'biopsy'
        assert trigger_date(self.test_user, qb) == obs.issued

        # add mock in-process QB - confirm current qb is still correct
        mock_qr('q', 'in-progress', qb=qb)
        self.test_user, qb = map(db.session.merge, (self.test_user, qb))
        qb_stat = QB_Status(user=self.test_user, as_of_date=now)
        assert qb_stat.current_qbd().questionnaire_bank == qb
Ejemplo n.º 19
0
    def test_outdated_inprogress_qb(self):
        # create base QB/RP
        org, rp, rp_id = self.setup_org_n_rp(org_name='testorg')
        qn = self.add_questionnaire(name='epic26')
        qn, org, self.test_user = map(db.session.merge,
                                      (qn, org, self.test_user))
        org_id = org.id
        qb = QuestionnaireBank(name='Test Questionnaire Bank',
                               classification='baseline',
                               research_protocol_id=rp_id,
                               start='{"days": 0}',
                               overdue='{"days": 7}',
                               expired='{"days": 90}')
        qbq = QuestionnaireBankQuestionnaire(questionnaire=qn, rank=0)
        qb.questionnaires.append(qbq)

        self.test_user.organizations.append(org)
        self.promote_user(role_name=ROLE.PATIENT.value)

        audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        uc = UserConsent(user_id=TEST_USER_ID,
                         organization=org,
                         audit=audit,
                         agreement_url='http://no.com',
                         acceptance_date=now)

        with SessionScope(db):
            db.session.add(qb)
            db.session.add(self.test_user)
            db.session.add(audit)
            db.session.add(uc)
            db.session.commit()
        qb = db.session.merge(qb)

        # create in-progress QNR for User/QB/RP
        mock_qr(instrument_id='epic26',
                status='in-progress',
                timestamp=now,
                qb=qb)

        # User associated with CRV org should generate appropriate
        # questionnaires
        self.test_user = db.session.merge(self.test_user)
        qb_stat = QB_Status(self.test_user, now)
        qb = qb_stat.current_qbd().questionnaire_bank
        assert qb.research_protocol.name == 'proto'

        # Pointing the User's org to a new QB/RP
        # marking the old RP as retired as of yesterday
        old_rp = OrganizationResearchProtocol.query.filter(
            OrganizationResearchProtocol.organization_id == org_id,
            OrganizationResearchProtocol.research_protocol_id == rp_id).one()
        old_rp.retired_as_of = now - timedelta(days=1)
        rp2 = ResearchProtocol(name='new_proto')
        qn2 = self.add_questionnaire(name='epic27')
        with SessionScope(db):
            db.session.add(rp2)
            db.session.commit()
        rp2 = db.session.merge(rp2)
        rp2_id = rp2.id

        qn2, org = map(db.session.merge, (qn2, org))
        qb2 = QuestionnaireBank(name='Test Questionnaire Bank 2',
                                classification='baseline',
                                research_protocol_id=rp2_id,
                                start='{"days": 0}',
                                overdue='{"days": 7}',
                                expired='{"days": 90}')
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=qn2, rank=0)
        qb2.questionnaires.append(qbq2)

        org.research_protocols.append(rp2)

        with SessionScope(db):
            db.session.add(qb2)
            db.session.add(org)
            db.session.commit()
        qb2 = db.session.merge(qb2)

        # outdated QB/RP should be used as long as User has in-progress QNR
        self.test_user = db.session.merge(self.test_user)
        qb_stat = QB_Status(self.test_user, now)
        qb = qb_stat.current_qbd().questionnaire_bank
        assert qb.name == 'Test Questionnaire Bank'
        assert qb.research_protocol.name == 'proto'

        # completing QNR should result in completed status
        # shouldn't pick up new protocol till next iteration
        mock_qr(instrument_id='epic26',
                status='completed',
                timestamp=now,
                qb=qb)

        self.test_user = db.session.merge(self.test_user)
        invalidate_users_QBT(TEST_USER_ID)
        qb_stat = QB_Status(self.test_user, now)
        qb = qb_stat.current_qbd().questionnaire_bank
        assert qb.name == 'Test Questionnaire Bank'
        assert qb_stat.overall_status == OverallStatus.completed
    def test_outdated_done_indef(self):
        """Confirm completed indefinite counts after RP switch"""

        # boiler plate to create baseline and indef with retired RP
        yesterday = now - timedelta(days=1)
        weekago = now - timedelta(weeks=1)
        org, rp2, rp2_id = self.setup_org_n_rp(
            org_name='testorg', rp_name='v2', retired_as_of=yesterday)
        org, rp3, rp3_id = self.setup_org_n_rp(org=org, rp_name='v3')
        org_id = org.id
        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(org)
        audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        uc = UserConsent(
            user_id=TEST_USER_ID, organization_id=org_id,
            audit=audit, agreement_url='http://no.com',
            acceptance_date=weekago)
        with SessionScope(db):
            db.session.add(audit)
            db.session.add(uc)
            db.session.commit()

        self.setup_qb(
            questionnaire_name='epic23', qb_name='baseline v2',
            classification='baseline', rp_id=rp2_id)
        self.setup_qb(
            questionnaire_name='epic26', qb_name='baseline v3',
            classification='baseline', rp_id=rp3_id)
        qb2_indef = self.setup_qb(
            questionnaire_name='irondemog', qb_name='indef v2',
            classification='indefinite', rp_id=rp2_id)
        self.setup_qb(
            questionnaire_name='irondemog_v3', qb_name='indef v3',
            classification='indefinite', rp_id=rp3_id)

        # for today, should get the v3 baseline
        user = db.session.merge(self.test_user)
        a_s = QB_Status(user=user, as_of_date=now)
        assert (['epic26', 'irondemog_v3'] ==
                a_s.instruments_needing_full_assessment(classification='all'))

        # create done QNR for indefinite dated prior to rp transition
        # belonging to older qb - confirm that clears indef work as of then
        mock_qr('irondemog', timestamp=weekago, qb=qb2_indef)
        user = db.session.merge(self.test_user)
        invalidate_users_QBT(user_id=TEST_USER_ID)
        a_s = QB_Status(user=user, as_of_date=weekago)
        assert (a_s.instruments_needing_full_assessment(
            classification='indefinite') == [])

        # move forward in time; user should no longer need indefinite, even
        # tho RP changed
        qb2_indef = db.session.merge(qb2_indef)
        a_s = QB_Status(user=user, as_of_date=now)
        assert qb2_indef == a_s.current_qbd(
            classification='indefinite').questionnaire_bank
        assert (a_s.instruments_needing_full_assessment(
            classification='indefinite') == [])
        assert (a_s.instruments_needing_full_assessment(
            classification='all') == ['epic26'])
    def test_outdated_inprogress_qb(self):
        # create base QB/RP
        org, rp, rp_id = self.setup_org_n_rp(org_name='testorg')
        qn = self.add_questionnaire(name='epic26')
        qn, org, self.test_user = map(
            db.session.merge, (qn, org, self.test_user))
        org_id = org.id
        qb = QuestionnaireBank(
            name='Test Questionnaire Bank',
            classification='baseline',
            research_protocol_id=rp_id,
            start='{"days": 0}',
            overdue='{"days": 7}',
            expired='{"days": 90}')
        qbq = QuestionnaireBankQuestionnaire(questionnaire=qn, rank=0)
        qb.questionnaires.append(qbq)

        self.test_user.organizations.append(org)
        self.promote_user(role_name=ROLE.PATIENT.value)

        audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        uc = UserConsent(
            user_id=TEST_USER_ID, organization=org,
            audit=audit, agreement_url='http://no.com',
            acceptance_date=now)

        with SessionScope(db):
            db.session.add(qb)
            db.session.add(self.test_user)
            db.session.add(audit)
            db.session.add(uc)
            db.session.commit()
        qb = db.session.merge(qb)

        # create in-progress QNR for User/QB/RP
        mock_qr(
            instrument_id='epic26', status='in-progress',
            timestamp=now, qb=qb)

        # User associated with CRV org should generate appropriate
        # questionnaires
        self.test_user = db.session.merge(self.test_user)
        qb_stat = QB_Status(self.test_user, now)
        qb = qb_stat.current_qbd().questionnaire_bank
        assert qb.research_protocol.name == 'proto'

        # Pointing the User's org to a new QB/RP
        # marking the old RP as retired as of yesterday
        old_rp = OrganizationResearchProtocol.query.filter(
            OrganizationResearchProtocol.organization_id == org_id,
            OrganizationResearchProtocol.research_protocol_id == rp_id).one()
        old_rp.retired_as_of = now - timedelta(days=1)
        rp2 = ResearchProtocol(name='new_proto')
        qn2 = self.add_questionnaire(name='epic27')
        with SessionScope(db):
            db.session.add(rp2)
            db.session.commit()
        rp2 = db.session.merge(rp2)
        rp2_id = rp2.id

        qn2, org = map(db.session.merge, (qn2, org))
        qb2 = QuestionnaireBank(
            name='Test Questionnaire Bank 2',
            classification='baseline',
            research_protocol_id=rp2_id,
            start='{"days": 0}',
            overdue='{"days": 7}',
            expired='{"days": 90}')
        qbq2 = QuestionnaireBankQuestionnaire(questionnaire=qn2, rank=0)
        qb2.questionnaires.append(qbq2)

        org.research_protocols.append(rp2)

        with SessionScope(db):
            db.session.add(qb2)
            db.session.add(org)
            db.session.commit()
        qb2 = db.session.merge(qb2)

        # outdated QB/RP should be used as long as User has in-progress QNR
        self.test_user = db.session.merge(self.test_user)
        qb_stat = QB_Status(self.test_user, now)
        qb = qb_stat.current_qbd().questionnaire_bank
        assert qb.name == 'Test Questionnaire Bank'
        assert qb.research_protocol.name == 'proto'

        # completing QNR should result in completed status
        # shouldn't pick up new protocol till next iteration
        mock_qr(
            instrument_id='epic26', status='completed',
            timestamp=now, qb=qb)

        self.test_user = db.session.merge(self.test_user)
        invalidate_users_QBT(TEST_USER_ID)
        qb_stat = QB_Status(self.test_user, now)
        qb = qb_stat.current_qbd().questionnaire_bank
        assert qb.name == 'Test Questionnaire Bank'
        assert qb_stat.overall_status == OverallStatus.completed
Ejemplo n.º 22
0
    def test_outdated_done_indef(self):
        """Confirm completed indefinite counts after RP switch"""

        # boiler plate to create baseline and indef with retired RP
        yesterday = now - timedelta(days=1)
        weekago = now - timedelta(weeks=1)
        org, rp2, rp2_id = self.setup_org_n_rp(org_name='testorg',
                                               rp_name='v2',
                                               retired_as_of=yesterday)
        org, rp3, rp3_id = self.setup_org_n_rp(org=org, rp_name='v3')
        org_id = org.id
        self.promote_user(role_name=ROLE.PATIENT.value)
        self.test_user = db.session.merge(self.test_user)
        self.test_user.organizations.append(org)
        audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        uc = UserConsent(user_id=TEST_USER_ID,
                         organization_id=org_id,
                         audit=audit,
                         agreement_url='http://no.com',
                         acceptance_date=weekago)
        with SessionScope(db):
            db.session.add(audit)
            db.session.add(uc)
            db.session.commit()

        self.setup_qb(questionnaire_name='epic23',
                      qb_name='baseline v2',
                      classification='baseline',
                      rp_id=rp2_id)
        self.setup_qb(questionnaire_name='epic26',
                      qb_name='baseline v3',
                      classification='baseline',
                      rp_id=rp3_id)
        qb2_indef = self.setup_qb(questionnaire_name='irondemog',
                                  qb_name='indef v2',
                                  classification='indefinite',
                                  rp_id=rp2_id)
        self.setup_qb(questionnaire_name='irondemog_v3',
                      qb_name='indef v3',
                      classification='indefinite',
                      rp_id=rp3_id)

        # for today, should get the v3 baseline
        user = db.session.merge(self.test_user)
        a_s = QB_Status(user=user, as_of_date=now)
        assert ([
            'epic26', 'irondemog_v3'
        ] == a_s.instruments_needing_full_assessment(classification='all'))

        # create done QNR for indefinite dated prior to rp transition
        # belonging to older qb - confirm that clears indef work as of then
        mock_qr('irondemog', timestamp=weekago, qb=qb2_indef)
        user = db.session.merge(self.test_user)
        invalidate_users_QBT(user_id=TEST_USER_ID)
        a_s = QB_Status(user=user, as_of_date=weekago)
        assert (a_s.instruments_needing_full_assessment(
            classification='indefinite') == [])

        # move forward in time; user should no longer need indefinite, even
        # tho RP changed
        qb2_indef = db.session.merge(qb2_indef)
        a_s = QB_Status(user=user, as_of_date=now)
        assert qb2_indef == a_s.current_qbd(
            classification='indefinite').questionnaire_bank
        assert (a_s.instruments_needing_full_assessment(
            classification='indefinite') == [])
        assert (a_s.instruments_needing_full_assessment(
            classification='all') == ['epic26'])