Skip to content

Commit

Permalink
Merge pull request #23 from usabilla/bugfix/item_iterator-reset-query…
Browse files Browse the repository at this point in the history
…-parameters-after-returning-all-items

APIClient::item_iterator: reset query_parameters after returning all items
  • Loading branch information
ricardofontanelli authored Oct 26, 2022
2 parents 5a90bae + d8ff5eb commit 0a66560
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Version 2.0.2
-------------

- Reset query arguments after returning all items from generator

Here you find a full list of changes.

Version 2.0.1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
except ImportError:
from distutils.core import setup

VERSION = '2.0.1'
VERSION = '2.0.2'

setup(
name='usabilla-api',
Expand Down
25 changes: 24 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from unittest.mock import call
import requests
import usabilla as ub

Expand Down Expand Up @@ -124,18 +125,40 @@ def test_item_iterator(self):
items = ['one', 'two', 'three', 'four']
has_more = {'hasMore': True, 'items': items[:2], 'lastTimestamp': 1400000000001}
no_more = {'hasMore': False, 'items': items[2:], 'lastTimestamp': 1400000000002}
expected_set_query_parameters_calls = [ call({'since': 1400000000001}), call({'since': 1400000000002}), call({}) ]

self.client.set_query_parameters = Mock()
self.client.send_signed_request = Mock(side_effect=[has_more, no_more])

index = 0
for item in self.client.item_iterator('/some/url'):
self.assertEqual(item, items[index])
index += 1
self.client.set_query_parameters.assert_called_with({'since': 1400000000002})

self.assertEqual(self.client.set_query_parameters.call_args_list, expected_set_query_parameters_calls)
self.assertEqual(self.client.send_signed_request.call_count, 2)

self.client.send_signed_request.side_effect = requests.exceptions.HTTPError('mocked error')
with self.assertRaises(requests.exceptions.HTTPError):
list(self.client.item_iterator('/some/url'))

def test_item_iterator_resets_query_parameters_after_returning_all_items(self):
first_response = {'hasMore': True, 'items': [1], 'lastTimestamp': 1400000000001}
second_response = {'hasMore': False, 'items': [2], 'lastTimestamp': 1400000000002}
third_response = {'hasMore': False, 'items': [3], 'lastTimestamp': 1400000000003}

self.client.send_signed_request = Mock(side_effect=[first_response, second_response, third_response])

expected_query_parameters = ['', 'since=1400000000001', '']

index = 0
for response in [self.client.item_iterator('/some/url'), self.client.item_iterator('/some/url')]:
for _ in response:
self.assertEqual(expected_query_parameters[index], self.client.get_query_parameters())
index+=1

self.assertEqual(self.client.send_signed_request.call_count, 3)

def test_get_resource(self):
self.client.item_iterator = Mock()
self.client.send_signed_request = Mock()
Expand Down
2 changes: 2 additions & 0 deletions usabilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ def item_iterator(self, url):
yield item
self.set_query_parameters({'since': results['lastTimestamp']})

self.set_query_parameters({})

def get_resource(self, scope, product, resource, resource_id=None, iterate=False):
"""Retrieves resources of the specified type
Expand Down

0 comments on commit 0a66560

Please sign in to comment.