Example #1
0
 def test_schedule_bad_crf_model(self):
     site_visit_schedules._registry = {}
     visit_schedule = VisitSchedule(
         name="visit_schedule",
         verbose_name="Visit Schedule",
         offstudy_model="visit_schedule_app.subjectoffstudy",
         death_report_model="visit_schedule_app.deathreport",
     )
     schedule = Schedule(
         name="schedule",
         onschedule_model="visit_schedule_app.onschedule",
         offschedule_model="visit_schedule_app.offschedule",
         appointment_model="edc_appointment.appointment",
         consent_model="visit_schedule_app.subjectconsent",
     )
     crfs = FormsCollection(
         Crf(show_order=10, model="blah.CrfOne"),
         Crf(show_order=20, model="blah.CrfTwo"),
         Crf(show_order=30, model="blah.CrfThree"),
     )
     visit = Visit(
         code="1000",
         rbase=relativedelta(days=0),
         rlower=relativedelta(days=0),
         rupper=relativedelta(days=6),
         facility_name="default",
         crfs=crfs,
         timepoint=1,
     )
     schedule.add_visit(visit)
     visit_schedule.add_schedule(schedule)
     site_visit_schedules.register(visit_schedule)
     errors = visit_schedule_check(app_configs=django_apps.get_app_configs())
     self.assertEqual(len(errors), 1)
     self.assertEqual("edc_visit_schedule.visits", errors[0].id)
 def test_add_visits_duplicate_title(self):
     schedule = Schedule(
         name="schedule",
         onschedule_model="visit_schedule_app.onschedule",
         offschedule_model="visit_schedule_app.offschedule",
         consent_model="visit_schedule_app.subjectconsent",
         appointment_model="edc_appointment.appointment",
     )
     visit = Visit(
         code=str(0),
         title="erik",
         timepoint=0,
         rbase=relativedelta(days=0),
         rlower=relativedelta(days=0),
         rupper=relativedelta(days=6),
     )
     schedule.add_visit(visit=visit)
     visit = Visit(
         code=str(1),
         title="erik",
         timepoint=1,
         rbase=relativedelta(days=1),
         rlower=relativedelta(days=0),
         rupper=relativedelta(days=6),
     )
     self.assertRaises(AlreadyRegisteredVisit, schedule.add_visit, visit=visit)
 def test_add_visits(self):
     schedule = Schedule(
         name="schedule",
         onschedule_model="visit_schedule_app.onschedule",
         offschedule_model="visit_schedule_app.offschedule",
         consent_model="visit_schedule_app.subjectconsent",
         appointment_model="edc_appointment.appointment",
     )
     for i in range(0, 5):
         visit = Visit(
             code=str(i),
             timepoint=i,
             rbase=relativedelta(days=i),
             rlower=relativedelta(days=0),
             rupper=relativedelta(days=6),
         )
         try:
             schedule.add_visit(visit=visit)
         except AlreadyRegisteredVisit as e:
             self.fail(f"Exception unexpectedly raised. Got {e}")
from edc_visit_schedule.visit import FormsCollection, Crf, Visit
from edc_visit_schedule.visit_schedule import VisitSchedule

from dateutil.relativedelta import relativedelta

crfs = FormsCollection(
    Crf(show_order=1, model=f'edc_reference.crfone', required=True), )

visit0 = Visit(code='1000',
               title='Day 1',
               timepoint=0,
               rbase=relativedelta(days=0),
               rlower=relativedelta(days=0),
               rupper=relativedelta(days=6),
               crfs=crfs,
               facility_name='default')

schedule = Schedule(name='schedule',
                    onschedule_model='edc_reference.onschedule',
                    offschedule_model='edc_reference.offschedule',
                    appointment_model='edc_appointment.appointment',
                    consent_model='edc_reference.subjectconsent')

schedule.add_visit(visit0)

visit_schedule = VisitSchedule(name='visit_schedule',
                               offstudy_model='edc_reference.subjectoffstudy',
                               death_report_model='edc_reference.deathreport')

