Coverage for test/unit/elements/test_elements.py: 97%

71 statements  

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

1"""Testing classes of module element""" 

2 

3import unittest 

4from pathlib import Path 

5 

6from bim2sim.elements import hvac_elements as hvac 

7from bim2sim.elements.base_elements import ProductBased, Factory 

8from bim2sim.elements.mapping.attribute import Attribute 

9from bim2sim.elements.mapping.ifc2python import load_ifc 

10from test.unit.elements.helper import SetupHelperHVAC 

11from bim2sim.utilities.types import IFCDomain 

12 

13TEST_MODELS = Path(__file__).parent.parent.parent / 'resources' 

14 

15 

16# TODO test: 

17# Element request attr 

18# Element/ProductBased validate 

19# Factory 

20# Factory/IfcMixin ifc_types 

21# ProductBased better subclass 

22 

23def get_ifc(file: str): 

24 """Get IfcOpenShell wrapper instance for file""" 

25 ifc = load_ifc(TEST_MODELS / 'hydraulic/ifc' / file) 

26 return ifc 

27 

28 

29class Element1(ProductBased): 

30 ifc_types = {'IFCPIPESEGMENT': ['*']} 

31 attr_a = Attribute() 

32 

33 

34class Element2(ProductBased): 

35 ifc_types = {'IFCPIPEFITTING': ['*']} 

36 attr_x = Attribute() 

37 

38 

39class TestSlap(ProductBased): 

40 ifc_types = {'IfcSlab': ['*', '-SomethingSpecialWeDontWant', 'BASESLAB']} 

41 

42 

43class TestRoof(ProductBased): 

44 ifc_types = { 

45 'IfcRoof': ['FLAT_ROOF', 'SHED_ROOF'], 

46 'IfcSlab': ['ROOF'] 

47 } 

48 

49 

50class TestProductBased(unittest.TestCase): 

51 

52 def test_init(self): 

53 item = Element1() 

54 self.assertIsInstance(item, ProductBased) 

55 

56 item2 = Element1(attr_a=4) 

57 self.assertEqual(4, item2.attr_a) 

58 

59 def test_from_ifc(self): 

60 ifc = get_ifc('B01_2_HeatExchanger_Pipes.ifc') 

61 guid = '2aUc0GQrtLYqyOs0qLuQL7' 

62 ifc_entity = ifc.by_guid(guid) 

63 item = Element1.from_ifc(ifc_entity) 

64 

65 self.assertIsInstance(item, ProductBased) 

66 self.assertEqual(guid, item.guid) 

67 

68 def test_validate_creation_two_port_pipe(self): 

69 helper = SetupHelperHVAC() 

70 two_port_pipe = helper.element_generator(hvac.Pipe, 

71 diameter=10, 

72 length=100) 

73 valid = two_port_pipe.validate_creation() 

74 self.assertTrue(valid) 

75 

76 def test_validate_creation_three_port_pipe(self): 

77 helper = SetupHelperHVAC() 

78 two_port_pipe = helper.element_generator(hvac.Pipe, 

79 n_ports=3, 

80 diameter=10, 

81 length=100) 

82 valid = two_port_pipe.validate_creation() 

83 self.assertFalse(valid) 

84 

85 

86@unittest.skip("Not implemented") 

87class TestRelationBased(unittest.TestCase): 

88 pass 

89 

90 

91class TestFactory(unittest.TestCase): 

92 

93 def test_init(self): 

94 relevant_elements = { 

95 Element1, 

96 Element2 

97 } 

98 factory = Factory( 

99 relevant_elements, ifc_units={}, ifc_domain=IFCDomain.arch, 

100 dummy=None) 

101 self.assertIsInstance(factory, Factory) 

102 

103 def test_factory_create(self): 

104 ifc = get_ifc('B01_2_HeatExchanger_Pipes.ifc') 

105 entities = ifc.by_type('IFCPIPESEGMENT') 

106 relevant_elements = { 

107 Element1, 

108 Element2 

109 } 

110 factory = Factory( 

111 relevant_elements, ifc_units={}, ifc_domain=IFCDomain.arch, 

112 dummy=None) 

113 item = factory(entities[0]) 

114 

115 self.assertIsInstance(item, ProductBased) 

116 

117 @unittest.skip("Not implemented") 

118 def test_factory_create_better_cls(self): 

119 pass 

120 

121 def test_create_mapping(self): 

122 """Test if Factory uses ifc_types correctly""" 

123 factory = Factory( 

124 {TestRoof, TestSlap}, ifc_units={}, ifc_domain=IFCDomain.arch) 

125 

126 self.assertIs(factory.get_element('IfcSlab', 'BASESLAB'), TestSlap) 

127 self.assertIs(factory.get_element('IfcSlab', 'OTHER'), TestSlap) 

128 self.assertIsNone(factory.get_element('IfcSlab', 'SomethingSpecialWeDontWant')) 

129 self.assertIs(factory.get_element('IfcSlab', 'ROOF'), TestRoof) 

130 

131 

132if __name__ == '__main__': 

133 unittest.main()