| 1 |
# BIMTester - OpenBIM Auditing Tool |
| 2 |
# Copyright (C) 2021 Dion Moult <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of BIMTester. |
| 5 |
# |
| 6 |
# BIMTester is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# BIMTester is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public License |
| 17 |
# along with BIMTester. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from behave import step, given, when, then, use_step_matcher |
| 20 |
|
| 21 |
from bimtester import util |
| 22 |
from bimtester.ifc import IfcStore |
| 23 |
from bimtester.table import TableModel |
| 24 |
|
| 25 |
use_step_matcher("parse") |
| 26 |
|
| 27 |
|
| 28 |
@step('There must be exactly {number} "{ifc_class}" element') |
| 29 |
@step('There must be exactly {number} "{ifc_class}" elements') |
| 30 |
def step_impl(context, number, ifc_class): |
| 31 |
num = len(IfcStore.file.by_type(ifc_class)) |
| 32 |
assert num == int( |
| 33 |
number |
| 34 |
), "Could not find {} elements of {}. \ |
| 35 |
Found {} element(s).".format( |
| 36 |
number, ifc_class, num |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@given('a set of (key,value) called ("{key_name}","{value_name}")') |
| 41 |
def step_impl(context, key_name, value_name): |
| 42 |
model = getattr(context, "model", None) |
| 43 |
if not model: |
| 44 |
context.model = TableModel(key_name, value_name) |
| 45 |
for row in context.table: |
| 46 |
context.model.add_row(row[key_name], row[value_name]) |
| 47 |
|
| 48 |
|
| 49 |
@given('a set of (key,value) called ("{key_name}","{value_name}") taken from the file "{path_file}"') |
| 50 |
def step_impl(context, key_name, value_name, path_file): |
| 51 |
import csv |
| 52 |
import os |
| 53 |
|
| 54 |
model = getattr(context, "model", None) |
| 55 |
if not model: |
| 56 |
context.model = TableModel(key_name, value_name) |
| 57 |
if context.config.userdata.get("path"): |
| 58 |
path_file = os.path.join(context.config.userdata.get("path"), path_file) |
| 59 |
if not os.path.exists(path_file): |
| 60 |
assert False, "File {} not found".format(path_file) |
| 61 |
with open(path_file, "r", encoding="utf-8-sig") as csvfile: |
| 62 |
reader = csv.DictReader(csvfile) |
| 63 |
for row in reader: |
| 64 |
context.model.add_row(row[key_name], row[value_name]) |
| 65 |
|
| 66 |
|
| 67 |
@then('there is an element of type key with an attribute "{attribute_name}" equals to value') |
| 68 |
def step_impl(context, attribute_name): |
| 69 |
if not context.model.key_name and not context.model.value_name: |
| 70 |
assert False, "Missing (key,value)" |
| 71 |
errors = [] |
| 72 |
rows = context.model.rows |
| 73 |
for key, values in rows.items(): |
| 74 |
for value in values: |
| 75 |
found = check_equals(attribute_name, value, IfcStore.file.by_type(key)) |
| 76 |
if not found: |
| 77 |
errors.append(f"The row ({key}, {value}) was not found.") |
| 78 |
assert not errors, "Errors occured:\n{}".format("\n".join(errors)) |
| 79 |
|
| 80 |
|
| 81 |
@then('there is an element of type key with an attribute "{attribute_name}" starting with value') |
| 82 |
def step_impl(context, attribute_name): |
| 83 |
# import re |
| 84 |
if not context.model.key_name and not context.model.value_name: |
| 85 |
assert False, "Missing (key,value)" |
| 86 |
errors = [] |
| 87 |
rows = context.model.rows |
| 88 |
for key, values in rows.items(): |
| 89 |
for value in values: |
| 90 |
found = check_starts_with(attribute_name, value, IfcStore.file.by_type(key)) |
| 91 |
if not found: |
| 92 |
errors.append(f"The row ({key}, {value}) was not found.") |
| 93 |
assert not errors, "Errors occured:\n{}".format("\n".join(errors)) |
| 94 |
|
| 95 |
|
| 96 |
@then('there must be exactly a number of "{ifc_class}" equals to the number of distinct value') |
| 97 |
def step_impl(context, ifc_class): |
| 98 |
try: |
| 99 |
context.execute_steps( |
| 100 |
""" |
| 101 |
then There must be exactly {number} "{ifc_class}" elements |
| 102 |
""".format( |
| 103 |
ifc_class=ifc_class, number=context.model.get_count_distinct_values() |
| 104 |
) |
| 105 |
) |
| 106 |
except AssertionError as error: |
| 107 |
str_error = str(error) |
| 108 |
assert False, str_error[: str_error.find("Traceback")] |
| 109 |
assert True |
| 110 |
|
| 111 |
|
| 112 |
@then('there is a relationship "{ifc_class}" between the two elements of each row') |
| 113 |
def step_impl(context, ifc_class): |
| 114 |
if not context.model.key_name and not context.model.value_name: |
| 115 |
assert False, "Missing (key,value)" |
| 116 |
errors = [] |
| 117 |
elements = IfcStore.file.by_type(ifc_class) |
| 118 |
|
| 119 |
# first, loop on the dataset to check if there are all in the relationships |
| 120 |
rows = context.model.rows |
| 121 |
for key, values in rows.items(): |
| 122 |
found = False |
| 123 |
# value is actually a list of values |
| 124 |
for value in values: |
| 125 |
for element in elements: |
| 126 |
if ( |
| 127 |
any(x.Name == key for x in getattr(element, context.model.key_name)) |
| 128 |
and getattr(element, context.model.value_name).Name == value |
| 129 |
): |
| 130 |
found = True |
| 131 |
if not found: |
| 132 |
errors.append(f"The row ({key}, {value}) does not have the relationship.") |
| 133 |
|
| 134 |
# second, loop on the elements to check if there are not too many relationships |
| 135 |
for element in elements: |
| 136 |
if hasattr(element, context.model.key_name) and hasattr(element, context.model.value_name): |
| 137 |
# list here |
| 138 |
keys = getattr(element, context.model.key_name) |
| 139 |
value = getattr(element, context.model.value_name).Name |
| 140 |
for key in keys: |
| 141 |
if not key.Name in rows or not value in rows[key.Name]: |
| 142 |
errors.append(f"The element ({element}) has a relation that cannot be found in the dataset: {key}") |
| 143 |
assert not errors, "Errors occured:\n{}".format("\n".join(errors)) |
| 144 |
|
| 145 |
|
| 146 |
use_step_matcher("re") |
| 147 |
|
| 148 |
|
| 149 |
@step('all IfcGroup must be linked to a type "(?P<linked_ifc_classes>.*)"') |
| 150 |
def step_impl(context, linked_ifc_classes): |
| 151 |
groups = IfcStore.file.by_type("IfcGroup") |
| 152 |
errors = [] |
| 153 |
for group in groups: |
| 154 |
if not hasattr(group, "IsGroupedBy"): |
| 155 |
errors.append(f'The element "{group.Name}" has no "IsGroupedBy" attribute.') |
| 156 |
else: |
| 157 |
for grouped_by in getattr(group, "IsGroupedBy"): |
| 158 |
if not hasattr(grouped_by, "RelatedObjects"): |
| 159 |
errors.append(f'The element "{grouped_by.Name}" has no "RelatedObjects" attribute.') |
| 160 |
else: |
| 161 |
for related_object in getattr(grouped_by, "RelatedObjects"): |
| 162 |
found = False |
| 163 |
for linked_ifc_class in linked_ifc_classes.split(","): |
| 164 |
if related_object.is_a(linked_ifc_class): |
| 165 |
found = True |
| 166 |
if not found: |
| 167 |
errors.append( |
| 168 |
f'The element "{related_object.Name}" does not have the right associated type.' |
| 169 |
) |
| 170 |
assert not errors, "Errors occured:\n{}".format("\n".join(errors)) |
| 171 |
|
| 172 |
|
| 173 |
@then('there is an element of type "(?P<ifc_types>.*)" with an attribute "(?P<attribute_name>.*)" for each key') |
| 174 |
def step_impl(context, ifc_types, attribute_name): |
| 175 |
check_if_element_exists_by_types_with_attribute_name(ifc_types, attribute_name, context.model.rows.keys()) |
| 176 |
|
| 177 |
|
| 178 |
@then('there is an element of type "(?P<ifc_types>.*)" with an attribute "(?P<attribute_name>.*)" for each value') |
| 179 |
def step_impl(context, ifc_types, attribute_name): |
| 180 |
values = set(item for sublist in context.model.rows.values() for item in sublist) |
| 181 |
check_if_element_exists_by_types_with_attribute_name(ifc_types, attribute_name, values) |
| 182 |
|
| 183 |
|
| 184 |
def check_if_element_exists_by_types_with_attribute_name(ifc_types, attribute_name, attribute_values): |
| 185 |
errors = [] |
| 186 |
# retrieve all elements of that type |
| 187 |
elements = util.by_types(IfcStore.file, ifc_types) |
| 188 |
# loop |
| 189 |
for attribute_value in attribute_values: |
| 190 |
found = False |
| 191 |
for element in elements: |
| 192 |
if hasattr(element, attribute_name) and getattr(element, attribute_name) == attribute_value: |
| 193 |
found = True |
| 194 |
if not found: |
| 195 |
errors.append(f'An element with attribute "{attribute_name}" equals to "{attribute_value}" was not found.') |
| 196 |
assert not errors, "Errors occured:\n{}".format("\n".join(errors)) |
| 197 |
|
| 198 |
|
| 199 |
def check_starts_with(attribute_name, value, elements): |
| 200 |
"""Make sure at least an element of the list has a attribute starting with the value""" |
| 201 |
if any(hasattr(x, attribute_name) and getattr(x, attribute_name).startswith(value) for x in elements): |
| 202 |
return True |
| 203 |
return False |
| 204 |
|
| 205 |
|
| 206 |
def check_equals(attribute_name, value, elements): |
| 207 |
"""Make sure at least an element of the list has a attribute equals to the value""" |
| 208 |
if any(hasattr(x, attribute_name) and getattr(x, attribute_name) == value for x in elements): |
| 209 |
return True |
| 210 |
return False |