visit_schedule.add_schedule(schedule)
class TestScheduleWithVisits(TestCase):
    def setUp(self):
        self.schedule = Schedule(
            name="schedule",
            onschedule_model="visit_schedule_app.onschedule",
            offschedule_model="visit_schedule_app.offschedule",
            consent_model="visit_schedule_app.subjectconsent",
            appointment_model="edc_appointment.appointment",
        )

    def test_order(self):
        for i in [3, 5, 1, 0, 2, 4]:
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        self.assertEqual(
            [v.timepoint for v in self.schedule.visits.values()], [0, 1, 2, 3, 4, 5]
        )

    def test_first_visit(self):
        for i in range(1, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        visit = Visit(
            code=str(0),
            timepoint=0,
            rbase=relativedelta(days=0),
            rlower=relativedelta(days=0),
            rupper=relativedelta(days=6),
        )
        self.schedule.add_visit(visit=visit)
        self.assertEqual(self.schedule.visits.first, visit)

    def test_last_visit(self):
        for i in range(0, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        visit = Visit(
            code=str(6),
            timepoint=6,
            rbase=relativedelta(days=6),
            rlower=relativedelta(days=0),
            rupper=relativedelta(days=6),
        )
        self.schedule.add_visit(visit=visit)
        self.assertEqual(self.schedule.visits.last, visit)

    def test_get_visit(self):
        for i in range(0, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        visit = self.schedule.visits.get("3")
        self.assertEqual(visit.code, "3")
        visit = self.schedule.visits.get("BLAH")
        self.assertIsNone(visit)

    def test_previous_visit(self):
        for i in range(0, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        visit = self.schedule.visits.previous("3")
        self.assertEqual(visit.code, "2")

    def test_next_visit(self):
        for i in range(0, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        visit = self.schedule.visits.next("3")
        self.assertEqual(visit.code, "4")

    def test_previous_visit_none(self):
        for i in range(0, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        self.assertIsNone(self.schedule.visits.previous("0"))

    def test_next_visit_none(self):
        for i in range(0, 5):
            visit = Visit(
                code=str(i),
                timepoint=i,
                rbase=relativedelta(days=i),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        self.assertIsNone(self.schedule.visits.next("5"))

    def test_visit_dates(self):
        dt = get_utcnow()
        for index, seq in enumerate(range(0, 5)):
            visit = Visit(
                code=str(seq),
                timepoint=seq * (index + 1),
                rbase=relativedelta(days=seq * (index + 1)),
                rlower=relativedelta(days=0),
                rupper=relativedelta(days=6),
            )
            self.schedule.add_visit(visit=visit)
        index = 0
        for k, v in self.schedule.visits.timepoint_dates(dt=dt).items():
            self.assertEqual(v - dt, timedelta(index * (index + 1)), msg=k)
            index += 1
from dateutil.relativedelta import relativedelta

from edc_visit_schedule.schedule import Schedule
from edc_visit_schedule.visit import Visit

from ..constants import E0
from .crfs_ess import crfs_ess
from .requisitions import requisitions

app_label = 'bcpp_subject'

# schedule for new participants
ess_schedule = Schedule(name='ess_schedule',
                        title='ESS',
                        enrollment_model=f'{app_label}.enrollmentess',
                        disenrollment_model=f'{app_label}.disenrollmentess')

ess_visit = Visit(code=E0,
                  title='End-of-study Household Survey',
                  timepoint=0,
                  rbase=relativedelta(days=0),
                  rlower=relativedelta(days=0),
                  rupper=relativedelta(days=364),
                  requisitions=requisitions.forms,
                  crfs=crfs_ess.forms)

ess_schedule.add_visit(visit=ess_visit)
ahs_schedule = Schedule(
    name='ahs_schedule',
    title='AHS',
    enrollment_model=f'{app_label}.enrollmentahs',
    disenrollment_model=f'{app_label}.disenrollmentahs',
)

ahs_visit1 = Visit(code=T1,
                   title='1st Annual Survey',
                   timepoint=1,
                   rbase=relativedelta(years=1),
                   rlower=relativedelta(years=1),
                   rupper=relativedelta(years=2),
                   base_interval_unit=YEARS,
                   requisitions=requisitions.forms,
                   crfs=crfs_ahs.forms)

ahs_visit2 = Visit(code=T2,
                   title='2nd Annual Survey',
                   timepoint=2,
                   rbase=relativedelta(years=2),
                   rlower=relativedelta(years=2),
                   rupper=relativedelta(years=3),
                   base_interval_unit=YEARS,
                   requisitions=requisitions.forms,
                   crfs=crfs_ahs.forms)

ahs_schedule.add_visit(visit=ahs_visit1)
ahs_schedule.add_visit(visit=ahs_visit2)
    timepoint=3,
    rbase=relativedelta(days=3),
    rlower=relativedelta(days=0),
    rupper=relativedelta(days=6),
    crfs=crfs,
)

schedule = Schedule(
    name="schedule",
    onschedule_model="visit_schedule_app.onschedule",
    offschedule_model="visit_schedule_app.offschedule",
    appointment_model="edc_appointment.appointment",
    consent_model="visit_schedule_app.subjectconsent",
)

schedule.add_visit(visit0)
schedule.add_visit(visit1)
schedule.add_visit(visit2)
schedule.add_visit(visit3)

visit_schedule = VisitSchedule(
    name="visit_schedule",
    offstudy_model="visit_schedule_app.subjectoffstudy",
    death_report_model="visit_schedule_app.deathreport",
)

visit_schedule.add_schedule(schedule)

# visit_schedule2
schedule2 = Schedule(
    name="schedule2",
Example #9
0
from dateutil.relativedelta import relativedelta

from edc_visit_schedule.schedule import Schedule
from edc_visit_schedule.visit import Visit

from ..constants import A0
from .crfs_ess import crfs_ess
from .requisitions import requisitions

app_label = 'bcpp_subject'

ano_schedule = Schedule(
    name='ano_schedule',
    title='Anonymous',
    enrollment_model=f'{app_label}.enrollmentano',
    disenrollment_model=f'{app_label}.disenrollmentano')

ano_visit = Visit(
    code=A0,
    title='Anonymous Survey',
    timepoint=0,
    rbase=relativedelta(days=0),
    rlower=relativedelta(days=0),
    rupper=relativedelta(days=364),
    requisitions=requisitions.forms,
    crfs=crfs_ess.forms)

ano_schedule.add_visit(visit=ano_visit)
Example #10
0
from dateutil.relativedelta import relativedelta

from edc_visit_schedule.schedule import Schedule
from edc_visit_schedule.visit import Visit

from ..constants import T0
from .crfs_bhs import crfs_bhs
from .requisitions import requisitions

app_label = 'bcpp_subject'

bhs_schedule = Schedule(
    name='bhs_schedule',
    title='BHS',
    enrollment_model=f'{app_label}.enrollmentbhs',
    disenrollment_model=f'{app_label}.disenrollmentbhs',
)

bhs_visit = Visit(code=T0,
                  title='Baseline Survey',
                  timepoint=0,
                  base_interval=0,
                  rbase=relativedelta(days=0),
                  rlower=relativedelta(days=0),
                  rupper=relativedelta(days=364),
                  requisitions=requisitions.forms,
                  crfs=crfs_bhs.forms)

bhs_schedule.add_visit(visit=bhs_visit)
class TestVisitSchedule4(SiteTestCaseMixin, TestCase):
    def setUp(self):
        v1_consent = Consent(
            "visit_schedule_app.subjectconsent",
            version="1",
            start=Protocol().study_open_datetime,
            end=Protocol().study_close_datetime,
            age_min=18,
            age_is_adult=18,
            age_max=64,
            gender=[MALE, FEMALE],
        )

        import_holidays()
        site_consents.registry = {}
        site_consents.register(v1_consent)
        self.visit_schedule = VisitSchedule(
            name="visit_schedule",
            verbose_name="Visit Schedule",
            offstudy_model="visit_schedule_app.subjectoffstudy",
            death_report_model="visit_schedule_app.deathreport",
        )

        self.schedule = Schedule(
            name="schedule",
            onschedule_model="visit_schedule_app.onschedule",
            offschedule_model="visit_schedule_app.offschedule",
            appointment_model="edc_appointment.appointment",
            consent_model="visit_schedule_app.subjectconsent",
        )

        visit = Visit(
            code="1000",
            rbase=relativedelta(days=0),
            rlower=relativedelta(days=0),
            rupper=relativedelta(days=6),
            timepoint=1,
        )
        self.schedule.add_visit(visit)
        visit = Visit(
            code="1010",
            rbase=relativedelta(days=28),
            rlower=relativedelta(days=0),
            rupper=relativedelta(days=6),
            timepoint=2,
        )
        self.schedule.add_visit(visit)

        self.visit_schedule.add_schedule(self.schedule)
        site_visit_schedules._registry = {}
        site_visit_schedules.register(self.visit_schedule)

        site_reference_configs.registry = {}
        site_reference_configs.register_from_visit_schedule(visit_models={
            "edc_appointment.appointment":
            "visit_schedule_app.subjectvisit"
        })

        self.subject_consent = SubjectConsent.objects.create(
            subject_identifier="12345",
            consent_datetime=get_utcnow() - relativedelta(seconds=1),
            dob=date(1995, 1, 1),
            identity="11111",
            confirm_identity="11111",
        )
        self.subject_identifier = self.subject_consent.subject_identifier
        onschedule_datetime = get_utcnow() - relativedelta(years=4)
        _, schedule = site_visit_schedules.get_by_onschedule_model(
            "visit_schedule_app.onschedule")
        schedule.put_on_schedule(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )
        self.appointments = Appointment.objects.all()

    def test_is_baseline(self):
        subject_visit_0 = SubjectVisit.objects.create(
            appointment=self.appointments[0],
            subject_identifier=self.subject_identifier,
            report_datetime=self.appointments[0].appt_datetime,
            reason=SCHEDULED,
        )
        subject_visit_1 = SubjectVisit.objects.create(
            appointment=self.appointments[1],
            subject_identifier=self.subject_identifier,
            report_datetime=self.appointments[1].appt_datetime,
            reason=SCHEDULED,
        )

        self.assertTrue(is_baseline(subject_visit_0))
        self.assertFalse(is_baseline(subject_visit_1))
Example #12
0
class TestVisitSchedule3(SiteTestCaseMixin, TestCase):
    def setUp(self):

        v1_consent = Consent(
            "visit_schedule_app.subjectconsent",
            version="1",
            start=Protocol().study_open_datetime,
            end=Protocol().study_close_datetime,
            age_min=18,
            age_is_adult=18,
            age_max=64,
            gender=[MALE, FEMALE],
        )

        import_holidays()
        site_consents.registry = {}
        site_consents.register(v1_consent)

        self.visit_schedule = VisitSchedule(
            name="visit_schedule",
            verbose_name="Visit Schedule",
            offstudy_model="visit_schedule_app.subjectoffstudy",
            death_report_model="visit_schedule_app.deathreport",
        )

        self.schedule = Schedule(
            name="schedule",
            onschedule_model="visit_schedule_app.onschedule",
            offschedule_model="visit_schedule_app.offschedule",
            appointment_model="edc_appointment.appointment",
            consent_model="visit_schedule_app.subjectconsent",
        )

        visit = Visit(
            code="1000",
            rbase=relativedelta(days=0),
            rlower=relativedelta(days=0),
            rupper=relativedelta(days=6),
            timepoint=1,
        )
        self.schedule.add_visit(visit)
        self.visit_schedule.add_schedule(self.schedule)
        site_visit_schedules._registry = {}
        site_visit_schedules.register(self.visit_schedule)

        site_reference_configs.registry = {}
        site_reference_configs.register_from_visit_schedule(visit_models={
            "edc_appointment.appointment":
            "visit_schedule_app.subjectvisit"
        })

        self.subject_consent = SubjectConsent.objects.create(
            subject_identifier="12345",
            consent_datetime=get_utcnow() - relativedelta(seconds=1),
            dob=date(1995, 1, 1),
            identity="11111",
            confirm_identity="11111",
        )
        self.subject_identifier = self.subject_consent.subject_identifier

    def test_put_on_schedule_creates_history(self):
        self.schedule.put_on_schedule(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=get_utcnow())
        self.assertEqual(
            SubjectScheduleHistory.objects.filter(
                subject_identifier=self.subject_identifier).count(),
            1,
        )

    def test_onschedule_creates_history(self):
        onschedule_model_obj = OnSchedule.objects.create(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=get_utcnow())
        self.assertEqual(
            SubjectScheduleHistory.objects.filter(
                subject_identifier=self.subject_identifier).count(),
            1,
        )
        history_obj = SubjectScheduleHistory.objects.get(
            subject_identifier=self.subject_identifier)
        self.assertIsNone(history_obj.__dict__.get("offschedule_datetime"))
        self.assertEqual(
            history_obj.__dict__.get("onschedule_datetime"),
            onschedule_model_obj.onschedule_datetime,
        )
        self.assertEqual(history_obj.__dict__.get("schedule_status"),
                         ON_SCHEDULE)

    def test_can_create_offschedule_with_onschedule(self):
        # signal puts on schedule
        OnSchedule.objects.create(subject_identifier=self.subject_identifier,
                                  onschedule_datetime=get_utcnow())
        try:
            OffSchedule.objects.create(
                subject_identifier=self.subject_identifier,
                offschedule_datetime=get_utcnow(),
            )
        except Exception as e:
            self.fail(f"Exception unexpectedly raised. Got {e}.")

    def test_creates_appointments(self):
        # signal puts on schedule
        onschedule_datetime = get_utcnow() - relativedelta(years=4)
        OnSchedule.objects.create(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )
        self.assertGreater(Appointment.objects.all().count(), 0)

    def test_creates_appointments_starting_with_onschedule_datetime(self):
        """Will pass as long as this is not a holiday"""
        onschedule_datetime = datetime(2017,
                                       7,
                                       19,
                                       15,
                                       29,
                                       44,
                                       903192,
                                       tzinfo=tzutc())
        _, schedule = site_visit_schedules.get_by_onschedule_model(
            "visit_schedule_app.onschedule")
        schedule.put_on_schedule(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )
        appointment = Appointment.objects.all().order_by(
            "appt_datetime").first()
        self.assertEqual(appointment.appt_datetime, onschedule_datetime)

    def test_cannot_create_offschedule_without_onschedule(self):
        self.assertEqual(
            OnSchedule.objects.filter(
                subject_identifier=self.subject_identifier).count(),
            0,
        )
        self.assertRaises(
            NotOnScheduleError,
            OffSchedule.objects.create,
            subject_identifier=self.subject_identifier,
        )

    def test_cannot_create_offschedule_before_onschedule(self):
        onschedule_datetime = get_utcnow() - relativedelta(years=4)
        OnSchedule.objects.create(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )
        self.assertRaises(
            InvalidOffscheduleDate,
            OffSchedule.objects.create,
            subject_identifier=self.subject_identifier,
            offschedule_datetime=onschedule_datetime - relativedelta(months=2),
        )

    def test_cannot_create_offschedule_before_last_visit(self):
        onschedule_datetime = get_utcnow() - relativedelta(years=4)
        _, schedule = site_visit_schedules.get_by_onschedule_model(
            "visit_schedule_app.onschedule")
        schedule.put_on_schedule(
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )
        appointments = Appointment.objects.all()
        SubjectVisit.objects.create(
            appointment=appointments[0],
            subject_identifier=self.subject_identifier,
            report_datetime=appointments[0].appt_datetime,
            reason=SCHEDULED,
        )
        self.assertRaises(
            InvalidOffscheduleDate,
            schedule.take_off_schedule,
            subject_identifier=self.subject_identifier,
            offschedule_datetime=appointments[0].appt_datetime -
            relativedelta(days=1),
        )

    def test_cannot_put_on_schedule_if_visit_schedule_not_registered_subject(
            self):
        onschedule_datetime = get_utcnow() - relativedelta(years=4)
        _, schedule = site_visit_schedules.get_by_onschedule_model(
            "visit_schedule_app.onschedule")
        RegisteredSubject.objects.all().delete()
        self.assertRaises(
            UnknownSubjectError,
            schedule.put_on_schedule,
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )

    def test_cannot_put_on_schedule_if_visit_schedule_not_consented(self):
        onschedule_datetime = get_utcnow() - relativedelta(years=4)
        _, schedule = site_visit_schedules.get_by_onschedule_model(
            "visit_schedule_app.onschedule")
        SubjectConsent.objects.all().delete()
        self.assertRaises(
            NotConsentedError,
            schedule.put_on_schedule,
            subject_identifier=self.subject_identifier,
            onschedule_datetime=onschedule_datetime,
        )

    def test_cannot_put_on_schedule_if_schedule_not_added(self):
        self.assertRaises(SiteVisitScheduleError,
                          OnScheduleThree.objects.create)