Working implementation of DefaultDataPresenter
This commit is contained in:
186
tests/test_default_data_presenter.py
Normal file
186
tests/test_default_data_presenter.py
Normal file
@@ -0,0 +1,186 @@
|
||||
import pytest
|
||||
|
||||
from core.Expando import Expando
|
||||
from workflow.DefaultDataPresenter import DefaultDataPresenter
|
||||
|
||||
|
||||
def test_i_can_present_static_mappings():
|
||||
mappings_def = "field1 = renamed_1 , field2 "
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
data = Expando({"field1": "value1", "field2": "value2", "field3": "value3"})
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert actual == Expando({"renamed_1": "value1", "field2": "value2"}) # field3 is removed
|
||||
|
||||
|
||||
def test_the_latter_mappings_take_precedence():
|
||||
mappings_def = "field1 = renamed_1 , field1 "
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
data = Expando({"field1": "value1", "field2": "value2", "field3": "value3"})
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert actual == Expando({"field1": "value1"}) # field3 is removed
|
||||
|
||||
|
||||
def test_i_can_present_static_mappings_with_sub_fields():
|
||||
mappings_def = "root.field1 = renamed_1 , root.field2, root.sub_field.field3, root.sub_field.field4=renamed4 "
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root": {"field1": "value1",
|
||||
"field2": "value2",
|
||||
"sub_field": {"field3": "value3",
|
||||
"field4": "value4"
|
||||
}}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert isinstance(actual, Expando)
|
||||
assert actual.as_dict() == {"renamed_1": "value1",
|
||||
"root.field2": "value2",
|
||||
"root.sub_field.field3": "value3",
|
||||
"renamed4": "value4"}
|
||||
|
||||
|
||||
def test_i_can_present_dynamic_mappings():
|
||||
mappings_def = "*"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
data = Expando({"field1": "value1", "field2": "value2", "field3": "value3"})
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert actual == Expando({"field1": "value1", "field2": "value2", "field3": "value3"})
|
||||
|
||||
|
||||
def test_i_can_present_dynamic_mappings_for_complex_data():
|
||||
mappings_def = "*"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root": {"field1": "value1",
|
||||
"field2": "value2",
|
||||
"sub_field": {"field3": "value3",
|
||||
"field4": "value4"
|
||||
}
|
||||
},
|
||||
"field5": "value5"}
|
||||
data = Expando(as_dict)
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert isinstance(actual, Expando)
|
||||
assert actual.as_dict() == as_dict
|
||||
|
||||
|
||||
def test_i_can_present_dynamic_mappings_with_sub_fields():
|
||||
mappings_def = "root.sub_field.*"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root": {"field1": "value1",
|
||||
"field2": "value2",
|
||||
"sub_field": {"field3": "value3",
|
||||
"field4": "value4"
|
||||
}}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert isinstance(actual, Expando)
|
||||
assert actual.as_dict() == {"root.sub_field.field3": "value3",
|
||||
"root.sub_field.field4": "value4"}
|
||||
|
||||
|
||||
def test_i_can_present_dynamic_mappings_with_sub_fields_and_renames():
|
||||
mappings_def = "root.sub_field.*=*"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root": {"field1": "value1",
|
||||
"field2": "value2",
|
||||
"sub_field": {"field3": "value3",
|
||||
"field4": "value4"
|
||||
}}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert isinstance(actual, Expando)
|
||||
assert actual.as_dict() == {"field3": "value3",
|
||||
"field4": "value4"}
|
||||
|
||||
|
||||
def test_i_can_present_dynamic_mappings_and_rename_them():
|
||||
mappings_def = "*=*" # does not really have effects as '*' only goes down one level
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root1": {"field1": "value1",
|
||||
"field2": "value2"},
|
||||
"root2": {"field3": "value3",
|
||||
"field4": "value4"}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert isinstance(actual, Expando)
|
||||
assert actual.as_dict() == as_dict
|
||||
|
||||
|
||||
def test_i_can_present_static_and_dynamic_mappings():
|
||||
mappings_def = "root.field1 = renamed_1, root.sub_field.*"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root": {"field1": "value1",
|
||||
"field2": "value2",
|
||||
"sub_field": {"field3": "value3",
|
||||
"field4": "value4"
|
||||
}}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert isinstance(actual, Expando)
|
||||
assert actual.as_dict() == {"renamed_1": "value1",
|
||||
"root.sub_field.field3": "value3",
|
||||
"root.sub_field.field4": "value4"}
|
||||
|
||||
|
||||
def test_another_example_of_static_and_dynamic_mappings():
|
||||
mappings_def = "* , field1 = renamed_1"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
data = Expando({"field1": "value1", "field2": "value2", "field3": "value3"})
|
||||
|
||||
actual = presenter.present(data)
|
||||
|
||||
assert actual == Expando({"renamed_1": "value1", "field2": "value2", "field3": "value3"}) # field3 is removed
|
||||
|
||||
|
||||
def test_i_can_detect_conflict_when_dynamically_renaming_a_field():
|
||||
mappings_def = "root_1.*=*, root_2.*=*"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root_1": {"field1": "value1",
|
||||
"field2": "value2"},
|
||||
"root_2": {"field1": "value1",
|
||||
"field2": "value2"}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
with pytest.raises(ValueError) as e:
|
||||
presenter.present(data)
|
||||
|
||||
assert str(e.value) == "Collision detected for field 'field1'. It is mapped from both 'root_1.*=*' and 'root_2.*=*'."
|
||||
|
||||
|
||||
def test_i_can_detect_declaration_error():
|
||||
mappings_def = "field1 ,, field2"
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
data = Expando({"field1": "value1", "field2": "value2", "field3": "value3"})
|
||||
|
||||
with pytest.raises(ValueError) as e:
|
||||
presenter.present(data)
|
||||
|
||||
|
||||
def test_i_can_detect_dynamic_error_declaration():
|
||||
mappings_def = "root.field1.*" # field1 is not an object
|
||||
presenter = DefaultDataPresenter("comp_id", mappings_def)
|
||||
as_dict = {"root": {"field1": "value1",
|
||||
"field2": "value2",
|
||||
"sub_field": {"field3": "value3",
|
||||
"field4": "value4"
|
||||
}}}
|
||||
data = Expando(as_dict)
|
||||
|
||||
with pytest.raises(ValueError) as e:
|
||||
presenter.present(data)
|
||||
@@ -2,6 +2,8 @@ from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from core.Expando import Expando
|
||||
from workflow.DefaultDataPresenter import DefaultDataPresenter
|
||||
from workflow.engine import WorkflowEngine, DataProcessor, DataProducer, DataFilter, DataPresenter
|
||||
|
||||
|
||||
@@ -11,6 +13,24 @@ def engine():
|
||||
return WorkflowEngine()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def presenter_sample_data():
|
||||
return Expando({
|
||||
"id": 123,
|
||||
"title": "My Awesome Task",
|
||||
"creator": {
|
||||
"id": 1,
|
||||
"name": "John Doe",
|
||||
"email": "john.doe@example.com"
|
||||
},
|
||||
"assignee": {
|
||||
"id": 2,
|
||||
"name": "Jane Smith",
|
||||
"email": "jane.smith@example.com"
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
def test_empty_workflow_initialization(engine):
|
||||
"""Test that a new WorkflowEngine has no processors."""
|
||||
assert len(engine.processors) == 0
|
||||
@@ -124,3 +144,21 @@ def test_branching_workflow(engine):
|
||||
|
||||
result = engine.run_to_list()
|
||||
assert result == [1, 10, 2, 20]
|
||||
|
||||
|
||||
def test_presenter_i_can_use_wildcards(presenter_sample_data):
|
||||
presenter1 = DefaultDataPresenter("component_id", "id, creator.*")
|
||||
res = presenter1.present(presenter_sample_data).as_dict()
|
||||
assert res == {"id": 123, "creator.id": 1, "creator.name": "John Doe", "creator.email": "john.doe@example.com"}
|
||||
|
||||
|
||||
def test_presenter_i_can_rename_wildcard_with_specific_override(presenter_sample_data):
|
||||
presenter1 = DefaultDataPresenter("component_id", "creator.*=*, creator.name=author_name")
|
||||
res = presenter1.present(presenter_sample_data).as_dict()
|
||||
assert res == {"id": 1, "email": "john.doe@example.com", "author_name": "John Doe"}
|
||||
|
||||
|
||||
def test_presenter_i_can_manage_collisions(presenter_sample_data):
|
||||
presenter1 = DefaultDataPresenter("component_id", "creator.*=*, assignee.*=*")
|
||||
with pytest.raises(ValueError, match="Collision detected for field"):
|
||||
presenter1.present(presenter_sample_data).as_dict()
|
||||
|
||||
Reference in New Issue
Block a user