def test_it_validates_the_group_scope(self, pyramid_request, annotation, _validate_group_scope): storage.update_annotation(pyramid_request, annotation.id, {"target_uri": "sentinel.target_uri"}) _validate_group_scope.assert_called_once_with(annotation.group, "sentinel.target_uri")
def test_it_raises_if_missing_group(self, annotation_data, pyramid_request, group_service): group_service.find.return_value = None with pytest.raises(ValidationError) as exc: storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert str(exc.value).startswith('group: ')
def test_it_updates_the_document_metadata_from_the_annotation( self, annotation_data, pyramid_request, datetime, update_document_metadata, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation_data['document']['document_meta_dicts'] = ( mock.sentinel.document_meta_dicts) annotation_data['document']['document_uri_dicts'] = ( mock.sentinel.document_uri_dicts) storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) update_document_metadata.assert_called_once_with( pyramid_request.db, annotation.target_uri, mock.sentinel.document_meta_dicts, mock.sentinel.document_uri_dicts, updated=datetime.utcnow() )
def test_it_does_not_call_update_document_meta_if_no_document_in_data( self, pyramid_request, update_document_metadata, group_service): storage.update_annotation(pyramid_request, "test_annotation_id", {}, group_service) assert not update_document_metadata.called
def test_it_updates_the_document_metadata_from_the_annotation( self, annotation_data, pyramid_request, datetime, update_document_metadata, groupfinder_service, ): annotation = pyramid_request.db.query.return_value.get.return_value annotation_data["document"][ "document_meta_dicts" ] = mock.sentinel.document_meta_dicts annotation_data["document"][ "document_uri_dicts" ] = mock.sentinel.document_uri_dicts storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, groupfinder_service ) update_document_metadata.assert_called_once_with( pyramid_request.db, annotation.target_uri, mock.sentinel.document_meta_dicts, mock.sentinel.document_uri_dicts, updated=datetime.utcnow(), )
def test_it_raises_when_group_scope_mismatch(self, annotation_data, pyramid_request, group_service, scoped_open_group): annotation_data['target_uri'] = u'http://www.bar.com/baz/ding.html' group_service.find.return_value = scoped_open_group with pytest.raises(ValidationError) as exc: storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert str(exc.value).startswith('group scope: ')
def test_it_updates_the_annotation(self, annotation_data, session): annotation = session.query.return_value.get.return_value storage.update_annotation(session, 'test_annotation_id', annotation_data) for key, value in annotation_data.items(): assert getattr(annotation, key) == value
def test_it_raises_when_group_scope_mismatch(self, annotation_data, pyramid_request, group_service, scoped_open_group): annotation_data['target_uri'] = 'http://www.bar.com/baz/ding.html' group_service.find.return_value = scoped_open_group with pytest.raises(ValidationError) as exc: storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert str(exc.value).startswith('group scope: ')
def test_it_queues_the_annotation_for_syncing_to_Elasticsearch( self, annotation, pyramid_request, search_index): storage.update_annotation(pyramid_request, annotation.id, {}) search_index._queue.add_by_id.assert_called_once_with( # pylint:disable=protected-access annotation.id, tag="storage.update_annotation", schedule_in=60)
def test_it_gets_the_annotation_model(self, annotation_data, models, session): storage.update_annotation(session, 'test_annotation_id', annotation_data) session.query.assert_called_once_with(models.Annotation) session.query.return_value.get.assert_called_once_with( 'test_annotation_id')
def test_it_does_not_call_update_document_meta_if_no_document_in_data( self, session, update_document_metadata): storage.update_annotation(session, 'test_annotation_id', {}) assert not update_document_metadata.called
def test_it_gets_the_annotation_model(self, pyramid_request, annotation_data, group_service, models): storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) pyramid_request.db.query.assert_called_once_with(models.Annotation) pyramid_request.db.query.return_value.get.assert_called_once_with( "test_annotation_id")
def test_it_updates_the_annotations_document_id(self, annotation_data, session, models): annotation = session.query.return_value.get.return_value document = mock.Mock() models.update_document_metadata.return_value = document storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.document == document
def test_it_overwrites_existing_extras(self, annotation_data, session): annotation = session.query.return_value.get.return_value annotation.extra = {'foo': 'original_value'} annotation_data['extra'] = {'foo': 'new_value'} storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.extra == {'foo': 'new_value'}
def test_it_does_not_call_update_document_meta_if_no_document_in_data( self, pyramid_request, update_document_metadata, group_service): storage.update_annotation(pyramid_request, 'test_annotation_id', {}, group_service) assert not update_document_metadata.called
def test_it_adds_new_extras(self, annotation_data, session): annotation = session.query.return_value.get.return_value annotation.extra = {} annotation_data['extra'] = {'foo': 'bar'} storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.extra == {'foo': 'bar'}
def test_it_updates_the_annotation(self, annotation_data, pyramid_request, group_service): annotation = pyramid_request.db.query.return_value.get.return_value storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) for key, value in annotation_data.items(): assert getattr(annotation, key) == value
def test_it_adds_new_extras(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {} annotation_data["extra"] = {"foo": "bar"} storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) assert annotation.extra == {"foo": "bar"}
def test_it_does_not_change_extras_if_none_are_sent( self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {'one': 1, 'two': 2} assert not annotation_data.get('extra') storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.extra == {'one': 1, 'two': 2}
def test_it_does_not_change_extras_if_none_are_sent( self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {"one": 1, "two": 2} assert not annotation_data.get("extra") storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) assert annotation.extra == {"one": 1, "two": 2}
def test_it_overwrites_existing_extras(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {'foo': 'original_value'} annotation_data['extra'] = {'foo': 'new_value'} storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.extra == {'foo': 'new_value'}
def test_it_updates_the_annotation(self, annotation_data, pyramid_request, group_service): annotation = pyramid_request.db.query.return_value.get.return_value storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) for key, value in annotation_data.items(): assert getattr(annotation, key) == value
def test_it_does_not_change_extras_if_none_are_sent( self, annotation_data, session): annotation = session.query.return_value.get.return_value annotation.extra = {'one': 1, 'two': 2} assert not annotation_data.get('extra') storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.extra == {'one': 1, 'two': 2}
def test_it_overwrites_existing_extras(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {"foo": "original_value"} annotation_data["extra"] = {"foo": "new_value"} storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) assert annotation.extra == {"foo": "new_value"}
def test_it_queues_the_annotation_for_syncing_to_Elasticsearch( self, annotation_data, groupfinder_service, pyramid_request, search_index ): storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, groupfinder_service ) search_index._queue.add_by_id.assert_called_once_with( "test_annotation_id", tag="storage.update_annotation", schedule_in=60 )
def test_it_adds_new_extras(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {} annotation_data['extra'] = {'foo': 'bar'} storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.extra == {'foo': 'bar'}
def test_it_adds_new_extras(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {} annotation_data["extra"] = {"foo": "bar"} storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert annotation.extra == {"foo": "bar"}
def test_it_updates_the_annotations_document_id( self, annotation_data, pyramid_request, update_document_metadata, group_service ): annotation = pyramid_request.db.query.return_value.get.return_value document = mock.Mock() update_document_metadata.return_value = document storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert annotation.document == document
def test_it_updates_the_annotations_document_id(self, annotation_data, pyramid_request, update_document_metadata, group_service): annotation = pyramid_request.db.query.return_value.get.return_value document = mock.Mock() update_document_metadata.return_value = document storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) assert annotation.document == document
def test_it_gets_the_annotation_model( self, pyramid_request, annotation_data, group_service, models ): storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) pyramid_request.db.query.assert_called_once_with(models.Annotation) pyramid_request.db.query.return_value.get.assert_called_once_with( "test_annotation_id" )
def test_it_does_not_change_extras_if_none_are_sent(self, annotation_data, session): annotation = session.query.return_value.get.return_value annotation.extra = {'one': 1, 'two': 2} assert not annotation_data.get('extra') storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.extra == {'one': 1, 'two': 2}
def test_it_updates_the_annotations_document_id(self, annotation_data, session, update_document_metadata): annotation = session.query.return_value.get.return_value document = mock.Mock() update_document_metadata.return_value = document storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.document == document
def test_it_does_not_change_extras_if_none_are_sent( self, pyramid_request, annotation_data, group_service ): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {"one": 1, "two": 2} assert not annotation_data.get("extra") storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert annotation.extra == {"one": 1, "two": 2}
def test_it_does_not_change_extras_that_are_not_sent( self, pyramid_request, annotation_data, groupfinder_service ): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {"one": 1, "two": 2} annotation_data["extra"] = {"two": 22} storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, groupfinder_service ) assert annotation.extra["one"] == 1
def test_it_overwrites_existing_extras( self, pyramid_request, annotation_data, group_service ): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {"foo": "original_value"} annotation_data["extra"] = {"foo": "new_value"} storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert annotation.extra == {"foo": "new_value"}
def test_it_does_not_change_extras_that_are_not_sent( self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = { 'one': 1, 'two': 2, } annotation_data['extra'] = {'two': 22} storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.extra['one'] == 1
def test_it_raises_when_group_scope_mismatch( self, annotation_data, pyramid_request, group_service, scoped_open_group ): # This target URI does not match any of the group's defined scopes annotation_data["target_uri"] = "http://www.bar.com/baz/ding.html" group_service.find.return_value = scoped_open_group with pytest.raises(ValidationError) as exc: storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert str(exc.value).startswith("group scope: ")
def test_it_raises_when_group_scope_mismatch(self, annotation_data, pyramid_request, group_service, scoped_open_group): # This target URI does not match any of the group's defined scopes annotation_data["target_uri"] = "http://www.bar.com/baz/ding.html" group_service.find.return_value = scoped_open_group with pytest.raises(ValidationError) as exc: storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) assert str(exc.value).startswith("group scope: ")
def test_it_does_not_change_extras_that_are_not_sent( self, annotation_data, session): annotation = session.query.return_value.get.return_value annotation.extra = { 'one': 1, 'two': 2, } annotation_data['extra'] = {'two': 22} storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.extra['one'] == 1
def test_it_does_not_change_extras_if_none_are_sent(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = {'one': 1, 'two': 2} assert not annotation_data.get('extra') storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.extra == {'one': 1, 'two': 2}
def test_it_does_not_change_extras_that_are_not_sent(self, annotation_data, session): annotation = session.query.return_value.get.return_value annotation.extra = { 'one': 1, 'two': 2, } annotation_data['extra'] = {'two': 22} storage.update_annotation(session, 'test_annotation_id', annotation_data) assert annotation.extra['one'] == 1
def test_it_returns_the_annotation(self, annotation_data, pyramid_request, group_service): annotation = storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation == pyramid_request.db.query.return_value.get.return_value
def test_it_changes_the_updated_timestamp(self, annotation_data, pyramid_request, datetime, group_service): annotation = storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.updated == datetime.utcnow()
def test_it_allows_when_group_scope_matches(self, annotation_data, pyramid_request, group_service, scoped_open_group, models): annotation_data['target_uri'] = 'http://www.foo.com/baz/ding.html' # this should not raise annotation = storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation == pyramid_request.db.query.return_value.get.return_value
def test_it_returns_the_annotation(self, annotation_data, pyramid_request, group_service): annotation = storage.update_annotation(pyramid_request, "test_annotation_id", annotation_data, group_service) assert annotation == pyramid_request.db.query.return_value.get.return_value
def test_it_allows_when_group_scope_matches(self, annotation_data, pyramid_request, group_service, scoped_open_group, models): annotation_data['target_uri'] = u'http://www.foo.com/baz/ding.html' # this should not raise annotation = storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation == pyramid_request.db.query.return_value.get.return_value
def test_it_does_not_change_extras_that_are_not_sent(self, pyramid_request, annotation_data, group_service): annotation = pyramid_request.db.query.return_value.get.return_value annotation.extra = { 'one': 1, 'two': 2, } annotation_data['extra'] = {'two': 22} storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation.extra['one'] == 1
def test_it_allows_group_scope_when_no_target_uri(self, annotation_data, pyramid_request, group_service, scoped_open_group): annotation_data.pop('target_uri') group_service.find.return_value = scoped_open_group # this should not raise annotation = storage.update_annotation(pyramid_request, 'test_annotation_id', annotation_data, group_service) assert annotation == pyramid_request.db.query.return_value.get.return_value
def test_it_allows_when_group_scope_matches( self, annotation_data, pyramid_request, group_service, scoped_open_group, models ): # This target URI matches at least one of the scopes for the group # it will go into... annotation_data["target_uri"] = "http://www.foo.com/baz/ding.html" # this should not raise annotation = storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert annotation == pyramid_request.db.query.return_value.get.return_value
def test_it_updates_the_document_metadata_from_the_annotation( self, annotation_data, session, datetime, update_document_metadata): annotation = session.query.return_value.get.return_value annotation_data['document']['document_meta_dicts'] = ( mock.sentinel.document_meta_dicts) annotation_data['document']['document_uri_dicts'] = ( mock.sentinel.document_uri_dicts) storage.update_annotation(session, 'test_annotation_id', annotation_data) update_document_metadata.assert_called_once_with( session, annotation.target_uri, mock.sentinel.document_meta_dicts, mock.sentinel.document_uri_dicts, updated=datetime.utcnow() )
def test_it_allows_scope_mismatch_when_enforce_scope_is_False( self, annotation_data, pyramid_request, group_service, scoped_open_group ): scoped_open_group.enforce_scope = False group_service.find.return_value = scoped_open_group # This target URI is not within any of the group's defined scopes annotation_data["target_uri"] = "http://www.bar.com/baz/ding.html" # This will not raise because ``enforce_scope`` is set to False for the # annotation's group annotation = storage.update_annotation( pyramid_request, "test_annotation_id", annotation_data, group_service ) assert annotation == pyramid_request.db.query.return_value.get.return_value