From 4b9134a8584cbf84d355c4f64946f0c4ec8de016 Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Thu, 21 Jan 2021 11:20:35 +0100 Subject: [PATCH] Add a test for iterate and fix it. (#40) --- airtable/airtable.py | 6 +++--- test_airtable.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/airtable/airtable.py b/airtable/airtable.py index 8ccf211..5388429 100644 --- a/airtable/airtable.py +++ b/airtable/airtable.py @@ -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 diff --git a/test_airtable.py b/test_airtable.py index 0f59215..3ef0595 100644 --- a/test_airtable.py +++ b/test_airtable.py @@ -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()