This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_client.py
242 lines (179 loc) · 7.25 KB
/
test_client.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from pytest import fixture, mark, raises
from todoms.client import ResourceNotFoundError, ResponseError
from todoms.fields.basic import Attribute
from todoms.resources import Resource, TaskList
from .utils.constants import API_BASE
from .utils.helpers import match_body
EXPECTED_ERRORS = [(404, ResourceNotFoundError), (500, ResponseError)]
@fixture
def resource_class():
class FakeResource(Resource):
ENDPOINT = "fake"
_id = Attribute[str]("id")
name = Attribute[str]("name")
@classmethod
def handle_list_filters(cls, **kwargs):
return {name: f"{value}-parsed" for name, value in kwargs.items()}
return FakeResource
@fixture
def resource_obj(resource_class):
return resource_class(name="name-1", _id="id-1")
def test_list_resource_returns_all(client, resource_class, requests_mock):
requests_mock.get(
f"{API_BASE}/{resource_class.ENDPOINT}/delta",
json={"value": [{"name": "res-1"}, {"name": "res-2"}]},
)
results = list(client.list(resource_class))
assert len(results) == 2
assert list(filter(lambda e: e.name == "res-1", results)) is not []
assert list(filter(lambda e: e.name == "res-2", results)) is not []
def test_list_resource_returns_all_when_parted(client, resource_class, requests_mock):
requests_mock.get(
f"{API_BASE}/{resource_class.ENDPOINT}/delta",
json={"value": [{"name": "res-1"}], "@odata.nextLink": "http://next/part/1"},
)
requests_mock.get(
"http://next/part/1",
json={"value": [{"name": "res-2"}], "@odata.nextLink": "http://next/part/2"},
)
requests_mock.get(
"http://next/part/2",
json={"value": [{"name": "res-3"}]},
)
results = list(client.list(resource_class))
assert len(results) == 3
assert list(filter(lambda e: e.name == "res-1", results)) is not []
assert list(filter(lambda e: e.name == "res-2", results)) is not []
assert list(filter(lambda e: e.name == "res-3", results)) is not []
def test_list_use_custom_endpoint(client, resource_class, requests_mock):
requests_mock.get(
f"{API_BASE}/my-endpoint/all/delta",
json={"value": [{"name": "res-1"}]},
)
results = list(client.list(resource_class, endpoint="my-endpoint/all"))
assert len(results) == 1
def test_client_task_lists_property_works(client, requests_mock):
requests_mock.get(
f"{API_BASE}/{TaskList.ENDPOINT}/delta",
json={"value": [{"id": "list-1"}, {"id": "list-2"}]},
)
results = list(client.task_lists)
assert len(results) == 2
assert isinstance(results[0], TaskList)
def test_client_saves_task_list(client, requests_mock):
requests_mock.post(
f"{API_BASE}/{TaskList.ENDPOINT}",
json={"id": "list-1"},
status_code=201,
)
task_list = TaskList(name="list-1")
result = client.save_list(task_list)
assert task_list.id == "list-1"
assert task_list._client is client
assert result is task_list
def test_list_resources_sends_filters(client, resource_class, requests_mock):
requests_mock.get(
f"{API_BASE}/{resource_class.ENDPOINT}?filter=test-parsed",
json={"value": [{"name": "res-1"}], "@odata.nextLink": "http://next/part/1"},
)
requests_mock.get(
"http://next/part/1",
json={"value": [{"name": "res-2"}]},
)
results = list(client.list(resource_class, filter="test"))
assert len(results) == 2
assert list(filter(lambda e: e.name == "res-1", results)) is not []
assert list(filter(lambda e: e.name == "res-2", results)) is not []
@mark.parametrize("error_code,exception", EXPECTED_ERRORS)
def test_list_resource_raises_on_http_error(
client, resource_class, requests_mock, error_code, exception
):
requests_mock.get(
f"{API_BASE}/{resource_class.ENDPOINT}/delta", status_code=error_code
)
with raises(exception):
list(client.list(resource_class))
def test_get_resource_returns_obj(client, resource_class, requests_mock):
requests_mock.get(
f"{API_BASE}/{resource_class.ENDPOINT}/id-1",
json={"name": "res-1"},
)
result = client.get(resource_class, "id-1")
assert result.name == "res-1"
assert isinstance(result, resource_class)
def test_get_uses_provided_endpoint(client, resource_class, requests_mock):
requests_mock.get(
f"{API_BASE}/pre-created-endpoint",
json={"name": "res-1"},
)
result = client.get(resource_class, endpoint="pre-created-endpoint")
assert result.name == "res-1"
assert isinstance(result, resource_class)
def test_get_raises_when_not_endpoint_and_id(client, resource_class):
with raises(ValueError):
client.get(resource_class)
@mark.parametrize("error_code,exception", EXPECTED_ERRORS)
def test_get_resource_raises_on_http_error(
client, resource_class, requests_mock, error_code, exception
):
requests_mock.get(
f"{API_BASE}/{resource_class.ENDPOINT}/fail", status_code=error_code
)
with raises(exception):
client.get(resource_class, "fail")
def test_delete_resource_pass(client, resource_obj, requests_mock):
requests_mock.delete(f"{API_BASE}/{resource_obj.ENDPOINT}/id-1", status_code=204)
client.delete(resource_obj)
@mark.parametrize("error_code,exception", EXPECTED_ERRORS)
def test_delete_resource_raises_on_http_error(
client, resource_obj, requests_mock, error_code, exception
):
requests_mock.delete(
f"{API_BASE}/{resource_obj.ENDPOINT}/id-1", status_code=error_code
)
with raises(exception):
client.delete(resource_obj)
@mark.parametrize("error_code,exception", EXPECTED_ERRORS)
def test_patch_resource_raises_on_http_error(
client, resource_obj, requests_mock, error_code, exception
):
requests_mock.patch(
f"{API_BASE}/{resource_obj.ENDPOINT}/id-1", status_code=error_code
)
with raises(exception):
client.patch(resource_obj)
def test_patch_sends_data(client, resource_obj, requests_mock):
requests_mock.patch(
f"{API_BASE}/{resource_obj.ENDPOINT}/id-1",
json={"ok": "true"},
additional_matcher=match_body({"name": "name-1", "id": "id-1"}),
)
response = client.patch(resource_obj)
assert response == {"ok": "true"}
def test_raw_post(client, requests_mock):
requests_mock.post(
f"{API_BASE}/my-endpoint/sending",
json={"result": "ok"},
status_code=201,
additional_matcher=match_body({"my": "body"}),
)
result = client.raw_post("my-endpoint/sending", data={"my": "body"})
assert result == {"result": "ok"}
def test_raw_post_with_non_default_status_code(client, requests_mock):
requests_mock.post(
f"{API_BASE}/my-endpoint/sending",
json={"result": "ok"},
status_code=205,
additional_matcher=match_body({"my": "body"}),
)
result = client.raw_post(
"my-endpoint/sending", data={"my": "body"}, expected_code=205
)
assert result == {"result": "ok"}
@mark.parametrize("error_code,exception", EXPECTED_ERRORS)
def test_raw_post_raises_on_error(client, requests_mock, error_code, exception):
requests_mock.post(
f"{API_BASE}/my-endpoint/sending", json={"result": "ok"}, status_code=error_code
)
with raises(exception):
client.raw_post("my-endpoint/sending", data={})