class Extension: def __init__(self,name,options, paginator = None, send_items_func = _print): _print('init Extension ' + name) self.name = name self.sendItems = send_items_func if paginator is None: _print('about to load paginator for extension ' + name) self.paginator = Paginator(options = options,extension_name = name) else: self.paginator = paginator def doWork(self): while self.paginator.next(): page_items = self.paginator.fetchPageItems() if page_items is not None: if len(page_items) > 0: self.sendItems(page_items) _print('Extension: ' + self.name + ' returned ' + str(len(page_items)) + ' new items.') else: _print('Extension: ' + self.name + ' finished pagination.') else: _print('Extension: ' + self.name + ' finished pagination.') break self.paginator.reset()
def test_auth_expires(api_mock): api_mock.fetchItems.return_value.status_code = 401 paginator = Paginator(options={ **options, **options_api }, api_adaptor=api_mock) items = paginator.fetchPageItems() assert items == None
def __init__(self,name,options, paginator = None, send_items_func = _print): _print('init Extension ' + name) self.name = name self.sendItems = send_items_func if paginator is None: _print('about to load paginator for extension ' + name) self.paginator = Paginator(options = options,extension_name = name) else: self.paginator = paginator
def test_negative_max_pages(): paginator = Paginator(options={ **options, **options_negative_max_page }, api_adaptor=api_mock(two_pages), state=State(last_item_id='5')) is_available = paginator.next() assert is_available == False
def test_bad_fetch_response(api_mock): api_mock.fetchItems.return_value.status_code = 500 paginator = Paginator(options={ **options, **options_api }, api_adaptor=api_mock) items = paginator.fetchPageItems() assert items == None
def test_mallformed_response(api_mock): api_mock.fetchItems.return_value.status_code = 200 api_mock.fetchItems.return_value.json.return_value = {'invalid': 'boom!'} paginator = Paginator(options={ **options, **options_api }, api_adaptor=api_mock) items = paginator.fetchPageItems() assert items == None
def test_single_page(): paginator = Paginator(options=options, api_adaptor=api_mock(single_page)) paginator.next() assert unwrap(paginator.fetchPageItems()) == first_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None
def test_no_new_items(): paginator = Paginator(options=options, api_adaptor=api_mock([first_page]), state=State(last_item_id='1')) paginator.next() assert unwrap(paginator.fetchPageItems()) == None paginator.next() assert unwrap(paginator.fetchPageItems()) == None
def test_one_item(): paginator = Paginator(options=options, api_adaptor=api_mock([first_page]), state=State(last_item_id='2')) paginator.next() assert unwrap(paginator.fetchPageItems()) == [{'id': '1'}] paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None
def test_max_pages(): paginator = Paginator(options={ **options, **options_two_page }, api_adaptor=api_mock(three_pages), state=State(last_item_id='100')) paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == first_page is_available = paginator.next() assert is_available == True items = unwrap(paginator.fetchPageItems()) assert items == second_page is_available = paginator.next() assert is_available == False
def test_empty_page(): paginator = Paginator(options=options, api_adaptor=api_mock()) paginator.next() assert unwrap(paginator.fetchPageItems()) == None
def test_state_based_pagination(): state = State() assert state.last_item_id == '' mocker = api_mock([first_page]) paginator = Paginator(options=options, api_adaptor=mocker, state=state) paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == first_page mocker.setPages([second_page, first_page]) paginator.reset() paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == second_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None mocker.setPages([fourth_page, third_page, second_page, first_page]) paginator.reset() paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == fourth_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == third_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None mocker.setPages([fourth_page]) paginator.reset() paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None
def test_4_items(): paginator = Paginator(options=options, api_adaptor=api_mock(two_pages), state=State(last_item_id='5')) paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == first_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == [{'id': '4'}] paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None
def test_two_pages(): paginator = Paginator(options=options, api_adaptor=api_mock(two_pages)) paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == first_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == second_page paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None
def test_no_max_pages(): with pytest.raises(KeyError): paginator = Paginator(options={})
def test_no_api_adaptor(): paginator = Paginator(options={**options, **options_api}, api_adaptor=None) assert paginator.api_adaptor is not None
def test_pagination_after_service_restart(): mocker = api_mock([second_page, first_page]) paginator = Paginator(options=options, api_adaptor=mocker) paginator.next() #fetch second page items = unwrap(paginator.fetchPageItems()) #fetch first page items = unwrap(paginator.fetchPageItems()) #persist paginator.reset() #restart opertion => new paginator mocker = api_mock([third_page + second_page, first_page]) paginator = Paginator(options=options, api_adaptor=mocker) paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == third_page #only new items from page three should appear paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None paginator.next() items = unwrap(paginator.fetchPageItems()) assert items == None