|
10 | 10 | """ |
11 | 11 |
|
12 | 12 | import unittest |
| 13 | +from unittest import mock |
13 | 14 | from unittest.mock import patch |
14 | 15 |
|
15 | 16 | import arrow |
@@ -675,5 +676,70 @@ def test_zero_minutes_disables_the_exit(self): |
675 | 676 | ) |
676 | 677 |
|
677 | 678 |
|
| 679 | +class FakeChooseDevice: |
| 680 | + def __init__(self, secret, tconnect): |
| 681 | + self.secret = secret |
| 682 | + self.tconnect = tconnect |
| 683 | + |
| 684 | + def choose(self): |
| 685 | + return { |
| 686 | + 'tconnectDeviceId': 'test-device-123', |
| 687 | + 'maxDateOfEvents': '2025-11-18T13:00:00-05:00', |
| 688 | + } |
| 689 | + |
| 690 | + |
| 691 | +class FakeProcessTimeRange: |
| 692 | + def __init__(self, tconnect, nightscout, tconnectDevice, pretend, secret, features=None): |
| 693 | + self.tconnect = tconnect |
| 694 | + self.nightscout = nightscout |
| 695 | + self.tconnectDevice = tconnectDevice |
| 696 | + self.pretend = pretend |
| 697 | + self.secret = secret |
| 698 | + self.features = features |
| 699 | + |
| 700 | + def process(self, time_start, time_end): |
| 701 | + return 0, None |
| 702 | + |
| 703 | + |
| 704 | +class TestTandemSourceAutoupdate(unittest.TestCase): |
| 705 | + def setUp(self): |
| 706 | + self.secret = build_secrets( |
| 707 | + AUTOUPDATE_MAX_LOOP_INVOCATIONS=2, |
| 708 | + AUTOUPDATE_DEFAULT_SLEEP_SECONDS=0, |
| 709 | + AUTOUPDATE_USE_FIXED_SLEEP=True, |
| 710 | + AUTOUPDATE_MAX_SLEEP_SECONDS=0, |
| 711 | + AUTOUPDATE_NO_DATA_FAILURE_MINUTES=9999, |
| 712 | + AUTOUPDATE_FAILURE_MINUTES=9999, |
| 713 | + AUTOUPDATE_UNEXPECTED_NO_INDEX_SLEEP_SECONDS=0, |
| 714 | + AUTOUPDATE_RESTART_ON_FAILURE=False, |
| 715 | + ) |
| 716 | + |
| 717 | + def test_process_does_not_crash_when_no_events_are_found(self): |
| 718 | + autoupdate = TandemSourceAutoupdate(self.secret) |
| 719 | + |
| 720 | + with mock.patch('tconnectsync.sync.tandemsource.autoupdate.ChooseDevice', FakeChooseDevice), \ |
| 721 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.ProcessTimeRange', FakeProcessTimeRange), \ |
| 722 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.time', side_effect=[1000, 1001, 1002]), \ |
| 723 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.sleep', return_value=None), \ |
| 724 | + self.assertLogs('tconnectsync.sync.tandemsource.autoupdate', level='INFO') as logs: |
| 725 | + result = autoupdate.process(object(), object(), pretend=False) |
| 726 | + |
| 727 | + self.assertEqual(result, 0) |
| 728 | + self.assertTrue(any('No new reported tandemsource data.' in message for message in logs.output)) |
| 729 | + |
| 730 | + def test_process_does_not_crash_in_pretend_mode_without_successful_update_time(self): |
| 731 | + autoupdate = TandemSourceAutoupdate(self.secret) |
| 732 | + |
| 733 | + with mock.patch('tconnectsync.sync.tandemsource.autoupdate.ChooseDevice', FakeChooseDevice), \ |
| 734 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.time', side_effect=[2000, 2001, 2002]), \ |
| 735 | + mock.patch('tconnectsync.sync.tandemsource.autoupdate.time.sleep', return_value=None), \ |
| 736 | + self.assertLogs('tconnectsync.sync.tandemsource.autoupdate', level='INFO') as logs: |
| 737 | + result = autoupdate.process(object(), object(), pretend=True) |
| 738 | + |
| 739 | + self.assertEqual(result, 0) |
| 740 | + self.assertIsNone(autoupdate.last_successful_process_time_range) |
| 741 | + self.assertTrue(any('No new reported tandemsource data.' in message for message in logs.output)) |
| 742 | + |
| 743 | + |
678 | 744 | if __name__ == "__main__": |
679 | 745 | unittest.main() |
0 commit comments