forked from pcorpet/airtable.py
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_airtable.py
300 lines (260 loc) · 9.88 KB
/
test_airtable.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
import airtable
import mock
import requests
from typing import Any, Dict
import unittest
FAKE_TABLE_NAME = 'TableName'
FAKE_BASE_ID = 'app12345'
FAKE_API_KEY = 'fake_api_key'
class TestAirtable(unittest.TestCase):
def setUp(self):
self.base_id = FAKE_BASE_ID
self.api_key = FAKE_API_KEY
self.airtable = airtable.Airtable(self.base_id, self.api_key)
def get(self, *args, **kwargs):
return self.airtable.get(FAKE_TABLE_NAME, *args, **kwargs)
def delete(self, *args, **kwargs):
return self.airtable.delete(FAKE_TABLE_NAME, *args, **kwargs)
def iterate(self, *args, **kwargs):
return self.airtable.iterate(FAKE_TABLE_NAME, *args, **kwargs)
def test_build_base_url(self):
self.assertEqual(self.airtable.base_url,
'https://api.airtable.com/v0/app12345')
def test_build_headers(self):
self.assertEqual(self.airtable.headers['Authorization'],
'Bearer fake_api_key')
@mock.patch.object(requests, 'request')
def test_get_all(self, mock_request):
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
'records': [
{
'id': 'reccA6yaHKzw5Zlp0',
'fields': {
'Name': 'John',
'Number': '(987) 654-3210'
}
},
{
'id': 'reccg3Kke0QvTDW0H',
'fields': {
'Name': 'Nico',
'Number': '(123) 222-1131'
}
}
],
'offset': 'reccg3Kke0QvTDW0H'
}
mock_request.return_value = mock_response
res = self.get()
self.assertEqual(len(res['records']), 2)
self.assertEqual(res['offset'], 'reccg3Kke0QvTDW0H')
@mock.patch.object(requests, 'request')
def test_order_of_fields_is_preserved(self, mock_request):
mock_response = requests.Response()
mock_response.status_code = 200
# Set the text content of the response to a JSON string so we can test
# how it gets deserialized
mock_response._content = b'''{
"records": [
{
"id": "reccA6yaHKzw5Zlp0",
"fields": {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
"i": 9,
"j": 10,
"k": 11,
"l": 12,
"m": 13
}
},
{
"id": "reccg3Kke0QvTDW0H",
"fields": {
"n": 14,
"o": 15,
"p": 16,
"q": 17,
"r": 18,
"s": 19,
"t": 20,
"u": 21,
"v": 22,
"w": 23,
"x": 24,
"y": 25,
"z": 26
}
}
]
}'''
mock_response.encoding = 'utf-8'
mock_request.return_value = mock_response
r = self.airtable.get(FAKE_TABLE_NAME)
self.assertEqual(list(r['records'][0]['fields'].keys()), list(u'abcdefghijklm'))
self.assertEqual(list(r['records'][1]['fields'].keys()), list(u'nopqrstuvwxyz'))
@mock.patch.object(requests, 'request')
def test_get_by_id(self, mock_request):
fake_id = 'reccA6yaHKzw5Zlp0'
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
'id': 'reccA6yaHKzw5Zlp0',
'fields': {
'Name': 'John',
'Number': '(987) 654-3210'
}
}
mock_request.return_value = mock_response
r = self.get(fake_id)
self.assertEqual(r['id'], fake_id)
@mock.patch.object(requests, 'request')
def test_get_not_found(self, mock_request):
mock_response = mock.MagicMock()
mock_response.status_code = 404
mock_response.json.return_value = {
'error': {
'type': 'TABLE_NOT_FOUND',
'message': 'Could not find table %s in application %s' % (
FAKE_TABLE_NAME, FAKE_BASE_ID),
},
}
mock_request.return_value = mock_response
with self.assertRaises(airtable.AirtableError) as error:
self.get('123')
self.assertEqual('TABLE_NOT_FOUND', error.exception.type)
@mock.patch.object(requests, 'request')
def test_get_view(self, mock_request):
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {'records': []}
mock_request.return_value = mock_response
r = self.get(view='My view')
mock_request.assert_called_once_with(
'GET', 'https://api.airtable.com/v0/app12345/TableName',
data=None, headers={'Authorization': 'Bearer fake_api_key'},
params={'view': 'My view'})
@mock.patch.object(requests, 'request')
def test_get_filter_by_formula(self, mock_request):
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {'records': []}
mock_request.return_value = mock_response
r = self.get(filter_by_formula="{title} = ''")
mock_request.assert_called_once_with(
'GET', 'https://api.airtable.com/v0/app12345/TableName',
data=None, headers={'Authorization': 'Bearer fake_api_key'},
params={'filterByFormula': "{title} = ''"})
def test_invalid_get(self):
with self.assertRaises(airtable.IsNotString):
self.get(123)
self.get(offset=123)
with self.assertRaises(airtable.IsNotInteger):
self.get(limit='1')
@mock.patch.object(requests, 'request')
def test_delete(self, mock_request):
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
'deleted': True,
'id': '1234'
}
mock_request.return_value = mock_response
r = self.delete('1234')
self.assertTrue(r['deleted'])
self.assertEqual(r['id'], '1234')
def test_invalid_delete(self):
with self.assertRaises(airtable.IsNotString):
self.delete(123)
@mock.patch.object(requests, 'request')
def test_iterate(self, mock_request):
mock_response1 = mock.MagicMock()
mock_response1.status_code = 200
mock_response1.json.return_value = {
'records': [
{
'id': 'reccA6yaHKzw5Zlp0',
'fields': {
'Name': 'John',
'Number': '(987) 654-3210'
}
},
{
'id': 'reccg3Kke0QvTDW0H',
'fields': {
'Name': 'Nico',
'Number': '(123) 222-1131'
}
}
],
'offset': 'reccg3Kke0QvTDW0H'
}
mock_response2 = mock.MagicMock()
mock_response2.status_code = 200
mock_response2.json.return_value = {
'records': [
{
'id': 'reccA23fSERw5Zlp0',
'fields': {
'Name': 'Ron',
'Number': '(987) 654-3210'
}
},
],
}
mock_request.side_effect = [mock_response1, mock_response2]
results = list(self.iterate())
self.assertEqual(
['John', 'Nico', 'Ron'], [r.get('fields', {}).get('Name') for r in results])
class TestTableFromBase(TestAirtable):
def setUp(self):
super(TestTableFromBase, self).setUp()
self.table = self.airtable.table(FAKE_TABLE_NAME)
def get(self, *args, **kwargs):
return self.table.get(*args, **kwargs)
def delete(self, *args, **kwargs):
return self.table.delete(*args, **kwargs)
def iterate(self, *args, **kwargs):
return self.table.iterate(*args, **kwargs)
class TestTableFromConfig(TestAirtable):
def setUp(self):
super(TestTableFromConfig, self).setUp()
self.table = airtable.Table(self.base_id, FAKE_TABLE_NAME, api_key=FAKE_API_KEY)
def get(self, *args, **kwargs):
return self.table.get(*args, **kwargs)
def delete(self, *args, **kwargs):
return self.table.delete(*args, **kwargs)
def iterate(self, *args, **kwargs):
return self.table.iterate(*args, **kwargs)
@mock.patch.object(requests, 'request')
def test_typed_table(self, mock_request):
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
'records': [
{
'id': 'reccA23fSERw5Zlp0',
'fields': {
'Name': 3,
'Number': 4
}
},
],
}
mock_request.return_value = mock_response
table = airtable.Table[Dict[str, int]](
self.base_id, FAKE_TABLE_NAME, api_key=FAKE_API_KEY)
response = table.get()
self.assertEqual(response['records'][0]['fields'], {'Name': 3, 'Number': 4})
# This line is here only to check that Python code supports having the Record used.
_UnusedType = airtable.Record[Dict[str, Any]]
if __name__ == '__main__':
unittest.main()