diff --git a/tests/test_swf/models/__init__.py b/tests/test_swf/models/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_swf/models/test_activity_task.py b/tests/test_swf/models/test_activity_task.py new file mode 100644 index 000000000..2e2bba2f6 --- /dev/null +++ b/tests/test_swf/models/test_activity_task.py @@ -0,0 +1,26 @@ +from sure import expect + +from moto.swf.models import ActivityTask + +from ..utils import make_workflow_execution + + +def test_activity_task_creation(): + wfe = make_workflow_execution() + task = ActivityTask( + activity_id="my-activity-123", + activity_type="foo", + input="optional", + workflow_execution=wfe, + ) + task.workflow_execution.should.equal(wfe) + task.state.should.equal("SCHEDULED") + task.task_token.should_not.be.empty + task.started_event_id.should.be.none + + task.start(123) + task.state.should.equal("STARTED") + task.started_event_id.should.equal(123) + + task.complete() + task.state.should.equal("COMPLETED") diff --git a/tests/test_swf/models/test_decision_task.py b/tests/test_swf/models/test_decision_task.py new file mode 100644 index 000000000..f1156a19e --- /dev/null +++ b/tests/test_swf/models/test_decision_task.py @@ -0,0 +1,31 @@ +from sure import expect + +from moto.swf.models import DecisionTask + +from ..utils import make_workflow_execution + + +def test_decision_task_creation(): + wfe = make_workflow_execution() + dt = DecisionTask(wfe, 123) + dt.workflow_execution.should.equal(wfe) + dt.state.should.equal("SCHEDULED") + dt.task_token.should_not.be.empty + dt.started_event_id.should.be.none + +def test_decision_task_full_dict_representation(): + wfe = make_workflow_execution() + wft = wfe.workflow_type + dt = DecisionTask(wfe, 123) + + fd = dt.to_full_dict() + fd["events"].should.be.a("list") + fd["previousStartedEventId"].should.equal(0) + fd.should_not.contain("startedEventId") + fd.should.contain("taskToken") + fd["workflowExecution"].should.equal(wfe.to_short_dict()) + fd["workflowType"].should.equal(wft.to_short_dict()) + + dt.start(1234) + fd = dt.to_full_dict() + fd["startedEventId"].should.equal(1234) diff --git a/tests/test_swf/models/test_domain.py b/tests/test_swf/models/test_domain.py new file mode 100644 index 000000000..68e3f8903 --- /dev/null +++ b/tests/test_swf/models/test_domain.py @@ -0,0 +1,29 @@ +from sure import expect + +from moto.swf.models import Domain + + +def test_domain_short_dict_representation(): + domain = Domain("foo", "52") + domain.to_short_dict().should.equal({"name":"foo", "status":"REGISTERED"}) + + domain.description = "foo bar" + domain.to_short_dict()["description"].should.equal("foo bar") + +def test_domain_full_dict_representation(): + domain = Domain("foo", "52") + + domain.to_full_dict()["domainInfo"].should.equal(domain.to_short_dict()) + _config = domain.to_full_dict()["configuration"] + _config["workflowExecutionRetentionPeriodInDays"].should.equal("52") + +def test_domain_string_representation(): + domain = Domain("my-domain", "60") + str(domain).should.equal("Domain(name: my-domain, status: REGISTERED)") + +def test_domain_add_to_task_list(): + domain = Domain("my-domain", "60") + domain.add_to_task_list("foo", "bar") + dict(domain.task_lists).should.equal({ + "foo": ["bar"] + }) diff --git a/tests/test_swf/models/test_generic_type.py b/tests/test_swf/models/test_generic_type.py new file mode 100644 index 000000000..8937836e5 --- /dev/null +++ b/tests/test_swf/models/test_generic_type.py @@ -0,0 +1,51 @@ +from sure import expect + +from moto.swf.models import GenericType + + +# Tests for GenericType (ActivityType, WorkflowType) +class FooType(GenericType): + @property + def kind(self): + return "foo" + + @property + def _configuration_keys(self): + return ["justAnExampleTimeout"] + + +def test_type_short_dict_representation(): + _type = FooType("test-foo", "v1.0") + _type.to_short_dict().should.equal({"name": "test-foo", "version": "v1.0"}) + +def test_type_medium_dict_representation(): + _type = FooType("test-foo", "v1.0") + _type.to_medium_dict()["fooType"].should.equal(_type.to_short_dict()) + _type.to_medium_dict()["status"].should.equal("REGISTERED") + _type.to_medium_dict().should.contain("creationDate") + _type.to_medium_dict().should_not.contain("deprecationDate") + _type.to_medium_dict().should_not.contain("description") + + _type.description = "foo bar" + _type.to_medium_dict()["description"].should.equal("foo bar") + + _type.status = "DEPRECATED" + _type.to_medium_dict().should.contain("deprecationDate") + +def test_type_full_dict_representation(): + _type = FooType("test-foo", "v1.0") + _type.to_full_dict()["typeInfo"].should.equal(_type.to_medium_dict()) + _type.to_full_dict()["configuration"].should.equal({}) + + _type.task_list = "foo" + _type.to_full_dict()["configuration"]["defaultTaskList"].should.equal({"name":"foo"}) + + _type.just_an_example_timeout = "60" + _type.to_full_dict()["configuration"]["justAnExampleTimeout"].should.equal("60") + + _type.non_whitelisted_property = "34" + _type.to_full_dict()["configuration"].keys().should.equal(["defaultTaskList", "justAnExampleTimeout"]) + +def test_type_string_representation(): + _type = FooType("test-foo", "v1.0") + str(_type).should.equal("FooType(name: test-foo, version: v1.0, status: REGISTERED)") diff --git a/tests/test_swf/models/test_history_event.py b/tests/test_swf/models/test_history_event.py new file mode 100644 index 000000000..89d73210c --- /dev/null +++ b/tests/test_swf/models/test_history_event.py @@ -0,0 +1,29 @@ +from sure import expect +from freezegun import freeze_time + +from moto.swf.models import HistoryEvent + + +@freeze_time("2015-01-01 12:00:00") +def test_history_event_creation(): + he = HistoryEvent(123, "DecisionTaskStarted", scheduled_event_id=2) + he.event_id.should.equal(123) + he.event_type.should.equal("DecisionTaskStarted") + he.event_timestamp.should.equal(1420110000.0) + +@freeze_time("2015-01-01 12:00:00") +def test_history_event_to_dict_representation(): + he = HistoryEvent(123, "DecisionTaskStarted", scheduled_event_id=2) + he.to_dict().should.equal({ + "eventId": 123, + "eventType": "DecisionTaskStarted", + "eventTimestamp": 1420110000.0, + "decisionTaskStartedEventAttributes": { + "scheduledEventId": 2 + } + }) + +def test_history_event_breaks_on_initialization_if_not_implemented(): + HistoryEvent.when.called_with( + 123, "UnknownHistoryEvent" + ).should.throw(NotImplementedError) diff --git a/tests/test_swf/test_models.py b/tests/test_swf/models/test_workflow_execution.py similarity index 71% rename from tests/test_swf/test_models.py rename to tests/test_swf/models/test_workflow_execution.py index a298139e8..b793d8d2c 100644 --- a/tests/test_swf/test_models.py +++ b/tests/test_swf/models/test_workflow_execution.py @@ -2,12 +2,7 @@ from sure import expect from freezegun import freeze_time from moto.swf.models import ( - ActivityTask, ActivityType, - DecisionTask, - Domain, - GenericType, - HistoryEvent, WorkflowType, WorkflowExecution, ) @@ -15,97 +10,13 @@ from moto.swf.exceptions import ( SWFDefaultUndefinedFault, ) -from .utils import ( +from ..utils import ( get_basic_domain, get_basic_workflow_type, + make_workflow_execution, ) -# Some utility methods -# TODO: move them in utils -def make_workflow_execution(**kwargs): - domain = get_basic_domain() - domain.add_type(ActivityType("test-activity", "v1.1")) - wft = get_basic_workflow_type() - return WorkflowExecution(domain, wft, "ab1234", **kwargs) - - -# Domain -def test_domain_short_dict_representation(): - domain = Domain("foo", "52") - domain.to_short_dict().should.equal({"name":"foo", "status":"REGISTERED"}) - - domain.description = "foo bar" - domain.to_short_dict()["description"].should.equal("foo bar") - -def test_domain_full_dict_representation(): - domain = Domain("foo", "52") - - domain.to_full_dict()["domainInfo"].should.equal(domain.to_short_dict()) - _config = domain.to_full_dict()["configuration"] - _config["workflowExecutionRetentionPeriodInDays"].should.equal("52") - -def test_domain_string_representation(): - domain = Domain("my-domain", "60") - str(domain).should.equal("Domain(name: my-domain, status: REGISTERED)") - -def test_domain_add_to_task_list(): - domain = Domain("my-domain", "60") - domain.add_to_task_list("foo", "bar") - dict(domain.task_lists).should.equal({ - "foo": ["bar"] - }) - - -# GenericType (ActivityType, WorkflowType) -class FooType(GenericType): - @property - def kind(self): - return "foo" - - @property - def _configuration_keys(self): - return ["justAnExampleTimeout"] - - -def test_type_short_dict_representation(): - _type = FooType("test-foo", "v1.0") - _type.to_short_dict().should.equal({"name": "test-foo", "version": "v1.0"}) - -def test_type_medium_dict_representation(): - _type = FooType("test-foo", "v1.0") - _type.to_medium_dict()["fooType"].should.equal(_type.to_short_dict()) - _type.to_medium_dict()["status"].should.equal("REGISTERED") - _type.to_medium_dict().should.contain("creationDate") - _type.to_medium_dict().should_not.contain("deprecationDate") - _type.to_medium_dict().should_not.contain("description") - - _type.description = "foo bar" - _type.to_medium_dict()["description"].should.equal("foo bar") - - _type.status = "DEPRECATED" - _type.to_medium_dict().should.contain("deprecationDate") - -def test_type_full_dict_representation(): - _type = FooType("test-foo", "v1.0") - _type.to_full_dict()["typeInfo"].should.equal(_type.to_medium_dict()) - _type.to_full_dict()["configuration"].should.equal({}) - - _type.task_list = "foo" - _type.to_full_dict()["configuration"]["defaultTaskList"].should.equal({"name":"foo"}) - - _type.just_an_example_timeout = "60" - _type.to_full_dict()["configuration"]["justAnExampleTimeout"].should.equal("60") - - _type.non_whitelisted_property = "34" - _type.to_full_dict()["configuration"].keys().should.equal(["defaultTaskList", "justAnExampleTimeout"]) - -def test_type_string_representation(): - _type = FooType("test-foo", "v1.0") - str(_type).should.equal("FooType(name: test-foo, version: v1.0, status: REGISTERED)") - - -# WorkflowExecution def test_workflow_execution_creation(): domain = get_basic_domain() wft = get_basic_workflow_type() @@ -445,78 +356,3 @@ def test_workflow_execution_schedule_activity_task_with_same_activity_id(): last_event = wfe.events()[-1] last_event.event_type.should.equal("ScheduleActivityTaskFailed") last_event.cause.should.equal("ACTIVITY_ID_ALREADY_IN_USE") - - -# HistoryEvent -@freeze_time("2015-01-01 12:00:00") -def test_history_event_creation(): - he = HistoryEvent(123, "DecisionTaskStarted", scheduled_event_id=2) - he.event_id.should.equal(123) - he.event_type.should.equal("DecisionTaskStarted") - he.event_timestamp.should.equal(1420110000.0) - -@freeze_time("2015-01-01 12:00:00") -def test_history_event_to_dict_representation(): - he = HistoryEvent(123, "DecisionTaskStarted", scheduled_event_id=2) - he.to_dict().should.equal({ - "eventId": 123, - "eventType": "DecisionTaskStarted", - "eventTimestamp": 1420110000.0, - "decisionTaskStartedEventAttributes": { - "scheduledEventId": 2 - } - }) - -def test_history_event_breaks_on_initialization_if_not_implemented(): - HistoryEvent.when.called_with( - 123, "UnknownHistoryEvent" - ).should.throw(NotImplementedError) - - -# DecisionTask -def test_decision_task_creation(): - wfe = make_workflow_execution() - dt = DecisionTask(wfe, 123) - dt.workflow_execution.should.equal(wfe) - dt.state.should.equal("SCHEDULED") - dt.task_token.should_not.be.empty - dt.started_event_id.should.be.none - -def test_decision_task_full_dict_representation(): - wfe = make_workflow_execution() - wft = wfe.workflow_type - dt = DecisionTask(wfe, 123) - - fd = dt.to_full_dict() - fd["events"].should.be.a("list") - fd["previousStartedEventId"].should.equal(0) - fd.should_not.contain("startedEventId") - fd.should.contain("taskToken") - fd["workflowExecution"].should.equal(wfe.to_short_dict()) - fd["workflowType"].should.equal(wft.to_short_dict()) - - dt.start(1234) - fd = dt.to_full_dict() - fd["startedEventId"].should.equal(1234) - - -# ActivityTask -def test_activity_task_creation(): - wfe = make_workflow_execution() - task = ActivityTask( - activity_id="my-activity-123", - activity_type="foo", - input="optional", - workflow_execution=wfe, - ) - task.workflow_execution.should.equal(wfe) - task.state.should.equal("SCHEDULED") - task.task_token.should_not.be.empty - task.started_event_id.should.be.none - - task.start(123) - task.state.should.equal("STARTED") - task.started_event_id.should.equal(123) - - task.complete() - task.state.should.equal("COMPLETED") diff --git a/tests/test_swf/responses/__init__.py b/tests/test_swf/responses/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_swf/test_activity_types.py b/tests/test_swf/responses/test_activity_types.py similarity index 100% rename from tests/test_swf/test_activity_types.py rename to tests/test_swf/responses/test_activity_types.py diff --git a/tests/test_swf/test_decision_tasks.py b/tests/test_swf/responses/test_decision_tasks.py similarity index 99% rename from tests/test_swf/test_decision_tasks.py rename to tests/test_swf/responses/test_decision_tasks.py index ecf59223f..f20626ee1 100644 --- a/tests/test_swf/test_decision_tasks.py +++ b/tests/test_swf/responses/test_decision_tasks.py @@ -9,7 +9,7 @@ from moto.swf.exceptions import ( SWFDecisionValidationException, ) -from .utils import mock_basic_workflow_type +from ..utils import mock_basic_workflow_type @mock_swf diff --git a/tests/test_swf/test_domains.py b/tests/test_swf/responses/test_domains.py similarity index 100% rename from tests/test_swf/test_domains.py rename to tests/test_swf/responses/test_domains.py diff --git a/tests/test_swf/test_workflow_executions.py b/tests/test_swf/responses/test_workflow_executions.py similarity index 100% rename from tests/test_swf/test_workflow_executions.py rename to tests/test_swf/responses/test_workflow_executions.py diff --git a/tests/test_swf/test_workflow_types.py b/tests/test_swf/responses/test_workflow_types.py similarity index 100% rename from tests/test_swf/test_workflow_types.py rename to tests/test_swf/responses/test_workflow_types.py diff --git a/tests/test_swf/utils.py b/tests/test_swf/utils.py index 5c93fe79b..55121b319 100644 --- a/tests/test_swf/utils.py +++ b/tests/test_swf/utils.py @@ -1,6 +1,8 @@ from moto.swf.models import ( + ActivityType, Domain, WorkflowType, + WorkflowExecution, ) @@ -9,7 +11,7 @@ def get_basic_domain(): return Domain("test-domain", "90") -# A generic test WorkflowType +# A test WorkflowType def _generic_workflow_type_attributes(): return [ "test-workflow", "v1.0" @@ -28,3 +30,11 @@ def mock_basic_workflow_type(domain_name, conn): args, kwargs = _generic_workflow_type_attributes() conn.register_workflow_type(domain_name, *args, **kwargs) return conn + + +# A test WorkflowExecution +def make_workflow_execution(**kwargs): + domain = get_basic_domain() + domain.add_type(ActivityType("test-activity", "v1.1")) + wft = get_basic_workflow_type() + return WorkflowExecution(domain, wft, "ab1234", **kwargs)