diff --git a/tests/test_swf/test_activity_types.py b/tests/test_swf/test_activity_types.py index c166b725f..e8612ef0f 100644 --- a/tests/test_swf/test_activity_types.py +++ b/tests/test_swf/test_activity_types.py @@ -1,5 +1,4 @@ import boto -from nose.tools import assert_raises from sure import expect from moto import mock_swf @@ -29,30 +28,18 @@ def test_register_already_existing_activity_type(): conn.register_domain("test-domain", "60") conn.register_activity_type("test-domain", "test-activity", "v1.0") - with assert_raises(SWFTypeAlreadyExistsFault) as err: - conn.register_activity_type("test-domain", "test-activity", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("TypeAlreadyExistsFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#TypeAlreadyExistsFault", - "message": "ActivityType=[name=test-activity, version=v1.0]" - }) + conn.register_activity_type.when.called_with( + "test-domain", "test-activity", "v1.0" + ).should.throw(SWFTypeAlreadyExistsFault) @mock_swf def test_register_with_wrong_parameter_type(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60") - with assert_raises(SWFSerializationException) as err: - conn.register_activity_type("test-domain", "test-activity", 12) - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("SerializationException") - ex.body["__type"].should.equal("com.amazonaws.swf.base.model#SerializationException") - + conn.register_activity_type.when.called_with( + "test-domain", "test-activity", 12 + ).should.throw(SWFSerializationException) # ListActivityTypes endpoint @mock_swf @@ -101,32 +88,18 @@ def test_deprecate_already_deprecated_activity_type(): conn.register_activity_type("test-domain", "test-activity", "v1.0") conn.deprecate_activity_type("test-domain", "test-activity", "v1.0") - with assert_raises(SWFTypeDeprecatedFault) as err: - conn.deprecate_activity_type("test-domain", "test-activity", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("TypeDeprecatedFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#TypeDeprecatedFault", - "message": "ActivityType=[name=test-activity, version=v1.0]" - }) + conn.deprecate_activity_type.when.called_with( + "test-domain", "test-activity", "v1.0" + ).should.throw(SWFTypeDeprecatedFault) @mock_swf def test_deprecate_non_existent_activity_type(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60") - with assert_raises(SWFUnknownResourceFault) as err: - conn.deprecate_activity_type("test-domain", "non-existent", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown type: ActivityType=[name=non-existent, version=v1.0]" - }) + conn.deprecate_activity_type.when.called_with( + "test-domain", "non-existent", "v1.0" + ).should.throw(SWFUnknownResourceFault) # DescribeActivityType endpoint @mock_swf @@ -148,13 +121,6 @@ def test_describe_non_existent_activity_type(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60") - with assert_raises(SWFUnknownResourceFault) as err: - conn.describe_activity_type("test-domain", "non-existent", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown type: ActivityType=[name=non-existent, version=v1.0]" - }) + conn.describe_activity_type.when.called_with( + "test-domain", "non-existent", "v1.0" + ).should.throw(SWFUnknownResourceFault) diff --git a/tests/test_swf/test_domains.py b/tests/test_swf/test_domains.py index 013eb1b63..f43200aaf 100644 --- a/tests/test_swf/test_domains.py +++ b/tests/test_swf/test_domains.py @@ -1,5 +1,4 @@ import boto -from nose.tools import assert_raises from sure import expect from moto import mock_swf @@ -29,28 +28,17 @@ def test_register_already_existing_domain(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60", description="A test domain") - with assert_raises(SWFDomainAlreadyExistsFault) as err: - conn.register_domain("test-domain", "60", description="A test domain") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("DomainAlreadyExistsFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#DomainAlreadyExistsFault", - "message": "test-domain" - }) + conn.register_domain.when.called_with( + "test-domain", "60", description="A test domain" + ).should.throw(SWFDomainAlreadyExistsFault) @mock_swf def test_register_with_wrong_parameter_type(): conn = boto.connect_swf("the_key", "the_secret") - with assert_raises(SWFSerializationException) as err: - conn.register_domain("test-domain", 60, description="A test domain") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("SerializationException") - ex.body["__type"].should.equal("com.amazonaws.swf.base.model#SerializationException") + conn.register_domain.when.called_with( + "test-domain", 60, description="A test domain" + ).should.throw(SWFSerializationException) # ListDomains endpoint @@ -95,31 +83,18 @@ def test_deprecate_already_deprecated_domain(): conn.register_domain("test-domain", "60", description="A test domain") conn.deprecate_domain("test-domain") - with assert_raises(SWFDomainDeprecatedFault) as err: - conn.deprecate_domain("test-domain") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("DomainDeprecatedFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#DomainDeprecatedFault", - "message": "test-domain" - }) + conn.deprecate_domain.when.called_with( + "test-domain" + ).should.throw(SWFDomainDeprecatedFault) @mock_swf def test_deprecate_non_existent_domain(): conn = boto.connect_swf("the_key", "the_secret") - with assert_raises(SWFUnknownResourceFault) as err: - conn.deprecate_domain("non-existent") + conn.deprecate_domain.when.called_with( + "non-existent" + ).should.throw(SWFUnknownResourceFault) - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown domain: non-existent" - }) # DescribeDomain endpoint @mock_swf @@ -137,13 +112,6 @@ def test_describe_domain(): def test_describe_non_existent_domain(): conn = boto.connect_swf("the_key", "the_secret") - with assert_raises(SWFUnknownResourceFault) as err: - conn.describe_domain("non-existent") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown domain: non-existent" - }) + conn.describe_domain.when.called_with( + "non-existent" + ).should.throw(SWFUnknownResourceFault) diff --git a/tests/test_swf/test_exceptions.py b/tests/test_swf/test_exceptions.py new file mode 100644 index 000000000..27d48c261 --- /dev/null +++ b/tests/test_swf/test_exceptions.py @@ -0,0 +1,105 @@ +from __future__ import unicode_literals + +from moto.swf.exceptions import ( + SWFClientError, + SWFUnknownResourceFault, + SWFDomainAlreadyExistsFault, + SWFDomainDeprecatedFault, + SWFSerializationException, + SWFTypeAlreadyExistsFault, + SWFTypeDeprecatedFault, + SWFWorkflowExecutionAlreadyStartedFault, + SWFDefaultUndefinedFault, +) +from moto.swf.models import ( + WorkflowType, +) + +def test_swf_client_error(): + ex = SWFClientError("error message", "ASpecificType") + + ex.status.should.equal(400) + ex.error_code.should.equal("ASpecificType") + ex.body.should.equal({ + "__type": "ASpecificType", + "message": "error message" + }) + +def test_swf_unknown_resource_fault(): + ex = SWFUnknownResourceFault("type", "detail") + + ex.status.should.equal(400) + ex.error_code.should.equal("UnknownResourceFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", + "message": "Unknown type: detail" + }) + +def test_swf_domain_already_exists_fault(): + ex = SWFDomainAlreadyExistsFault("domain-name") + + ex.status.should.equal(400) + ex.error_code.should.equal("DomainAlreadyExistsFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#DomainAlreadyExistsFault", + "message": "domain-name" + }) + +def test_swf_domain_deprecated_fault(): + ex = SWFDomainDeprecatedFault("domain-name") + + ex.status.should.equal(400) + ex.error_code.should.equal("DomainDeprecatedFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#DomainDeprecatedFault", + "message": "domain-name" + }) + +def test_swf_serialization_exception(): + ex = SWFSerializationException("value") + + ex.status.should.equal(400) + ex.error_code.should.equal("SerializationException") + ex.body["__type"].should.equal("com.amazonaws.swf.base.model#SerializationException") + ex.body["Message"].should.match(r"class java.lang.Foo can not be converted to an String") + +def test_swf_type_already_exists_fault(): + wft = WorkflowType("wf-name", "wf-version") + ex = SWFTypeAlreadyExistsFault(wft) + + ex.status.should.equal(400) + ex.error_code.should.equal("TypeAlreadyExistsFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#TypeAlreadyExistsFault", + "message": "WorkflowType=[name=wf-name, version=wf-version]" + }) + +def test_swf_type_deprecated_fault(): + wft = WorkflowType("wf-name", "wf-version") + ex = SWFTypeDeprecatedFault(wft) + + ex.status.should.equal(400) + ex.error_code.should.equal("TypeDeprecatedFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#TypeDeprecatedFault", + "message": "WorkflowType=[name=wf-name, version=wf-version]" + }) + +def test_swf_workflow_execution_already_started_fault(): + ex = SWFWorkflowExecutionAlreadyStartedFault() + + ex.status.should.equal(400) + ex.error_code.should.equal("WorkflowExecutionAlreadyStartedFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#WorkflowExecutionAlreadyStartedFault", + }) + +def test_swf_default_undefined_fault(): + ex = SWFDefaultUndefinedFault("execution_start_to_close_timeout") + + ex.status.should.equal(400) + ex.error_code.should.equal("DefaultUndefinedFault") + ex.body.should.equal({ + "__type": "com.amazonaws.swf.base.model#DefaultUndefinedFault", + "message": "executionStartToCloseTimeout", + }) diff --git a/tests/test_swf/test_models.py b/tests/test_swf/test_models.py index a8b76330f..813afc3ca 100644 --- a/tests/test_swf/test_models.py +++ b/tests/test_swf/test_models.py @@ -1,5 +1,4 @@ from sure import expect -from nose.tools import assert_raises from freezegun import freeze_time from moto.swf.models import ( @@ -112,16 +111,9 @@ def test_workflow_execution_creation_child_policy_logic(): child_policy="REQUEST_CANCEL" ).child_policy.should.equal("REQUEST_CANCEL") - with assert_raises(SWFDefaultUndefinedFault) as err: - WorkflowExecution(WorkflowType("test-workflow", "v1.0"), "ab1234") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("DefaultUndefinedFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#DefaultUndefinedFault", - "message": "executionStartToCloseTimeout" - }) + WorkflowExecution.when.called_with( + WorkflowType("test-workflow", "v1.0"), "ab1234" + ).should.throw(SWFDefaultUndefinedFault) def test_workflow_execution_string_representation(): diff --git a/tests/test_swf/test_workflow_executions.py b/tests/test_swf/test_workflow_executions.py index 27feb8b4a..07aea90fb 100644 --- a/tests/test_swf/test_workflow_executions.py +++ b/tests/test_swf/test_workflow_executions.py @@ -1,5 +1,4 @@ import boto -from nose.tools import assert_raises from sure import expect from moto import mock_swf @@ -38,31 +37,18 @@ def test_start_already_started_workflow_execution(): conn = setup_swf_environment() conn.start_workflow_execution("test-domain", "uid-abcd1234", "test-workflow", "v1.0") - with assert_raises(SWFWorkflowExecutionAlreadyStartedFault) as err: - conn.start_workflow_execution("test-domain", "uid-abcd1234", "test-workflow", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("WorkflowExecutionAlreadyStartedFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#WorkflowExecutionAlreadyStartedFault", - }) + conn.start_workflow_execution.when.called_with( + "test-domain", "uid-abcd1234", "test-workflow", "v1.0" + ).should.throw(SWFWorkflowExecutionAlreadyStartedFault) @mock_swf def test_start_workflow_execution_on_deprecated_type(): conn = setup_swf_environment() conn.deprecate_workflow_type("test-domain", "test-workflow", "v1.0") - with assert_raises(SWFTypeDeprecatedFault) as err: - conn.start_workflow_execution("test-domain", "uid-abcd1234", "test-workflow", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("TypeDeprecatedFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#TypeDeprecatedFault", - "message": "WorkflowType=[name=test-workflow, version=v1.0]" - }) + conn.start_workflow_execution.when.called_with( + "test-domain", "uid-abcd1234", "test-workflow", "v1.0" + ).should.throw(SWFTypeDeprecatedFault) # DescribeWorkflowExecution endpoint @@ -80,16 +66,9 @@ def test_describe_workflow_execution(): def test_describe_non_existent_workflow_execution(): conn = setup_swf_environment() - with assert_raises(SWFUnknownResourceFault) as err: - conn.describe_workflow_execution("test-domain", "wrong-run-id", "wrong-workflow-id") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown execution: WorkflowExecution=[workflowId=wrong-workflow-id, runId=wrong-run-id]" - }) + conn.describe_workflow_execution.when.called_with( + "test-domain", "wrong-run-id", "wrong-workflow-id" + ).should.throw(SWFUnknownResourceFault) # GetWorkflowExecutionHistory endpoint @@ -109,7 +88,6 @@ def test_get_workflow_execution_history(): def test_get_workflow_execution_history_on_non_existent_workflow_execution(): conn = setup_swf_environment() - with assert_raises(SWFUnknownResourceFault) as err: - conn.get_workflow_execution_history("test-domain", "wrong-run-id", "wrong-workflow-id") - - # (the rest is already tested above) + conn.get_workflow_execution_history.when.called_with( + "test-domain", "wrong-run-id", "wrong-workflow-id" + ).should.throw(SWFUnknownResourceFault) diff --git a/tests/test_swf/test_workflow_types.py b/tests/test_swf/test_workflow_types.py index 67424710f..adcd81cc6 100644 --- a/tests/test_swf/test_workflow_types.py +++ b/tests/test_swf/test_workflow_types.py @@ -1,5 +1,4 @@ import boto -from nose.tools import assert_raises from sure import expect from moto import mock_swf @@ -29,29 +28,18 @@ def test_register_already_existing_workflow_type(): conn.register_domain("test-domain", "60") conn.register_workflow_type("test-domain", "test-workflow", "v1.0") - with assert_raises(SWFTypeAlreadyExistsFault) as err: - conn.register_workflow_type("test-domain", "test-workflow", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("TypeAlreadyExistsFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#TypeAlreadyExistsFault", - "message": "WorkflowType=[name=test-workflow, version=v1.0]" - }) + conn.register_workflow_type.when.called_with( + "test-domain", "test-workflow", "v1.0" + ).should.throw(SWFTypeAlreadyExistsFault) @mock_swf def test_register_with_wrong_parameter_type(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60") - with assert_raises(SWFSerializationException) as err: - conn.register_workflow_type("test-domain", "test-workflow", 12) - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("SerializationException") - ex.body["__type"].should.equal("com.amazonaws.swf.base.model#SerializationException") + conn.register_workflow_type.when.called_with( + "test-domain", "test-workflow", 12 + ).should.throw(SWFSerializationException) # ListWorkflowTypes endpoint @@ -101,32 +89,19 @@ def test_deprecate_already_deprecated_workflow_type(): conn.register_workflow_type("test-domain", "test-workflow", "v1.0") conn.deprecate_workflow_type("test-domain", "test-workflow", "v1.0") - with assert_raises(SWFTypeDeprecatedFault) as err: - conn.deprecate_workflow_type("test-domain", "test-workflow", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("TypeDeprecatedFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#TypeDeprecatedFault", - "message": "WorkflowType=[name=test-workflow, version=v1.0]" - }) + conn.deprecate_workflow_type.when.called_with( + "test-domain", "test-workflow", "v1.0" + ).should.throw(SWFTypeDeprecatedFault) @mock_swf def test_deprecate_non_existent_workflow_type(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60") - with assert_raises(SWFUnknownResourceFault) as err: - conn.deprecate_workflow_type("test-domain", "non-existent", "v1.0") + conn.deprecate_workflow_type.when.called_with( + "test-domain", "non-existent", "v1.0" + ).should.throw(SWFUnknownResourceFault) - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown type: WorkflowType=[name=non-existent, version=v1.0]" - }) # DescribeWorkflowType endpoint @mock_swf @@ -150,13 +125,6 @@ def test_describe_non_existent_workflow_type(): conn = boto.connect_swf("the_key", "the_secret") conn.register_domain("test-domain", "60") - with assert_raises(SWFUnknownResourceFault) as err: - conn.describe_workflow_type("test-domain", "non-existent", "v1.0") - - ex = err.exception - ex.status.should.equal(400) - ex.error_code.should.equal("UnknownResourceFault") - ex.body.should.equal({ - "__type": "com.amazonaws.swf.base.model#UnknownResourceFault", - "message": "Unknown type: WorkflowType=[name=non-existent, version=v1.0]" - }) + conn.describe_workflow_type.when.called_with( + "test-domain", "non-existent", "v1.0" + ).should.throw(SWFUnknownResourceFault)