-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_startup_scripts.py
More file actions
166 lines (131 loc) · 5.6 KB
/
test_startup_scripts.py
File metadata and controls
166 lines (131 loc) · 5.6 KB
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
166
import pytest
import responses # https://github.com/getsentry/responses
from verda.exceptions import APIException
from verda.startup_scripts import StartupScript, StartupScriptsService
INVALID_REQUEST = 'invalid_request'
INVALID_REQUEST_MESSAGE = 'Your existence is invalid'
SCRIPT_ID = 'deadc0de-a5d2-4972-ae4e-d429115d055b'
SCRIPT_NAME = 'next episode of _____'
SCRIPT_VALUE = 'this was not in the script!'
script_ID_2 = 'beefbeef-a5d2-4972-ae4e-d429115d055b'
PAYLOAD = [{'id': SCRIPT_ID, 'name': SCRIPT_NAME, 'script': SCRIPT_VALUE}]
class TestStartupScripts:
@pytest.fixture
def startup_script_service(self, http_client):
return StartupScriptsService(http_client)
@pytest.fixture
def endpoint(self, http_client):
return http_client._base_url + '/scripts'
def test_get_scripts(self, startup_script_service, endpoint):
# arrange - add response mock
responses.add(responses.GET, endpoint, json=PAYLOAD, status=200)
# act
scripts = startup_script_service.get()
# assert
assert isinstance(scripts, list)
assert len(scripts) == 1
assert isinstance(scripts[0], StartupScript)
assert scripts[0].id == SCRIPT_ID
assert scripts[0].name == SCRIPT_NAME
assert scripts[0].script == SCRIPT_VALUE
assert responses.assert_call_count(endpoint, 1) is True
def test_get_script_by_id_successful(self, startup_script_service, endpoint):
# arrange - add response mock
url = endpoint + '/' + SCRIPT_ID
responses.add(responses.GET, url, json=PAYLOAD, status=200)
# act
script = startup_script_service.get_by_id(SCRIPT_ID)
# assert
assert isinstance(script, StartupScript)
assert script.id == SCRIPT_ID
assert script.name == SCRIPT_NAME
assert script.script == SCRIPT_VALUE
assert responses.assert_call_count(url, 1) is True
def test_get_script_by_id_failed(self, startup_script_service, endpoint):
# arrange - add response mock
url = endpoint + '/x'
responses.add(
responses.GET,
url,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
startup_script_service.get_by_id('x')
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(url, 1) is True
def test_create_script_successful(self, startup_script_service, endpoint):
# arrange - add response mock
responses.add(responses.POST, endpoint, body=SCRIPT_ID, status=201)
# act
script = startup_script_service.create(SCRIPT_NAME, SCRIPT_VALUE)
# assert
assert isinstance(script, StartupScript)
assert script.id == SCRIPT_ID
assert responses.assert_call_count(endpoint, 1) is True
def test_create_script_failed(self, startup_script_service, endpoint):
# arrange - add response mock
responses.add(
responses.POST,
endpoint,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
startup_script_service.create(SCRIPT_NAME, SCRIPT_VALUE)
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(endpoint, 1) is True
def test_delete_scripts_successful(self, startup_script_service, endpoint):
# arrange - add response mock
responses.add(responses.DELETE, endpoint, status=200)
# act
result = startup_script_service.delete([SCRIPT_ID, script_ID_2])
# assert
assert result is None
assert responses.assert_call_count(endpoint, 1) is True
def test_delete_scripts_failed(self, startup_script_service, endpoint):
# arrange - add response mock
responses.add(
responses.DELETE,
endpoint,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
startup_script_service.delete(['x'])
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(endpoint, 1) is True
def test_delete_script_by_id_successful(self, startup_script_service, endpoint):
# arrange - add response mock
url = endpoint + '/' + SCRIPT_ID
responses.add(responses.DELETE, url, status=200)
# act
result = startup_script_service.delete_by_id(SCRIPT_ID)
# assert
assert result is None
assert responses.assert_call_count(url, 1) is True
def test_delete_script_by_id_failed(self, startup_script_service, endpoint):
# arrange - add response mock
url = endpoint + '/x'
responses.add(
responses.DELETE,
url,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
startup_script_service.delete_by_id('x')
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(url, 1) is True