Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_update_jsparams.py: 100%

1 import pytest

2

3 from mixinforge.json_processor import dumpjs, loadjs, update_jsparams

4

5

6 class ParamObj:

7 def __init__(self, x=0, y="", z=None, cfg=None):

8 self.x = x

9 self.y = y

10 self.z = z

11 self.cfg = cfg

12

13 def get_params(self):

14 # Only parameters returned here are serialized into the JSON under PARAMS->DICT

15 return {"x": self.x, "y": self.y, "z": self.z, "cfg": self.cfg}

16

17

18 def test_update_jsparams_replaces_existing_param():

19 o = ParamObj(x=1, y="a")

20 js = dumpjs(o)

21

22 js_updated = update_jsparams(js, x=5)

23 o2 = loadjs(js_updated)

24

25 assert isinstance(o2, ParamObj)

26 assert o2.x == 5 # replaced

27 assert o2.y == "a" # unchanged

28 assert o2.z is None

29

30

31 def test_update_jsparams_adds_new_param():

32 # Start with object missing z (None); then add z via update

33 o = ParamObj(x=2, y="b")

34 js = dumpjs(o)

35

36 js_updated = update_jsparams(js, z=99)

37 o2 = loadjs(js_updated)

38

39 assert o2.z == 99

40 assert o2.x == 2 and o2.y == "b"

41

42

43 def test_update_jsparams_nested_values_roundtrip():

44 o = ParamObj(x=3, y="c")

45 js = dumpjs(o)

46

47 # nested list is supported directly by the loader; nested dicts would require internal DICT markers

48 nested_list = [1, 2, [3, 4], "ok"]

49 js_updated = update_jsparams(js, cfg=nested_list)

50 o2 = loadjs(js_updated)

51

52 assert o2.cfg == nested_list

53

54