def test_deletes_patient_task_schedule(self) -> None:
        schedule = TaskSchedule()
        schedule.group_id = self.group.id
        self.dbsession.add(schedule)
        self.dbsession.flush()

        item = TaskScheduleItem()
        item.schedule_id = schedule.id
        item.task_table_name = "ace3"
        item.due_from = pendulum.Duration(days=30)
        item.due_by = pendulum.Duration(days=60)
        self.dbsession.add(item)
        self.dbsession.flush()

        patient = self.create_patient()

        pts = PatientTaskSchedule()
        pts.schedule_id = schedule.id
        pts.patient_pk = patient.pk
        self.dbsession.add(pts)
        self.dbsession.commit()

        self.assertIsNotNone(
            self.dbsession.query(TaskSchedule).filter(
                TaskSchedule.id == schedule.id).one_or_none())
        self.assertIsNotNone(
            self.dbsession.query(TaskScheduleItem).filter(
                TaskScheduleItem.id == item.id).one_or_none())
        self.assertIsNotNone(
            self.dbsession.query(PatientTaskSchedule).filter(
                PatientTaskSchedule.id == pts.id).one_or_none())

        self.dbsession.delete(patient)
        self.dbsession.commit()

        self.assertIsNotNone(
            self.dbsession.query(TaskSchedule).filter(
                TaskSchedule.id == schedule.id).one_or_none())
        self.assertIsNotNone(
            self.dbsession.query(TaskScheduleItem).filter(
                TaskScheduleItem.id == item.id).one_or_none())

        self.assertIsNone(
            self.dbsession.query(PatientTaskSchedule).filter(
                PatientTaskSchedule.id == pts.id).one_or_none())
    def test_deleting_deletes_related_objects(self) -> None:
        schedule = TaskSchedule()
        schedule.group_id = self.group.id
        self.dbsession.add(schedule)
        self.dbsession.flush()

        item = TaskScheduleItem()
        item.schedule_id = schedule.id
        item.task_table_name = "ace3"
        item.due_from = Duration(days=30)
        item.due_by = Duration(days=60)
        self.dbsession.add(item)
        self.dbsession.flush()

        patient = self.create_patient()

        pts = PatientTaskSchedule()
        pts.schedule_id = schedule.id
        pts.patient_pk = patient.pk
        self.dbsession.add(pts)
        self.dbsession.flush()

        email = Email()
        self.dbsession.add(email)
        self.dbsession.flush()

        pts_email = PatientTaskScheduleEmail()
        pts_email.email_id = email.id
        pts_email.patient_task_schedule_id = pts.id
        self.dbsession.add(pts_email)
        self.dbsession.commit()

        self.assertIsNotNone(
            self.dbsession.query(TaskScheduleItem).filter(
                TaskScheduleItem.id == item.id).one_or_none())
        self.assertIsNotNone(
            self.dbsession.query(PatientTaskSchedule).filter(
                PatientTaskSchedule.id == pts.id).one_or_none())
        self.assertIsNotNone(
            self.dbsession.query(PatientTaskScheduleEmail).filter(
                PatientTaskScheduleEmail.patient_task_schedule_id ==
                pts.id).one_or_none())
        self.assertIsNotNone(
            self.dbsession.query(Email).filter(
                Email.id == email.id).one_or_none())

        self.dbsession.delete(schedule)
        self.dbsession.commit()

        self.assertIsNone(
            self.dbsession.query(TaskScheduleItem).filter(
                TaskScheduleItem.id == item.id).one_or_none())
        self.assertIsNone(
            self.dbsession.query(PatientTaskSchedule).filter(
                PatientTaskSchedule.id == pts.id).one_or_none())
        self.assertIsNone(
            self.dbsession.query(PatientTaskScheduleEmail).filter(
                PatientTaskScheduleEmail.patient_task_schedule_id ==
                pts.id).one_or_none())
        self.assertIsNone(
            self.dbsession.query(Email).filter(
                Email.id == email.id).one_or_none())
