def setUp(self): self.service = Mock() self.converter = Mock() self.extra_parameters = ['extra'] self.resource = CRUDResource(self.service, self.converter, self.extra_parameters)
class TestCRUDResource(unittest.TestCase): def setUp(self): self.service = Mock() self.converter = Mock() self.extra_parameters = ['extra'] self.resource = CRUDResource(self.service, self.converter, self.extra_parameters) @patch('xivo_confd.helpers.resource.extract_search_parameters') def test_when_search_requested_then_search_service_called( self, mock_extract, request, __): expected_parameters = mock_extract.return_value self.resource.search() mock_extract.assert_called_once_with(request.args, self.extra_parameters) self.service.search.assert_called_once_with(expected_parameters) def test_when_search_requested_then_search_result_converted( self, request, __): request.args = {} search_result = self.service.search.return_value expected_response = self.converter.encode_list.return_value response = self.resource.search() self.converter.encode_list.assert_called_once_with( search_result.items, search_result.total) assert_that( response, equal_to((expected_response, 200, { 'Content-Type': 'application/json' }))) def test_when_get_requested_then_service_called(self, _, __): self.resource.get(sentinel.resource_id) self.service.get.assert_called_once_with(sentinel.resource_id) def test_when_get_requested_then_resource_encoded(self, request, url_for): expected_resource = self.service.get.return_value expected_response = self.converter.encode.return_value expected_location = url_for.return_value response = self.resource.get(sentinel.resource_id) self.converter.encode.assert_called_once_with(expected_resource) url_for.assert_called_once_with('.get', resource_id=expected_resource.id) assert_that( response, equal_to((expected_response, 200, { 'Location': expected_location, 'Content-Type': 'application/json' }))) def test_when_create_requested_then_request_decoded(self, request, __): self.resource.create() self.converter.decode.assert_called_once_with(request) def test_when_create_requested_then_service_called(self, _, __): expected_resource = self.converter.decode.return_value self.resource.create() self.service.create.assert_called_once_with(expected_resource) def test_when_create_requested_then_resource_encoded( self, request, url_for): expected_resource = self.service.create.return_value expected_response = self.converter.encode.return_value expected_location = url_for.return_value response = self.resource.create() self.converter.encode.assert_called_once_with(expected_resource) url_for.assert_called_once_with('.get', resource_id=expected_resource.id) assert_that( response, equal_to((expected_response, 201, { 'Location': expected_location, 'Content-Type': 'application/json' }))) def test_when_edit_requested_then_resource_acquired_through_service( self, _, __): self.resource.edit(sentinel.resource_id) self.service.get.assert_called_once_with(sentinel.resource_id) def test_when_edit_requested_then_resource_updated(self, request, __): expected_resource = self.service.get.return_value self.resource.edit(sentinel.resource_id) self.converter.update.assert_called_once_with(request, expected_resource) def test_when_edit_requested_then_service_edits_resource(self, _, __): expected_resource = self.service.get.return_value self.resource.edit(sentinel.resource_id) self.service.edit.assert_called_once_with(expected_resource) def test_when_edit_requested_then_returns_empty_response(self, _, __): response = self.resource.edit(sentinel.resource_id) assert_that(response, equal_to(('', 204))) def test_when_delete_requested_then_resource_acquired_through_service( self, _, __): self.resource.delete(sentinel.resource_id) self.service.get.assert_called_once_with(sentinel.resource_id) def test_when_delete_requested_then_service_deletes_resource(self, _, __): expected_resource = self.service.get.return_value self.resource.delete(sentinel.resource_id) self.service.delete.assert_called_once_with(expected_resource) def test_when_delete_requested_then_returns_empty_response(self, _, __): response = self.resource.edit(sentinel.resource_id) assert_that(response, equal_to(('', 204)))
class TestCRUDResource(unittest.TestCase): def setUp(self): self.service = Mock() self.converter = Mock() self.extra_parameters = ['extra'] self.resource = CRUDResource(self.service, self.converter, self.extra_parameters) @patch('xivo_confd.helpers.resource.extract_search_parameters') def test_when_search_requested_then_search_service_called(self, mock_extract, request, _): expected_parameters = mock_extract.return_value self.resource.search() mock_extract.assert_called_once_with(request.args, self.extra_parameters) self.service.search.assert_called_once_with(expected_parameters) def test_when_search_requested_then_search_result_converted(self, request, _): request.args = {} search_result = self.service.search.return_value expected_response = self.converter.encode_list.return_value response = self.resource.search() self.converter.encode_list.assert_called_once_with(search_result.items, search_result.total) assert_that(response, equal_to((expected_response, 200, {'Content-Type': 'application/json'}))) def test_when_get_requested_then_service_called(self, request, _): self.resource.get(sentinel.resource_id) self.service.get.assert_called_once_with(sentinel.resource_id) def test_when_get_requested_then_resource_encoded(self, request, url_for): expected_resource = self.service.get.return_value expected_response = self.converter.encode.return_value expected_location = url_for.return_value response = self.resource.get(sentinel.resource_id) self.converter.encode.assert_called_once_with(expected_resource) url_for.assert_called_once_with('.get', resource_id=expected_resource.id) assert_that(response, equal_to((expected_response, 200, {'Location': expected_location, 'Content-Type': 'application/json'}))) def test_when_create_requested_then_request_decoded(self, request, _): self.resource.create() self.converter.decode.assert_called_once_with(request) def test_when_create_requested_then_service_called(self, request, _): expected_resource = self.converter.decode.return_value self.resource.create() self.service.create.assert_called_once_with(expected_resource) def test_when_create_requested_then_resource_encoded(self, request, url_for): expected_resource = self.service.create.return_value expected_response = self.converter.encode.return_value expected_location = url_for.return_value response = self.resource.create() self.converter.encode.assert_called_once_with(expected_resource) url_for.assert_called_once_with('.get', resource_id=expected_resource.id) assert_that(response, equal_to((expected_response, 201, {'Location': expected_location, 'Content-Type': 'application/json'}))) def test_when_edit_requested_then_resource_acquired_through_service(self, request, _): self.resource.edit(sentinel.resource_id) self.service.get.assert_called_once_with(sentinel.resource_id) def test_when_edit_requested_then_resource_updated(self, request, _): expected_resource = self.service.get.return_value self.resource.edit(sentinel.resource_id) self.converter.update.assert_called_once_with(request, expected_resource) def test_when_edit_requested_then_service_edits_resource(self, request, _): expected_resource = self.service.get.return_value self.resource.edit(sentinel.resource_id) self.service.edit.assert_called_once_with(expected_resource) def test_when_edit_requested_then_returns_empty_response(self, request, _): response = self.resource.edit(sentinel.resource_id) assert_that(response, equal_to(('', 204))) def test_when_delete_requested_then_resource_acquired_through_service(self, request, _): self.resource.delete(sentinel.resource_id) self.service.get.assert_called_once_with(sentinel.resource_id) def test_when_delete_requested_then_service_deletes_resource(self, request, _): expected_resource = self.service.get.return_value self.resource.delete(sentinel.resource_id) self.service.delete.assert_called_once_with(expected_resource) def test_when_delete_requested_then_returns_empty_response(self, request, _): response = self.resource.edit(sentinel.resource_id) assert_that(response, equal_to(('', 204)))