From 7516226bd364a0f48932d7b238cecd17205e2ca2 Mon Sep 17 00:00:00 2001 From: James Woglom Date: Wed, 1 Jul 2026 02:40:04 +0000 Subject: [PATCH] Verify UpdateProfiles builds Nightscout profiles from the new settings UpdateProfiles already sources settings.details via pump_metadata(); add end-to-end compare_profiles tests over a real-shape PumpSettings confirming the per-segment basal/carbratio/sens schedule, flat-cgm target_low/high, and defaultProfile are translated correctly, and that a matching Nightscout profile yields no change. --- .../sync/tandemsource/test_update_profiles.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/sync/tandemsource/test_update_profiles.py b/tests/sync/tandemsource/test_update_profiles.py index 54084b4..2f19d89 100644 --- a/tests/sync/tandemsource/test_update_profiles.py +++ b/tests/sync/tandemsource/test_update_profiles.py @@ -52,6 +52,60 @@ class FakeTandemSourceApi: return False +# A fuller real-shape settings.details for end-to-end compare_profiles tests. +FULL_SETTINGS_DETAILS = { + 'profiles': { + 'numberOfProfiles': 1, 'activeSegment': 0, 'activeIdp': 0, + 'profile': [ + { + 'idp': 0, 'timeDependentSegmentNumber': 2, 'name': 'A', + 'carbEntry': 'UnitsAsCarbs', 'maxBolus': 25000, 'insulinDuration': 300, + 'timeDependentSegments': [ + {'startTime': 0, 'basalRate': 800, 'carbRatio': 6000, 'targetBg': 110, 'isf': 30, 'status': []}, + {'startTime': 480, 'basalRate': 1200, 'carbRatio': 6000, 'targetBg': 110, 'isf': 30, 'status': []}, + ], + }, + ], + }, + 'cgmSettings': {'highGlucoseAlertMgPerDl': 200, 'lowGlucoseAlertMgPerDl': 80}, +} + + +class TestCompareProfilesWithNewSettings(unittest.TestCase): + maxDiff = None + + def _updater(self): + tconnect = TConnectApi() + tconnect._tandemsource = FakeTandemSourceApi([]) + return UpdateProfiles(tconnect, NightscoutApi(), DEVICE_ID, pretend=True) + + def test_builds_ns_profile_from_new_settings(self): + pump_settings = PumpSettings.from_dict(FULL_SETTINGS_DETAILS) + changed, new_profile = self._updater().compare_profiles(pump_settings, {}) + + self.assertTrue(changed) + self.assertEqual(new_profile['defaultProfile'], 'A') + self.assertIn('A', new_profile['store']) + store = new_profile['store']['A'] + # milliunit->unit scaling and per-segment schedule preserved + self.assertEqual([b['value'] for b in store['basal']], [0.8, 1.2]) + self.assertEqual([b['time'] for b in store['basal']], ['00:00', '08:00']) + self.assertEqual(store['carbratio'][0]['value'], 6.0) + self.assertEqual(store['sens'][0]['value'], 30) + # target_low/high sourced from the flat cgmSettings + self.assertEqual(store['target_low'][0]['value'], 80) + self.assertEqual(store['target_high'][0]['value'], 200) + self.assertEqual(store['dia'], '5.0') + + def test_no_change_when_ns_already_matches(self): + pump_settings = PumpSettings.from_dict(FULL_SETTINGS_DETAILS) + updater = self._updater() + _, built = updater.compare_profiles(pump_settings, {}) + # Feed the freshly built profile back in as the current NS profile. + changed, _ = updater.compare_profiles(pump_settings, built) + self.assertFalse(changed) + + class TestUpdateProfilesSettingsSourcing(unittest.TestCase): maxDiff = None