Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch creation API. #6

Open
wants to merge 1 commit into
base: cyrille-fix-tests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- 2.7
- 3.8
install:
- pip install -r requirements.txt
script: python airtable/airtable_test.py
Expand Down
5 changes: 4 additions & 1 deletion airtable/airtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ def iterate(

def create(self, table_name, data):
if check_string(table_name):
payload = create_payload(data)
if isinstance(data, list):
payload = [create_payload(record) for record in data]
else:
payload = create_payload(data)
return self.__request('POST', table_name,
payload=json.dumps(payload))

Expand Down
8 changes: 7 additions & 1 deletion airtable/airtable.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ class Airtable(object):
-> _Record:
...

@typing.overload
def create(self, table_name: str, data: typing.Dict[str, typing.Any]) -> _Record:
...


@typing.overload
def create(self, table_name: str, data: typing.List[typing.Dict[str, typing.Any]]) \
-> typing.List[_Record]:
...

def update(self, table_name: str, record_id: str, data: typing.Dict[str, typing.Any]) -> _Record:
...

Expand Down
36 changes: 36 additions & 0 deletions airtable/airtable_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import airtable
import json
import mock
import requests
import unittest
Expand Down Expand Up @@ -152,5 +153,40 @@ def test_invalid_delete(self):
with self.assertRaises(airtable.IsNotString):
self.airtable.delete(FAKE_TABLE_NAME, 123)

@mock.patch.object(requests, 'request')
def test_create(self, mock_request):
record = {
'field1': 'value1',
'field2': 'value2',
}
self.airtable.create(FAKE_TABLE_NAME, record)
mock_request.assert_called_once()
unused_args, kwargs = mock_request.call_args
sent_data = json.loads(kwargs['data'])
self.assertEqual(
{'fields': {'field1': 'value1', 'field2': 'value2'}},
sent_data)

@mock.patch.object(requests, 'request')
def test_batch_create(self, mock_request):
records = [
{
'field1': 'value1',
'field2': 'value2',
},
{
'field1': 'value3',
'field2': 'value4',
},
]
self.airtable.create(FAKE_TABLE_NAME, records)
mock_request.assert_called_once()
unused_args, kwargs = mock_request.call_args
sent_data = json.loads(kwargs['data'])
self.assertEqual([
{'fields': {'field1': 'value1', 'field2': 'value2'}},
{'fields': {'field1': 'value3', 'field2': 'value4'}},
], sent_data)

if __name__ == '__main__':
unittest.main()