Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_json_processor_remaining_branches.py: 88%

1 import pytest

2

3 from mixinforge.json_processor import _to_serializable_dict, _recreate_object, _Markers

4

5

6 class Plain:

7 def __init__(self):

8 self.a = 1

9 self.b = "x"

10

11

12 class PlainMaskGetstate:

13 def __init__(self):

14 self.a = 2

15 self.b = "y"

16

17 def __getattribute__(self, name):

18 if name == "__getstate__":

19 raise AttributeError

20 return object.__getattribute__(self, name)

21

22

23 def test_plain_class_uses_dict_state_branch():

24 p = PlainMaskGetstate()

25 ser = _to_serializable_dict(p)

26 # Should be encoded via STATE of its __dict__

27 assert _Markers.STATE in ser

28 back = _recreate_object(ser)

29 assert isinstance(back, PlainMaskGetstate)

30 assert back.a == 2 and back.b == "y"

31

32

33 class SlotlessMeta(type):

34 def __getattribute__(cls, name):

35 if name == "__slots__":

36 raise AttributeError

37 return super().__getattribute__(name)

38

39

40 class Trick(metaclass=SlotlessMeta):

41 # Do not define __slots__; instances normally have __dict__, but we mask it

42 def __getattribute__(self, name):

43 if name == "__dict__":

44 raise AttributeError

45 return object.__getattribute__(self, name)

46

47

48 def test_unsupported_early_types_raise():

49 # Functions are explicitly unsupported

50 with pytest.raises(TypeError):

51 _to_serializable_dict(lambda x: x)

52

53

54 class HideSlotsMeta(type):

55 def __getattribute__(cls, name):

56 if name == "__slots__":

57 raise AttributeError

58 return super().__getattribute__(name)

59

60

61 class ElseTarget(metaclass=HideSlotsMeta):

62 __slots__ = ("a",)

63

64 def __init__(self):

65 self.a = 1

66

67 def __getattribute__(self, name):

68 if name in ("__dict__", "__getstate__"):

69 raise AttributeError

70 return object.__getattribute__(self, name)

71

72

73 def test_final_else_branch_raises_type_error():

74 with pytest.raises(TypeError):

75 _to_serializable_dict(ElseTarget())