Ejemplo n.º 1
0
    def test_new_ciq_activity_events(self):
        tconnect = TConnectApi()

        # datetimes are unused by the API fake
        start = datetime.datetime(2021, 5, 1, 0, 0)
        end = datetime.datetime(2021, 5, 3, 0, 0)

        def fake_therapy_timeline(time_start, time_end):
            self.assertEqual(time_start, start)
            self.assertEqual(time_end, end)

            return {
                **TestBasalSync.base,
                "events": [{
                    "duration": 1200,
                    "eventType": 2, # Exercise
                    "continuation": None, 
                    "timeZoneId": "America/Los_Angeles",
                    "x": 1619901912 # 2021-05-01 13:45:12-04:00
                }, {
                    "duration": 30661,
                    "eventType": 1, # Sleep
                    "continuation": None,
                    "timeZoneId": "America/Los_Angeles",
                    "x": 1619992000 # 2021-05-02 14:46:40-04:00
                }]
            }

        tconnect.controliq.therapy_timeline = fake_therapy_timeline
        tconnect.ws2.therapy_timeline_csv = self.stub_therapy_timeline_csv
        tconnect.ws2.basalsuspension = self.stub_ws2_basalsuspension

        nightscout = NightscoutApi()

        nightscout.last_uploaded_entry = self.stub_last_uploaded_entry
        nightscout.last_uploaded_activity = self.stub_last_uploaded_activity

        process_time_range(tconnect, nightscout, start, end, pretend=False, features=[PUMP_EVENTS])

        self.assertEqual(len(nightscout.uploaded_entries["treatments"]), 2)
        self.assertDictEqual(dict(nightscout.uploaded_entries), {
            "treatments": [
                NightscoutEntry.activity(created_at="2021-05-01 13:45:12-04:00", duration=20, reason="Exercise", event_type=EXERCISE_EVENTTYPE),
                NightscoutEntry.activity(created_at="2021-05-02 14:46:40-04:00", duration=30661/60, reason="Sleep", event_type=SLEEP_EVENTTYPE),
        ]})
        self.assertDictEqual(nightscout.put_entries, {})
        self.assertListEqual(nightscout.deleted_entries, [])
Ejemplo n.º 2
0
    def test_existing_ciq_activity_events(self):
        tconnect = TConnectApi()

        # datetimes are unused by the API fake
        start = datetime.datetime(2021, 5, 1, 0, 0)
        end = datetime.datetime(2021, 5, 3, 0, 0)

        def fake_therapy_timeline(time_start, time_end):
            self.assertEqual(time_start, start)
            self.assertEqual(time_end, end)

            return {
                **TestBasalSync.base,
                "events": [{
                    "duration": 1200,
                    "eventType": 2, # Exercise
                    "continuation": None, 
                    "timeZoneId": "America/Los_Angeles",
                    "x": 1619901912 # 2021-05-01 13:45:12-04:00
                }, {
                    "duration": 4200, # Currently 60 mins (3600), changing to 70 mins (4200)
                    "eventType": 1, # Sleep
                    "continuation": None,
                    "timeZoneId": "America/Los_Angeles",
                    "x": 1619992000 # 2021-05-02 14:46:40-04:00
                }]
            }

        tconnect.controliq.therapy_timeline = fake_therapy_timeline
        tconnect.ws2.therapy_timeline_csv = self.stub_therapy_timeline_csv
        tconnect.ws2.basalsuspension = self.stub_ws2_basalsuspension

        nightscout = NightscoutApi()

        def fake_last_uploaded_entry(event_type):
            if event_type == "Sleep":
                return {
                    "created_at": "2021-05-02 14:46:40-04:00",
                    "duration": 60,
                    "_id": "old_sleep"
                }
            elif event_type == "Exercise":
                return {
                    "created_at": "2021-05-01 13:45:12-04:00",
                    "duration": 20,
                    "_id": "exercise"
                }
            return self.stub_last_uploaded_entry()

        nightscout.last_uploaded_entry = fake_last_uploaded_entry
        nightscout.last_uploaded_activity = self.stub_last_uploaded_activity

        process_time_range(tconnect, nightscout, start, end, pretend=False, features=[PUMP_EVENTS])

        self.assertEqual(len(nightscout.uploaded_entries["treatments"]), 1)
        self.assertDictEqual(dict(nightscout.uploaded_entries), {
            "treatments": [
                # Already exists:
                # NightscoutEntry.activity(created_at="2021-05-01 13:45:12-04:00", duration=20, reason="Exercise", event_type=EXERCISE_EVENTTYPE),
                # Updated event duration:
                NightscoutEntry.activity(created_at="2021-05-02 14:46:40-04:00", duration=70, reason="Sleep", event_type=SLEEP_EVENTTYPE),
        ]})
        self.assertDictEqual(nightscout.put_entries, {})
        self.assertEqual(len(nightscout.deleted_entries), 1)
        self.assertListEqual(nightscout.deleted_entries, [
            "treatments/old_sleep"
        ])