From e7d5add13dfb4f536fdaf68e482f61cf54d76cc6 Mon Sep 17 00:00:00 2001 From: CyrilWaechter Date: Tue, 22 Nov 2022 00:40:27 +0100 Subject: [PATCH] Add test suite for ifcopenshell.open #2350 Fix open function: format argument was unused --- .../ifcopenshell/__init__.py | 28 ++++---- src/ifcopenshell-python/test/test_open.py | 72 +++++++++++++++++++ 2 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 src/ifcopenshell-python/test/test_open.py diff --git a/src/ifcopenshell-python/ifcopenshell/__init__.py b/src/ifcopenshell-python/ifcopenshell/__init__.py index f6bd455efc..c83183e66a 100644 --- a/src/ifcopenshell-python/ifcopenshell/__init__.py +++ b/src/ifcopenshell-python/ifcopenshell/__init__.py @@ -91,7 +91,17 @@ def guess_format(path): return ".ifcXML" -def open_path(path: os.PathLike | str, format: str = None) -> file: +def open(path: os.PathLike | str, format: str = None) -> file: + """Loads an IFC dataset from a filepath + + You can specify a file format. If no format is given, it is guessed from its extension. + Currently supported specified format : .ifc | .ifcZIP | .ifcXML + + Examples: + model = ifcopenshell.open("/path/to/model.ifc") + model = ifcopenshell.open("/path/to/model.ifcXML") + model = ifcopenshell.open("/path/to/model.any_extension", ".ifc") + """ path = Path(path) if format is None: format = guess_format(path) @@ -105,7 +115,7 @@ def open_path(path: os.PathLike | str, format: str = None) -> file: with zipfile.ZipFile(path) as zf: for name in zf.namelist(): if Path(name).suffix.lower() in (".ifc", ".ifcxml"): - return open_path(zf.extract(name, unzipped_path)) + return open(zf.extract(name, unzipped_path)) else: raise LookupError(f"No .ifc or .ifcXML file found in {path}") f = ifcopenshell_wrapper.open(str(path.absolute())) @@ -124,20 +134,6 @@ def open_path(path: os.PathLike | str, format: str = None) -> file: raise exc(msg) -def open(path: os.PathLike | str, format: str = None) -> file: - """Loads an IFC dataset from a filepath - - You can specify a file format. If no format is given, it is guessed from its extension. - Currently supported specified format : .ifc | .ifcZIP | .ifcXML - - Examples: - model = ifcopenshell.open("/path/to/model.ifc") - model = ifcopenshell.open("/path/to/model.ifcXML") - model = ifcopenshell.open("/path/to/model.any_extension", ".ifc") - """ - return open_path(path) - - def create_entity(type, schema="IFC4", *args, **kwargs): """Creates a new IFC entity that does not belong to an IFC file object diff --git a/src/ifcopenshell-python/test/test_open.py b/src/ifcopenshell-python/test/test_open.py new file mode 100644 index 0000000000..6bb3ecfb27 --- /dev/null +++ b/src/ifcopenshell-python/test/test_open.py @@ -0,0 +1,72 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Thomas Krijnen +# +# This file is part of IfcOpenShell. +# +# IfcOpenShell is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcOpenShell is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with IfcOpenShell. If not, see . + +from pathlib import Path +import pytest +import ifcopenshell + + +TEST_FILE_DIR = Path("./test/input/") + + +class TestOpen: + def test_open_ifcspf(self): + assert ifcopenshell.open(TEST_FILE_DIR / "WallInstance_IFC4Add2.ifc") + + def test_open_ifcxml(self): + assert ifcopenshell.open(TEST_FILE_DIR / "wall-with-opening-and-window.ifcxml") + + def test_open_ifc_zip_ifcxml_format(self): + assert ifcopenshell.open( + TEST_FILE_DIR / "wall-with-opening-and-window_ifcxml_format.ifczip" + ) + + def test_open_ifc_zip_ifcspf_format(self): + assert ifcopenshell.open( + TEST_FILE_DIR / "WallInstance_IFC4Add2_ifcspf_format.ifczip" + ) + + def test_open_zip(self): + assert ifcopenshell.open( + TEST_FILE_DIR / "WallInstance_IFC4Add2_ifcspf_format.zip" + ) + + def test_open_anyextension_ifcspf_format(self): + assert ifcopenshell.open( + TEST_FILE_DIR / "WallInstance_IFC4Add2_ifcspf_format.anyextension" + ) + + def test_open_anyextension_ifczip_ifcspf_format(self): + assert ifcopenshell.open( + TEST_FILE_DIR / "WallInstance_IFC4Add2_ifczip_ifcspf_format.anyextension", + ".ifcZIP", + ) + + def test_open_anyextension_ifcxml_format(self): + assert ifcopenshell.open( + TEST_FILE_DIR / "wall-with-opening-and-window_ifcxml_format.anyextension", + ".ifcXML", + ) + + def test_invalid_ifcspf(self): + with pytest.raises(ifcopenshell.Error): + assert ifcopenshell.open(TEST_FILE_DIR / "invalid.ifc") + + def test_invalid_ifcxml(self): + with pytest.raises(IOError): + assert ifcopenshell.open(TEST_FILE_DIR / "invalid.ifcxml")