Skip to content

Commit

Permalink
fix: fixed unit test cases after required attributes no longer can be…
Browse files Browse the repository at this point in the history
… deleted
  • Loading branch information
collinlokken committed Aug 24, 2023
1 parent 3fce6d5 commit 05428d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/services/document_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BadRequestException,
MissingPrivilegeException,
NotFoundException,
ValidationException,
)
from common.tree_node_serializer import (
tree_node_from_dict,
Expand Down
12 changes: 10 additions & 2 deletions src/tests/unit/mock_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ def flatten_dict(dd, separator="_", prefix=""):
"description": "First blueprint",
"extends": [SIMOS.NAMED_ENTITY.value],
"attributes": [
{"attributeType": "basic_blueprint", "type": SIMOS.BLUEPRINT_ATTRIBUTE.value, "name": "nested"},
{"attributeType": "basic_blueprint", "type": SIMOS.BLUEPRINT_ATTRIBUTE.value, "name": "reference"},
{
"attributeType": "basic_blueprint",
"type": SIMOS.BLUEPRINT_ATTRIBUTE.value,
"name": "nested",
},
{
"attributeType": "basic_blueprint",
"type": SIMOS.BLUEPRINT_ATTRIBUTE.value,
"name": "reference",
},
{
"attributeType": "basic_blueprint",
"type": SIMOS.BLUEPRINT_ATTRIBUTE.value,
Expand Down
24 changes: 11 additions & 13 deletions src/tests/unit/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ def mock_update(entity: dict, *args, **kwargs):
document_service=document_service,
)

# TODO if the attribute to remove is required, document_service.remove() should give an error.
# This test must be updated such that document_service.remove() tries to remove an optional attribute instead.
def test_remove_reference(self):
repository = mock.Mock()

Expand Down Expand Up @@ -174,8 +172,9 @@ def mock_update(entity: dict, *args, **kwargs):
repository.update = mock_update
document_service = get_mock_document_service(lambda x, y: repository)

document_service.remove(Address("$1.uncontained_in_every_way", "testing"))
assert "uncontained_in_every_way" not in doc_storage["1"]
self.assertRaises(
ValidationException, document_service.remove, Address("$1.uncontained_in_every_way", "testing")
)

def test_remove_nested_reference(self):
repository = mock.Mock()
Expand Down Expand Up @@ -214,8 +213,11 @@ def mock_update(entity: dict, *args, **kwargs):
repository.update = mock_update
document_service = get_mock_document_service(lambda x, y: repository)

document_service.remove(Address("$1.i_have_a_uncontained_attribute.uncontained_in_every_way", "testing"))
assert "uncontained_in_every_way" not in doc_storage["1"]["i_have_a_uncontained_attribute"]
self.assertRaises(
ValidationException,
document_service.remove,
Address("$1.i_have_a_uncontained_attribute.uncontained_in_every_way", "testing"),
)

def test_remove_reference_in_list(self):
repository = mock.Mock()
Expand Down Expand Up @@ -258,13 +260,9 @@ def mock_update(entity: dict, *args, **kwargs):
repository.update = mock_update
document_service = get_mock_document_service(lambda x, y: repository)

document_service.remove(Address("$1.uncontained_in_every_way[0]", "testing"))
assert len(doc_storage["1"]["uncontained_in_every_way"]) == 1
assert doc_storage["1"]["uncontained_in_every_way"][0] == {
"address": "$42dbe4a5-0eb0-4ee2-826c-695172c3c35a",
"type": SIMOS.REFERENCE.value,
"referenceType": REFERENCE_TYPES.LINK.value,
}
self.assertRaises(
ValidationException, document_service.remove, Address("$1.uncontained_in_every_way[0]", "testing")
)

def test_add_reference_in_list(self):
repository = mock.Mock()
Expand Down
47 changes: 24 additions & 23 deletions src/tests/unit/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,33 @@ def test_remove_document(self):

def test_remove_required_attribute(self):
doc_1 = {
"uid": "1",
"name": "Parent",
"description": "",
"type": "uncontained_blueprint",
"uncontained_in_every_way": {"_id": "2", "name": "a_reference", "type": "basic_blueprint"},
"_id": "1",
"name": "Car rental",
"type": "test_data/complex/CarRental",
"cars": [
{
"address": "2",
"type": SIMOS.REFERENCE.value,
"referenceType": REFERENCE_TYPES.STORAGE.value,
}
],
"customers": [
{
"name": "Jane",
"type": "test_data/complex/Customer",
"car": {
"address": "2",
"type": SIMOS.REFERENCE.value,
"referenceType": REFERENCE_TYPES.STORAGE.value,
},
}
],
}
doc_2 = {"_id": "2", "name": "car1", "type": "test_data/complex/RentalCar", "plateNumber": "xyz"}

self.storage = {"1": doc_1}
self.storage = {"1": doc_1, "2": doc_2}

self.assertRaises(
ValidationException, self.document_service.remove, Address("$1.uncontained_in_every_way", "testing")
)
self.assertRaises(ValidationException, self.document_service.remove, Address("$1.cars", "testing"))

def test_remove_document_wo_existing_blueprint(self):
self.storage = {
Expand Down Expand Up @@ -100,9 +115,6 @@ def test_remove_child_dict(self):
}

self.assertRaises(ValidationException, self.document_service.remove, Address("$1.nested", "testing", "dmss"))
# TODO Test fails due to modelling error in blueprint. Cannot delete required attribute
# assert self.storage["1"].get("nested") is None
# assert self.storage.get("2") is None

def test_remove_child_list(self):
self.storage = {
Expand All @@ -123,9 +135,6 @@ def test_remove_child_list(self):
}

self.assertRaises(ValidationException, self.document_service.remove, Address("$1.references", "testing"))
# TODO Test fails due to modelling error in blueprint. Cannot delete required attribute
# assert self.storage["1"].get("references") is None
# assert self.storage.get("2") is None

def test_remove_second_level_nested(self):
self.storage = {
Expand Down Expand Up @@ -222,14 +231,6 @@ def test_remove_reference(self):
}

self.assertRaises(ValidationException, self.document_service.remove, Address("$1.reference", "testing"))
# TODO Test fails due to modelling error in blueprint. Cannot delete required attribute
# assert self.storage["1"] == {
# "_id": "1",
# "name": "Parent",
# "description": "",
# "type": "all_contained_cases_blueprint",
# }
# assert self.storage.get("2")

def test_remove_optional(self):
self.storage = {
Expand Down

0 comments on commit 05428d2

Please sign in to comment.