def test_it_validates_the_posted_data(self, mock_request, schemas): annotation = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value views.update(annotation, mock_request) schema.validate.assert_called_once_with(mock_request.json_body)
def test_update_event(AnnotationEvent): request = mock.Mock() annotation = mock.Mock() event = AnnotationEvent.return_value views.update(annotation, request) AnnotationEvent.assert_called_once_with(request, annotation, 'update') request.registry.notify.assert_called_once_with(event)
def test_it_inits_the_schema(self, mock_request, schemas): annotation = mock.Mock() views.update(annotation, mock_request) schemas.UpdateAnnotationSchema.assert_called_once_with( mock_request, annotation.target_uri, annotation.groupid)
def test_update_calls_validator(schemas): request = mock.Mock() views.update(mock.Mock(), request) schemas.AnnotationSchema.return_value.validate.assert_called_once_with( request.json_body)
def test_update_passes_has_admin_permission_to_update_annotation(logic): request = mock.Mock() views.update(mock.Mock(), request) assert logic.update_annotation.call_args[0][2] == ( request.has_permission.return_value)
def test_update_calls_has_permission(): annotation = mock.Mock() request = mock.Mock() views.update(annotation, request) request.has_permission.assert_called_once_with('admin', annotation)
def test_update_calls_validator(schemas): request = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value views.update(mock.Mock(), request) schema.validate.assert_called_once_with(request.json_body)
def test_update_calls_validator(validators): request = mock.Mock() views.update(mock.Mock(), request) validators.Annotation.return_value.validate.assert_called_once_with( request.json_body)
def test_update_calls_validator(schemas): annotation = mock.Mock() request = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value views.update(annotation, request) schema.validate.assert_called_once_with(request.json_body)
def test_it_calls_validator(self, schemas): annotation = mock.Mock() request = mock.Mock() schema = schemas.LegacyUpdateAnnotationSchema.return_value views.update(annotation, request) schema.validate.assert_called_once_with(request.json_body)
def test_it_raises_if_json_parsing_fails(self, mock_request): """It raises PayloadError if parsing of the request body fails.""" # Make accessing the request.json_body property raise ValueError. type(mock_request).json_body = mock.PropertyMock( side_effect=ValueError) with pytest.raises(views.PayloadError): views.update(mock.Mock(), mock_request)
def test_it_inits_a_presenter(self, AnnotationJSONPresenter, mock_request, storage): views.update(mock.Mock(), mock_request) AnnotationJSONPresenter.assert_any_call( mock_request, storage.update_annotation.return_value)
def test_update_raises_if_json_parsing_fails(): """The view raises PayloadError if parsing of the request body fails.""" request = mock.Mock() # Make accessing the request.json_body property raise ValueError. type(request).json_body = mock.PropertyMock(side_effect=ValueError) with pytest.raises(views.PayloadError): views.update(mock.Mock(), request)
def test_update_calls_update_annotation(storage, schemas): context = mock.Mock() request = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.update(context, request) storage.update_annotation.assert_called_once_with(context.id, {'foo': 123})
def test_update_calls_update_annotation(logic, schemas): context = mock.Mock() request = mock.Mock() schemas.AnnotationSchema.return_value.validate.return_value = {'foo': 123} views.update(context, request) logic.update_annotation.assert_called_once_with( context.model, {'foo': 123}, request.authenticated_userid)
def test_it_raises_if_json_parsing_fails(self, pyramid_request): """It raises PayloadError if parsing of the request body fails.""" # Make accessing the request.json_body property raise ValueError. type(pyramid_request).json_body = {} with mock.patch.object(type(pyramid_request), 'json_body', new_callable=mock.PropertyMock) as json_body: json_body.side_effect = ValueError() with pytest.raises(views.PayloadError): views.update(mock.Mock(), pyramid_request)
def test_update_event(AnnotationEvent, storage): annotation = mock.Mock() request = mock.Mock() event = AnnotationEvent.return_value annotation_out = storage.update_annotation.return_value views.update(annotation, request) AnnotationEvent.assert_called_once_with(request, annotation_out, 'update') request.registry.notify.assert_called_once_with(event)
def test_update_calls_update_annotation(storage, schemas): annotation = mock.Mock() request = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.update(annotation, request) storage.update_annotation.assert_called_once_with(request, annotation.id, {'foo': 123})
def test_update_event(AnnotationEvent, storage): context = mock.Mock() request = mock.Mock() event = AnnotationEvent.return_value annotation = storage.update_annotation.return_value views.update(context, request) AnnotationEvent.assert_called_once_with(request, annotation, 'update') request.registry.notify.assert_called_once_with(event)
def test_update_calls_update_annotation(logic): context = mock.Mock() request = mock.Mock() views.update(context, request) logic.update_annotation.assert_called_once_with( context.model, request.json_body, request.authenticated_userid)
def test_it_updates_the_annotation_in_storage(self, mock_request, storage, schemas): annotation = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value schema.validate.return_value = mock.sentinel.validated_data views.update(annotation, mock_request) storage.update_annotation.assert_called_once_with( mock_request.db, annotation.id, mock.sentinel.validated_data)
def test_it_inits_an_AnnotationEvent(self, AnnotationEvent, storage, mock_request): annotation = mock.Mock() views.update(annotation, mock_request) AnnotationEvent.assert_called_once_with( mock_request, storage.update_annotation.return_value.id, 'update', annotation_dict=None)
def test_it_calls_update_annotation(self, storage, schemas): annotation = mock.Mock() request = mock.Mock() schema = schemas.LegacyUpdateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.update(annotation, request) storage.update_annotation.assert_called_once_with(request, annotation.id, {'foo': 123})
def test_it_calls_notify_with_an_event(self, AnnotationEvent, storage): annotation = mock.Mock() request = mock.Mock() event = AnnotationEvent.return_value annotation_out = storage.update_annotation.return_value views.update(annotation, request) AnnotationEvent.assert_called_once_with(request, annotation_out, 'update') request.registry.notify.assert_called_once_with(event)
def test_it_calls_notify_with_an_event(self, AnnotationEvent, AnnotationJSONPresenter, storage): annotation = mock.Mock() request = mock.Mock() event = AnnotationEvent.return_value annotation_out = storage.update_annotation.return_value views.update(annotation, request) AnnotationJSONPresenter.assert_called_once_with(request, annotation_out) presented = AnnotationJSONPresenter.return_value.asdict() AnnotationEvent.assert_called_once_with(request, presented, 'update') request.notify_after_commit.assert_called_once_with(event)
def test_it_updates_the_annotation_in_storage(self, mock_request, storage, schemas): annotation = mock.Mock() schema = schemas.UpdateAnnotationSchema.return_value schema.validate.return_value = mock.sentinel.validated_data views.update(annotation, mock_request) storage.update_annotation.assert_called_once_with( mock_request.db, annotation.id, mock.sentinel.validated_data )
def test_it_returns_a_presented_dict(self, AnnotationJSONPresenter, mock_request): returned = views.update(mock.Mock(), mock_request) assert returned == ( AnnotationJSONPresenter.return_value.asdict.return_value)
def test_update_returns_annotation(storage): context = mock.Mock() request = mock.Mock() result = views.update(context, request) assert result == storage.update_annotation.return_value
def test_update_returns_error_if_json_parsing_fails(): request = mock.Mock() # Make accessing the request.json_body property raise ValueError. type(request).json_body = mock.PropertyMock(side_effect=ValueError) error = views.update(mock.Mock(), request) assert error['status'] == 'failure'
def test_update_returns_presented_annotation(AnnotationJSONPresenter, storage): annotation = mock.Mock() request = mock.Mock() presenter = mock.Mock() AnnotationJSONPresenter.return_value = presenter result = views.update(annotation, request) AnnotationJSONPresenter.assert_called_once_with( storage.update_annotation.return_value) assert result == presenter.asdict()
def test_update_returns_api_error_for_validation_error(schemas): class ValidationError(Exception): pass schemas.ValidationError = ValidationError schemas.AnnotationSchema.return_value.validate.side_effect = ( schemas.ValidationError(mock.sentinel.reason)) response = views.update(mock.Mock(), mock.Mock()) assert response['status'] == 'failure' assert response['reason'] == mock.sentinel.reason
def test_update_returns_api_error_for_validation_error(validators): class Error(Exception): pass validators.Error = Error validators.Annotation.return_value.validate.side_effect = ( validators.Error(mock.sentinel.reason)) response = views.update(mock.Mock(), mock.Mock()) assert response['status'] == 'failure' assert response['reason'] == mock.sentinel.reason
def test_it_raises_if_storage_raises(self, mock_request, storage): storage.update_annotation.side_effect = ValidationError('asplode') with pytest.raises(ValidationError): views.update(mock.Mock(), mock_request)
def test_update_returns_rendered_annotation(search_lib): assert views.update(mock.Mock(), mock.Mock()) == (search_lib.render.return_value)
def test_it_fires_the_AnnotationEvent(self, AnnotationEvent, mock_request): views.update(mock.Mock(), mock_request) mock_request.notify_after_commit.assert_called_once_with( AnnotationEvent.return_value)
def test_it_dictizes_the_presenter(self, AnnotationJSONPresenter, mock_request): views.update(mock.Mock(), mock_request) AnnotationJSONPresenter.return_value.asdict.assert_called_with()
def test_update_returns_error_if_update_annotation_raises(logic): logic.update_annotation.side_effect = RuntimeError("Nope", 401) error = views.update(mock.Mock(), mock.Mock()) assert error['status'] == 'failure'
def test_update_passes_annotation_to_update_annotation(logic): annotation = mock.Mock() views.update(annotation, mock.Mock()) assert logic.update_annotation.call_args[0][0] == annotation
def test_update_passes_fields_to_update_annotation(logic): request = mock.Mock() views.update(mock.Mock(), request) assert logic.update_annotation.call_args[0][1] == request.json_body
def test_it_raises_if_validate_raises(self, mock_request, schemas): schemas.UpdateAnnotationSchema.return_value.validate\ .side_effect = ValidationError('asplode') with pytest.raises(ValidationError): views.update(mock.Mock(), mock_request)
def test_update_calls_update_annotation_once(logic): views.update(mock.Mock(), mock.Mock()) assert logic.update_annotation.call_count == 1
def test_update_calls_render(search_lib): annotation = mock.Mock() views.update(annotation, mock.Mock()) search_lib.render.assert_called_once_with(annotation.model)