def test_iterate_over_pageable_resource_with_multiple_pages(self):
        objects = [
            {
                'items': ['foo']
            },
            {
                'items': ['bar']
            },
            {
                'items': ['buzz']
            },
            {
                'items': []
            },
        ]
        resource_func = mock.Mock(side_effect=objects)

        items = iterate_over_pageable_resource(resource_func,
                                               {'query_params': {}})
        assert ['foo'] == list(items)

        resource_func.reset_mock()
        resource_func = mock.Mock(side_effect=objects)
        items = iterate_over_pageable_resource(resource_func,
                                               {'query_params': {
                                                   'limit': 1
                                               }})
        assert ['foo', 'bar', 'buzz'] == list(items)
    def test_iterate_over_pageable_resource_raises_exception_when_server_returned_more_items_than_requested(
            self):
        resource_func = mock.Mock(side_effect=[
            {
                'items': ['foo', 'redundant_bar']
            },
            {
                'items': []
            },
        ])

        with pytest.raises(FtdUnexpectedResponse):
            list(
                iterate_over_pageable_resource(
                    resource_func,
                    {'query_params': {
                        'offset': '1',
                        'limit': '1'
                    }}))

        resource_func.assert_has_calls(
            [call(params={'query_params': {
                'offset': '1',
                'limit': '1'
            }})])
    def test_iterate_over_pageable_resource_should_pass_with_string_offset_and_limit(
            self):
        resource_func = mock.Mock(side_effect=[
            {
                'items': ['foo']
            },
            {
                'items': []
            },
        ])

        items = iterate_over_pageable_resource(
            resource_func, {'query_params': {
                'offset': '1',
                'limit': '1'
            }})

        assert ['foo'] == list(items)
        resource_func.assert_has_calls([
            call(params={'query_params': {
                'offset': '1',
                'limit': '1'
            }}),
            call(params={'query_params': {
                'offset': 2,
                'limit': '1'
            }})
        ])
    def test_iterate_over_pageable_resource_with_no_items(self):
        resource_func = mock.Mock(return_value={'items': []})

        items = iterate_over_pageable_resource(resource_func,
                                               {'query_params': {}})

        assert [] == list(items)
    def test_iterate_over_pageable_resource_should_preserve_query_params(self):
        resource_func = mock.Mock(return_value={'items': []})

        items = iterate_over_pageable_resource(
            resource_func, {'query_params': {
                'filter': 'name:123'
            }})

        assert [] == list(items)
        resource_func.assert_called_once_with(params={
            'query_params': {
                'filter': 'name:123',
                'offset': 0,
                'limit': 10
            }
        })
    def test_iterate_over_pageable_resource_with_one_page(self):
        resource_func = mock.Mock(side_effect=[
            {
                'items': ['foo', 'bar']
            },
            {
                'items': []
            },
        ])

        items = iterate_over_pageable_resource(resource_func,
                                               {'query_params': {}})

        assert ['foo', 'bar'] == list(items)
        resource_func.assert_has_calls(
            [call(params={'query_params': {
                'offset': 0,
                'limit': 10
            }})])
    def test_iterate_over_pageable_resource_should_preserve_limit(self):
        resource_func = mock.Mock(side_effect=[
            {
                'items': ['foo']
            },
            {
                'items': []
            },
        ])

        items = iterate_over_pageable_resource(resource_func,
                                               {'query_params': {
                                                   'limit': 1
                                               }})

        assert ['foo'] == list(items)
        resource_func.assert_has_calls(
            [call(params={'query_params': {
                'offset': 0,
                'limit': 1
            }})])