forked from gtalarico/pyairtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api_table.py
467 lines (378 loc) · 14.6 KB
/
test_api_table.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
from posixpath import join as urljoin
import pytest
from requests import Request
from requests_mock import Mocker
from pyairtable import Api, Base, Table
from pyairtable.models.schema import TableSchema
from pyairtable.testing import fake_id, fake_record
from pyairtable.utils import chunked
@pytest.fixture()
def table_schema(sample_json) -> TableSchema:
return TableSchema.parse_obj(sample_json("TableSchema"))
@pytest.fixture
def mock_schema(table, requests_mock, sample_json):
table_schema = sample_json("TableSchema")
table_schema["id"] = table.name = fake_id("tbl")
return requests_mock.get(
table.base.meta_url("tables") + "?include=visibleFieldIds",
json={"tables": [table_schema]},
)
def test_constructor(base: Base):
"""
Test the constructor.
"""
table = Table(None, base, "table_name")
assert table.api == base.api
assert table.base == base
assert table.name == "table_name"
def test_constructor_with_schema(base: Base, table_schema: TableSchema):
table = Table(None, base, table_schema)
assert table.api == base.api
assert table.base == base
assert table.name == table_schema.name
assert table.url == f"https://api.airtable.com/v0/{base.id}/{table_schema.id}"
assert (
repr(table)
== f"<Table base='{base.id}' id='{table_schema.id}' name='{table_schema.name}'>"
)
def test_deprecated_constructor(api: Api, base: Base):
"""
Test that "legacy" constructor (passing strings instead of instances)
will throw deprecation warning, but it _will_ work.
"""
with pytest.warns(DeprecationWarning):
table = Table(api.api_key, base.id, "table_name", timeout=(1, 99))
assert table.api.api_key == api.api_key
assert table.api.timeout == (1, 99)
assert table.base.id == base.id
assert table.name == "table_name"
def test_invalid_constructor(api, base):
"""
Test that we get a TypeError if passing invalid args to Table.
"""
for args in [
[api, "base_id", "table_name"],
["api_key", base, "table_name"],
[api, base, "table_name"],
[None, base, -1],
]:
kwargs = args.pop() if isinstance(args[-1], dict) else {}
with pytest.raises(TypeError):
print(args, kwargs)
Table(*args, **kwargs)
def test_repr(table: Table):
assert repr(table) == "<Table base='appJMY16gZDQrMWpA' name='Table Name'>"
def test_schema(base, requests_mock, sample_json):
"""
Test that we can load schema from API.
"""
table = base.table("Apartments")
m = requests_mock.get(base.meta_url("tables"), json=sample_json("BaseSchema"))
assert isinstance(schema := table.schema(), TableSchema)
assert m.call_count == 1
assert schema.id == "tbltp8DGLhqbUmjK1"
def test_id(base, requests_mock, sample_json):
"""
Test that we load schema from API if we need the ID and don't have it,
but if we get a name that *looks* like an ID, we trust it.
"""
m = requests_mock.get(base.meta_url("tables"), json=sample_json("BaseSchema"))
table = base.table("tbltp8DGLhqbUmjK1")
assert table.id == "tbltp8DGLhqbUmjK1"
assert m.call_count == 0
table = base.table("Apartments")
assert table.id == "tbltp8DGLhqbUmjK1"
assert m.call_count == 1
@pytest.mark.parametrize(
"base_id,table_name,table_url_suffix",
[
("abc", "My Table", "abc/My%20Table"),
("abc", "SomeTable", "abc/SomeTable"),
("abc", "Table-fake", "abc/Table-fake"),
],
)
def test_url(api: Api, base_id, table_name, table_url_suffix):
table = api.table(base_id, table_name)
assert table.url == f"https://api.airtable.com/v0/{table_url_suffix}"
def test_chunk(table: Table):
chunks = [chunk for chunk in chunked([0, 0, 1, 1, 2, 2, 3], 2)]
assert chunks[0] == [0, 0]
assert chunks[1] == [1, 1]
assert chunks[2] == [2, 2]
assert chunks[3] == [3]
def test_record_url(table: Table):
rv = table.record_url("xxx")
assert rv == urljoin(table.url, "xxx")
def test_api_key(table: Table, mock_response_single):
def match_auth_header(request):
expected_auth_header = "Bearer {}".format(table.api.api_key)
return (
"Authorization" in request.headers
and request.headers["Authorization"] == expected_auth_header
)
with Mocker() as m:
m.get(
table.record_url("rec"),
status_code=200,
json=mock_response_single,
additional_matcher=match_auth_header,
)
table.get("rec")
def test_get(table: Table, mock_response_single):
_id = mock_response_single["id"]
with Mocker() as mock:
mock.get(table.record_url(_id), status_code=200, json=mock_response_single)
resp = table.get(_id)
assert dict_equals(resp, mock_response_single)
def test_first(table: Table, mock_response_single):
mock_response = {"records": [mock_response_single]}
with Mocker() as mock:
url = Request("get", table.url, params={"maxRecords": 1}).prepare().url
mock.get(
url,
status_code=200,
json=mock_response,
)
rv = table.first()
assert rv
assert rv["id"] == mock_response_single["id"]
def test_first_via_post(table: Table, mock_response_single):
mock_response = {"records": [mock_response_single]}
with Mocker() as mock:
url = table.url + "/listRecords"
formula = f"RECORD_ID() != '{'x' * 17000}'"
mock_endpoint = mock.post(url, status_code=200, json=mock_response)
rv = table.first(formula=formula)
assert mock_endpoint.last_request.json() == {
"filterByFormula": formula,
"maxRecords": 1,
"pageSize": 1,
}
assert rv == mock_response_single
def test_first_none(table: Table, mock_response_single):
mock_response = {"records": []}
with Mocker() as mock:
url = Request("get", table.url, params={"maxRecords": 1}).prepare().url
mock.get(
url,
status_code=200,
json=mock_response,
)
rv = table.first()
assert rv is None
def test_all(table: Table, mock_response_list, mock_records):
with Mocker() as mock:
mock.get(
table.url,
status_code=200,
json=mock_response_list[0],
complete_qs=True,
)
for n, resp in enumerate(mock_response_list, 1):
offset = resp.get("offset", None)
if not offset:
continue
offset_url = table.url + "?offset={}".format(offset)
mock.get(
offset_url,
status_code=200,
json=mock_response_list[1],
complete_qs=True,
)
response = table.all()
for n, resp in enumerate(response):
assert dict_equals(resp, mock_records[n])
def test_iterate(table: Table, mock_response_list, mock_records):
with Mocker() as mock:
mock.get(
table.url,
status_code=200,
json=mock_response_list[0],
complete_qs=True,
)
for n, resp in enumerate(mock_response_list, 1):
offset = resp.get("offset", None)
if not offset:
continue
params = {"offset": offset}
offset_url = Request("get", table.url, params=params).prepare().url
mock.get(
offset_url,
status_code=200,
json=mock_response_list[1],
complete_qs=True,
)
pages = []
for page in table.iterate():
pages.append(page)
for n, response in enumerate(mock_response_list):
assert seq_equals(pages[n], response["records"])
def test_create(table: Table, mock_response_single):
with Mocker() as mock:
post_data = mock_response_single["fields"]
mock.post(
table.url,
status_code=201,
json=mock_response_single,
additional_matcher=match_request_data(post_data),
)
resp = table.create(post_data)
assert dict_equals(resp, mock_response_single)
@pytest.mark.parametrize("container", [list, tuple, iter])
def test_batch_create(table: Table, container, mock_records):
with Mocker() as mock:
for chunk in _chunk(mock_records, 10):
mock.post(
table.url,
status_code=201,
json={"records": chunk},
)
records = [i["fields"] for i in mock_records]
resp = table.batch_create(container(records))
assert seq_equals(resp, mock_records)
@pytest.mark.parametrize("replace,http_method", [(False, "PATCH"), (True, "PUT")])
def test_update(table: Table, mock_response_single, replace, http_method):
id_ = mock_response_single["id"]
post_data = mock_response_single["fields"]
with Mocker() as mock:
mock.register_uri(
http_method,
urljoin(table.url, id_),
status_code=201,
json=mock_response_single,
additional_matcher=match_request_data(post_data),
)
resp = table.update(id_, post_data, replace=replace)
assert dict_equals(resp, mock_response_single)
@pytest.mark.parametrize("container", [list, tuple, iter])
@pytest.mark.parametrize("replace,http_method", [(False, "PATCH"), (True, "PUT")])
def test_batch_update(table: Table, container, replace, http_method):
records = [fake_record(fieldvalue=index) for index in range(50)]
with Mocker() as mock:
mock.register_uri(
http_method,
table.url,
response_list=[
{"json": {"records": chunk}} for chunk in table.api.chunked(records)
],
)
resp = table.batch_update(container(records), replace=replace)
assert resp == records
@pytest.mark.parametrize("container", [list, tuple, iter])
@pytest.mark.parametrize("replace,http_method", [(False, "PATCH"), (True, "PUT")])
def test_batch_upsert(table: Table, container, replace, http_method, monkeypatch):
field_name = "Name"
exists1 = fake_record({field_name: "Exists 1"})
exists2 = fake_record({field_name: "Exists 2"})
created = fake_record({field_name: "Does not exist"})
payload = [
{"id": exists1["id"], "fields": {field_name: "Exists 1"}},
{"fields": {field_name: "Exists 2"}},
{"fields": {field_name: "Does not exist"}},
]
responses = [
{"createdRecords": [], "updatedRecords": [exists1["id"]], "records": [exists1]},
{"createdRecords": [], "updatedRecords": [exists2["id"]], "records": [exists2]},
{"createdRecords": [created["id"]], "updatedRecords": [], "records": [created]},
]
with Mocker() as mock:
mock.register_uri(
http_method,
table.url,
response_list=[{"json": response} for response in responses],
)
monkeypatch.setattr(table.api, "MAX_RECORDS_PER_REQUEST", 1)
resp = table.batch_upsert(
container(payload), key_fields=[field_name], replace=replace
)
assert resp == {
"createdRecords": [created["id"]],
"updatedRecords": [exists1["id"], exists2["id"]],
"records": [exists1, exists2, created],
}
def test_batch_upsert__missing_field(table: Table, requests_mock):
"""
Test that batch_upsert raises an exception if a record in the input
is missing one of the key_fields, since this will create an error
on the API.
"""
with pytest.raises(ValueError):
table.batch_upsert([{"fields": {"Name": "Alice"}}], key_fields=["Email"])
def test_delete(table: Table, mock_response_single):
id_ = mock_response_single["id"]
expected = {"deleted": True, "id": id_}
with Mocker() as mock:
mock.delete(urljoin(table.url, id_), status_code=201, json=expected)
resp = table.delete(id_)
assert resp == expected
@pytest.mark.parametrize("container", [list, tuple, iter])
def test_batch_delete(table: Table, container, mock_records):
ids = [i["id"] for i in mock_records]
with Mocker() as mock:
for chunk in _chunk(ids, 10):
json_response = {"records": [{"deleted": True, "id": id_} for id_ in chunk]}
url_match = (
Request("get", table.url, params={"records[]": chunk}).prepare().url
)
mock.delete(
url_match,
status_code=201,
json=json_response,
)
resp = table.batch_delete(container(ids))
expected = [{"deleted": True, "id": i} for i in ids]
assert resp == expected
def test_create_field(table, mock_schema, requests_mock, sample_json):
"""
Tests the API for creating a field (but without actually performing the operation).
"""
mock_create = requests_mock.post(
table.meta_url("fields"),
json=sample_json("field_schema/SingleSelectFieldSchema"),
)
# Ensure we have pre-loaded our schema
table.schema()
assert mock_schema.call_count == 1
# Create the field
choices = ["Todo", "In progress", "Done"]
fld = table.create_field(
"Status",
"singleSelect",
description="field description",
options={"choices": choices},
)
assert mock_create.call_count == 1
assert mock_create.request_history[-1].json() == {
"name": "Status",
"type": "singleSelect",
"description": "field description",
"options": {"choices": choices},
}
# Test the result we got back
assert fld.id == "fldqCjrs1UhXgHUIc"
assert fld.name == "Status"
assert {c.name for c in fld.options.choices} == set(choices)
# Test that we constructed the URL correctly
assert fld._url.endswith(f"/{table.base.id}/tables/{table.name}/fields/{fld.id}")
# Test that the schema has been updated without a second API call
assert table._schema.field(fld.id).name == "Status"
assert mock_schema.call_count == 1
def test_delete_view(table, mock_schema, requests_mock):
view = table.schema().view("Grid view")
m = requests_mock.delete(view._url)
view.delete()
assert m.call_count == 1
# Helpers
def _chunk(iterable, chunk_size):
for i in range(0, len(iterable), chunk_size):
yield iterable[i : i + chunk_size]
def match_request_data(post_data):
"""Custom Matches, check that provided Request data is correct"""
def _match_request_data(request):
request_data_fields = request.json()["fields"]
return dict_equals(request_data_fields, post_data)
return _match_request_data
def dict_equals(d1, d2):
return sorted(d1.items()) == sorted(d2.items())
def seq_equals(s1, s2):
return all(dict_equals(s1, s2) for s1, s2 in zip(s1, s2))