-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_api.py
More file actions
126 lines (107 loc) · 3.92 KB
/
test_api.py
File metadata and controls
126 lines (107 loc) · 3.92 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
import random
import string
from unittest import TestCase
import requests
class ApiTestCase(TestCase):
def setUp(self) -> None:
self.client = requests.Session()
@staticmethod
def _generate_random_string(length=10, use_numbers=True, use_letters=True):
chars = ''
if use_numbers:
chars = chars + string.digits
if use_letters:
chars = chars + string.ascii_uppercase
return ''.join(random.choices(chars, k=length))
def url(self, relative):
return 'http://localhost:8000/' + relative
def test_status(self):
result = self.client.get(url=self.url('status'))
self.assertEqual(200, result.status_code)
def test_create(self):
description = self._generate_random_string()
body = {
'description': description
}
result = self.client.post(url=self.url('items'), json=body)
self.assertEqual(201, result.status_code)
self.assertEqual({
'description': description,
'id': AlwaysTrue(),
'available_amount': 0
}, result.json())
def test_list(self):
first_description = self._generate_random_string()
body = {
'description': first_description
}
self.client.post(url=self.url('items'), json=body)
second_description = self._generate_random_string()
body = {
'description': second_description
}
self.client.post(url=self.url('items'), json=body)
params = {
'amount': 2,
'order_by': 'id',
'order': 'DESC'
}
result = self.client.get(url=self.url('items'), params=params)
self.assertEqual(200, result.status_code)
self.assertEqual([{
'description': second_description,
'id': AlwaysTrue(),
'available_amount': 0
}, {
'description': first_description,
'id': AlwaysTrue(),
'available_amount': 0
}], result.json())
def test_get(self):
description = self._generate_random_string()
body = {
'description': description
}
result = self.client.post(url=self.url('items'), json=body)
self.assertEqual(201, result.status_code)
item_id = result.json()['id']
result = self.client.get(url=self.url('items/{}'.format(item_id)))
self.assertEqual(200, result.status_code)
self.assertEqual({
'description': description,
'id': item_id,
'available_amount': 0
}, result.json())
result = self.client.get(url=self.url('items/{}'.format(random.randint(1, 100000000))))
self.assertEqual(404, result.status_code)
self.assertEqual({'error_code': 'ITEM_NOT_FOUND'}, result.json())
def test_update(self):
description = self._generate_random_string()
body = {
'description': description
}
result = self.client.post(url=self.url('items'), json=body)
self.assertEqual(201, result.status_code)
item_id = result.json()['id']
result = self.client.get(url=self.url('items/{}'.format(item_id)))
self.assertEqual(200, result.status_code)
self.assertEqual({
'description': description,
'id': item_id,
'available_amount': 0
}, result.json())
body = {
'increment': True
}
result = self.client.put(url=self.url('items/{}'.format(item_id)), json=body)
self.assertEqual(200, result.status_code)
result = self.client.get(url=self.url('items/{}'.format(item_id)))
self.assertEqual(200, result.status_code)
self.assertEqual({
'description': description,
'id': item_id,
'available_amount': 1
}, result.json())
class AlwaysTrue(object):
def __eq__(self, other):
return other is not None