copy_cost_schedule - add a test

This commit is contained in:
Andrej730
2025-07-24 12:05:01 +05:00
parent f8a933e094
commit f3efb1601a
2 changed files with 51 additions and 1 deletions
@@ -22,6 +22,7 @@ from typing import Optional, Union, Literal, Any
from collections.abc import Generator
import ifcopenshell.ifcopenshell_wrapper as ifcopenshell_wrapper
import ifcopenshell.util.attribute
import ifcopenshell.util.element
from ifcopenshell.util.doc import get_predefined_type_doc
from ifcopenshell.util.element import get_psets
from ifcopenshell.util.unit import get_unit_symbol
@@ -211,12 +212,13 @@ def get_nested_cost_items(cost_item: ifcopenshell.entity_instance, is_deep=False
if is_deep:
return list(get_all_nested_cost_items(cost_item))
else:
return [obj for rel in cost_item.IsNestedBy for obj in rel.RelatedObjects]
return ifcopenshell.util.element.get_components(cost_item)
def get_schedule_cost_items(
cost_schedule: ifcopenshell.entity_instance,
) -> Generator[ifcopenshell.entity_instance, None, None]:
"""Get all cost schedule cost items, including the nested ones."""
for cost_item in get_root_cost_items(cost_schedule):
yield cost_item
yield from get_all_nested_cost_items(cost_item)
@@ -0,0 +1,48 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2024 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
import ifcopenshell.api.cost
import ifcopenshell.util.cost
import test.bootstrap
class TestCopyCostSchedule(test.bootstrap.IFC4):
def test_run(self):
schedule = ifcopenshell.api.cost.add_cost_schedule(self.file, name="Foo")
item = ifcopenshell.api.cost.add_cost_item(self.file, cost_schedule=schedule)
ifcopenshell.api.cost.add_cost_item(self.file, cost_item=item) # Subitem.
old_cost_items = set(self.file.by_type("IfcCostItem"))
new_schedule = ifcopenshell.api.cost.copy_cost_schedule(self.file, schedule)
# We don't check how well IfcCostItems are copied,
# it should be tested separately in test_copy_cost_item.
assert isinstance(new_schedule, ifcopenshell.entity_instance)
assert new_schedule != schedule
assert len(ifcopenshell.util.cost.get_root_cost_items(new_schedule)) == 1
new_cost_items = set(ifcopenshell.util.cost.get_schedule_cost_items(new_schedule))
assert len(new_cost_items) == 2
assert len(new_cost_items.intersection(old_cost_items)) == 0
class TestCopyCostScheduleIFC2X3(test.bootstrap.IFC2X3, TestCopyCostSchedule):
pass
class TestCopyCostScheduleIFC4X3(test.bootstrap.IFC4X3, TestCopyCostSchedule):
pass