/
tests.py
194 lines (153 loc) · 7.17 KB
/
tests.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import datetime
import noseapp
from noseapp.ext.requests import RequestsEx, make_config
from requests import HTTPError
# setting up
endpoint = make_config()
endpoint.configure(
base_url='http://127.0.0.1:5000/',
key='127.0.0.1:5000'
)
endpoint.session_configure(
always_return_json=True,
raise_on_http_error=False
)
requests_ex = RequestsEx(endpoint)
# gather all tests
suite = noseapp.Suite('first_suite')
# setup for requests
api = requests_ex.get_endpoint_session('127.0.0.1:5000')
@suite.register
class TestCaseGetOne(noseapp.TestCase):
"""
get request
"""
def test_get_404(self):
# check existence
self.assertEqual(HTTPError(404).message, api.get('dictionary/123')['code'])
def test_get_added_element(self):
# add element for test
api.post('dictionary', {"key": "test_get", "value": "test_me"})
# check existing element
self.assertTrue(api.get('dictionary/test_get').get('test_get'))
self.assertEqual("test_me", api.get('dictionary/test_get')['test_get'])
# delete element
self.assertEqual(api.delete('dictionary/test_get')['test_get'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_get').get('test_get'))
# check time - must be equivalent to out time
def test_get_time(self):
# add element for test
api.post('dictionary', {"key": "test_get", "value": "test_me"})
# check existing element
self.assertTrue(api.get('dictionary/test_get').get('test_get'))
self.assertEqual(api.get('dictionary/test_get')['time'], datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
# delete element
self.assertEqual(api.delete('dictionary/test_get')['test_get'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_get').get('test_get'))
@suite.register
class TestCasePost(noseapp.TestCase):
"""
post requests
"""
def test_post_added_element(self):
# add element for test
api.post('dictionary', {"key": "test_post", "value": "target"})
# check existing element
self.assertTrue(api.get('dictionary/test_post').get('test_post'))
self.assertEqual("target", api.get('dictionary/test_post')['test_post'])
# delete element
self.assertEqual(api.delete('dictionary/test_post')['test_post'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_post').get('test_post'))
# adding already existed element
def test_post_409(self):
# add element for test
api.post('dictionary', {"key": "test_post", "value": "target"})
# check existing element
self.assertTrue(api.get('dictionary/test_post').get('test_post'))
self.assertEqual(HTTPError(409).message, api.post('dictionary', {"key": "test_post", "value": "1"})['code'])
# delete element
self.assertEqual(api.delete('dictionary/test_post')['test_post'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_post').get('test_post'))
# pass one missing param
def test_post_400_one_param(self):
self.assertEqual(HTTPError(400).message, api.post('dictionary', {"key": "test_post", "value": ""})['code'])
# pass two missing params
def test_post_400_two_params(self):
self.assertEqual(HTTPError(400).message, api.post('dictionary', {"key": "", "value": ""})['code'])
# check time - must be equivalent to out time
def test_post_time(self):
self.assertEqual(api.post('dictionary', {"key": "test_post", "value": "target"})['time'], datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
# delete element
self.assertEqual(api.delete('dictionary/test_post')['test_post'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_post').get('test_post'))
@suite.register
class TestCasePut(noseapp.TestCase):
"""
put requests
"""
def test_put_change_element(self):
# add element for test
api.post('dictionary', {"key": "test_put", "value": "test_me"})
# check existing element
self.assertTrue(api.get('dictionary/test_put').get('test_put'))
# change element
api.put('dictionary', {"key": "test_put", "value": "test_me_again"})
self.assertTrue(api.get('dictionary/test_put').get('test_put'))
self.assertEqual("test_me_again", api.get('dictionary/test_put')['test_put'])
# delete element
self.assertEqual(api.delete('dictionary/test_put')['test_put'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_put').get('test_put'))
def test_put_404(self):
# add element for test
api.post('dictionary', {"key": "test_put", "value": "test_me"})
# check existing element
self.assertTrue(api.get('dictionary/test_put').get('test_put'))
self.assertEqual(HTTPError(404).message, api.put('dictionary', {"key": 'another_key', "value": 'another_value'})['code'])
# delete element
self.assertEqual(api.delete('dictionary/test_put')['test_put'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_put').get('test_put'))
def test_put_time(self):
# add element for test
api.post('dictionary', {"key": "test_put", "value": "test_me"})
# check existing element
self.assertTrue(api.get('dictionary/test_put').get('test_put'))
self.assertEqual(api.put('dictionary', {"key": 'test_put', "value": 'another_value'})['time'], datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
# delete element
self.assertEqual(api.delete('dictionary/test_put')['test_put'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_put').get('test_put'))
@suite.register
class TestCaseDelete(noseapp.TestCase):
"""
delete requests
"""
# double deleting same element to check 200
def test_delete_ok(self):
# add element for test
api.post('dictionary', {"key": "test_delete", "value": "speech"})
# check existing element
self.assertTrue(api.get('dictionary/test_delete').get('test_delete'))
self.assertEqual(api.delete('dictionary/test_delete')['test_delete'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_delete').get('test_delete'))
self.assertEqual(api.delete('dictionary/test_delete')['test_delete'], 'null')
# check existing element
self.assertTrue(not api.get('dictionary/test_delete').get('test_delete'))
def test_delete_time(self):
# add element for test
api.post('dictionary', {"key": "test_delete", "value": "speech"})
# check existing element
self.assertTrue(api.get('dictionary/test_delete').get('test_delete'))
self.assertEqual(api.delete('dictionary/test_delete')['time'], datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
# check existing element
self.assertTrue(not api.get('dictionary/test_delete').get('test_delete'))
app = noseapp.NoseApp('flask_test_with_noseapp')
app.register_suite(suite)
app.run()