Files
IfcOpenShell/src/ifcopenshell-python/test/test_rules.py
T

53 lines
1.5 KiB
Python
Raw Normal View History

from pathlib import Path
2022-12-26 11:29:32 +01:00
import pytest
import tabulate
import ifcopenshell.express.rule_executor
2025-12-19 18:53:04 +05:00
import ifcopenshell.validate
from .fixture_generate import FailObj, Result, parse_result
2022-12-26 11:29:32 +01:00
2026-07-26 18:03:09 +10:00
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
if "filepath" not in metafunc.fixturenames:
return
rule = metafunc.config.getoption("--rule")
filepaths = [fp for fp in (Path(__file__).parent / "fixtures/rules").glob("*.ifc") if not rule or rule in fp.name]
metafunc.parametrize(
"filepath,expected_result",
[(fp, parse_result(fp)) for fp in filepaths],
ids=[fp.name for fp in filepaths],
)
def test_file(filepath: Path, expected_result: Result):
file = ifcopenshell.open(filepath)
2022-12-26 11:29:32 +01:00
logger = ifcopenshell.validate.json_logger()
ifcopenshell.express.rule_executor.run(file, logger)
results = logger.statements
print()
print(filepath.name)
2022-12-26 11:29:32 +01:00
print()
print(f"{len(results)} errors")
if results:
2024-07-26 12:00:28 +10:00
print(
tabulate.tabulate(
[[c or "" for c in r.values()] for r in results],
maxcolwidths=[20, 100, 20],
tablefmt="simple_grid",
headers=results[0].keys(),
)
)
2022-12-26 11:29:32 +01:00
match expected_result:
case FailObj(expected_count=expected_count):
assert len(results) == expected_count
case "pass":
assert len(results) == 0
2022-12-26 11:29:32 +01:00
if __name__ == "__main__":
2026-07-26 18:03:09 +10:00
pytest.main(["-sx", __file__, "--import-mode=importlib"])