Coverage for tests/tests/test_helpers.py: 0%

1#!/usr/bin/env python3

2"""RAMSES RF - Test the various helper APIs."""

3

4# TODO: add test for ramses_tx.frame.pkt_header()

5

6 from typing import Any

7

8 from ramses_rf.const import DEV_ROLE_MAP, DEV_TYPE_MAP

9 from ramses_rf.helpers import deep_merge

10 from ramses_tx.const import attr_dict_factory

11

12 from .helpers import assert_raises

13

14

15 def test_merge_dicts() -> None:

16 """Deep merge a src dict (precedent) into a dst dict and return the result."""

17

18 src: dict[str, Any]

19 dst: dict[str, Any]

20 out: dict[str, Any]

21

22 src = {"top": {"deep": {"in_both": "1", "in_src": "dog"}}}

23 dst = {"top": {"deep": {"in_both": "9", "in_dst": "cat"}}}

24 out = {"top": {"deep": {"in_both": "1", "in_src": "dog", "in_dst": "cat"}}}

25 assert out == deep_merge(src, dst)

26

27 assert out != dst

28 assert out == deep_merge(src, dst, _dc=True)

29 assert out == dst

30

31 src = {"top": {"deep": {"in_both": [0, 1]}}}

32 dst = {"top": {"deep": {"in_both": [0, 9]}}}

33 out = {"top": {"deep": {"in_both": [0, 1, 9]}}}

34 assert out == deep_merge(src, dst)

35

36 src = {"top": {"deep": {"in_both": "non-list"}}}

37 dst = {"top": {"deep": {"in_both": [0, 9]}}}

38 out = {"top": {"deep": {"in_both": "non-list"}}}

39 assert out == deep_merge(src, dst)

40

41 src = {"top": {"deep": {"in_both": [0, 1]}}}

42 dst = {"top": {"deep": {"in_both": "non-list"}}}

43 out = {"top": {"deep": {"in_both": [0, 1]}}}

44 assert out == deep_merge(src, dst)

45

46

47 def test_attrdict_class() -> None:

48 _ = attr_dict_factory(MAIN_DICT, attr_table=ATTR_DICT)

49

50 assert_raises(KeyError, DEV_TYPE_MAP.slug, "_rubbish_")

51 assert_raises(KeyError, DEV_TYPE_MAP.slug, None)

52 # assert DEV_TYPE_MAP.slug(None), "DEV")

53

54 try:

55 DEV_ROLE_MAP["_08"]

56 except KeyError:

57 pass

58 else:

59 assert False

60

61 assert DEV_ROLE_MAP.DHW == "0D"

62 assert DEV_ROLE_MAP._0D == "dhw_sensor"

63 assert DEV_ROLE_MAP.DHW_SENSOR == "0D"

64

65 assert DEV_ROLE_MAP["RAD"] == "rad_actuator"

66 assert DEV_ROLE_MAP["08"] == "rad_actuator"

67 assert DEV_ROLE_MAP["rad_actuator"] == "08"

68

69 assert DEV_ROLE_MAP._hex("SEN") == "04"

70 assert_raises(KeyError, DEV_ROLE_MAP._hex, "04") # aka: DEV_ROLE_MAP._hex("04")

71 assert DEV_ROLE_MAP._hex("zone_sensor") == "04"

72

73 assert_raises(KeyError, DEV_ROLE_MAP._hex, "_rubbish_")

74 assert_raises(KeyError, DEV_ROLE_MAP._hex, None)

75

76 assert DEV_ROLE_MAP._str("OUT") == "out_sensor"

77 assert DEV_ROLE_MAP._str("0C") == "out_sensor"

78 assert_raises(KeyError, DEV_ROLE_MAP._str, "out_sensor")

79

80 assert_raises(KeyError, DEV_ROLE_MAP._str, "_rubbish_")

81 assert_raises(KeyError, DEV_ROLE_MAP._str, None)

82

83 assert_raises(KeyError, DEV_ROLE_MAP.slug, "RFG") # aka: DEV_ROLE_MAP.slug("RFG")

84 assert DEV_ROLE_MAP.slug("10") == "RFG"

85 assert DEV_ROLE_MAP.slug("remote_gateway") == "RFG"

86

87 assert_raises(KeyError, DEV_ROLE_MAP.slug, "_rubbish_")

88 assert_raises(KeyError, DEV_ROLE_MAP.slug, None)

89

90 assert (

91 "HTG" not in DEV_ROLE_MAP

92 and "0E" in DEV_ROLE_MAP

93 and "heating_relay" not in DEV_ROLE_MAP

94 )

95 assert (

96 "DHW" not in DEV_ROLE_MAP.values()

97 and "0D" not in DEV_ROLE_MAP.values()

98 and "dhw_sensor" in DEV_ROLE_MAP.values()

99 )

100

101 assert (

102 "DHW" in DEV_ROLE_MAP.slugs()

103 and "0D" not in DEV_ROLE_MAP.slugs()

104 and "dhw_sensor" not in DEV_ROLE_MAP.slugs()

105 )

106

107 assert DEV_ROLE_MAP.SLUGS, MAIN_SLUGS

108 assert DEV_ROLE_MAP.HEAT_DEVICES == ("00", "04", "08", "09", "0A", "0B", "11")

109

110

111 MAIN_DICT = {

112 "ACT": {"00": "zone_actuator"},

113 "SEN": {"04": "zone_sensor"},

114 "RAD": {"08": "rad_actuator"},

115 "UFH": {"09": "ufh_actuator"},

116 "VAL": {"0A": "val_actuator"},

117 "MIX": {"0B": "mix_actuator"},

118 "OUT": {"0C": "out_sensor"},

119 "DHW": {"0D": "dhw_sensor"},

120 "HTG": {"0E": "hotwater_valve"},

121 "HT1": {None: "heating_valve"},

122 "APP": {"0F": "appliance_control"},

123 "RFG": {"10": "remote_gateway"},

124 "ELE": {"11": "ele_actuator"},

125}

126 ATTR_DICT = {

127 "HEAT_DEVICES": ("08", "09", "0A", "0B", "11"),

128}

129 MAIN_SLUGS = tuple(sorted(MAIN_DICT.keys()))