-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
192 lines (152 loc) · 8.16 KB
/
test_app.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
import pytest
from http import HTTPStatus
from chalice import BadRequestError
from chalice.app import Request
import app
from chalicelib.db import DynamoDBTodo
from tests.testdata.ddb_items import TESTDATA_DDB_ITEMS
class TestApp:
@staticmethod
def set_env(client, monkeypatch):
client.get('/')
class TestGetIndex(TestApp):
def test_Return_status_code_200_and_json(self, client):
"""get_index: ステータスコード200とJSONを返すことができる"""
response = client.get('/')
assert response.status_code == HTTPStatus.OK
assert True if response.json else False == True
class TestGetAppDB(TestApp):
def test_Return_DynamoDBTodo_class(self):
"""get_app_db: get_app_dbの返り値クラスと、元のクラスが一致する"""
assert app.get_app_db().__class__ is DynamoDBTodo
class TestGetTodos(TestApp):
def _monkeys(self, client, monkeypatch):
super().set_env(client, monkeypatch)
monkeypatch.setattr(DynamoDBTodo, 'list_items',
lambda *_, **__: TESTDATA_DDB_ITEMS)
def test_Return_todos_list(self, client, monkeypatch):
"""get_todos: すべてのアイテムを取得することができる"""
self._monkeys(client, monkeypatch)
assert app.get_todos() == TESTDATA_DDB_ITEMS
class TestAddNewTodo(TestApp):
def _monkeys(self, client, monkeypatch):
super().set_env(client, monkeypatch)
from uuid import uuid4
monkeypatch.setattr(DynamoDBTodo, 'add_item',
lambda *_, **__: str(uuid4()))
monkeypatch.setattr(app, 'get_authorized_username',
lambda *_, **__: 'default')
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid_case_subject_and_description(self, client, monkeypatch, item):
"""add_new_todo: subjectとdescriptionがあるケース、uidを受け取ることができる"""
self._monkeys(client, monkeypatch)
json_body = {
'subject': item['subject'],
'description': item['description']}
monkeypatch.setattr(Request, 'json_body', json_body)
actual = app.add_new_todo()
assert type(actual) == str
assert len(actual) == 36
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid_case_subject_only(self, client, monkeypatch, item):
"""add_new_todo: subjectのみのケース、uidを受け取ることができる"""
self._monkeys(client, monkeypatch)
json_body = {'subject': item['subject']}
monkeypatch.setattr(Request, 'json_body', json_body)
actual = app.add_new_todo()
assert type(actual) == str
assert len(actual) == 36
def test_Raise_BadRequestError_case_None_subject(self, client, monkeypatch):
"""add_new_todo: subjectが無いケース、例外を発生させることができる"""
self._monkeys(client, monkeypatch)
json_body = {}
monkeypatch.setattr(Request, 'json_body', json_body)
with pytest.raises(BadRequestError):
app.add_new_todo()
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Raise_BadRequestError_case_without_subject(self, client, monkeypatch, item):
"""add_new_todo: descriptionのみのケース、例外を発生させることができる"""
self._monkeys(client, monkeypatch)
json_body = {'description': item['description']}
monkeypatch.setattr(Request, 'json_body', json_body)
with pytest.raises(BadRequestError):
app.add_new_todo()
def test_Raise_BadRequestError_case_None_json_body(self, client, monkeypatch):
"""add_new_todo: json_bodyがNoneのケース、例外を発生させることができる"""
self._monkeys(client, monkeypatch)
with pytest.raises(BadRequestError):
app.add_new_todo()
class TestGetTodo(TestApp):
def _monkeys(self, monkeypatch):
def _get_item(self, uid, username):
for item in TESTDATA_DDB_ITEMS:
if item['uid'] == uid and item['username'] == username:
return item
monkeypatch.setattr(DynamoDBTodo, 'get_item', _get_item)
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_todo_dict(self, monkeypatch, item):
"""get_todo: 取得に指定したuid、usernameのTodoを受け取ることができる"""
self._monkeys(monkeypatch)
monkeypatch.setattr(app, 'get_authorized_username',
lambda *_, **__: item['username'])
assert app.get_todo(uid=item['uid']) == item
class TestDeleteTodo(TestApp):
def _monkeys(self, monkeypatch):
def _delete_item(self, uid, username):
for item in TESTDATA_DDB_ITEMS:
if item['uid'] == uid and item['username'] == username:
return item['uid']
monkeypatch.setattr(DynamoDBTodo, 'delete_item', _delete_item)
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid(self, monkeypatch, item):
"""delete_todo: 削除に指定したuid、usernameのTodoのuidを受け取ることができる"""
self._monkeys(monkeypatch)
monkeypatch.setattr(app, 'get_authorized_username',
lambda *_, **__: item['username'])
assert app.delete_todo(uid=item['uid']) == item['uid']
class TestUpdateTodo(TestApp):
def _monkeys(self, client, monkeypatch, item=None):
super().set_env(client, monkeypatch)
def _update_item(self, uid, username, **__):
for item in TESTDATA_DDB_ITEMS:
if item['uid'] == uid and item['username'] == username:
return item['uid']
monkeypatch.setattr(DynamoDBTodo, 'update_item', _update_item)
monkeypatch.setattr(app, 'get_authorized_username',
lambda *_, **__: item['username'])
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid_case_subject_description_state(self, client, monkeypatch, item):
"""update_todo: すべての属性のケース、特定のTodoのuidを受け取ることができる"""
self._monkeys(client, monkeypatch, item)
json_body = {
'subject': item['subject']+"_updated",
'description': item['description']+"_updated",
'state': item['state']}
monkeypatch.setattr(Request, 'json_body', json_body)
assert app.update_todo(item['uid']) == item['uid']
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid_case_subject_only(self, client, monkeypatch, item):
"""update_todo: subjectのみのケース、特定のTodoのuidを受け取ることができる"""
self._monkeys(client, monkeypatch, item)
json_body = {'subject': item['subject']+"_updated"}
monkeypatch.setattr(Request, 'json_body', json_body)
assert app.update_todo(item['uid']) == item['uid']
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid_case_discription_only(self, client, monkeypatch, item):
"""update_todo: discriptionのみのケース、特定のTodoのuidを受け取ることができる"""
self._monkeys(client, monkeypatch, item)
json_body = {'discription': item['description']+"_updated"}
monkeypatch.setattr(Request, 'json_body', json_body)
assert app.update_todo(item['uid']) == item['uid']
@pytest.mark.parametrize('item', TESTDATA_DDB_ITEMS)
def test_Return_uid_case_state_only(self, client, monkeypatch, item):
"""update_todo: stateのみのケース、特定のTodoのuidを受け取ることができる"""
self._monkeys(client, monkeypatch, item)
json_body = {'state': item['state']}
monkeypatch.setattr(Request, 'json_body', json_body)
assert app.update_todo(item['uid']) == item['uid']
def test_Raise_BadRequestError_case_None_json_body(self, client, monkeypatch):
"""update_todo: json_bodyがNoneのケース、例外を発生させることができる"""
self._monkeys(client, monkeypatch)
with pytest.raises(BadRequestError):
app.update_todo("_uid")