def test_create_event(AnnotationEvent, logic): request = mock.Mock() annotation = logic.create_annotation.return_value event = AnnotationEvent.return_value views.create(request) AnnotationEvent.assert_called_once_with == (request, annotation, 'create') request.registry.notify.assert_called_once_with(event)
def test_create_calls_validator(schemas): request = mock.Mock() schema = schemas.CreateAnnotationSchema.return_value views.create(request) schema.validate.assert_called_once_with(request.json_body)
def test_create_passes_json_to_create_annotation(logic): """It should pass the JSON from the request to create_annotation().""" request = mock.Mock() views.create(request) assert logic.create_annotation.call_args[1]['fields'] == request.json_body
def test_create_calls_create_annotation_once(logic): """It should call logic.create_annotation() exactly once.""" request = mock.Mock() views.create(request) assert logic.create_annotation.call_count == 1
def test_create_calls_validator(validators): request = mock.Mock() views.create(request) validators.Annotation.return_value.validate.assert_called_once_with( request.json_body)
def test_it_raises_if_create_annotation_raises(self, mock_request, storage): storage.create_annotation.side_effect = ValidationError('asplode') with pytest.raises(ValidationError) as exc: views.create(mock_request) assert exc.value.message == 'asplode'
def test_it_creates_the_annotation_in_storage(self, mock_request, storage, schemas): schema = schemas.CreateAnnotationSchema.return_value views.create(mock_request) storage.create_annotation.assert_called_once_with( mock_request, schema.validate.return_value)
def test_it_raises_if_validate_raises(self, mock_request, schemas): schemas.CreateAnnotationSchema.return_value.validate.side_effect = ( ValidationError('asplode')) with pytest.raises(ValidationError) as exc: views.create(mock_request) assert exc.value.message == 'asplode'
def test_it_calls_create_annotation(self, storage, schemas): request = self.mock_request() schema = schemas.CreateAnnotationSchema.return_value views.create(request) storage.create_annotation.assert_called_once_with( request, schema.validate.return_value)
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.create(mock_request)
def test_it_inits_AnnotationJSONPresenter(self, AnnotationJSONPresenter, storage): request = self.mock_request() views.create(request) AnnotationJSONPresenter.assert_called_once_with( request, storage.create_annotation.return_value)
def test_it_calls_validator(self, schemas, copy): request = self.mock_request() copy.deepcopy.side_effect = lambda x: x schema = schemas.LegacyCreateAnnotationSchema.return_value views.create(request) schema.validate.assert_called_once_with(request.json_body)
def test_create_calls_logic(logic, get_user): """It should call logic.create_annotation() appropriately.""" request = mock.Mock() views.create(request) logic.create_annotation.assert_called_once_with( fields=request.json_body, user=get_user.return_value)
def test_it_inits_AnnotationJSONPresenter(self, AnnotationJSONPresenter, mock_request, storage): views.create(mock_request) AnnotationJSONPresenter.assert_called_once_with( mock_request, storage.create_annotation.return_value)
def test_create_calls_create_annotation(logic, schemas): """It should call logic.create_annotation() appropriately.""" request = mock.Mock() schemas.AnnotationSchema.return_value.validate.return_value = {'foo': 123} views.create(request) logic.create_annotation.assert_called_once_with( {'foo': 123}, userid=request.authenticated_userid)
def test_create_calls_create_annotation(logic, schemas): """It should call logic.create_annotation() appropriately.""" request = mock.Mock() schema = schemas.CreateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.create(request) logic.create_annotation.assert_called_once_with({'foo': 123})
def test_it_calls_legacy_create_annotation(self, storage, schemas): request = self.mock_request() schema = schemas.LegacyCreateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.create(request) storage.legacy_create_annotation.assert_called_once_with(request, {'foo': 123})
def test_create_calls_create_annotation(logic): """It should call logic.create_annotation() appropriately.""" request = mock.Mock() views.create(request) logic.create_annotation.assert_called_once_with( request.json_body, userid=request.authenticated_userid)
def test_it_inits_AnnotationJSONPresenter(self, AnnotationJSONPresenter, storage): request = self.mock_request() views.create(request) AnnotationJSONPresenter.assert_called_once_with( request, storage.legacy_create_annotation.return_value)
def test_create_calls_create_annotation(storage, schemas): """It should call storage.create_annotation() appropriately.""" request = mock.Mock() schema = schemas.CreateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.create(request) storage.create_annotation.assert_called_once_with(request, {'foo': 123})
def test_it_calls_legacy_create_annotation(self, storage, schemas): request = self.mock_request() schema = schemas.LegacyCreateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.create(request) storage.legacy_create_annotation.assert_called_once_with( request, {'foo': 123})
def test_it_calls_legacy_create_annotation(self, storage, schemas): """It should call storage.create_annotation() appropriately.""" request = self.mock_request() schema = schemas.LegacyCreateAnnotationSchema.return_value views.create(request) storage.legacy_create_annotation.assert_called_once_with( request, schema.validate.return_value)
def test_it_publishes_annotation_event(self, AnnotationEvent, storage): """It publishes an annotation "create" event for the annotation.""" request = self.mock_request() views.create(request) AnnotationEvent.assert_called_once_with( request, storage.create_annotation.return_value, 'create') request.registry.notify.assert_called_once_with( AnnotationEvent.return_value)
def test_it_reuses_the_postgres_annotation_id_in_elasticsearch( self, schemas, storage): request = self.mock_request() schema = schemas.LegacyCreateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.create(request) assert storage.legacy_create_annotation.call_args[0][1]['id'] == ( storage.create_annotation.return_value.id)
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.create(pyramid_request)
def test_it_calls_legacy_schema_validate(self, copy, schemas): """It should call validate() with a deep copy of json_body.""" copy.deepcopy.side_effect = [mock.sentinel.first_copy, mock.sentinel.second_copy] request = self.mock_request() views.create(request) assert copy.deepcopy.call_args_list[0] == mock.call(request.json_body) schemas.LegacyCreateAnnotationSchema.return_value.validate\ .assert_called_once_with(mock.sentinel.second_copy)
def test_it_reuses_the_postgres_annotation_id_in_elasticsearch(self, schemas, storage): request = self.mock_request() schema = schemas.LegacyCreateAnnotationSchema.return_value schema.validate.return_value = {'foo': 123} views.create(request) assert storage.legacy_create_annotation.call_args[0][1]['id'] == ( storage.create_annotation.return_value.id)
def test_it_calls_legacy_schema_validate(self, copy, schemas): """It should call validate() with a deep copy of json_body.""" copy.deepcopy.side_effect = [ mock.sentinel.first_copy, mock.sentinel.second_copy ] request = self.mock_request() views.create(request) assert copy.deepcopy.call_args_list[0] == mock.call(request.json_body) schemas.LegacyCreateAnnotationSchema.return_value.validate\ .assert_called_once_with(mock.sentinel.second_copy)
def test_it_publishes_annotation_event(self, AnnotationEvent, mock_request, storage): """It publishes an annotation "create" event for the annotation.""" views.create(mock_request) annotation = storage.create_annotation.return_value AnnotationEvent.assert_called_once_with(mock_request, annotation.id, 'create', annotation_dict=None) mock_request.notify_after_commit.assert_called_once_with( AnnotationEvent.return_value)
def test_it_publishes_annotation_event(self, AnnotationEvent, mock_request, storage): """It publishes an annotation "create" event for the annotation.""" views.create(mock_request) annotation = storage.create_annotation.return_value AnnotationEvent.assert_called_once_with( mock_request, annotation.id, 'create', annotation_dict=None) mock_request.notify_after_commit.assert_called_once_with( AnnotationEvent.return_value)
def test_it_returns_presented_annotation(self, AnnotationJSONPresenter, mock_request): result = views.create(mock_request) AnnotationJSONPresenter.return_value.asdict.assert_called_once_with() assert result == ( AnnotationJSONPresenter.return_value.asdict.return_value)
def test_create_returns_error_if_parsing_json_fails(): """It should return an error if JSON 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) error = views.create(request) assert error['status'] == 'failure'
def test_create_returns_presented_annotation(AnnotationJSONPresenter, storage): request = mock.Mock() presenter = mock.Mock() AnnotationJSONPresenter.return_value = presenter result = views.create(request) AnnotationJSONPresenter.assert_called_once_with( storage.create_annotation.return_value) assert result == presenter.asdict()
def test_it_returns_presented_annotation(self, AnnotationJSONPresenter, storage): request = self.mock_request() result = views.create(request) AnnotationJSONPresenter.assert_called_once_with( request, storage.legacy_create_annotation.return_value) assert result == ( AnnotationJSONPresenter.return_value.asdict.return_value)
def test_create_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.create(mock.Mock()) assert response['status'] == 'failure' assert response['reason'] == mock.sentinel.reason
def test_create_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.create(mock.Mock()) assert response['status'] == 'failure' assert response['reason'] == mock.sentinel.reason
def test_it_publishes_annotation_event(self, AnnotationEvent, AnnotationJSONPresenter, storage): """It publishes an annotation "create" event for the annotation.""" request = self.mock_request() views.create(request) annotation = storage.create_annotation.return_value AnnotationJSONPresenter.assert_called_once_with(request, annotation) presented = AnnotationJSONPresenter.return_value.asdict() AnnotationEvent.assert_called_once_with( request, presented, 'create') request.notify_after_commit.assert_called_once_with( AnnotationEvent.return_value)
def test_it_validates_the_posted_data(self, mock_request, schemas): """It should call validate() with a request.json_body.""" views.create(mock_request) schemas.CreateAnnotationSchema.return_value.validate\ .assert_called_once_with(mock_request.json_body)
def test_create_inits_AnnotationEvent_once(AnnotationEvent): views.create(mock.Mock()) assert AnnotationEvent.call_count == 1
def test_it_inits_CreateAnnotationSchema(self, mock_request, schemas): views.create(mock_request) schemas.CreateAnnotationSchema.assert_called_once_with(mock_request)
def test_it_inits_LegacyCreateAnnotationSchema(self, schemas): request = self.mock_request() views.create(request) schemas.LegacyCreateAnnotationSchema.assert_called_once_with(request)
def test_create_calls_logic(logic, get_user): """It should call logic.create_annotation() appropriately.""" request = mock.Mock() user = get_user.return_value views.create(request) logic.create_annotation.assert_called_once_with(request.json_body, user)
def test_create_returns_render(search_lib): """It should return what render() returns.""" assert views.create(mock.Mock()) == search_lib.render.return_value
def test_create_passes_annotation_to_render(logic, search_lib): views.create(mock.Mock()) search_lib.render.assert_called_once_with( logic.create_annotation.return_value)