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

37 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-01 16:37 -0600

1import json 

2 

3from mixinforge.json_processor import ( 

4 _to_serializable_dict, 

5 _recreate_object, 

6 _Markers, 

7 update_jsparams, 

8) 

9 

10 

11class OnlySlots: 

12 __slots__ = ("m", "n") 

13 

14 def __init__(self, m=1, n=2): 

15 self.m = m 

16 self.n = n 

17 

18 

19class HybridSlots: 

20 __slots__ = ("__dict__", "p") 

21 

22 def __init__(self, p=10): 

23 self.p = p 

24 self.extra = "x" 

25 

26 

27def test_to_serializable_uses_slots_branch_for_slots_only(): 

28 obj = OnlySlots(7, 8) 

29 ser = _to_serializable_dict(obj) 

30 

31 # Ensure we went through the slots path that emits STATE 

32 assert _Markers.STATE in ser 

33 

34 # Ensure recreation works and populates slots 

35 back = _recreate_object(ser) 

36 assert isinstance(back, OnlySlots) 

37 assert back.m == 7 and back.n == 8 

38 

39 

40def test_to_serializable_uses_slots_branch_for_hybrid_and_dict_is_preserved(): 

41 obj = HybridSlots(33) 

42 obj.extra = "changed" 

43 obj.more = 123 

44 

45 ser = _to_serializable_dict(obj) 

46 assert _Markers.STATE in ser 

47 

48 back = _recreate_object(ser) 

49 assert isinstance(back, HybridSlots) 

50 # slots applied in order 

51 assert back.p == 33 

52 # dict attributes also restored 

53 assert back.extra == "changed" 

54 assert back.more == 123 

55 

56 

57def test_update_jsparams_else_branch_on_explicit_DICT_payload(): 

58 # Construct a JSON exactly matching the DICT marker structure to force else-branch 

59 params = {_Markers.DICT: {"a": 1}} 

60 js = json.dumps(params) 

61 out = update_jsparams(js, b=2) 

62 decoded = json.loads(out) 

63 # PARAMS should still be absent; DICT must carry both keys 

64 assert _Markers.PARAMS not in decoded 

65 assert decoded[_Markers.DICT] == {"a": 1, "b": 2}