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

1 import pytest

2

3 from mixinforge.json_processor import dumpjs, loadjs, access_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_access_jsparams_returns_selected_params():

19 o = ParamObj(x=10, y="foo", z=3)

20 js = dumpjs(o)

21

22 result = access_jsparams(js, "x", "y")

23

24 assert result == {"x": 10, "y": "foo"}

25

26

27 def test_access_jsparams_with_nested_values():

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

29 o = ParamObj(x=1, y="a", cfg=nested_list)

30 js = dumpjs(o)

31

32 result = access_jsparams(js, "cfg")

33

34 # access_jsparams returns JSON-native structures directly

35 assert result == {"cfg": nested_list}

36

37

38 def test_access_jsparams_missing_key_raises_keyerror():

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

40 js = dumpjs(o)

41

42 with pytest.raises(KeyError):

43 access_jsparams(js, "missing")

44

45

46 def test_access_jsparams_invalid_structure_raises_keyerror():

47 # Use dumps on a plain dict — it won't contain PARAMS->DICT structure

48 js = dumpjs({"plain": 1})

49

50 with pytest.raises(KeyError):

51 access_jsparams(js, "x")