forked from IfcOpenShell/IfcOpenShell
/
test_ids.py
327 lines (270 loc) · 15.9 KB
/
test_ids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import unittest
import ids
# from ids import ids
import requests
import os
import logging
# from xmlschema.validators.exceptions import XMLSchemaChildrenValidationError
import ifcopenshell
from bcf import bcfxml
def read_web_file(URL):
return requests.get(URL).text
class TestIdsParsing(unittest.TestCase):
"""Parsing basic IDS files"""
def test_parse_basic_ids(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_all_fields.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(type(ids_file).__name__, "ids")
def test_parse_entity_facet(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_entity.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]["simpleValue"], "IfcWall")
def test_parse_predefinedtype_facet(self):
IDS_URL = (
"https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_predefinedtype.xml"
)
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(
ids_file.specifications[0].requirements.terms[0].node["predefinedtype"]["simpleValue"], "CLADDING"
)
def test_parse_property_facet(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(
ids_file.specifications[0].requirements.terms[0].node["propertyset"]["simpleValue"], "Test_PropertySet"
)
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]["simpleValue"], "Test_Parameter")
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["value"]["simpleValue"], "Test_Value")
def test_parse_material_facet(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_material.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["value"]["simpleValue"], "Test_Material")
def test_parse_classification_facet(self):
IDS_URL = (
"https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_classification.xml"
)
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(
ids_file.specifications[0].requirements.terms[0].node["value"]["simpleValue"], "Test_Classification"
)
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["system"]["simpleValue"], "Test_System")
""" Parsing invalid IDS.xml """
# TODO
# def test_invalid_classification_facet(self):
# IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/Invalid_IDS_Wall_needs_classification.xml"
# self.assertRaises( XMLSchemaChildrenValidationError, ids.parse(read_web_file(IDS_URL)) )
""" Saving parsed IDS to IDS.xml """
def test_parsed_ids_to_xml(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_all_fields.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
fn = "TEST_FILE.xml"
result = ids_file.to_xml(fn)
os.remove(fn)
self.assertTrue(result)
""" Parsing IDS files with restrictions """
def test_parse_restrictions_enumeration(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property_with_restriction_enumeration.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]["simpleValue"], "Test_Parameter")
self.assertEqual(
[
x["@value"]
for x in ids_file.specifications[0].requirements.terms[0].node["value"]["restriction"][0]["enumeration"]
],
["testA", "testB"],
)
def test_parse_restrictions_bounds(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property_with_restriction_bounds.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]["simpleValue"], "Test_Parameter")
self.assertEqual(
ids_file.specifications[0].requirements.terms[0].node["value"]["restriction"][0]["minInclusive"]["@value"],
"0",
)
def test_parse_restrictions_pattern_simple(self):
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property_with_restriction_pattern.xml"
ids_file = ids.ids.parse(read_web_file(IDS_URL))
self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]["simpleValue"], "Test_Parameter")
self.assertEqual(
ids_file.specifications[0].requirements.terms[0].node["value"]["restriction"][0]["pattern"]["@value"],
"[A-Z]{2,4}",
)
class TestIdsAuthoring(unittest.TestCase):
"""Creating basic IDS"""
def test_entity_create(self):
e = ids.entity.create(name="Test_Name", predefinedtype="Test_PredefinedType")
self.assertEqual(e.name, "Test_Name")
self.assertEqual(e.predefinedtype, "Test_PredefinedType")
def test_classification_create(self):
c = ids.classification.create(location="any", value="Test_Value", system="Test_System")
self.assertEqual(c.location, "any")
self.assertEqual(c.value, "Test_Value")
self.assertEqual(c.system, "Test_System")
def test_property_create(self):
p = ids.property.create(
location="any", propertyset="Test_PropertySet", name="Test_Parameter", value="Test_Value"
)
self.assertEqual(p.location, "any")
self.assertEqual(p.propertyset, "Test_PropertySet")
self.assertEqual(p.name, "Test_Parameter")
self.assertEqual(p.value, "Test_Value")
def test_material_create(self):
m = ids.material.create(location="any", value="Test_Value")
self.assertEqual(m.location, "any")
self.assertEqual(m.value, "Test_Value")
def test_specification_create(self):
s = ids.specification(name="Test_Specification")
self.assertEqual(s.name, "Test_Specification")
def test_ids_create(self):
i = ids.ids()
self.assertEqual(i.specifications, [])
self.assertEqual(i.info, None)
def test_ids_add_content(self):
i = ids.ids()
i.specifications.append(ids.specification(name="Test_Specification"))
self.assertEqual(i.specifications[0].name, "Test_Specification")
m = ids.material.create(location="any", value="Test_Value")
i.specifications[0].add_applicability(m)
self.assertEqual(i.specifications[0].applicability.terms[0].value, "Test_Value")
i.specifications[0].add_applicability(m)
self.assertEqual(i.specifications[0].applicability.terms[1].value, "Test_Value")
i.specifications[0].add_requirement(m)
self.assertEqual(i.specifications[0].requirements.terms[0].value, "Test_Value")
i.specifications[0].add_requirement(m)
self.assertEqual(i.specifications[0].requirements.terms[1].value, "Test_Value")
""" Creating IDS with restrictions """
def test_create_restrictions_enumeration(self):
i = ids.ids()
i.specifications.append(ids.specification(name="Test_Specification"))
i.specifications[0].add_applicability(ids.entity.create(name="Test_Name"))
r = ids.restriction.create(options=["testA", "testB"], type="enumeration", base="string")
m = ids.material.create(location="any", value=r)
i.specifications[0].add_requirement(m)
self.assertEqual(i.specifications[0].requirements.terms[0].value, "testA")
self.assertEqual(i.specifications[0].requirements.terms[0].value, "testB")
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, "testC")
def test_create_restrictions_bounds(self):
i = ids.ids()
i.specifications.append(ids.specification(name="Test_Specification"))
i.specifications[0].add_applicability(ids.entity.create(name="Test_Name"))
r = ids.restriction.create(
options={"minInclusive": 0, "maxExclusive": 10}, type="bounds", base="integer"
)
p = ids.property.create(location="any", propertyset="Test", name="Test", value=r)
i.specifications[0].add_requirement(p)
self.assertEqual(i.specifications[0].requirements.terms[0].value, 0)
self.assertEqual(i.specifications[0].requirements.terms[0].value, 5)
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, -1)
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, 10)
def test_create_restrictions_pattern_simple(self):
i = ids.ids()
i.specifications.append(ids.specification(name="Test_Specification"))
i.specifications[0].add_applicability(ids.entity.create(name="Test_Name"))
r = ids.restriction.create(options="[A-Z]{2,4}", type="pattern", base="string")
p = ids.property.create(location="any", propertyset="Test", name="Test", value=r)
i.specifications[0].add_requirement(p)
self.assertEqual(i.specifications[0].requirements.terms[0].value, "XYZ")
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, "abc")
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, "ABCDE")
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, "A")
def test_create_restrictions_pattern_advanced(self):
i = ids.ids()
i.specifications.append(ids.specification(name="Test_Specification"))
i.specifications[0].add_applicability(ids.entity.create(name="Test_Name"))
# r = ids.restriction.create(options="^(Wanddurchbruch.*|Deckendurchbruch.*)", type="pattern", base="string")
r = ids.restriction.create(
options="(Wanddurchbruch|Deckendurchbruch).*", type="pattern", base="string"
)
p = ids.property.create(location="any", propertyset="Test", name="Test", value=r)
i.specifications[0].add_requirement(p)
self.assertEqual(i.specifications[0].requirements.terms[0].value, "Wanddurchbruch")
self.assertEqual(i.specifications[0].requirements.terms[0].value, "Deckendurchbruch")
self.assertNotEqual(i.specifications[0].requirements.terms[0].value, "Deeckendurchbruch")
""" Saving created IDS to IDS.xml """
def test_created_ids_to_xml(self):
i = ids.ids()
i.specifications.append(ids.specification(name="Test_Specification"))
e = ids.entity.create(name="Test_Name", predefinedtype="Test_PredefinedType")
c = ids.classification.create(location="any", value="Test_Value", system="Test_System")
m = ids.material.create(location="any", value="Test_Value")
re = ids.restriction.create(options=["testA", "testB"], type="enumeration", base="string")
rb = ids.restriction.create(options={"minInclusive": 0, "maxExclusive": 10}, type="bounds", base="integer")
rp = ids.restriction.create(options="[A-Z]{2,4}", type="pattern", base="string")
p1 = ids.property.create(location="any", propertyset="Test_PropertySet", name="Test_Parameter", value=re)
p2 = ids.property.create(location="any", propertyset="Test_PropertySet", name="Test_Parameter", value=rb)
p3 = ids.property.create(location="any", propertyset="Test_PropertySet", name="Test_Parameter", value=rp)
p4 = ids.property.create(location="any", propertyset="Test_PropertySet", name="Test_Parameter", value=[re,rb,rp])
i.specifications[0].add_applicability(e)
i.specifications[0].add_applicability(m)
i.specifications[0].add_requirement(c)
i.specifications[0].add_requirement(p1)
i.specifications[0].add_requirement(p2)
i.specifications[0].add_requirement(p3)
i.specifications[0].add_requirement(p4)
fn = "TEST_FILE.xml"
result = i.to_xml(fn)
os.remove(fn)
self.assertTrue(result)
class TestIfcValidation(unittest.TestCase):
def test_validate_simple(self):
# TODO
pass
def test_validate_all_facets(self):
# TODO
pass
""" Validating IDS files with restrictions """
# def test_validate_restrictions_enumeration(self):
# IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property_with_restriction_enumeration.xml"
# ids_file = ids.ids.parse(read_web_file(IDS_URL))
# self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]['simpleValue'], "Test_Parameter")
# self.assertEqual( [x['@value'] for x in ids_file.specifications[0].requirements.terms[0].node["value"]['restriction'][0]['enumeration'] ], ['testA', 'testB'])
# # TODO actual test of validation result
# # self.assertTrue( )
# def test_validate_restrictions_boundsInclusive(self):
# IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property_with_restriction_bounds.xml"
# ids_file = ids.ids.parse(read_web_file(IDS_URL))
# self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]['simpleValue'], "Test_Parameter")
# self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["value"]['restriction'][0]['minInclusive']['@value'], '0')
# # TODO actual test of validation result
# # self.assertTrue( )
# def test_validate_restrictions_boundsExclusive(self):
# #TODO
# pass
# def test_validate_restrictions_pattern_simple(self):
# IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_property_with_restriction_pattern.xml"
# ids_file = ids.ids.parse(read_web_file(IDS_URL))
# self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["name"]['simpleValue'], "Test_Parameter")
# self.assertEqual(ids_file.specifications[0].requirements.terms[0].node["value"]['restriction'][0]['pattern']['@value'], '[A-Z]{2,4}')
# # TODO actual test of validation result
# # self.assertTrue( )
class TestIdsReporting(unittest.TestCase):
TEST_PATH = os.getcwd()
IFC_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IFC/IFC4_Wall_3_with_properties.ifc"
IDS_URL = "https://raw.githubusercontent.com/atomczak/Sample-BIM-Files/main/IDS/IDS_Wall_needs_all_fields.xml"
logger = logging.getLogger("IDS_Logger")
# logging.basicConfig(level=logging.INFO, format="%(message)s")
# logging.basicConfig(filename=TEST_PATH+r"\log.txt", level=logging.INFO, format="%(message)s")
content = read_web_file(IFC_URL)
file = open(TEST_PATH + r"\test.ifc", "w")
file.write(content)
file.close()
ifc_file = ifcopenshell.open(TEST_PATH + r"\test.ifc")
os.remove(TEST_PATH + r"\test.ifc")
def test_simple_report(self):
ids_file = ids.ids.parse(read_web_file(self.IDS_URL))
report = ids.SimpleHandler()
self.logger.addHandler(report)
ids_file.validate(self.ifc_file, self.logger)
self.assertEqual(len(report.statements), 5)
def test_bcf_report(self):
ids_file = ids.ids.parse(read_web_file(self.IDS_URL))
bcf_handler = ids.BcfHandler(
project_name="Default IDS Project", author="your@email.com", filepath=self.TEST_PATH + r"\bcf_test.bcfzip"
)
self.logger.addHandler(bcf_handler)
ids_file.validate(self.ifc_file, self.logger)
my_bcfxml = bcfxml.load(self.TEST_PATH + r"\bcf_test.bcfzip")
topics = my_bcfxml.get_topics()
self.assertEqual(len(topics), 5)
if __name__ == "__main__":
unittest.main()