Skip to content

Commit

Permalink
test: made simos blueprints explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
pbullhove committed Sep 19, 2023
1 parent 145932a commit e5d3335
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from common.exceptions import ApplicationException, NotFoundException
from common.utils.resolve_address import resolve_address
from enums import REFERENCE_TYPES, SIMOS
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service


Expand Down Expand Up @@ -78,7 +79,11 @@ def setUp(self):
self.document_repository.name = "datasource"
self.document_repository.get = self.mock_get
self.document_repository.find = self.mock_find
self.document_service = get_mock_document_service(lambda x, y: self.document_repository)
simos_blueprints = []
mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
self.document_service = get_mock_document_service(
repository_provider=lambda x, y: self.document_repository, blueprint_provider=mock_blueprint_provider
)

def mock_get(self, document_id: str):
if document_id == "car_rental_company_id":
Expand Down
16 changes: 9 additions & 7 deletions src/tests/unit/common/utils/test_create_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

from common.utils.create_entity import CreateEntity
from domain_classes.blueprint_attribute import BlueprintAttribute
from storage.repositories.file import LocalFileRepository
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service

file_repository_test = LocalFileRepository()

document_service = get_mock_document_service()
blueprint_provider = document_service.get_blueprint


class CreateEntityTestCase(unittest.TestCase):
def setUp(self):
simos_blueprints = [
"dmss://system/SIMOS/AttributeTypes",
"dmss://system/SIMOS/BlueprintAttribute",
"dmss://system/SIMOS/NamedEntity",
]
self.mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
self.mock_document_service = get_mock_document_service(blueprint_provider=self.mock_blueprint_provider)
self.maxDiff = None

def test_blueprint_entity(self):
Expand Down Expand Up @@ -41,7 +43,7 @@ def test_blueprint_entity(self):
"stringValues": ["one", "two", "three"],
}

entity = CreateEntity(blueprint_provider=blueprint_provider, type="CarTest").entity
entity = CreateEntity(blueprint_provider=self.mock_document_service.get_blueprint, type="CarTest").entity

self.assertEqual(expected_entity, entity)

Expand Down
10 changes: 9 additions & 1 deletion src/tests/unit/common/utils/validators/test_validate_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
from common.exceptions import ValidationException
from common.tree_node_serializer import tree_node_from_dict, tree_node_to_dict
from common.utils.validators import validate_entity, validate_entity_against_self
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service
from tests.unit.mock_data.mock_recipe_provider import mock_storage_recipe_provider


class ValidateEntityTestCase(unittest.TestCase):
get_blueprint = get_mock_document_service().get_blueprint
simos_blueprints = [
"dmss://system/SIMOS/Blueprint",
"dmss://system/SIMOS/NamedEntity",
"dmss://system/SIMOS/BlueprintAttribute",
]
mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
mock_document_service = get_mock_document_service(blueprint_provider=mock_blueprint_provider)
get_blueprint = mock_document_service.get_blueprint

