def test_get_data_for_page(self): cofecms_result = CofeCMSResult() cofecms_result.endpoint_url = 'http://example.com/some_end_point' cofecms_result.diocese_id = 123 cofecms_result.search_params = {'keyword': 'smith'} cofecms_result.offset = 10 cofecms_result.limit = 5 cofecms_result.basic_params = {'fields': ['forenames', 'surname']} mock_api_obj = mock.Mock(spec=CofeCMS) cofecms_result.api_obj = mock_api_obj mock_result = mock.Mock(spec=CofeCMS) mock_api_obj.paged_get.return_value = mock_result result = cofecms_result.get_data_for_page(3) self.assertEqual(result, mock_result) mock_api_obj.paged_get.assert_called_once_with( endpoint_url='http://example.com/some_end_point', diocese_id=123, search_params={'keyword': 'smith'}, offset=15, limit=5, fields=['forenames', 'surname'], )
def test_paged_get__with_offset(self): endpoint_url = 'https://cmsapi.cofeportal.org/v2/contacts' get_return = CofeCMSResult([{'contact': ['id', 'surname']}]) get_return.headers = { 'X-Total-Count': '678', 'X-Rate-Limit': '60', 'X-Rate-Limit-Remaining': '59', } get_return.basic_params = {} self.cofecms.get = mock.Mock(spec=self.cofecms.get, return_value=get_return) result = self.cofecms.paged_get( endpoint_url=endpoint_url, diocese_id=123, offset=100, limit=500, ) self.assertEqual(result, get_return) self.assertEqual(result.total_count, 678) self.assertEqual(result.offset, 100) self.assertEqual(result.limit, 500) self.cofecms.get.assert_called_once_with( endpoint_url=endpoint_url, diocese_id=123, search_params=None, offset=100, limit=500, )
def test_works_as_list(self): cofecms_result = CofeCMSResult() cofecms_result.append('wibble') cofecms_result.append('wobble') self.assertEqual(cofecms_result, ['wibble', 'wobble']) self.assertIn('wibble', cofecms_result) self.assertIn('wobble', cofecms_result) pop_result = cofecms_result.pop() self.assertEqual(pop_result, 'wobble') self.assertEqual(cofecms_result, ['wibble'])
def test_all(self): cofecms_result = CofeCMSResult() cofecms_result.pages_generator = mock.Mock( spec=cofecms_result.pages_generator, return_value=[[{ 'a': 'aa' }], [{ 'b': 'bb' }]]) result = cofecms_result.all() self.assertEqual(result, [{'a': 'aa'}, {'b': 'bb'}])
def test_pages_generator(self): with mock.patch( 'cofecms.api.CofeCMSResult.total_pages', new_callable=mock.PropertyMock, ) as mock_total_pages: mock_total_pages.return_value = 2 cofecms_result = CofeCMSResult([{'a': 'aa'}]) cofecms_result.get_data_for_page = mock.Mock( spec=cofecms_result.get_data_for_page, return_value=[{ 'b': 'bb' }], ) results = [] for result in cofecms_result.pages_generator(): results.append(result) self.assertEqual(results, [[{'a': 'aa'}], [{'b': 'bb'}]]) cofecms_result.get_data_for_page.assert_called_once_with(1)
def test_total_pages(self): cofecms_result = CofeCMSResult() cofecms_result.total_count = 1 cofecms_result.limit = 100 self.assertEqual(cofecms_result.total_pages, 1) cofecms_result.total_count = 9 cofecms_result.limit = 7 self.assertEqual(cofecms_result.total_pages, 2)
def test_getattr(self): cofecms_result = CofeCMSResult() cofecms_result.some_attr = 'wibble' self.assertEqual(cofecms_result.some_attr, 'wibble')
def test_init(self): cofecms_result = CofeCMSResult([1, 2, 3]) self.assertEqual(cofecms_result, [1, 2, 3])