Coverage for tests/merge_dicts_test.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-11-25 20:25 +0000

1""" 

2Test_Merge_Dicts: 

3""" 

4 

5sphinx_needs_test_spec = '' 

6""" 

7.. test_spec:: Test of merge_dicts 

8 :id: TS_MERGE_DICTS 

9 :status: verified 

10 :verified_by: TEST_RES_merge_dicts 

11 :tests: SWRQ_TOOL_merge_dicts, 

12 SWRQ_MERGE_DICTS, 

13 SWRQ_LIST_OF_DICTS, 

14 VERIFY_SWRQ_LIST_OF_DICTS, 

15 SWRQ_DETECT_MERGE_CONFLICTS, 

16 VERIFY_SWRQ_DETECT_MERGE_CONFLICTS, 

17 SWRQ_ALLOW_DOUBLE_DEFINITION, 

18 VERIFY_SWRQ_ALLOW_DOUBLE_DEFINITION, 

19 SWRQ_USE_FIRST_VALUE_FOR_KEY, 

20 VERIFY_SWRQ_USE_FIRST_VALUE_FOR_KEY, 

21 M_MERGE_DICTS, 

22 IF_MERGE_DICTS 

23 

24 This is a test specification for the module :need:`M_MERGE_DICTS`. 

25 

26 **Test execution** 

27 

28 Run tests in test class with pytest. Command: `pytest -q merge_dicts_test.py` 

29 

30 **Test steps** 

31 

32 .. autoclass:: merge_dicts_test.Test_Merge_Dicts 

33 :members: 

34 :undoc-members: 

35 

36""" 

37 

38import sys 

39import os 

40sys.path.append(os.path.abspath('..')) 

41from merge_dicts import merge_dicts 

42import pytest 

43 

44class Test_Merge_Dicts: 

45 def test_merge_dictionaries(self): 

46 """1. It shall be possible to merge dictionaries.""" 

47 a = { 

48 'a': 1, 

49 'b': 2, 

50 'c': 3, 

51 } 

52 #print(a) 

53 b = { 

54 'x': 4, 

55 'y': 5, 

56 'z': 6, 

57 } 

58 #print(b) 

59 [c, merge_conflict] = merge_dicts(a,b) 

60 #print(c) 

61 #print(merge_conflict) 

62 

63 assert len(c) == 6 

64 assert ~merge_conflict 

65 

66 def test_merge_dictionaries_different_content(self): 

67 """2. If we have the same key in two dictinaries but different content, 

68 the merge_conflict bit shall be set.""" 

69 a = { 

70 'a': 1, 

71 'b': 2, 

72 'c': 3, 

73 } 

74 #print(a) 

75 d = { 

76 'x': 4, 

77 'b': 5, # b is now double defined with different value 

78 'z': 6, 

79 } 

80 #print(d) 

81 [e, merge_conflict] = merge_dicts(a,d) 

82 #print(e) 

83 #print(merge_conflict) 

84 

85 assert len(e) == 5 

86 assert merge_conflict 

87 

88 def test_merge_dictionaries_double_defined_content(self): 

89 """3. If we have the same key in two dictinaries and the same content, 

90 the merge_conflict bit shall not be set.""" 

91 a = { 

92 'a': 1, 

93 'b': 2, 

94 'c': 3, 

95 } 

96 #print(a) 

97 f = { 

98 'x': 5, 

99 'b': 2, # b is now double defined with same value 

100 'z': 6, 

101 } 

102 #print(f) 

103 [g, merge_conflict] = merge_dicts(a,f) 

104 #print(g) 

105 #print(merge_conflict) 

106 

107 assert len(g) == 5 

108 assert ~merge_conflict 

109 

110 def test_merge_dictionaries_no_dict_exception(self): 

111 """4. If we put in something else than a list of dictionaries, 

112 we shall get an "AttributeError" exception.""" 

113 h = ['a','b', 'c',] 

114 i = ['x','y', 'z',] 

115 

116 with pytest.raises(AttributeError): 

117 [j, merge_conflict] = merge_dicts(h,i) 

118 

119 def test_merge_empty_dictionaries(self): 

120 """5. It shall be possible to give in empty dictionaries.""" 

121 k = {} 

122 #print(k) 

123 [l, merge_conflict] = merge_dicts(k) 

124 #print(l) 

125 #print(merge_conflict) 

126 

127 assert len(l) == 0 

128 assert ~merge_conflict 

129 

130 def test_merge_dictionaries_no_parameter(self): 

131 """6. It shall be possible to give nothing to the function""" 

132 

133 [m, merge_conflict] = merge_dicts() 

134 #print(l) 

135 #print(merge_conflict) 

136 

137 assert len(m) == 0 

138 assert ~merge_conflict 

139 

140 def test_merge_dictionaries_latest_defined_value_in(self): 

141 """7. If we have the same key in two dictinaries but different content, 

142 the first value shall be in the final dict.""" 

143 a = { 

144 'a': 1, 

145 'b': 2, 

146 'c': 3, 

147 } 

148 #print(a) 

149 n = { 

150 'x': 4, 

151 'b': 5, # b is now double defined with different value 

152 'z': 6, 

153 } 

154 #print(n) 

155 [o, merge_conflict] = merge_dicts(a,n) 

156 #print(o) 

157 #print(merge_conflict) 

158 

159 assert len(o) == 5 

160 assert o['b'] == 2 

161 assert merge_conflict 

162 

163 def test_merge_many_dictionaries(self): 

164 """8. It shall be possible to give in more than two dictinaries.""" 

165 a = { 

166 'a': 1, 

167 'b': 2, 

168 'c': 3, 

169 } 

170 #print(a) 

171 p = { 

172 'l': 4, 

173 'm': 5, 

174 'n': 6, 

175 } 

176 #print(p) 

177 q = { 

178 'x': 7, 

179 'y': 8, 

180 'z': 9, 

181 } 

182 #print(q) 

183 [r, merge_conflict] = merge_dicts(a, p, q, ) 

184 #print(r) 

185 #print(merge_conflict) 

186 

187 assert len(r) == 9 

188 assert ~merge_conflict 

189