def test_a_simple_valid_entity(self):
test_entity = {
Expand Down
33 changes: 16 additions & 17 deletions src/tests/unit/mock_data/mock_blueprint_provider.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import json
from typing import List

from domain_classes.blueprint import Blueprint
from storage.repositories.file import LocalFileRepository

file_repository_test = LocalFileRepository()

_FILE_PATH = "src/tests/unit/mock_data/mock_blueprints/"

class MockBlueprintProvider:
def __init__(
self,
test_blueprint_data_path: str = "src/tests/unit/mock_data/mock_blueprints",
simos_blueprints_available_for_test: List[str] = None,
):
if simos_blueprints_available_for_test is None:
simos_blueprints_available_for_test = []
if test_blueprint_data_path.endswith("/"):
test_blueprint_data_path = test_blueprint_data_path[:-1]
self.test_blueprint_data_path = test_blueprint_data_path
self.simos_blueprints_available_for_test = simos_blueprints_available_for_test

class BlueprintProvider:
@staticmethod
def get_blueprint(type: str):
def get_blueprint(self, type: str):
if type in [
"BaseChild",
"ExtendedBlueprint",
Expand All @@ -37,21 +47,10 @@ def get_blueprint(type: str):
"Customer",
"RentalCar",
]:
with open(_FILE_PATH + type + ".blueprint.json") as f:
with open(f"{self.test_blueprint_data_path}/{type}.blueprint.json") as f:
return Blueprint(json.load(f), type)
if type in [
"dmss://system/SIMOS/AttributeTypes",
"dmss://system/SIMOS/Blob",
"dmss://system/SIMOS/Blueprint",
"dmss://system/SIMOS/BlueprintAttribute",
"dmss://system/SIMOS/NamedEntity",
"dmss://system/SIMOS/Package",
"dmss://system/SIMOS/Reference",
]:
if type in self.simos_blueprints_available_for_test:
return Blueprint(file_repository_test.get(type), type)
raise FileNotFoundError(
f"Invalid type {type} provided to the MockBlueprintProvider. No such blueprint found in the test data."
)


blueprint_provider = BlueprintProvider()
3 changes: 1 addition & 2 deletions src/tests/unit/mock_data/mock_document_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from services.document_service import DocumentService
from tests.unit.mock_data.mock_blueprint_provider import blueprint_provider
from tests.unit.mock_data.mock_recipe_provider import mock_storage_recipe_provider


def get_mock_document_service(
blueprint_provider,
repository_provider=None,
blueprint_provider=blueprint_provider,
recipe_provider=mock_storage_recipe_provider,
user=None,
context=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from domain_classes.tree_node import Node
from enums import StorageDataTypes
from storage.data_source_class import DataSource
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service
from tests.unit.mock_data.mock_recipe_provider import mock_storage_recipe_provider

Expand All @@ -15,6 +16,11 @@


class DataSourceTestCase(unittest.TestCase):
def setUp(self) -> None:
simos_blueprints = ["dmss://system/SIMOS/NamedEntity"]
self.mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
pass

def test_save_into_multiple_repositories(self):
uncontained_doc = {
"name": "Parent",
Expand Down Expand Up @@ -67,7 +73,11 @@ def mock_find_one(**kwargs):
data_source_collection=data_source_collection,
)

document_service = get_mock_document_service(lambda x, y: data_source, user=test_user)
document_service = get_mock_document_service(
repository_provider=lambda x, y: data_source,
blueprint_provider=self.mock_blueprint_provider,
user=test_user,
)

node: Node = tree_node_from_dict(
uncontained_doc,
Expand Down Expand Up @@ -128,7 +138,11 @@ def mock_find_one(**kwargs):
data_source_collection=data_source_collection,
)

document_service = get_mock_document_service(lambda x, y: data_source, user=test_user)
document_service = get_mock_document_service(
repository_provider=lambda x, y: data_source,
blueprint_provider=self.mock_blueprint_provider,
user=test_user,
)

node: Node = tree_node_from_dict(
blob_doc,
Expand Down Expand Up @@ -194,7 +208,11 @@ def mock_find_one(**kwargs):
data_source_collection=data_source_collection,
)

document_service = get_mock_document_service(lambda x, y: data_source, user=test_user)
document_service = get_mock_document_service(
repository_provider=lambda x, y: data_source,
blueprint_provider=self.mock_blueprint_provider,
user=test_user,
)

node: Node = tree_node_from_dict(
blob_doc, document_service.get_blueprint, uid="1", recipe_provider=mock_storage_recipe_provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from common.tree_node_serializer import tree_node_to_dict
from common.utils.data_structure.compare import get_and_print_diff
from enums import REFERENCE_TYPES, SIMOS
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service


Expand Down Expand Up @@ -66,7 +67,12 @@ def setUp(self):
self.document_repository.name = "datasource"
self.document_repository.get = self.mock_get
self.document_repository.find = self.mock_find
self.document_service = get_mock_document_service(lambda x, y: self.document_repository)

simos_blueprints = ["dmss://system/SIMOS/NamedEntity", "dmss://system/SIMOS/Reference"]
mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
self.document_service = get_mock_document_service(
repository_provider=lambda x, y: self.document_repository, blueprint_provider=mock_blueprint_provider
)

def mock_get(self, document_id: str):
if document_id == "1":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
from common.utils.data_structure.compare import get_and_print_diff
from domain_classes.blueprint import Blueprint
from domain_classes.tree_node import Node
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service


class GetExtendedBlueprintTestCase(unittest.TestCase):
def setUp(self):
simos_blueprints = ["dmss://system/SIMOS/NamedEntity"]
self.mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
self.document_service = get_mock_document_service(blueprint_provider=self.mock_blueprint_provider)

def test_get_simple_extended(self):
document_service = get_mock_document_service()
full_blueprint: Blueprint = document_service.get_blueprint("ExtendedBlueprint")
full_blueprint: Blueprint = self.document_service.get_blueprint("ExtendedBlueprint")

assert next((attr for attr in full_blueprint.attributes if attr.name == "description"), None) is not None

def test_get_second_level_extended(self):
document_service = get_mock_document_service()
full_blueprint: Blueprint = document_service.get_blueprint("SecondLevelExtendedBlueprint")
full_blueprint: Blueprint = self.document_service.get_blueprint("SecondLevelExtendedBlueprint")

assert next((attr for attr in full_blueprint.attributes if attr.name == "description"), None) is not None
assert next((attr for attr in full_blueprint.attributes if attr.name == "another_value"), None) is not None
Expand Down Expand Up @@ -51,7 +55,9 @@ def mock_update(entity: dict, *args, **kwargs):

repository.get = lambda doc_id: doc_storage[doc_id]
repository.update = mock_update
document_service = get_mock_document_service(lambda x, y: repository)
document_service = get_mock_document_service(
repository_provider=lambda x, y: repository, blueprint_provider=self.mock_blueprint_provider
)

node: Node = document_service.get_document(Address.from_absolute("testing/$1"))
node.update(doc_1_after)
Expand Down
13 changes: 11 additions & 2 deletions src/tests/unit/services/document_service/test_reference_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
from common.utils.data_structure.compare import get_and_print_diff
from common.utils.data_structure.has_key_value_pairs import has_key_value_pairs
from enums import REFERENCE_TYPES, SIMOS, Protocols
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service


class GetDocumentResolveTestCase(unittest.TestCase):
def setUp(self) -> None:
simos_blueprints = ["dmss://system/SIMOS/Package", "dmss://system/SIMOS/NamedEntity"]
self.mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)

def test_references_that_uses_wrong_protocol(self):
my_car_rental = {
"_id": "1",
Expand Down Expand Up @@ -54,7 +59,9 @@ def mock_find(target: dict):
document_repository.get = mock_get
document_repository.find = mock_find

document_service = get_mock_document_service(lambda x, y: document_repository)
document_service = get_mock_document_service(
repository_provider=lambda x, y: document_repository, blueprint_provider=self.mock_blueprint_provider
)
with pytest.raises(Exception, match=r"The protocol 'wrong' is not supported"):
tree_node_to_dict(document_service.get_document(Address.from_absolute("datasource/$1"), depth=9))

Expand Down Expand Up @@ -249,7 +256,9 @@ def mock_data_source(data_source_id: str, user: dict):
document_repository.find = lambda target: find(target, another_data_source)
return document_repository

document_service = get_mock_document_service(mock_data_source)
document_service = get_mock_document_service(
repository_provider=mock_data_source, blueprint_provider=self.mock_blueprint_provider
)
actual = tree_node_to_dict(document_service.get_document(Address.from_absolute("test_data/$2"), depth=99))
complex_package = tree_node_to_dict(
document_service.get_document(Address.from_absolute("test_data/$1"), depth=99)
Expand Down
18 changes: 15 additions & 3 deletions src/tests/unit/services/document_service/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from common.exceptions import ValidationException
from common.utils.data_structure.compare import get_and_print_diff
from enums import REFERENCE_TYPES, SIMOS
from tests.unit.mock_data.mock_blueprint_provider import MockBlueprintProvider
from tests.unit.mock_data.mock_document_service import get_mock_document_service


Expand All @@ -16,7 +17,16 @@ def setUp(self):
self.repository.update = self.mock_update
self.repository.delete = self.mock_delete
self.repository.delete_blob = self.mock_delete
self.document_service = get_mock_document_service(lambda x, y: self.repository)

simos_blueprints = [
"dmss://system/SIMOS/NamedEntity",
"dmss://system/SIMOS/Reference",
"dmss://system/SIMOS/Blob",
]
self.mock_blueprint_provider = MockBlueprintProvider(simos_blueprints_available_for_test=simos_blueprints)
self.document_service = get_mock_document_service(
repository_provider=lambda x, y: self.repository, blueprint_provider=self.mock_blueprint_provider
)

def mock_get(self, document_id: str):
return self.storage[document_id]
Expand All @@ -34,7 +44,9 @@ def test_remove_document(self):

document_repository.get = lambda id: document_1.copy()

document_service = get_mock_document_service(lambda id, user: document_repository)
document_service = get_mock_document_service(
repository_provider=lambda id, user: document_repository, blueprint_provider=self.mock_blueprint_provider
)
document_service.remove(Address("$1", "testing"))
document_repository.delete.assert_called_with("1")

Expand Down Expand Up @@ -79,7 +91,7 @@ def test_remove_document_wo_existing_blueprint(self):
}

self.document_service = get_mock_document_service(
repository_provider=lambda x, y: self.repository,
repository_provider=lambda x, y: self.repository, blueprint_provider=self.mock_blueprint_provider
)
self.assertRaises(FileNotFoundError, self.document_service.remove, Address("$1", "testing"))

Expand Down
Loading

0 comments on commit e5d3335

Please sign in to comment.