Coverage for test/unit/utilities/test_common_functions.py: 100%

133 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 17:09 +0000

1"""Test for common_functions.py""" 

2import re 

3import unittest 

4from pathlib import Path 

5 

6import bim2sim.elements.aggregation.bps_aggregations 

7import bim2sim.utilities.common_functions as cf 

8from bim2sim.elements.bps_elements import BPSProduct, Wall, Window, Door 

9from bim2sim.sim_settings import SettingsManager, BuildingSimSettings 

10 

11 

12class TestCommonFunctions(unittest.TestCase): 

13 """General common functions reltead tests""" 

14 

15 def test_angle_equivalent(self): 

16 """test angle_equivalent function""" 

17 input_angles = [0, 180, 360, -180, -360] 

18 expected_angles = [0, 180, 0, 180, 0] 

19 output_angles = [] 

20 for input_angle in input_angles: 

21 output_angles.append(cf.angle_equivalent(input_angle)) 

22 self.assertEqual(expected_angles, output_angles, ) 

23 

24 def test_vector_angle(self): 

25 """test vector_angle function""" 

26 input_vectors = [[1, 0], [0, 1], [1, 1], [-1, 1], [1, -1], [-1, -1]] 

27 expected_angles = [90, 0.0, 45.0, 315.0, 135.0, 225.0] 

28 output_angles = [] 

29 for input_vector in input_vectors: 

30 output_angles.append(cf.vector_angle(input_vector)) 

31 self.assertEqual(expected_angles, output_angles, ) 

32 

33 def test_get_usage_dict(self): 

34 """test get_use_conditions_dict function (perform test for 4 

35 samples)""" 

36 use_conditions_dh_path = Path(bim2sim.__file__).parent.parent / \ 

37 'test/resources/arch/custom_usages/' \ 

38 'UseConditionsFM_ARC_DigitalHub.json' 

39 use_conditions_dict = cf.get_use_conditions_dict( 

40 use_conditions_dh_path) 

41 self.assertIsInstance(use_conditions_dict, dict) 

42 expected_heating_profile = [ 

43 291.15, 291.15, 291.15, 291.15, 291.15, 294.15, 294.15, 294.15, 

44 294.15, 294.15, 294.15, 294.15, 294.15, 294.15, 294.15, 294.15, 

45 294.15, 294.15, 294.15, 294.15, 294.15, 291.15, 291.15, 291.15] 

46 expected_cooling_profile = [ 

47 309.15, 309.15, 309.15, 309.15, 309.15, 299.15, 299.15, 299.15, 

48 299.15, 299.15, 299.15, 299.15, 299.15, 299.15, 299.15, 299.15, 

49 299.15, 299.15, 299.15, 299.15, 299.15, 309.15, 309.15, 309.15] 

50 self.assertEqual( 

51 use_conditions_dict[ 

52 'Group Office (between 2 and 6 employees)']['heating_profile'], 

53 expected_heating_profile) 

54 self.assertEqual( 

55 use_conditions_dict[ 

56 'Group Office (between 2 and 6 employees)']['cooling_profile'], 

57 expected_cooling_profile) 

58 

59 def test_get_common_pattern_usage(self): 

60 """test get_common_pattern_usage function (perform test for three 

61 samples)""" 

62 usage_dict = cf.get_common_pattern_usage() 

63 self.assertIsInstance(usage_dict, dict) 

64 self.assertEqual( 

65 usage_dict['Auxiliary areas (without common rooms)'], ['Nebenrau']) 

66 self.assertEqual( 

67 usage_dict['Canteen'], ["Kantine", "Cafeteria"]) 

68 self.assertEqual( 

69 usage_dict['Classroom'], ["Klassenzimmer"]) 

70 

71 def test_get_custom_pattern_usage(self): 

72 """test get_custom_pattern_usage function (perform test for two 

73 samples)""" 

74 usage_dict_dh_path = Path(bim2sim.__file__).parent.parent / \ 

75 'test/resources/arch/custom_usages/' \ 

76 'customUsagesFM_ARC_DigitalHub_fixed002.json' 

77 usage_dict = cf.get_custom_pattern_usage(usage_dict_dh_path) 

