Skip to content

Commit

Permalink
Add a test for iterate and fix it. (#40)
Browse files Browse the repository at this point in the history
pcorpet authored Jan 21, 2021
1 parent e66bfb5 commit 4b9134a
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions airtable/airtable.py
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ def check_integer(n):
if not n:
return False
elif not isinstance(n, six.integer_types):
raise IsNotInteger('Expected an integer')
raise IsNotInteger('Expected an integer', n)
else:
return True

@@ -29,7 +29,7 @@ def check_string(s):
if not s:
return False
elif not isinstance(s, six.string_types):
raise IsNotString('Expected a string')
raise IsNotString('Expected a string', s)
else:
return True

@@ -144,7 +144,7 @@ def iterate(
for record in response.pop('records'):
yield record
if 'offset' in response:
offset = response['offset'].encode('ascii', 'ignore')
offset = response['offset']
else:
break

50 changes: 50 additions & 0 deletions test_airtable.py
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@ def get(self, *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')
@@ -192,6 +195,47 @@ 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):

@@ -205,6 +249,9 @@ def get(self, *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):

@@ -218,6 +265,9 @@ def get(self, *args, **kwargs):
def delete(self, *args, **kwargs):
return self.table.delete(*args, **kwargs)

def iterate(self, *args, **kwargs):
return self.table.iterate(*args, **kwargs)


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

0 comments on commit 4b9134a

Please sign in to comment.