Skip to content

Commit

Permalink
Use the new annotation create method in the API views
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Betts committed May 4, 2023
1 parent e5fc2f9 commit 2166d28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 54 deletions.
5 changes: 4 additions & 1 deletion h/views/api/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from h.schemas.util import validate_query_params
from h.security import Permission
from h.services import AnnotationWriteService
from h.views.api.config import api_config
from h.views.api.exceptions import PayloadError

Expand Down Expand Up @@ -78,7 +79,9 @@ def create(request):
schema = CreateAnnotationSchema(request)
appstruct = schema.validate(_json_payload(request))

annotation = storage.create_annotation(request, appstruct)
annotation = request.find_service(AnnotationWriteService).create_annotation(
data=appstruct
)

_publish_annotation_event(request, annotation, "create")

Expand Down
90 changes: 37 additions & 53 deletions tests/h/views/api/annotations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from h.services.annotation_delete import AnnotationDeleteService
from h.traversal import AnnotationContext
from h.views.api import annotations as views
from h.views.api.exceptions import PayloadError


@pytest.mark.usefixtures("annotation_json_service", "search_lib")
Expand Down Expand Up @@ -85,79 +86,52 @@ def search_run(self, search_lib):
return search_lib.Search.return_value.run


@pytest.mark.usefixtures(
"AnnotationEvent",
"create_schema",
"links_service",
"annotation_json_service",
"storage",
)
class TestCreate:
def test_it(self, pyramid_request, create_schema, storage, annotation_json_service):
def test_it(
self,
pyramid_request,
CreateAnnotationSchema,
annotation_write_service,
annotation_json_service,
AnnotationEvent,
):
result = views.create(pyramid_request)

create_schema.assert_called_once_with(pyramid_request)
create_schema.return_value.validate.assert_called_once_with(
pyramid_request.json_body
)
storage.create_annotation.assert_called_once_with(
pyramid_request, create_schema.return_value.validate.return_value
)

annotation_json_service.present_for_user.assert_called_once_with(
annotation=storage.create_annotation.return_value, user=pyramid_request.user
# Check we validate
CreateAnnotationSchema.assert_called_once_with(pyramid_request)
schema = CreateAnnotationSchema.return_value
schema.validate.assert_called_once_with(pyramid_request.json_body)
# Check we create
annotation_write_service.create_annotation.assert_called_once_with(
data=schema.validate.return_value
)

assert result == annotation_json_service.present_for_user.return_value

def test_it_publishes_annotation_event(
self, AnnotationEvent, pyramid_request, storage
):
views.create(pyramid_request)

annotation = storage.create_annotation.return_value
annotation = annotation_write_service.create_annotation.return_value
# Check the event is raised
AnnotationEvent.assert_called_once_with(
pyramid_request, annotation.id, "create"
)
pyramid_request.notify_after_commit.assert_called_once_with(
AnnotationEvent.return_value
)
# Check we present
annotation_json_service.present_for_user.assert_called_once_with(
annotation=annotation, user=pyramid_request.user
)
assert result == annotation_json_service.present_for_user.return_value

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_raises_if_validate_raises(self, pyramid_request, create_schema):
create_schema.return_value.validate.side_effect = ValidationError("asplode")

with pytest.raises(ValidationError) as exc:
views.create(pyramid_request)

assert str(exc.value) == "asplode"

def test_it_raises_if_create_annotation_raises(self, pyramid_request, storage):
storage.create_annotation.side_effect = ValidationError("asplode")

with pytest.raises(ValidationError) as exc:
@pytest.mark.usefixtures("with_invalid_json_body")
def test_it_raises_for_invalid_json(self, pyramid_request):
with pytest.raises(PayloadError):
views.create(pyramid_request)

assert str(exc.value) == "asplode"

@pytest.fixture
def pyramid_request(self, pyramid_request):
pyramid_request.json_body = {}
pyramid_request.notify_after_commit = mock.Mock()
return pyramid_request

@pytest.fixture
def create_schema(self, patch):
def CreateAnnotationSchema(self, patch):
return patch("h.views.api.annotations.CreateAnnotationSchema")


Expand Down Expand Up @@ -339,6 +313,16 @@ def annotation(factories):
return factories.Annotation()


@pytest.fixture
def with_invalid_json_body(pyramid_request):
type(pyramid_request).json_body = None
with mock.patch.object(
type(pyramid_request), "json_body", new_callable=mock.PropertyMock
) as json_body:
json_body.side_effect = ValueError()
yield json_body


@pytest.fixture
def pyramid_request(pyramid_request):
pyramid_request.notify_after_commit = mock.Mock(spec_set=[])
Expand Down

0 comments on commit 2166d28

Please sign in to comment.