From d51080f6d04a17eaa260ea5a487a8c0a0084f1bf Mon Sep 17 00:00:00 2001 From: James Woglom Date: Sat, 7 Jun 2025 23:24:41 -0400 Subject: [PATCH] Fix profile test --- tconnectsync/sync/tandemsource/autoupdate.py | 2 +- tests/parser/test_nightscout.py | 2 +- .../test_process_device_status.py | 87 ++++++++++ tests/sync/test_pump_events.py | 149 ------------------ 4 files changed, 89 insertions(+), 151 deletions(-) delete mode 100644 tests/sync/test_pump_events.py diff --git a/tconnectsync/sync/tandemsource/autoupdate.py b/tconnectsync/sync/tandemsource/autoupdate.py index ec76551..9d34d99 100644 --- a/tconnectsync/sync/tandemsource/autoupdate.py +++ b/tconnectsync/sync/tandemsource/autoupdate.py @@ -31,7 +31,7 @@ class TandemSourceAutoupdate: if features is None: features = DEFAULT_FEATURES - # Read from android api, find exact interval to cut down on API calls + # Query for data, find exact interval to cut down on API calls # Refresh API token. If failure, die, have wrapper script re-run. self.autoupdate_start = time.time() diff --git a/tests/parser/test_nightscout.py b/tests/parser/test_nightscout.py index a460a96..9f7afc8 100644 --- a/tests/parser/test_nightscout.py +++ b/tests/parser/test_nightscout.py @@ -4,7 +4,7 @@ import unittest from tconnectsync.parser.nightscout import NightscoutEntry, InvalidBolusTypeException, tandem_to_ns_time, tandem_to_ns_time_seconds from tconnectsync.domain.device_settings import Profile, ProfileSegment, DeviceSettings -from ..sync.test_profile import DEVICE_PROFILE_A, DEVICE_SETTINGS, NS_PROFILE_A +from ..sync.test_profile_data import DEVICE_PROFILE_A, DEVICE_SETTINGS, NS_PROFILE_A class TestNightscoutEntry(unittest.TestCase): maxDiff = None def test_basal(self): diff --git a/tests/sync/tandemsource/test_process_device_status.py b/tests/sync/tandemsource/test_process_device_status.py index 8b5fa60..3d8a55a 100644 --- a/tests/sync/tandemsource/test_process_device_status.py +++ b/tests/sync/tandemsource/test_process_device_status.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 import unittest +import struct from tconnectsync.sync.tandemsource.process_device_status import ProcessDeviceStatus from tconnectsync.eventparser import events as eventtypes +from tconnectsync.eventparser.events import UINT16 from tconnectsync.eventparser.generic import Event from ...api.fake import TConnectApi @@ -145,9 +147,94 @@ class TestProcessDeviceStatus(unittest.TestCase): 'pump_event_id': '1047614' }) + @unittest.skip + def test_device_status_battery_calculation(self): + self.nightscout.last_uploaded_devicestatus = lambda *args, **kwargs: None + + def test_percentage_about(percent_str, event): + with self.subTest(percent_str, event=event): + # p = self.process.process([event], time_start=None, time_end=None) + # self.assertEqual(len(p), 1) + # self.assertEqual(p[0]['pump']['battery']['status'], percent_str) + msb = event.batterychargepercentmsbRaw + lsb = event.batterychargepercentlsbRaw + MAX = struct.unpack(UINT16, bytearray((16, 128)))[0] + MIN = struct.unpack(UINT16, bytearray((14, 128)))[0] + val = struct.unpack(UINT16, bytearray((msb, lsb)))[0] + pct = (val - MIN)/(MAX-MIN) + # print(pct) + # print(struct.unpack(UINT16, bytearray((msb, lsb)) + # (14, 100) =~ 0% + # (15, 100) =~ 50% + # (16, 98) =~ 100% + # msb*256 + lsb - 14*256 + # pct = (msb*256 + lsb - 14*256 - 100) / 512 + calc_str = "%.0f%s" % (100*pct, '%') + print(f'comp {calc_str=} {percent_str=}') + self.assertEqual(calc_str, percent_str) + + + test_percentage_about('80%', Event(b'\x00Q\x1f\xfdm\xf5\x00\x00\x04P@4\xa7\xed?L\xcc\xcd@+\xd8\x81\x0f\xa1P\x00')) + test_percentage_about('55%', Event(b'\x00Q \x04\x15\xdd\x00\x00E\xb1A\x86\xe0\xcf\x00\x00\x00\x00A9\xd2w\x0f\x1d=\x00')) + test_percentage_about('45%', Event(b'\x00Q \x04\xd8f\x00\x00L^A^\xc5\x9a>49X\x00\x00\x00\x00\x0e\xfa7\x00')) + test_percentage_about('15%', Event(b'\x00Q \x06#U\x00\x00X{A\t\xed\xeb?\tx\xd5<\xe0\x81[\x0e\xb5 \x00')) + test_percentage_about('20%', Event(b'\x00Q \x1d\xbb\xa5\x00\x019zA\x1e\x1f`@%p\xa4>\xad\xaa\xf1\x0e\xc1$\x00')) + test_percentage_about('10%', Event(b'\x00Q \x1e\\m\x00\x01?}A\xa0\xe2\x8b?L\xcc\xcd?\xda\xeaj\x0e\xa1\x1b\x00')) + test_percentage_about('10%', Event(b'\x00Q \x1ej~\x00\x01@2A\xa5\xafZ\x00\x00\x00\x00@\x8c[\xed\x0e\x9f\x1a\x00')) + test_percentage_about('5%', Event(b'\x00Q \x1e\xa2\xbd\x00\x01C\x1a?\xd9?}@MO\xdf@uz<\x0e\x88\x15\x00')) + + +# Mobi @ MAX seen +# bytearray(b'\x00Q \x19U=\x00\x01\x0f\xa8A\xa5\x04V?L\xcc\xcd?s\x83b\x10Pd\x01') +# batterychargepercentmsbRaw=16, batterychargepercentlsbRaw=80, batterylipomillivolts=25601 + +# Mobi @ ~80% +# bytearray(b'\x00Q\x1f\xfdm\xf5\x00\x00\x04P@4\xa7\xed?L\xcc\xcd@+\xd8\x81\x0f\xa1P\x00'): +# batterychargepercentmsbRaw=15, batterychargepercentlsbRaw=161, batterylipomillivolts=20480 +# calc batteryChargePercent = 0.54296875 + +# Mobi @ ~55% +# bytearray(b'\x00Q \x04\x15\xdd\x00\x00E\xb1A\x86\xe0\xcf\x00\x00\x00\x00A9\xd2w\x0f\x1d=\x00') +# batterychargepercentmsbRaw=15, batterychargepercentlsbRaw=29, batterylipomillivolts=15616 +# calc batteryChargePercent = 0.37109375 + +# Mobi @ ~45% +# bytearray(b'\x00Q \x04\xd8f\x00\x00L^A^\xc5\x9a>49X\x00\x00\x00\x00\x0e\xfa7\x00') +# batterychargepercentmsbRaw=14, batterychargepercentlsbRaw=250 batterylipomillivolts=14080 +# calc batteryChargePercent = 0.3255208333333333 + +# Mobi @ ~15% +# bytearray(b'\x00Q \x06#U\x00\x00X{A\t\xed\xeb?\tx\xd5<\xe0\x81[\x0e\xb5 \x00') +# batterychargepercentmsbRaw=14, batterychargepercentlsbRaw=181 batterylipomillivolts=8192 +# calc batteryChargePercent = 0.23567708333333334 + +# Mobi @ ~20% +# bytearray(b'\x00Q \x1d\xbb\xa5\x00\x019zA\x1e\x1f`@%p\xa4>\xad\xaa\xf1\x0e\xc1$\x00') +# batterychargepercentmsbRaw=14, batterychargepercentlsbRaw=193 batterylipomillivolts=9216 +# calc batteryChargePercent = 0.2513020833333333 + +# Mobi @ ~10% +# bytearray(b'\x00Q \x1e\\m\x00\x01?}A\xa0\xe2\x8b?L\xcc\xcd?\xda\xeaj\x0e\xa1\x1b\x00') +# batterychargepercentmsbRaw=14, batterychargepercentlsbRaw=161 batterylipomillivolts=6912 +# calc batteryChargePercent = 0.20963541666666666 + +# Mobi @ ~10% +# bytearray(b'\x00Q \x1ej~\x00\x01@2A\xa5\xafZ\x00\x00\x00\x00@\x8c[\xed\x0e\x9f\x1a\x00') +# batterychargepercentmsbRaw=14, batterychargepercentlsbRaw=159 batterylipomillivolts=6656 +# calc batteryChargePercent = 0.20703125 + +# Mobi @ ~5% +# bytearray(b'\x00Q \x1e\xa2\xbd\x00\x01C\x1a?\xd9?}@MO\xdf@uz<\x0e\x88\x15\x00') +# batterychargepercentmsbRaw=14, batterychargepercentlsbRaw=136 +# calc batteryChargePercent = 0.17708333333333334 batterylipomillivolts=5376 + +# Mobi @ ~5% +# bytearray(b'\x00Q \x1e\xb0\xcd\x00\x01C\xd3@L9W@#33@\x8b\x04\xd2\x0e\x88\x15\x00') +# batterychargepercentmsbRaw=14 batterychargepercentlsbRaw=136 batterylipomillivolts=5376 +# calc 0.17708333333333334 if __name__ == '__main__': diff --git a/tests/sync/test_pump_events.py b/tests/sync/test_pump_events.py deleted file mode 100644 index 37215b6..0000000 --- a/tests/sync/test_pump_events.py +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/env python3 - -import json -import unittest -from typing import Dict -import copy - -from tconnectsync.sync.pump_events import ns_write_pump_events - -from ..nightscout_fake import NightscoutApi - - -def _stub_last_uploaded_entry(eventType, time_start=None, time_end=None): - return None - -class TestNsWritePumpEvents(unittest.TestCase): - - SITE_CHANGE_EVENT = {'time': '2024-02-04 16:27:50-05:00', 'event_type': 'Site/Cartridge Change'} - SITE_CHANGE_NS = {'created_at': '2024-02-04 16:27:50-05:00', 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Site Change', 'notes': 'Site/Cartridge Change','reason': 'Site/Cartridge Change', 'pump_event_id': ''} - def test_write_single_sitechange_event(self): - nightscout = NightscoutApi() - nightscout.last_uploaded_entry = _stub_last_uploaded_entry - ns_write_pump_events(nightscout, [ - self.SITE_CHANGE_EVENT - ]) - - self.assertDictEqual({ - 'treatments': [ - self.SITE_CHANGE_NS - ] - }, dict(nightscout.uploaded_entries)) - - EMPTY_CART_EVENT = {'time': '2024-05-14 09:01:59-04:00', 'event_type': 'Empty Cartridge/Pump Shutdown'} - EMPTY_CART_NS = {'created_at': '2024-05-14 09:01:59-04:00', 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Basal Suspension', 'notes': 'Empty Cartridge/Pump Shutdown', 'reason': 'Empty Cartridge/Pump Shutdown', 'pump_event_id': ''} - def test_write_single_emptycart_event(self): - nightscout = NightscoutApi() - nightscout.last_uploaded_entry = _stub_last_uploaded_entry - ns_write_pump_events(nightscout, [ - self.EMPTY_CART_EVENT - ]) - - self.assertDictEqual({ - 'treatments': [ - self.EMPTY_CART_NS - ] - }, dict(nightscout.uploaded_entries)) - - USER_SUSPENDED_EVENT = {'time': '2024-02-05 09:48:22-05:00', 'event_type': 'User Suspended'} - USER_SUSPENDED_NS = {'created_at': '2024-02-05 09:48:22-05:00', 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Basal Suspension', 'notes': 'User Suspended', 'reason': 'User Suspended', 'pump_event_id': ''} - def test_write_single_usersuspended_event(self): - nightscout = NightscoutApi() - nightscout.last_uploaded_entry = _stub_last_uploaded_entry - ns_write_pump_events(nightscout, [ - self.USER_SUSPENDED_EVENT - ]) - - self.assertDictEqual({ - 'treatments': [ - self.USER_SUSPENDED_NS - ] - }, dict(nightscout.uploaded_entries)) - - EXERCISE_EVENT = {'time': '2024-03-10 08:53:20-04:00', 'duration_mins': 269.3833333333333, 'event_type': 'Exercise'} - EXERCISE_NS = {'created_at': '2024-03-10 08:53:20-04:00', 'duration': 269.3833333333333, 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Exercise', 'notes': 'Exercise', 'reason': 'Exercise', 'pump_event_id': ''} - EXERCISE_EXTENDED_EVENT = {'time': '2024-03-10 08:53:20-04:00', 'duration_mins': 585.8833333333333, 'event_type': 'Exercise'} - EXERCISE_EXTENDED_NS = {'created_at': '2024-03-10 08:53:20-04:00', 'duration': 585.8833333333333, 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Exercise', 'notes': 'Exercise', 'reason': 'Exercise', 'pump_event_id': ''} - def test_write_exercise_events(self): - nightscout = NightscoutApi() - nightscout.last_uploaded_entry = _stub_last_uploaded_entry - ns_write_pump_events(nightscout, [ - self.EXERCISE_EVENT - ]) - - self.assertDictEqual({ - 'treatments': [ - self.EXERCISE_NS - ] - }, dict(nightscout.uploaded_entries)) - - _id = 'exercise_id' - def _last_uploaded_entry(eventType, time_start=None, time_end=None): - if eventType == 'Exercise': - return { - '_id': _id, - **self.EXERCISE_NS - } - - nightscout.last_uploaded_entry = _last_uploaded_entry - - ns_write_pump_events(nightscout, [ - self.EXERCISE_EXTENDED_EVENT - ]) - - self.assertListEqual([ - 'treatments/%s' % _id - ], nightscout.deleted_entries) - - self.assertDictEqual({ - 'treatments': [ - self.EXERCISE_NS, - self.EXERCISE_EXTENDED_NS - ] - }, dict(nightscout.uploaded_entries)) - - SLEEP_EVENT = {'time': '2024-02-07 00:00:19-05:00', 'duration_mins': 13.916666666666666, 'event_type': 'Sleep'} - SLEEP_NS = {'created_at': '2024-02-07 00:00:19-05:00', 'duration': 13.916666666666666, 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Sleep', 'notes': 'Sleep', 'reason': 'Sleep', 'pump_event_id': ''} - SLEEP_EXTENDED_EVENT = {'time': '2024-02-07 00:00:19-05:00', 'duration_mins': 74.0, 'event_type': 'Sleep'} - SLEEP_EXTENDED_NS = {'created_at': '2024-02-07 00:00:19-05:00', 'duration': 74.0, 'enteredBy': 'Pump (tconnectsync)', 'eventType': 'Sleep', 'notes': 'Sleep', 'reason': 'Sleep', 'pump_event_id': ''} - def test_write_sleep_events(self): - nightscout = NightscoutApi() - nightscout.last_uploaded_entry = _stub_last_uploaded_entry - ns_write_pump_events(nightscout, [ - self.SLEEP_EVENT - ]) - - self.assertDictEqual({ - 'treatments': [ - self.SLEEP_NS - ] - }, dict(nightscout.uploaded_entries)) - - _id = 'sleep_id' - def _last_uploaded_entry(eventType, time_start=None, time_end=None): - if eventType == 'Sleep': - return { - '_id': _id, - **self.SLEEP_NS - } - - nightscout.last_uploaded_entry = _last_uploaded_entry - - ns_write_pump_events(nightscout, [ - self.SLEEP_EXTENDED_EVENT - ]) - - self.assertListEqual([ - 'treatments/%s' % _id - ], nightscout.deleted_entries) - - self.assertDictEqual({ - 'treatments': [ - self.SLEEP_NS, - self.SLEEP_EXTENDED_NS - ] - }, dict(nightscout.uploaded_entries)) - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file