mirror of
https://github.com/bckelley/tconnectsync.git
synced 2026-08-24 03:34:12 -05:00
ensure permutations of the same number in different types are considered the same NS profile
This commit is contained in:
@@ -102,6 +102,10 @@ def compare_profiles(device_profiles: List[Profile], device_settings: DeviceSett
|
||||
return True, new_ns_profile
|
||||
|
||||
def nightscout_profiles_identical(configured: dict, translated: dict) -> bool:
|
||||
if configured == translated:
|
||||
logger.debug("direct dicts equal")
|
||||
return True
|
||||
|
||||
if json.dumps(configured, sort_keys=True, indent=None) == json.dumps(translated, sort_keys=True, indent=None):
|
||||
logger.debug("initial JSON dump identical")
|
||||
return True
|
||||
@@ -126,10 +130,21 @@ def nightscout_profiles_identical(configured: dict, translated: dict) -> bool:
|
||||
else:
|
||||
ob[i] = func(v)
|
||||
|
||||
def to_numeric(x):
|
||||
if type(x) in [int, float]:
|
||||
return '%f' % x
|
||||
try:
|
||||
return '%f' % float(x)
|
||||
except (ValueError, TypeError):
|
||||
return x
|
||||
|
||||
|
||||
convert_func = lambda x: to_numeric(x)
|
||||
|
||||
configured_str = json.loads(json.dumps(configured))
|
||||
map_nested_dicts_modify(configured_str, lambda x: str(x))
|
||||
map_nested_dicts_modify(configured_str, convert_func)
|
||||
translated_str = json.loads(json.dumps(translated))
|
||||
map_nested_dicts_modify(translated_str, lambda x: str(x))
|
||||
map_nested_dicts_modify(translated_str, convert_func)
|
||||
|
||||
if json.dumps(configured_str, sort_keys=True, indent=None) == json.dumps(translated_str, sort_keys=True, indent=None):
|
||||
logger.debug("map_nested_dicts JSON dump identical")
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import unittest
|
||||
from typing import Dict
|
||||
import copy
|
||||
|
||||
from tconnectsync.sync.profile import get_pump_profiles, compare_profiles
|
||||
from tconnectsync.sync.profile import get_pump_profiles, compare_profiles, nightscout_profiles_identical
|
||||
from tconnectsync.domain.device_settings import Profile, ProfileSegment, DeviceSettings
|
||||
from tconnectsync.secret import NIGHTSCOUT_PROFILE_CARBS_HR_VALUE, NIGHTSCOUT_PROFILE_DELAY_VALUE, TIMEZONE_NAME
|
||||
|
||||
@@ -467,5 +468,57 @@ class TestCompareProfiles(unittest.TestCase):
|
||||
)
|
||||
self.assertFalse(changed, 'did not stabilize')
|
||||
|
||||
class TestNightscoutProfilesIdentical(unittest.TestCase):
|
||||
def test_same_dict(self):
|
||||
self.assertTrue(nightscout_profiles_identical(NS_PROFILE_A, NS_PROFILE_A))
|
||||
|
||||
def test_different_dict(self):
|
||||
self.assertFalse(nightscout_profiles_identical(NS_PROFILE_A, NS_PROFILE_B))
|
||||
|
||||
def test_same_re_jsoned(self):
|
||||
self.assertTrue(nightscout_profiles_identical(NS_PROFILE_A, json.loads(json.dumps(NS_PROFILE_A))))
|
||||
|
||||
def test_same_different_number_types(self):
|
||||
a = b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
a['dia'] = 5
|
||||
b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
b['dia'] = 5.0
|
||||
self.assertTrue(nightscout_profiles_identical(a, b))
|
||||
|
||||
def test_same_different_number_strings_1(self):
|
||||
a = b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
a['dia'] = 5
|
||||
b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
b['dia'] = '5.0'
|
||||
self.assertTrue(nightscout_profiles_identical(a, b))
|
||||
|
||||
def test_same_different_number_strings_2(self):
|
||||
a = b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
a['dia'] = '5'
|
||||
b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
b['dia'] = 5.0
|
||||
self.assertTrue(nightscout_profiles_identical(a, b))
|
||||
|
||||
def test_same_different_number_strings_3(self):
|
||||
a = b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
a['dia'] = '5'
|
||||
b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
b['dia'] = '5.000'
|
||||
self.assertTrue(nightscout_profiles_identical(a, b))
|
||||
|
||||
def test_different_numbers(self):
|
||||
a = b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
a['dia'] = 5
|
||||
b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
b['dia'] = 5.1
|
||||
self.assertFalse(nightscout_profiles_identical(a, b))
|
||||
|
||||
def test_different_numbers_strings(self):
|
||||
a = b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
a['dia'] = '5'
|
||||
b = json.loads(json.dumps(NS_PROFILE_A))
|
||||
b['dia'] = '5.01'
|
||||
self.assertFalse(nightscout_profiles_identical(a, b))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user