File: test_exceptions.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (165 lines) | stat: -rw-r--r-- 4,872 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import re

from moto.swf.exceptions import (
    SWFClientError,
    SWFDecisionValidationException,
    SWFDefaultUndefinedFault,
    SWFDomainAlreadyExistsFault,
    SWFDomainDeprecatedFault,
    SWFSerializationException,
    SWFTypeAlreadyExistsFault,
    SWFTypeDeprecatedFault,
    SWFUnknownResourceFault,
    SWFValidationException,
    SWFWorkflowExecutionAlreadyStartedFault,
)
from moto.swf.models import WorkflowType


def test_swf_client_error():
    ex = SWFClientError("ASpecificType", "error message")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "ASpecificType",
        "message": "error message",
    }


def test_swf_unknown_resource_fault():
    ex = SWFUnknownResourceFault("type", "detail")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#UnknownResourceFault",
        "message": "Unknown type: detail",
    }


def test_swf_unknown_resource_fault_with_only_one_parameter():
    ex = SWFUnknownResourceFault("foo bar baz")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#UnknownResourceFault",
        "message": "Unknown foo bar baz",
    }


def test_swf_domain_already_exists_fault():
    ex = SWFDomainAlreadyExistsFault("domain-name")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#DomainAlreadyExistsFault",
        "message": "domain-name",
    }


def test_swf_domain_deprecated_fault():
    ex = SWFDomainDeprecatedFault("domain-name")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#DomainDeprecatedFault",
        "message": "domain-name",
    }


def test_swf_serialization_exception():
    ex = SWFSerializationException("value")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#SerializationException",
        "message": (
            "class java.lang.Foo can not be converted to an String  (not "
            "a real SWF exception ; happened on: value)"
        ),
    }


def test_swf_type_already_exists_fault():
    wft = WorkflowType("wf-name", "wf-version")
    ex = SWFTypeAlreadyExistsFault(wft)

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__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)

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__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()

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#WorkflowExecutionAlreadyStartedFault",
        "message": "Already Started",
    }


def test_swf_default_undefined_fault():
    ex = SWFDefaultUndefinedFault("execution_start_to_close_timeout")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazonaws.swf.base.model#DefaultUndefinedFault",
        "message": "executionStartToCloseTimeout",
    }


def test_swf_validation_exception():
    ex = SWFValidationException("Invalid token")

    assert ex.code == 400
    assert json.loads(ex.get_body()) == {
        "__type": "com.amazon.coral.validate#ValidationException",
        "message": "Invalid token",
    }


def test_swf_decision_validation_error():
    ex = SWFDecisionValidationException(
        [
            {
                "type": "null_value",
                "where": "decisions.1.member.startTimerDecisionAttributes.startToFireTimeout",
            },
            {
                "type": "bad_decision_type",
                "value": "FooBar",
                "where": "decisions.1.member.decisionType",
                "possible_values": "Foo, Bar, Baz",
            },
        ]
    )

    assert ex.code == 400
    assert ex.error_type == "com.amazon.coral.validate#ValidationException"

    msg = ex.get_body()
    assert re.search(r"2 validation errors detected:", msg)
    assert re.search(
        r"Value null at 'decisions.1.member.startTimerDecisionAttributes.startToFireTimeout' "
        r"failed to satisfy constraint: Member must not be null;",
        msg,
    )
    assert re.search(
        r"Value 'FooBar' at 'decisions.1.member.decisionType' failed to satisfy constraint: "
        r"Member must satisfy enum value set: \[Foo, Bar, Baz\]",
        msg,
    )