validate fixtures: validate expected error count

This commit is contained in:
Andrej730
2026-08-03 19:46:18 +05:00
parent c1c94b9362
commit cdf107e579
40 changed files with 72 additions and 48 deletions
@@ -52,15 +52,17 @@ def write_fixture(file: ifcopenshell.file, source: str, result: Result, stem: st
prefix = "fail" if expected_count == 1 else f"fail-expected-{expected_count}"
case _:
prefix = result
Path(f"{prefix}-{stem}.ifc").write_text(content)
(Path(source).parent / f"{prefix}-{stem}.ifc").write_text(content)
if __name__ == "__main__":
directory = Path(__file__).parent
root = Path(__file__).parent.parent
directory = Path(__file__).parent / "fixtures/rules"
for path in directory.glob("*.ifc"):
path.unlink()
for path in sorted(directory.glob("generate_*.py")):
module = f"test.fixtures.rules.{path.stem}"
print(f"=== {path.name} ===")
subprocess.run([sys.executable, str(path)], cwd=directory, check=True)
subprocess.run([sys.executable, "-m", module], cwd=root, check=True)
+1 -1
View File
@@ -1,7 +1,7 @@
import time
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for i in range(3):
f = ifcopenshell.file(schema="IFC2X3")
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for i, box_alignment in enumerate(["top-left", "center", "invalid", "CENTER"]):
f = ifcopenshell.file(schema="IFC2X3")
+1 -1
View File
@@ -1,7 +1,7 @@
import time
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
latitudes = [
(False, (-361, 0, 0), 1),
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for depth in (-1.0, 0.0, 1.0):
f = ifcopenshell.file(schema="IFC2X3")
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import fail_if, normalize_header, write_fixture
from ...fixture_generate import fail_if, normalize_header, write_fixture
for i, (r1, r2) in enumerate(
[("SUPPLIER", None), ("SUPPLIER", "Valid"), ("USERDEFINED", "Valid"), ("USERDEFINED", None)]
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
options = {
"IfcPostalAddress": ({}, {"Country": "The Netherlands"}, {"Country": "The Netherlands", "Town": "Eindhoven"}),
+1 -1
View File
@@ -2,7 +2,7 @@ import itertools
import time
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for pty, ety in itertools.product(("GRILLE", "USERDEFINED"), (None, "Something")):
f = ifcopenshell.file(schema="IFC2X3")
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import fail_if, normalize_header, write_fixture
from ...fixture_generate import fail_if, normalize_header, write_fixture
create_none = lambda f: None
create_polyline = lambda f: f.createIfcPolyline(
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import fail_if, normalize_header, write_fixture
from ...fixture_generate import fail_if, normalize_header, write_fixture
create_plane = lambda f: f.createIfcPlane(f.createIfcAxis2Placement3D(f.createIfcCartesianPoint((0.0, 0.0, 0.0))))
create_polyline = lambda f: f.createIfcPolyline(
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
pts = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)]
dims = [(2, 3, 3), (3, 3, 2), (2, 2, 2), (3, 3, 3)]
+1 -1
View File
@@ -1,7 +1,7 @@
import itertools
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
defaults = {"Girth": 1.0, "WallThickness": 0.11}
depths = [2.0, 3.0]
+1 -1
View File
@@ -1,6 +1,6 @@
import ifcopenshell
import ifcopenshell.util.schema
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
depth = 1.0
for dir_x, dir_z in ((0.0, -1.0), (0.0, 0.0), (1.0, 0.0), (1.0, 0.001), (0.0, 1.0)):
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, write_fixture
from ...fixture_generate import normalize_header, write_fixture
coords = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
make_3d = lambda cs: [c + (0.0,) for c in cs]
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, write_fixture
from ...fixture_generate import normalize_header, write_fixture
coords = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
make_3d = lambda cs: [c + (0.0,) for c in cs]
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
dims = [
(1, 0, 0, 0, 0, 0, 0),
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
coords = [(0.0, 0.0), (10.0, 0.0)]
coords_2 = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
+1 -1
View File
@@ -1,7 +1,7 @@
import time
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
names = [("Same", "Same"), ("Different", "SomethingElse")]
+1 -1
View File
@@ -1,7 +1,7 @@
import time
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for i, nm in enumerate(("no-mls", "mls", "mlsu")):
f = ifcopenshell.file(schema="IFC2X3")
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for i, type_decl in enumerate(("IfcLengthMeasure", "IfcPlaneAngleMeasure")):
f = ifcopenshell.file(schema="IFC4X3")
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
make_nd = lambda d: lambda *c: (c + (0.0,)) if d == 3 else c
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
task = lambda f: f.createIfcTask(ifcopenshell.guid.new(), None, "sleep", IsMilestone=True)
wall = lambda f: f.createIfcWall(ifcopenshell.guid.new())
+1 -1
View File
@@ -1,7 +1,7 @@
import itertools
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
def EXISTS(v):
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for cnt in range(0, 3):
f = ifcopenshell.file(schema="IFC4")
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
fns = [
(False, lambda f: f.createIfcLengthMeasure(-1.0)),
+1 -1
View File
@@ -1,7 +1,7 @@
import time
import ifcopenshell
from generate import normalize_header, pass_if, write_fixture
from ...fixture_generate import normalize_header, pass_if, write_fixture
for ents in [
("IfcWall",),
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import fail_if, normalize_header, write_fixture
from ...fixture_generate import fail_if, normalize_header, write_fixture
segs = [
lambda _: None,
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, write_fixture
from ...fixture_generate import normalize_header, write_fixture
cases = (
("pass", "label-values", ("test1", "test2")),
+1 -1
View File
@@ -1,5 +1,5 @@
import ifcopenshell
from generate import normalize_header, write_fixture
from ...fixture_generate import normalize_header, write_fixture
ifc_file = ifcopenshell.file(schema="IFC2X3")
ifc_file.create_entity(
@@ -5,7 +5,9 @@ FILE_NAME('','2022-10-01T16:31:47',(''),(''),'IfcOpenShell 0.7.0','IfcOpenShell
FILE_SCHEMA(('IFC4'));
ENDSEC;
DATA;
/* Non-existent entity. */
#1=IFCBUILTELEMENT('2CbOYFeCHBku9$oytmyQ2e',$,$,$,$,$,$,$);
/* Non-existsent entity used as part of an attribute. */
#2=IFCRELASSOCIATESMATERIAL('0e6aLxV9b8OvwguVdjzRsn',$,$,$,(#1),#3);
#3=IFCMATERIAL('brick',$,$);
ENDSEC;
@@ -1,8 +1,7 @@
from pathlib import Path
import ifcopenshell
import ifcopenshell.api.owner
import ifcopenshell.api.root
from ...fixture_generate import normalize_header, pass_if, write_fixture
guid1 = "3pdqyORIn8KBHZAhhtJ72T"
guid2 = "1sEzC8v31DshmvW5t5P631"
@@ -23,5 +22,5 @@ for schema in ("IFC2X3", "IFC4", "IFC4X3"):
wall1.GlobalId = guid1
wall2.GlobalId = guid1 if status == "fail" else guid2
filename = f"{status}-{'not-' if status == 'pass' else ''}duplicated-guids-{schema.lower()}.ifc"
f.write(Path(__file__).parent / filename)
normalize_header(f)
write_fixture(f, __file__, pass_if(status == "pass"), f"duplicated-guids-{schema.lower()}")
+1 -1
View File
@@ -5,7 +5,7 @@ import tabulate
import ifcopenshell.express.rule_executor
import ifcopenshell.validate
from .fixtures.rules.generate import FailObj, Result, parse_result
from .fixture_generate import FailObj, Result, parse_result
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
+33 -12
View File
@@ -16,29 +16,50 @@
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import glob
import os
from pathlib import Path
import pytest
import tabulate
import ifcopenshell.validate
from .fixture_generate import FailObj, Result, parse_result
_filepaths = list((Path(__file__).parent / "fixtures/validate").glob("*.ifc"))
@pytest.mark.parametrize(
"file",
glob.glob(os.path.join(os.path.dirname(__file__), "fixtures/validate/*.ifc")),
"filepath,expected_result",
[(fp, parse_result(fp)) for fp in _filepaths],
ids=[fp.name for fp in _filepaths],
)
def test_file(file):
def test_file(filepath: Path, expected_result: Result):
logger = ifcopenshell.validate.json_logger()
try:
ifcopenshell.validate.validate(file, logger)
except ifcopenshell.SchemaError as e:
ifcopenshell.validate.validate(filepath, logger)
except ifcopenshell.SchemaError:
pytest.skip()
file = os.path.basename(file)
if file.startswith("fail-"):
assert len(logger.statements) > 0
if file.startswith("pass-"):
assert len(logger.statements) == 0
results = logger.statements
print()
print(filepath.name)
print()
print(f"{len(results)} errors")
if results:
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(),
)
)
match expected_result:
case FailObj(expected_count=expected_count):
assert len(results) == expected_count
case "pass":
assert len(results) == 0
if __name__ == "__main__":