Beispiel #3
0
    def test_returns_task_schedules(self) -> None:
        from pendulum import DateTime as Pendulum, Duration, local, parse

        from camcops_server.cc_modules.cc_taskindex import (
            PatientIdNumIndexEntry,
            TaskIndexEntry,
        )
        from camcops_server.cc_modules.cc_taskschedule import (
            PatientTaskSchedule,
            TaskSchedule,
            TaskScheduleItem,
        )
        from camcops_server.tasks.bmi import Bmi

        schedule1 = TaskSchedule()
        schedule1.group_id = self.group.id
        schedule1.name = "Test 1"
        self.dbsession.add(schedule1)

        schedule2 = TaskSchedule()
        schedule2.group_id = self.group.id
        self.dbsession.add(schedule2)
        self.dbsession.commit()

        item1 = TaskScheduleItem()
        item1.schedule_id = schedule1.id
        item1.task_table_name = "phq9"
        item1.due_from = Duration(days=0)
        item1.due_by = Duration(days=7)
        self.dbsession.add(item1)

        item2 = TaskScheduleItem()
        item2.schedule_id = schedule1.id
        item2.task_table_name = "bmi"
        item2.due_from = Duration(days=0)
        item2.due_by = Duration(days=8)
        self.dbsession.add(item2)

        item3 = TaskScheduleItem()
        item3.schedule_id = schedule1.id
        item3.task_table_name = "phq9"
        item3.due_from = Duration(days=30)
        item3.due_by = Duration(days=37)
        self.dbsession.add(item3)

        item4 = TaskScheduleItem()
        item4.schedule_id = schedule1.id
        item4.task_table_name = "gmcpq"
        item4.due_from = Duration(days=30)
        item4.due_by = Duration(days=38)
        self.dbsession.add(item4)
        self.dbsession.commit()

        patient = self.create_patient()
        idnum = self.create_patient_idnum(
            patient_id=patient.id,
            which_idnum=self.nhs_iddef.which_idnum,
            idnum_value=TEST_NHS_NUMBER,
        )
        PatientIdNumIndexEntry.index_idnum(idnum, self.dbsession)

        server_patient = self.create_patient(as_server_patient=True)
        _ = self.create_patient_idnum(
            patient_id=server_patient.id,
            which_idnum=self.nhs_iddef.which_idnum,
            idnum_value=TEST_NHS_NUMBER,
            as_server_patient=True,
        )

        schedule_1 = PatientTaskSchedule()
        schedule_1.patient_pk = server_patient.pk
        schedule_1.schedule_id = schedule1.id
        schedule_1.settings = {
            "bmi": {"bmi_key": "bmi_value"},
            "phq9": {"phq9_key": "phq9_value"},
        }
        schedule_1.start_datetime = local(2020, 7, 31)
        self.dbsession.add(schedule_1)

        schedule_2 = PatientTaskSchedule()
        schedule_2.patient_pk = server_patient.pk
        schedule_2.schedule_id = schedule2.id
        self.dbsession.add(schedule_2)

        bmi = Bmi()
        self.apply_standard_task_fields(bmi)
        bmi.id = 1
        bmi.height_m = 1.83
        bmi.mass_kg = 67.57
        bmi.patient_id = patient.id
        bmi.when_created = local(2020, 8, 1)
        self.dbsession.add(bmi)
        self.dbsession.commit()
        self.assertTrue(bmi.is_complete())

        TaskIndexEntry.index_task(
            bmi, self.dbsession, indexed_at_utc=Pendulum.utcnow()
        )
        self.dbsession.commit()

        proquint = server_patient.uuid_as_proquint

        # For type checker
        assert proquint is not None
        assert self.other_device.name is not None

        self.req.fake_request_post_from_dict(
            {
                TabletParam.CAMCOPS_VERSION: MINIMUM_TABLET_VERSION,
                TabletParam.DEVICE: self.other_device.name,
                TabletParam.OPERATION: Operations.GET_TASK_SCHEDULES,
                TabletParam.PATIENT_PROQUINT: proquint,
            }
        )
        response = client_api(self.req)
        reply_dict = get_reply_dict_from_response(response)

        self.assertEqual(
            reply_dict[TabletParam.SUCCESS], SUCCESS_CODE, msg=reply_dict
        )

        task_schedules = json.loads(reply_dict[TabletParam.TASK_SCHEDULES])

        self.assertEqual(len(task_schedules), 2)

        s = task_schedules[0]
        self.assertEqual(s[TabletParam.TASK_SCHEDULE_NAME], "Test 1")

        schedule_items = s[TabletParam.TASK_SCHEDULE_ITEMS]
        self.assertEqual(len(schedule_items), 4)

        phq9_1_sched = schedule_items[0]
        self.assertEqual(phq9_1_sched[TabletParam.TABLE], "phq9")
        self.assertEqual(
            phq9_1_sched[TabletParam.SETTINGS], {"phq9_key": "phq9_value"}
        )
        self.assertEqual(
            parse(phq9_1_sched[TabletParam.DUE_FROM]), local(2020, 7, 31)
        )
        self.assertEqual(
            parse(phq9_1_sched[TabletParam.DUE_BY]), local(2020, 8, 7)
        )
        self.assertFalse(phq9_1_sched[TabletParam.COMPLETE])
        self.assertFalse(phq9_1_sched[TabletParam.ANONYMOUS])

        bmi_sched = schedule_items[1]
        self.assertEqual(bmi_sched[TabletParam.TABLE], "bmi")
        self.assertEqual(
            bmi_sched[TabletParam.SETTINGS], {"bmi_key": "bmi_value"}
        )
        self.assertEqual(
            parse(bmi_sched[TabletParam.DUE_FROM]), local(2020, 7, 31)
        )
        self.assertEqual(
            parse(bmi_sched[TabletParam.DUE_BY]), local(2020, 8, 8)
        )
        self.assertTrue(bmi_sched[TabletParam.COMPLETE])
        self.assertFalse(bmi_sched[TabletParam.ANONYMOUS])

        phq9_2_sched = schedule_items[2]
        self.assertEqual(phq9_2_sched[TabletParam.TABLE], "phq9")
        self.assertEqual(
            phq9_2_sched[TabletParam.SETTINGS], {"phq9_key": "phq9_value"}
        )
        self.assertEqual(
            parse(phq9_2_sched[TabletParam.DUE_FROM]), local(2020, 8, 30)
        )
        self.assertEqual(
            parse(phq9_2_sched[TabletParam.DUE_BY]), local(2020, 9, 6)
        )
        self.assertFalse(phq9_2_sched[TabletParam.COMPLETE])
        self.assertFalse(phq9_2_sched[TabletParam.ANONYMOUS])

        # GMCPQ
        gmcpq_sched = schedule_items[3]
        self.assertTrue(gmcpq_sched[TabletParam.ANONYMOUS])