78 

79 self.assertIsInstance(usage_dict, dict) 

80 self.assertEqual( 

81 usage_dict['Group Office (between 2 and 6 employees)'][0], 

82 'Space_13') 

83 self.assertEqual( 

84 usage_dict[ 

85 'Stock, technical equipment, archives'][0], 'Aufzug West 300') 

86 

87 def test_get_custom_pattern_usage_2(self): 

88 """test get_custom_pattern_usage function (perform test for two 

89 samples)""" 

90 use_conditions_dh_path = Path(bim2sim.__file__).parent.parent / \ 

91 'test/resources/arch/custom_usages/' \ 

92 'UseConditionsFM_ARC_DigitalHub.json' 

93 use_conditions_dict = cf.get_use_conditions_dict( 

94 use_conditions_dh_path) 

95 usage_dict_dh_path = Path(bim2sim.__file__).parent.parent / \ 

96 'test/resources/arch/custom_usages/' \ 

97 'customUsagesFM_ARC_DigitalHub_fixed002.json' 

98 

99 pattern_usage = cf.get_pattern_usage( 

100 use_conditions_dict, usage_dict_dh_path) 

101 self.assertEqual( 

102 pattern_usage['Group Office (between 2 and 6 employees)'][ 

103 'common'], 

104 [re.compile('(.*?)Group(.*?)Office(.*?)', re.IGNORECASE), 

105 re.compile('(.*?)Gruppen Buero', re.IGNORECASE)] 

106 ) 

107 self.assertEqual( 

108 pattern_usage[ 

109 'Group Office (between 2 and 6 employees)']['custom'][0], 

110 'Space_13') 

111 

112 def test_get_type_building_elements(self): 

113 """test get_type_building_elements function (perform test for 1 

114 sample)""" 

115 type_building_elements = cf.get_type_building_elements( 

116 data_file="TypeElements_IWU.json") 

117 self.assertIsInstance(type_building_elements, dict) 

118 self.assertEqual( 

119 type_building_elements['OuterWall']['[0, 1918]']['iwu_heavy'][ 

120 'layer']['0']['thickness'], 0.015) 

121 

122 def test_get_material_templates(self): 

123 """test get_material_templates function (perform test for 1 

124 sample)""" 

125 material_templates = cf.get_material_templates() 

126 self.assertIsInstance(material_templates, dict) 

127 self.assertEqual( 

128 material_templates[ 

129 '245ce424-3a43-11e7-8714-2cd444b2e704']['heat_capac'], 0.84) 

130 

131 def test_filter_elements(self): 

132 """test filter_elements function""" 

133 wall_1 = Wall() 

134 wall_2 = Wall() 

135 window_1 = Window() 

136 window_2 = Window() 

137 door_1 = Door() 

138 door_2 = Door() 

139 elements = [wall_1, wall_2, window_1, window_2, door_1, door_2] 

140 filtered_elements = cf.filter_elements(elements, 'Wall') 

141 expected_elements = [wall_1, wall_2] 

142 self.assertIsInstance(elements, list) 

143 self.assertEqual(filtered_elements, expected_elements) 

144 

145 def test_remove_umlaut(self): 

146 """test remove_umlaut function""" 

147 input_string = 'äöü' 

148 expected_string = 'aeoeue' 

149 output_string = cf.remove_umlaut(input_string) 

150 self.assertEqual(output_string, expected_string) 

151 

152 def test_translate_deep(self): 

153 """test translate_deep function""" 

154 input_texts = ['cheese', 'tomatoes', 'potatoes'] 

155 translated = [] 

156 expected_translations = ['käse', 'tomaten', 'kartoffeln'] 

157 for input_text in input_texts: 

158 translated.append( 

159 cf.translate_deep(input_text, target='de').lower()) 

160 self.assertEqual(translated, expected_translations) 

161 

162 def test_all_subclasses(self): 

163 """test all_subclasses function""" 

164 all_subclasses = cf.all_subclasses(BPSProduct) 

165 self.assertIsInstance(all_subclasses, set) 

166 self.assertEqual(len(all_subclasses), 29) 

167 

168 

