adapted to get pytest -p no:pytest-blender test/core/test_document.py working. Black formating

This commit is contained in:
falken10vdl
2025-08-21 12:05:41 +02:00
parent 454ed2c5a1
commit 81fdf63bd0
7 changed files with 91 additions and 44 deletions
@@ -23,6 +23,7 @@ import bonsai.tool as tool
import bonsai.core.document as core import bonsai.core.document as core
from bonsai.bim.module.document.data import DocumentData, ObjectDocumentData from bonsai.bim.module.document.data import DocumentData, ObjectDocumentData
class LoadProjectDocuments(bpy.types.Operator): class LoadProjectDocuments(bpy.types.Operator):
bl_idname = "bim.load_project_documents" bl_idname = "bim.load_project_documents"
bl_label = "Load Project Documents" bl_label = "Load Project Documents"
@@ -287,6 +288,7 @@ class OpenIFCDocument(bpy.types.Operator):
return {"FINISHED"} return {"FINISHED"}
class ToggleDocument(bpy.types.Operator): class ToggleDocument(bpy.types.Operator):
bl_idname = "bim.toggle_document" bl_idname = "bim.toggle_document"
bl_label = "Toggle Document" bl_label = "Toggle Document"
@@ -22,6 +22,7 @@ from bpy.types import Panel, UIList
from bonsai.bim.helper import draw_attributes from bonsai.bim.helper import draw_attributes
from bonsai.bim.module.document.data import DocumentData, ObjectDocumentData from bonsai.bim.module.document.data import DocumentData, ObjectDocumentData
class BIM_PT_documents(Panel): class BIM_PT_documents(Panel):
bl_label = "Documents" bl_label = "Documents"
bl_idname = "BIM_PT_documents" bl_idname = "BIM_PT_documents"
+2 -2
View File
@@ -53,7 +53,7 @@ def add_information(ifc: tool.Ifc, document: tool.Document, parent=None) -> ifco
document.clear_document_tree() document.clear_document_tree()
if parent is None: if parent is None:
parent = document.get_default_parent_for_information(ifc) parent = document.get_default_parent_for_information()
information = ifc.run("document.add_information", parent=parent) information = ifc.run("document.add_information", parent=parent)
ifc.run("document.add_reference", information=information) ifc.run("document.add_reference", information=information)
@@ -66,7 +66,7 @@ def add_information(ifc: tool.Ifc, document: tool.Document, parent=None) -> ifco
def add_reference(ifc: tool.Ifc, document: tool.Document) -> None: def add_reference(ifc: tool.Ifc, document: tool.Document) -> None:
parent = document.get_selected_document_information(ifc) parent = document.get_selected_document_information()
if parent: if parent:
reference = ifc.run("document.add_reference", information=parent) reference = ifc.run("document.add_reference", information=parent)
+14
View File
@@ -289,6 +289,7 @@ class Debug:
class Document: class Document:
def clear_document_tree(cls): pass def clear_document_tree(cls): pass
def disable_editing_document(cls): pass def disable_editing_document(cls): pass
def disable_object_editing_ui(cls): pass
def disable_editing_ui(cls): pass def disable_editing_ui(cls): pass
def enable_editing_ui(cls): pass def enable_editing_ui(cls): pass
def export_document_attributes(cls): pass def export_document_attributes(cls): pass
@@ -296,6 +297,19 @@ class Document:
def import_project_documents(cls): pass def import_project_documents(cls): pass
def is_document_information(cls, document): pass def is_document_information(cls, document): pass
def set_active_document(cls, document): pass def set_active_document(cls, document): pass
def clear_active_document(cls): pass
def clear_document_attributes(cls): pass
def expand_document(cls, document): pass
def get_default_parent_for_information(cls): pass
def get_selected_document_information(cls): pass
def get_document_information_id(cls, document): pass
def set_document_information_id(cls, document, value): pass
def get_external_reference_id(cls, reference): pass
def set_external_reference_id(cls, reference, value): pass
def get_document_references(cls, document): pass
def refresh_document_data(cls): pass
def load_document_objects_into_props(cls, document_id): pass
def update_document_objects(cls, document_id): pass
@interface @interface
+13 -7
View File
@@ -173,7 +173,11 @@ class Document(bonsai.core.tool.Document):
new.tree_depth = depth new.tree_depth = depth
new.name = document.Name or "" new.name = document.Name or ""
new.identification = cls.get_document_information_id(document) if new.document_type == "INFORMATION" else cls.get_external_reference_id(document) new.identification = (
cls.get_document_information_id(document)
if new.document_type == "INFORMATION"
else cls.get_external_reference_id(document)
)
new.identification = new.identification or "" new.identification = new.identification or ""
new.description = document.Description or "" new.description = document.Description or ""
new.location = document.Location or "" new.location = document.Location or ""
@@ -200,12 +204,12 @@ class Document(bonsai.core.tool.Document):
info_children = natsorted( info_children = natsorted(
[d for d in children if d.is_a("IfcDocumentInformation")], [d for d in children if d.is_a("IfcDocumentInformation")],
key=lambda doc: (cls.get_document_information_id(doc) or "", doc.Name or "") key=lambda doc: (cls.get_document_information_id(doc) or "", doc.Name or ""),
) )
ref_children = natsorted( ref_children = natsorted(
[d for d in children if not d.is_a("IfcDocumentInformation")], [d for d in children if not d.is_a("IfcDocumentInformation")],
key=lambda doc: (cls.get_external_reference_id(doc) or "", doc.Description or doc.Name or "") key=lambda doc: (cls.get_external_reference_id(doc) or "", doc.Description or doc.Name or ""),
) )
for child in info_children + ref_children: for child in info_children + ref_children:
@@ -272,16 +276,18 @@ class Document(bonsai.core.tool.Document):
props.json_string = json.dumps(expanded_docs) props.json_string = json.dumps(expanded_docs)
@classmethod @classmethod
def get_default_parent_for_information(cls, ifc) -> Union[ifcopenshell.entity_instance, None]: def get_default_parent_for_information(cls) -> Union[ifcopenshell.entity_instance, None]:
projects = ifc.get().by_type("IfcProject") file = tool.Ifc.get()
projects = file.by_type("IfcProject")
return projects[0] if projects else None return projects[0] if projects else None
@classmethod @classmethod
def get_selected_document_information(cls, ifc) -> Union[ifcopenshell.entity_instance, None]: def get_selected_document_information(cls) -> Union[ifcopenshell.entity_instance, None]:
props = cls.get_document_props() props = cls.get_document_props()
if props.active_document and props.active_document.document_type == "INFORMATION": if props.active_document and props.active_document.document_type == "INFORMATION":
return ifc.get().by_id(props.active_document.ifc_definition_id) file = tool.Ifc.get()
return file.by_id(props.active_document.ifc_definition_id)
return None return None
@classmethod @classmethod
+48 -22
View File
@@ -16,7 +16,6 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>. # along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
import bonsai.core.document as subject import bonsai.core.document as subject
from test.core.bootstrap import ifc, document from test.core.bootstrap import ifc, document
@@ -29,14 +28,6 @@ class TestLoadProjectDocuments:
subject.load_project_documents(document) subject.load_project_documents(document)
class TestLoadDocument:
def test_run(self, document):
document.clear_document_tree().should_be_called()
document.disable_editing_document().should_be_called()
document.add_breadcrumb("document").should_be_called()
subject.load_document(document, document="document")
class TestDisableDocumentEditingUi: class TestDisableDocumentEditingUi:
def test_run(self, document): def test_run(self, document):
document.disable_editing_ui().should_be_called() document.disable_editing_ui().should_be_called()
@@ -44,38 +35,71 @@ class TestDisableDocumentEditingUi:
subject.disable_document_editing_ui(document) subject.disable_document_editing_ui(document)
class TestDisableObjectDocumentEditingUi:
def test_run(self, document):
document.disable_object_editing_ui().should_be_called()
subject.disable_object_document_editing_ui(document)
class TestEnableEditingDocument: class TestEnableEditingDocument:
def test_run(self, document): def test_run(self, document):
document.import_document_attributes("document").should_be_called()
document.set_active_document("document").should_be_called() document.set_active_document("document").should_be_called()
subject.enable_editing_document(document, document="document") document.import_document_attributes("document").should_be_called()
subject.enable_editing_document(document, ifc_document="document")
class TestDisableEditingDocument: class TestDisableEditingDocument:
def test_run(self, document): def test_run(self, document):
document.disable_editing_document().should_be_called() document.clear_active_document().should_be_called()
document.clear_document_attributes().should_be_called()
subject.disable_editing_document(document) subject.disable_editing_document(document)
class TestAddInformation: class TestAddInformation:
def test_add_and_reload_tree_at_project_root(self, ifc, document): def test_add_and_reload_tree_at_project_root(self, ifc, document):
document.clear_document_tree().should_be_called() document.clear_document_tree().should_be_called()
ifc.run("document.add_information", parent=None).should_be_called().will_return("information") document.get_default_parent_for_information().should_be_called().will_return("default_parent")
ifc.run("document.add_information", parent="default_parent").should_be_called().will_return("information")
ifc.run("document.add_reference", information="information").should_be_called() ifc.run("document.add_reference", information="information").should_be_called()
document.is_document_information("default_parent").should_be_called().will_return(True)
document.expand_document("default_parent").should_be_called()
document.import_project_documents().should_be_called() document.import_project_documents().should_be_called()
subject.add_information(ifc, document) subject.add_information(ifc, document)
def test_add_and_reload_tree_at_current_parent(self, ifc, document): def test_add_and_reload_tree_at_current_parent(self, ifc, document):
document.clear_document_tree().should_be_called() document.clear_document_tree().should_be_called()
ifc.run("document.add_information", parent="parent").should_be_called().will_return("information") ifc.run("document.add_information", parent="parent").should_be_called().will_return("information")
ifc.run("document.add_reference", information="information").should_be_called() ifc.run("document.add_reference", information="information").should_be_called()
subject.add_information(ifc, document) document.is_document_information("parent").should_be_called().will_return(True)
document.expand_document("parent").should_be_called()
document.import_project_documents().should_be_called()
subject.add_information(ifc, document, parent="parent")
def test_add_without_expanding_if_parent_is_not_information(self, ifc, document):
document.clear_document_tree().should_be_called()
ifc.run("document.add_information", parent="parent").should_be_called().will_return("information")
ifc.run("document.add_reference", information="information").should_be_called()
document.is_document_information("parent").should_be_called().will_return(False)
document.import_project_documents().should_be_called()
subject.add_information(ifc, document, parent="parent")
class TestAddReference: class TestAddReference:
def test_run(self, ifc, document): def test_run_with_selected_parent(self, ifc, document):
document.get_selected_document_information().should_be_called().will_return("parent")
ifc.run("document.add_reference", information="parent").should_be_called() ifc.run("document.add_reference", information="parent").should_be_called()
document.clear_document_tree().should_be_called() document.expand_document("parent").should_be_called()
document.import_project_documents().should_be_called()
subject.add_reference(ifc, document)
def test_run_without_selected_parent(self, ifc, document):
document.get_selected_document_information().should_be_called().will_return(None)
document.import_project_documents().should_be_called()
subject.add_reference(ifc, document) subject.add_reference(ifc, document)
@@ -87,7 +111,7 @@ class TestEditDocument:
document.disable_editing_document().should_be_called() document.disable_editing_document().should_be_called()
document.clear_document_tree().should_be_called() document.clear_document_tree().should_be_called()
document.import_project_documents().should_be_called() document.import_project_documents().should_be_called()
subject.edit_document(ifc, document, document="document") subject.edit_document(ifc, document, ifc_document="document")
def test_edit_reference(self, ifc, document): def test_edit_reference(self, ifc, document):
document.export_document_attributes().should_be_called().will_return("attributes") document.export_document_attributes().should_be_called().will_return("attributes")
@@ -95,7 +119,8 @@ class TestEditDocument:
ifc.run("document.edit_reference", reference="document", attributes="attributes").should_be_called() ifc.run("document.edit_reference", reference="document", attributes="attributes").should_be_called()
document.disable_editing_document().should_be_called() document.disable_editing_document().should_be_called()
document.clear_document_tree().should_be_called() document.clear_document_tree().should_be_called()
subject.edit_document(ifc, document, document="document") document.import_project_documents().should_be_called()
subject.edit_document(ifc, document, ifc_document="document")
class TestRemoveDocument: class TestRemoveDocument:
@@ -104,22 +129,23 @@ class TestRemoveDocument:
document.is_document_information("document").should_be_called().will_return(True) document.is_document_information("document").should_be_called().will_return(True)
ifc.run("document.remove_information", information="document").should_be_called() ifc.run("document.remove_information", information="document").should_be_called()
document.import_project_documents().should_be_called() document.import_project_documents().should_be_called()
subject.remove_document(ifc, document, document="document") subject.remove_document(ifc, document, ifc_document="document")
def test_remove_reference(self, ifc, document): def test_remove_reference(self, ifc, document):
document.clear_document_tree().should_be_called() document.clear_document_tree().should_be_called()
document.is_document_information("document").should_be_called().will_return(False) document.is_document_information("document").should_be_called().will_return(False)
ifc.run("document.remove_reference", reference="document").should_be_called() ifc.run("document.remove_reference", reference="document").should_be_called()
subject.remove_document(ifc, document, document="document") document.import_project_documents().should_be_called()
subject.remove_document(ifc, document, ifc_document="document")
class TestAssignDocument: class TestAssignDocument:
def test_run(self, ifc): def test_run(self, ifc):
ifc.run("document.assign_document", products=["product"], document="document").should_be_called() ifc.run("document.assign_document", products=["product"], document="document").should_be_called()
subject.assign_document(ifc, product="product", document="document") subject.assign_document(ifc, product="product", ifc_document="document")
class TestUnassignDocument: class TestUnassignDocument:
def test_run(self, ifc): def test_run(self, ifc):
ifc.run("document.unassign_document", products=["product"], document="document").should_be_called() ifc.run("document.unassign_document", products=["product"], document="document").should_be_called()
subject.unassign_document(ifc, product="product", document="document") subject.unassign_document(ifc, product="product", ifc_document="document")
-2
View File
@@ -148,7 +148,6 @@ class TestImportProjectDocumentsExpanded(NewFile):
document = ifcopenshell.api.document.add_information(ifc) document = ifcopenshell.api.document.add_information(ifc)
reference = ifcopenshell.api.document.add_reference(ifc, information=document) reference = ifcopenshell.api.document.add_reference(ifc, information=document)
props = tool.Document.get_document_props() props = tool.Document.get_document_props()
expanded_docs = [document.id()] # Mark document as expanded expanded_docs = [document.id()] # Mark document as expanded
props.json_string = json.dumps(expanded_docs) props.json_string = json.dumps(expanded_docs)
@@ -181,7 +180,6 @@ class TestImportProjectDocumentsCollapsed(NewFile):
document = ifcopenshell.api.document.add_information(ifc) document = ifcopenshell.api.document.add_information(ifc)
reference = ifcopenshell.api.document.add_reference(ifc, information=document) reference = ifcopenshell.api.document.add_reference(ifc, information=document)
props = tool.Document.get_document_props() props = tool.Document.get_document_props()
props.json_string = json.dumps([]) # Empty expanded list props.json_string = json.dumps([]) # Empty expanded list