|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from tconnectsync.sync.tandemsource.autoupdate import TandemSourceAutoupdate |
| 7 | + |
| 8 | +from ...secrets import build_secrets |
| 9 | + |
| 10 | + |
| 11 | +class FakeChooseDevice: |
| 12 | + def __init__(self, secret, tconnect): |
| 13 | + self.secret = secret |
| 14 | + self.tconnect = tconnect |
| 15 | + |
| 16 | + def choose(self): |
| 17 | + return { |
| 18 | + 'tconnectDeviceId': 'test-device-123', |
| 19 | + 'maxDateWithEvents': '2025-11-18T13:00:00-05:00', |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | +class FakeProcessTimeRange: |
| 24 | + def __init__(self, tconnect, nightscout, tconnectDevice, pretend, secret, features=None): |
| 25 | + self.tconnect = tconnect |
| 26 | + self.nightscout = nightscout |
| 27 | + self.tconnectDevice = tconnectDevice |
| 28 | + self.pretend = pretend |
| 29 | + self.secret = secret |
| 30 | + self.features = features |
| 31 | + |
| 32 | + def process(self, time_start, time_end): |
| 33 | + return 0, None |
| 34 | + |
| 35 | + |
| 36 | +class TestTandemSourceAutoupdate(unittest.TestCase): |
| 37 | + def setUp(self): |
| 38 | + self.secret = build_secrets( |
| 39 | + AUTOUPDATE_MAX_LOOP_INVOCATIONS=2, |
| 40 | + AUTOUPDATE_DEFAULT_SLEEP_SECONDS=0, |
| 41 | + AUTOUPDATE_USE_FIXED_SLEEP=True, |
| 42 | + AUTOUPDATE_MAX_SLEEP_SECONDS=0, |
| 43 | + AUTOUPDATE_NO_DATA_FAILURE_MINUTES=9999, |
| 44 | + AUTOUPDATE_FAILURE_MINUTES=9999, |
| 45 | + AUTOUPDATE_UNEXPECTED_NO_INDEX_SLEEP_SECONDS=0, |
| 46 | + AUTOUPDATE_RESTART_ON_FAILURE=False, |
| 47 | + ) |
| 48 | + |
| 49 | + def test_process_does_not_crash_when_no_events_are_found(self): |
| 50 | + autoupdate = TandemSourceAutoupdate(self.secret) |
| 51 | + |
| 52 | + with mock.patch('tconnectsync.sync.tandemsource.autoupdate.ChooseDevice', FakeChooseDevice), \ |
| 53 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.ProcessTimeRange', FakeProcessTimeRange), \ |
| 54 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.time', side_effect=[1000, 1001, 1002]), \ |
| 55 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.sleep', return_value=None), \ |
| 56 | + self.assertLogs('tconnectsync.sync.tandemsource.autoupdate', level='INFO') as logs: |
| 57 | + result = autoupdate.process(object(), object(), pretend=False) |
| 58 | + |
| 59 | + self.assertEqual(result, 0) |
| 60 | + self.assertTrue(any('No new reported tandemsource data.' in message for message in logs.output)) |
| 61 | + |
| 62 | + def test_process_does_not_crash_in_pretend_mode_without_successful_update_time(self): |
| 63 | + autoupdate = TandemSourceAutoupdate(self.secret) |
| 64 | + |
| 65 | + with mock.patch('tconnectsync.sync.tandemsource.autoupdate.ChooseDevice', FakeChooseDevice), \ |
| 66 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.time', side_effect=[2000, 2001, 2002]), \ |
| 67 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.sleep', return_value=None), \ |
| 68 | + self.assertLogs('tconnectsync.sync.tandemsource.autoupdate', level='INFO') as logs: |
| 69 | + result = autoupdate.process(object(), object(), pretend=True) |
| 70 | + |
| 71 | + self.assertEqual(result, 0) |
| 72 | + self.assertIsNone(autoupdate.last_successful_process_time_range) |
| 73 | + self.assertTrue(any('No new reported tandemsource data.' in message for message in logs.output)) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + unittest.main() |
0 commit comments