169class TestConstructionClassChoices(unittest.TestCase): 

170 def setUp(self): 

171 self.building_sim_settings = BuildingSimSettings() 

172 self.settings_manager = SettingsManager(self.building_sim_settings) 

173 

174 def test_construction_class_walls_choices(self): 

175 """Test that all wall construction choices can be loaded""" 

176 walls_choices = self.settings_manager[ 

177 'construction_class_walls'].choices 

178 self.assertIsInstance(walls_choices, dict) 

179 self.assertGreater(len(walls_choices), 0) 

180 

181 # Test some specific expected choices 

182 expected_choices = [ 

183 'iwu_heavy', 

184 'iwu_light', 

185 'kfw_40', 

186 'tabula_de_standard_1_SFH', 

187 'tabula_dk_standard' 

188 ] 

189 for choice in expected_choices: 

190 self.assertIn(choice, walls_choices) 

191 self.assertIsInstance(walls_choices[choice], str) 

192 self.assertGreater(len(walls_choices[choice]), 0) 

193 

194 def test_construction_class_windows_choices(self): 

195 """Test that all window construction choices can be loaded""" 

196 windows_choices = self.settings_manager[ 

197 'construction_class_windows'].choices 

198 self.assertIsInstance(windows_choices, dict) 

199 self.assertGreater(len(windows_choices), 0) 

200 

201 # Test some specific expected choices 

202 expected_choices = [ 

203 'Holzfenster, zweifach', 

204 'Kunststofffenster, Isolierverglasung', 

205 'tabula_de_standard_1_SFH', 

206 'tabula_dk_standard' 

207 ] 

208 for choice in expected_choices: 

209 self.assertIn(choice, windows_choices) 

210 self.assertIsInstance(windows_choices[choice], str) 

211 self.assertGreater(len(windows_choices[choice]), 0) 

212 

213 def test_construction_class_doors_choices(self): 

214 """Test that all door construction choices can be loaded""" 

215 doors_choices = self.settings_manager[ 

216 'construction_class_doors'].choices 

217 self.assertIsInstance(doors_choices, dict) 

218 self.assertGreater(len(doors_choices), 0) 

219 

220 # Test some specific expected choices 

221 expected_choices = [ 

222 'iwu_typical', 

223 'kfw_40', 

224 'tabula_de_standard_1_SFH', 

225 'tabula_dk_standard_1_SFH' 

226 ] 

227 for choice in expected_choices: 

228 self.assertIn(choice, doors_choices) 

229 self.assertIsInstance(doors_choices[choice], str) 

230 self.assertGreater(len(doors_choices[choice]), 0) 

231 

232 def test_default_values(self): 

233 """Test that default values are set and valid""" 

234 walls_default = self.settings_manager[ 

235 'construction_class_walls'].default 

236 windows_default = self.settings_manager[ 

237 'construction_class_windows'].default 

238 doors_default = self.settings_manager[ 

239 'construction_class_doors'].default 

240 

241 self.assertEqual(walls_default, 'iwu_heavy') 

242 self.assertEqual(windows_default, 

243 'Alu- oder Stahlfenster, Waermeschutzverglasung, ' 

244 'zweifach') 

245 self.assertEqual(doors_default, 'iwu_typical') 

246 

247 # Verify defaults are in choices 

248 self.assertIn(walls_default, self.settings_manager[ 

249 'construction_class_walls'].choices) 

250 self.assertIn(windows_default, self.settings_manager[ 

251 'construction_class_windows'].choices) 

252 self.assertIn(doors_default, self.settings_manager[ 

253 'construction_class_doors'].choices) 

254 

255 def test_choice_counts(self): 

256 """Test the expected number of choices for each construction class""" 

257 walls_count = len( 

258 self.settings_manager['construction_class_walls'].choices) 

259 windows_count = len( 

260 self.settings_manager['construction_class_windows'].choices) 

261 doors_count = len( 

262 self.settings_manager['construction_class_doors'].choices) 

263 

264 self.assertEqual(walls_count, 

265 42) 

266 self.assertEqual(windows_count, 

267 40) 

268 self.assertEqual(doors_count, 

269 19)