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_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'
            }})])
Exemplo n.º 3
0
    def test_edit_upserted_object(self, _set_default_mock,
                                  copy_properties_mock, edit_object_mock,
                                  get_operation_mock):
        model_operations = mock.MagicMock()
        existing_object = mock.MagicMock()
        params = {'path_params': {}, 'data': {}}

        result = self._resource._edit_upserted_object(model_operations,
                                                      existing_object, params)

        assert result == edit_object_mock.return_value

        _set_default_mock.assert_has_calls([
            mock.call(params, 'path_params', {}),
            mock.call(params, 'data', {})
        ])
        get_operation_mock.assert_called_once_with(
            self._resource._operation_checker.is_edit_operation,
            model_operations)
        copy_properties_mock.assert_called_once_with(existing_object,
                                                     params['data'])
        edit_object_mock.assert_called_once_with(
            get_operation_mock.return_value, params)
    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
            }})])
    def test_get_objects_by_filter_with_multiple_filters(
            self, send_request_mock, fetch_system_info_mock, connection_mock):
        objects = [{
            'name': 'obj1',
            'type': 1,
            'foo': {
                'bar': 'buzz'
            }
        }, {
            'name': 'obj2',
            'type': 1,
            'foo': {
                'bar': 'buz'
            }
        }, {
            'name': 'obj3',
            'type': 2,
            'foo': {
                'bar': 'buzz'
            }
        }]

        fetch_system_info_mock.return_value = {
            'databaseInfo': {
                'buildVersion': '6.3.0'
            }
        }

        connection_mock.get_operation_spec.return_value = {
            'method': HTTPMethod.GET,
            'url': '/object/'
        }
        resource = BaseConfigurationResource(connection_mock, False)

        send_request_mock.side_effect = [{'items': objects}, {'items': []}]
        # resource.get_objects_by_filter returns generator so to be able compare generated list with expected list
        # we need evaluate it.
        assert objects == list(resource.get_objects_by_filter('test', {}))
        send_request_mock.assert_has_calls(
            [mock.call('/object/', 'get', {}, {}, {
                'limit': 10,
                'offset': 0
            })])

        send_request_mock.reset_mock()
        send_request_mock.side_effect = [{'items': objects}, {'items': []}]
        # resource.get_objects_by_filter returns generator so to be able compare generated list with expected list
        # we need evaluate it.
        assert [objects[0]] == list(
            resource.get_objects_by_filter(
                'test', {ParamName.FILTERS: {
                    'name': 'obj1'
                }}))
        send_request_mock.assert_has_calls([
            mock.call('/object/', 'get', {}, {}, {
                QueryParams.FILTER: 'name:obj1',
                'limit': 10,
                'offset': 0
            })
        ])

        send_request_mock.reset_mock()
        send_request_mock.side_effect = [{'items': objects}, {'items': []}]
        # resource.get_objects_by_filter returns generator so to be able compare generated list with expected list
        # we need evaluate it.
        assert [objects[1]] == list(
            resource.get_objects_by_filter(
                'test', {
                    ParamName.FILTERS: {
                        'name': 'obj2',
                        'type': 1,
                        'foo': {
                            'bar': 'buz'
                        }
                    }
                }))

        send_request_mock.assert_has_calls([
            mock.call('/object/', 'get', {}, {}, {
                QueryParams.FILTER: 'name:obj2',
                'limit': 10,
                'offset': 0
            })
        ])
    def test_get_objects_by_filter_with_multiple_responses(
            self, send_request_mock, fetch_system_info_mock, connection_mock):
        send_request_mock.side_effect = [{
            'items': [{
                'name': 'obj1',
                'type': 'foo'
            }, {
                'name': 'obj2',
                'type': 'bar'
            }]
        }, {
            'items': [{
                'name': 'obj3',
                'type': 'foo'
            }]
        }, {
            'items': []
        }]
        fetch_system_info_mock.return_value = {
            'databaseInfo': {
                'buildVersion': '6.3.0'
            }
        }
        connection_mock.get_operation_spec.return_value = {
            'method': HTTPMethod.GET,
            'url': '/object/'
        }
        resource = BaseConfigurationResource(connection_mock, False)
        assert [{
            'name': 'obj1',
            'type': 'foo'
        }] == list(
            resource.get_objects_by_filter(
                'test', {ParamName.FILTERS: {
                    'type': 'foo'
                }}))
        send_request_mock.assert_has_calls(
            [mock.call('/object/', 'get', {}, {}, {
                'limit': 10,
                'offset': 0
            })])

        send_request_mock.reset_mock()
        send_request_mock.side_effect = [{
            'items': [{
                'name': 'obj1',
                'type': 'foo'
            }, {
                'name': 'obj2',
                'type': 'bar'
            }]
        }, {
            'items': [{
                'name': 'obj3',
                'type': 'foo'
            }]
        }, {
            'items': []
        }]
        resp = list(
            resource.get_objects_by_filter(
                'test', {
                    ParamName.FILTERS: {
                        'type': 'foo'
                    },
                    ParamName.QUERY_PARAMS: {
                        'limit': 2
                    }
                }))
        assert [{
            'name': 'obj1',
            'type': 'foo'
        }, {
            'name': 'obj3',
            'type': 'foo'
        }] == resp
        send_request_mock.assert_has_calls([
            mock.call('/object/', 'get', {}, {}, {
                'limit': 2,
                'offset': 0
            }),
            mock.call('/object/', 'get', {}, {}, {
                'limit': 2,
                'offset': 2
            })
        ])