Add support for null attributes for people, roles, and addresses. New prototoype testable architecture. See #1711.

This commit is contained in:
Dion Moult
2021-09-29 14:14:10 +10:00
parent 6fb4714317
commit 086d8749ec
31 changed files with 1975 additions and 361 deletions
+26 -22
View File
@@ -35,25 +35,25 @@ def blender():
prophet.verify()
def subject(sus):
def decorate(cls):
cls.sus = sus
return cls
return decorate
@pytest.fixture
def person_editor():
prophet = Prophecy(blenderbim.core.tool.PersonEditor)
yield prophet
prophet.verify()
class Spec:
@pytest.fixture(autouse=True)
def setup(self):
self.subject = None
@pytest.fixture
def role_editor():
prophet = Prophecy(blenderbim.core.tool.RoleEditor)
yield prophet
prophet.verify()
def construct_with(self, *args, **kwargs):
self.subject = self.sus(*args, **kwargs)
return self.subject
def predict(self, cls):
return Prophecy(cls)
@pytest.fixture
def address_editor():
prophet = Prophecy(blenderbim.core.tool.AddressEditor)
yield prophet
prophet.verify()
class Prophecy:
@@ -66,10 +66,12 @@ class Prophecy:
def __getattr__(self, attr):
if not hasattr(self.subject, attr):
raise AttributeError(f"Prophecy has no attribute {attr}")
raise AttributeError(f"Prophecy {self.subject} has no attribute {attr}")
def decorate(*args, **kwargs):
call = {"name": attr, "args": args, "kwargs": kwargs}
# Ensure that signature is valid
getattr(self.subject, attr)(*args, **kwargs)
try:
key = json.dumps(call, sort_keys=True)
self.calls.append(call)
@@ -81,23 +83,25 @@ class Prophecy:
return decorate
def should(self):
def should_be_called(self, number=None):
self.should_call = self.calls.pop()
return self
def be_called(self, number=None):
self.predictions.append({"type": "SHOULD_BE_CALLED", "number": number, "call": self.should_call})
return self
def return_with(self, value):
def will_return(self, value):
key = json.dumps(self.should_call, sort_keys=True)
self.return_values[key] = value
return self
def verify(self):
predicted_calls = []
for prediction in self.predictions:
predicted_calls.append(prediction["call"])
if prediction["type"] == "SHOULD_BE_CALLED":
self.verify_should_be_called(prediction)
for call in self.calls:
if call not in predicted_calls:
raise Exception(f"Unpredicted call: {call}")
def verify_should_be_called(self, prediction):
if prediction["number"]:
@@ -106,4 +110,4 @@ class Prophecy:
raise Exception(f"Called {count}: {prediction}")
else:
if prediction["call"] not in self.calls:
raise Exception("Not called", prediction)
raise Exception(f"{self.subject} was not called with {prediction['call']['name']}: {prediction}")
+149
View File
@@ -0,0 +1,149 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BlenderBIM Add-on 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import blenderbim.core.owner as subject
from test.core.bootstrap import ifc, blender, person_editor, role_editor, address_editor
class TestAddPerson:
def test_run(self, ifc):
ifc.run("owner.add_person").should_be_called().will_return("person")
assert subject.add_person(ifc) == "person"
class TestRemovePerson:
def test_run(self, ifc):
ifc.run("owner.remove_person", person="person").should_be_called()
subject.remove_person(ifc, person="person")
class TestEnableEditingPerson:
def test_run(self, person_editor):
person_editor.set_person("person").should_be_called()
person_editor.import_attributes().should_be_called()
subject.enable_editing_person(person_editor, person="person")
class TestDisableEditingPerson:
def test_run(self, person_editor):
person_editor.clear_person().should_be_called()
subject.disable_editing_person(person_editor)
class TestEditPerson:
def test_run(self, ifc, person_editor):
person_editor.get_person().should_be_called().will_return("person")
person_editor.export_attributes().should_be_called().will_return("attributes")
ifc.run("owner.edit_person", person="person", attributes="attributes").should_be_called()
person_editor.clear_person().should_be_called()
subject.edit_person(ifc, person_editor)
class TestAddPersonAttribute:
def test_run(self, person_editor):
person_editor.add_attribute("name").should_be_called()
subject.add_person_attribute(person_editor, name="name")
class TestRemovePersonAttribute:
def test_run(self, person_editor):
person_editor.remove_attribute("name", "id").should_be_called()
subject.remove_person_attribute(person_editor, name="name", id="id")
class TestAddRole:
def test_run(self, ifc):
ifc.run("owner.add_role", assigned_object="parent").should_be_called().will_return("role")
assert subject.add_role(ifc, parent="parent") == "role"
class TestRemoveRole:
def test_run(self, ifc):
ifc.run("owner.remove_role", role="role").should_be_called()
subject.remove_role(ifc, role="role")
class TestEnableEditingRole:
def test_run(self, role_editor):
role_editor.set_role("role").should_be_called()
role_editor.import_attributes().should_be_called()
subject.enable_editing_role(role_editor, role="role")
class TestDisableEditingRole:
def test_run(self, role_editor):
role_editor.clear_role().should_be_called()
subject.disable_editing_role(role_editor)
class TestEditRole:
def test_run(self, ifc, role_editor):
role_editor.export_attributes().should_be_called().will_return("attributes")
role_editor.get_role().should_be_called().will_return("role")
ifc.run("owner.edit_role", role="role", attributes="attributes").should_be_called()
role_editor.clear_role().should_be_called()
subject.edit_role(ifc, role_editor)
class TestAddAddress:
def test_run(self, ifc):
ifc.run(
"owner.add_address", assigned_object="parent", ifc_class="IfcPostalAddress"
).should_be_called().will_return("address")
assert subject.add_address(ifc, parent="parent", ifc_class="IfcPostalAddress") == "address"
class TestRemoveAddress:
def test_run(self, ifc):
ifc.run("owner.remove_address", address="address").should_be_called()
subject.remove_address(ifc, address="address")
class TestEnableEditingAddress:
def test_run(self, address_editor):
address_editor.set_address("address").should_be_called()
address_editor.import_attributes().should_be_called()
subject.enable_editing_address(address_editor, address="address")
class TestDisableEditingAddress:
def test_run(self, address_editor):
address_editor.clear_address().should_be_called()
subject.disable_editing_address(address_editor)
class TestEditAddress:
def test_run(self, ifc, address_editor):
address_editor.get_address().should_be_called().will_return("address")
address_editor.export_attributes().should_be_called().will_return("attributes")
ifc.run("owner.edit_address", address="address", attributes="attributes").should_be_called()
address_editor.clear_address().should_be_called()
subject.edit_address(ifc, address_editor)
class TestAddAddressAttribute:
def test_run(self, address_editor):
address_editor.add_attribute("name").should_be_called()
subject.add_address_attribute(address_editor, name="name")
class TestRemoveAddressAttribute:
def test_run(self, address_editor):
address_editor.remove_attribute("name", "id").should_be_called()
subject.remove_address_attribute(address_editor, name="name", id="id")