Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
def test_create_calls_validator(validators):
    request = mock.Mock()

    views.create(request)

    validators.Annotation.return_value.validate.assert_called_once_with(
        request.json_body)
Exemplo n.º 8
0
    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'
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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'
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
    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)
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
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)
Exemplo n.º 19
0
    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'
Exemplo n.º 20
0
    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)
Exemplo n.º 21
0
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)
Exemplo n.º 22
0
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})
Exemplo n.º 23
0
    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})
Exemplo n.º 24
0
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)
Exemplo n.º 25
0
    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)
Exemplo n.º 26
0
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})
Exemplo n.º 27
0
    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'
Exemplo n.º 28
0
    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})
Exemplo n.º 29
0
    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)
Exemplo n.º 30
0
    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)
Exemplo n.º 31
0
    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)
Exemplo n.º 32
0
    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)
Exemplo n.º 33
0
    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)
Exemplo n.º 34
0
 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)
Exemplo n.º 35
0
    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)
Exemplo n.º 36
0
    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)
Exemplo n.º 37
0
    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)
Exemplo n.º 38
0
    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)
Exemplo n.º 39
0
    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)
Exemplo n.º 40
0
    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)
Exemplo n.º 41
0
    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)
Exemplo n.º 42
0
    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)
Exemplo n.º 43
0
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'
Exemplo n.º 44
0
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'
Exemplo n.º 45
0
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()
Exemplo n.º 46
0
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()
Exemplo n.º 47
0
    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)
Exemplo n.º 48
0
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
Exemplo n.º 49
0
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
Exemplo n.º 50
0
    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)
Exemplo n.º 51
0
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
Exemplo n.º 52
0
    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)
Exemplo n.º 53
0
    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)
Exemplo n.º 54
0
def test_create_inits_AnnotationEvent_once(AnnotationEvent):
    views.create(mock.Mock())

    assert AnnotationEvent.call_count == 1
Exemplo n.º 55
0
    def test_it_inits_CreateAnnotationSchema(self, mock_request, schemas):
        views.create(mock_request)

        schemas.CreateAnnotationSchema.assert_called_once_with(mock_request)
Exemplo n.º 56
0
    def test_it_inits_LegacyCreateAnnotationSchema(self, schemas):
        request = self.mock_request()

        views.create(request)

        schemas.LegacyCreateAnnotationSchema.assert_called_once_with(request)
Exemplo n.º 57
0
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)
Exemplo n.º 58
0
def test_create_returns_render(search_lib):
    """It should return what render() returns."""
    assert views.create(mock.Mock()) == search_lib.render.return_value
Exemplo n.º 59
0
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)