From 8792c6db63dc9daa3a28e7d18c8ad49db270fe1b Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 31 Aug 2021 17:28:31 +1000 Subject: [PATCH] Test IFC file module. See #1711. --- src/ifcopenshell-python/Makefile | 2 +- src/ifcopenshell-python/ifcopenshell/file.py | 15 +- src/ifcopenshell-python/test/test_file.py | 210 +++++++++++++++++++ 3 files changed, 214 insertions(+), 13 deletions(-) create mode 100644 src/ifcopenshell-python/test/test_file.py diff --git a/src/ifcopenshell-python/Makefile b/src/ifcopenshell-python/Makefile index 13c83f1139..914fdbae0e 100644 --- a/src/ifcopenshell-python/Makefile +++ b/src/ifcopenshell-python/Makefile @@ -1,6 +1,6 @@ .PHONY: test test: - pytest -o "testpaths=test" + cd test && pytest .PHONY: coverage coverage: diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index 98fc4799b1..0edd4cce33 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -23,6 +23,7 @@ from __future__ import print_function import numbers import functools +import ifcopenshell.util.element from . import ifcopenshell_wrapper from .entity_instance import entity_instance @@ -111,17 +112,11 @@ class Transaction: for inverse in self.file.get_inverse(element): inverse_references = [] for i, attribute in enumerate(inverse): - if self.has_element_reference(attribute, element): + if ifcopenshell.util.element.has_element_reference(attribute, element): inverse_references.append((i, self.serialise_value(inverse, attribute))) inverses[inverse.id()] = inverse_references return inverses - def has_element_reference(self, value, element): - if isinstance(value, (tuple, list)): - for v in value: - return self.has_element_reference(v, element) - return value == element - def rollback(self): for operation in self.operations[::-1]: if operation["action"] == "create": @@ -259,11 +254,7 @@ class file(object): f.create_entity('IfcPerson', Identification='Foobar') >>> #3=IfcPerson('Foobar',$,$,$,$,$,$,$) """ - eid = -1 - try: - eid = kwargs.pop("id", -1) - except: - pass + eid = kwargs.pop("id", -1) e = entity_instance((self.schema, type), self) diff --git a/src/ifcopenshell-python/test/test_file.py b/src/ifcopenshell-python/test/test_file.py new file mode 100644 index 0000000000..7aed49a7e0 --- /dev/null +++ b/src/ifcopenshell-python/test/test_file.py @@ -0,0 +1,210 @@ +import pytest +import test.bootstrap +import ifcopenshell.api +import ifcopenshell.util.element + + +class TestTransaction(test.bootstrap.IFC4): + def test_that_nothing_happens_without_a_transaction(self): + wall = self.file.createIfcWall() + self.file.undo() + assert wall + + def test_that_you_can_undo_and_redo_creation(self): + unchanged = self.file.createIfcWall() + self.file.begin_transaction() + wall = self.file.createIfcWall() + self.file.end_transaction() + self.file.undo() + assert unchanged + with pytest.raises(RuntimeError): + self.file.by_id(2) + self.file.redo() + self.file.by_id(2) + + def test_that_you_can_undo_and_redo_editing(self): + element = self.file.createIfcWall(Name="foo") + self.file.begin_transaction() + element.Name = "bar" + self.file.end_transaction() + self.file.undo() + assert element.Name == "foo" + self.file.redo() + assert element.Name == "bar" + + def test_that_you_can_undo_and_redo_deletion(self): + element = self.file.createIfcWall(GlobalId="id") + self.file.begin_transaction() + self.file.remove(element) + self.file.end_transaction() + self.file.undo() + assert self.file.by_id(1) + self.file.redo() + with pytest.raises(RuntimeError): + self.file.by_id(1) + + def test_that_you_can_undo_and_redo_deletion_with_inverse_relationships(self): + element = self.file.createIfcWall(GlobalId="id") + rel = self.file.createIfcRelAggregates() + rel.RelatingObject = element + self.file.begin_transaction() + self.file.remove(element) + self.file.end_transaction() + self.file.undo() + assert rel.RelatingObject == self.file.by_id(1) + self.file.redo() + assert rel.RelatingObject is None + + def test_that_you_can_undo_and_redo_batched_deletion_with_inverse_relationships(self): + element = self.file.createIfcWall(GlobalId="id") + rel = self.file.createIfcRelAggregates() + rel.RelatingObject = element + self.file.begin_transaction() + self.file.batch() + self.file.remove(element) + self.file.unbatch() + self.file.end_transaction() + self.file.undo() + assert rel.RelatingObject == self.file.by_id(1) + self.file.redo() + assert rel.RelatingObject is None + + def test_that_you_can_undo_and_redo_deletion_with_aggregated_inverse_relationships(self): + element = self.file.createIfcWall(GlobalId="id") + rel = self.file.createIfcRelAggregates() + rel.RelatedObjects = [element] + self.file.begin_transaction() + self.file.remove(element) + self.file.end_transaction() + self.file.undo() + assert rel.RelatedObjects == (self.file.by_id(1),) + self.file.redo() + assert len(rel.RelatedObjects) == 0 + + def test_the_editing_of_invalid_default_values(self): + element = self.file.createIfcWall() # This element is invalid, as GlobalId is None + self.file.begin_transaction() + element.GlobalId = "id" + self.file.end_transaction() + self.file.undo() + + def test_setting_the_history_size(self): + self.file.set_history_size(2) + self.file.begin_transaction() + self.file.end_transaction() + self.file.begin_transaction() + self.file.end_transaction() + self.file.begin_transaction() + self.file.end_transaction() + assert len(self.file.history) == 2 + self.file.set_history_size(1) + assert len(self.file.history) == 1 + + def test_discarding_the_active_transaction(self): + self.file.begin_transaction() + self.file.discard_transaction() + self.file.end_transaction() + assert len(self.file.history) == 0 + + def test_redoing_without_anything_in_the_redo_stack(self): + self.file.redo() + + def test_that_you_can_undo_and_redo_added_elements(self): + g = ifcopenshell.file() + element = g.createIfcWall() + self.file.begin_transaction() + self.file.add(element) + self.file.end_transaction() + self.file.undo() + assert len(list(self.file)) == 0 + self.file.redo() + assert len(list(self.file)) == 1 + + def test_that_you_can_undo_and_redo_added_subelements(self): + g = ifcopenshell.file() + owner = g.createIfcOwnerHistory() + element = g.createIfcWall(OwnerHistory=owner) + self.file.begin_transaction() + self.file.add(element) + self.file.end_transaction() + self.file.undo() + assert len(list(self.file)) == 0 + self.file.redo() + assert len(list(self.file)) == 2 + + +class TestFile(test.bootstrap.IFC4): + def test_creating_a_new_file(self): + f = ifcopenshell.file(schema="IFC4") + assert f.schema == "IFC4" + + def test_creating_an_entity(self): + element = self.file.create_entity("IfcPerson") + assert element.is_a("IfcPerson") + element = self.file.create_entity("IfcPerson", "identification") + assert element.Identification == "identification" + element = self.file.create_entity("IfcPerson", Identification="identification") + assert element.Identification == "identification" + element = self.file.create_entity("IfcPerson", Identification="identification", id=42) + assert element.id() == 42 + element = self.file.createIfcPerson() + assert element.is_a("IfcPerson") + + def test_getting_an_element_by_id(self): + element = self.file.createIfcWall("id") + assert self.file.by_id(1) == element + assert self.file.by_id("id") == element + + def test_getting_an_element_by_guid(self): + element = self.file.createIfcWall("id") + assert self.file.by_guid(1) == element + assert self.file.by_guid("id") == element + + def test_adding_an_element(self): + g = ifcopenshell.file() + element = g.createIfcWall() + result = self.file.add(element) + assert result.is_a() == element.is_a() + + def test_getting_elements_by_type(self): + wall = self.file.createIfcWall() + slab = self.file.createIfcSlab() + assert self.file.by_type("IfcWall") == [wall] + + def test_getting_elements_by_exact_type(self): + wall = self.file.createIfcWall() + assert self.file.by_type("IfcElement") == [wall] + assert len(self.file.by_type("IfcElement", include_subtypes=False)) == 0 + + def test_traversing_direct_attributes_of_an_element(self): + owner = self.file.createIfcOwnerHistory() + element = self.file.createIfcWall(OwnerHistory=owner) + assert self.file.traverse(element) == [element, owner] + + def test_traversing_direct_attributes_of_an_element_to_a_limited_level(self): + app = self.file.createIfcApplication() + owner = self.file.createIfcOwnerHistory(OwningApplication=app) + element = self.file.createIfcWall(OwnerHistory=owner) + assert self.file.traverse(element, max_levels=1) == [element, owner] + + def test_getting_inverse_references_of_an_element(self): + owner = self.file.createIfcOwnerHistory() + element = self.file.createIfcWall(OwnerHistory=owner) + assert self.file.get_inverse(owner) == [element] + + def test_removing_an_element(self): + element = self.file.createIfcWall(GlobalId="global_id") + self.file.remove(element) + assert len(list(self.file)) == 0 + + def test_batched_removing_an_element(self): + element = self.file.createIfcWall(GlobalId="global_id") + self.file.batch() + self.file.remove(element) + self.file.unbatch() + assert len(list(self.file)) == 0 + + def test_creating_ifc_data_from_a_string(self): + element = self.file.createIfcWall() + g = ifcopenshell.file.from_string(self.file.wrapped_data.to_string()) + assert g.by_id(1).is_a("IfcWall")