def test_search_by_collection(self): """Test search by collection.""" anno = AnnotationFactory() AnnotationFactory() collection_iri = anno.collection.id results = self.search.search(collection=collection_iri) assert_equal(results, [anno])
def test_search_by_contains(self): """Test search by contains.""" data = {'foo': 'bar'} anno = AnnotationFactory(data=data) AnnotationFactory(data={'baz': 'qux'}) results = self.search.search(contains=data) assert_equal(results, [anno])
def test_search_by_collection_and_contains(self): """Test search by collection and contains.""" data = {'foo': 'bar'} anno = AnnotationFactory(data=data) AnnotationFactory(data={'baz': 'qux'}) collection_iri = anno.collection.id results = self.search.search(collection=collection_iri, contains=data) assert_equal(results, [anno])
def test_collection_modified_when_annotation_updated(self): """Test Collection modified time updated when Annotation updated.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) data = annotation.dictize().copy() data['body'] = "My new body" endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) self.app_put_json_ld(endpoint, data=data) assert_not_equal(annotation.modified, None) assert_equal(collection.modified, annotation.modified)
def test_search_by_fts_different_case(self): """Test search by fts with different case.""" anno1 = AnnotationFactory(data={'body': {'source': 'FOO'}}) anno2 = AnnotationFactory(data={'body': 'bar'}) fts_query = { 'body': { 'query': 'fo' } } results = self.search.search(fts=fts_query) assert_equal(results, [anno1])
def test_search_by_fts_default(self): """Test search by fts with default settings.""" anno1 = AnnotationFactory(data={'body': {'source': 'foo'}}) anno2 = AnnotationFactory(data={'body': 'bar'}) fts_query = { 'body': { 'query': 'fo' } } results = self.search.search(fts=fts_query) assert_equal(results, [anno1])
def test_search_by_range_gt(self): """Test search by range greater than.""" anno = AnnotationFactory(data={'body': 43}) AnnotationFactory() range_query = { 'body': { 'gt': 42 } } results = self.search.search(range=range_query) assert_equal(results, [anno])
def test_search_by_fts_without_prefix(self): """Test search by fts without prefix.""" anno1 = AnnotationFactory(data={'body': 'qux'}) AnnotationFactory(data={'body': 'quxx'}) fts_query = { 'body': { 'query': 'qux', 'prefix': False } } results = self.search.search(fts=fts_query) assert_equal(results, [anno1])
def test_batch_delete_annotations(self): """Test batch delete Annotations.""" annotations = [ AnnotationFactory(id='foo'), AnnotationFactory(id='bar') ] data = [anno.dictize() for anno in annotations][1:] endpoint = '/batch/' res = self.app_delete_json_ld(endpoint, data=data) assert_equal(res.status_code, 204, res.data) annotations_after = repo.filter_by(Annotation, deleted=False) assert_equal(annotations_after, [annotations[0]])
def test_search_by_fts_phrase(self): """Test search by fts phrase.""" anno1 = AnnotationFactory(data={'body': {'source': 'foo bar baz'}}) anno2 = AnnotationFactory(data={'body': 'foo bar baz qux'}) AnnotationFactory(data={'body': 'foo baz'}) fts_phrase_query = { 'body': { 'query': 'foo bar baz' } } results = self.search.search(fts_phrase=fts_phrase_query) assert_equal(results, [anno1, anno2])
def test_search_by_fts_phrase_with_distance(self): """Test search by fts phrase with distance.""" anno1 = AnnotationFactory(data={'body': 'foo bar baz qux'}) AnnotationFactory(data={'body': 'foo bar qux'}) AnnotationFactory(data={'body': 'foo qux'}) fts_phrase_query = { 'body': { 'query': 'foo qux', 'distance': 3 } } results = self.search.search(fts_phrase=fts_phrase_query) assert_equal(results, [anno1])
def test_404_when_page_does_not_exist(self): """Test 404 when AnnotationPage does not exist.""" collection = CollectionFactory() endpoint = u'/annotations/{0}/?page={1}'.format(collection.id, 0) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data) per_page = current_app.config.get('ANNOTATIONS_PER_PAGE') AnnotationFactory.create_batch(per_page, collection=collection) endpoint = u'/annotations/{0}/?page={1}'.format(collection.id, 1) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data)
def test_search_by_fts_default_with_or(self): """Test search by fts with default settings with or.""" anno1 = AnnotationFactory(data={'body': {'source': 'foo'}}) anno2 = AnnotationFactory(data={'body': 'bar'}) anno3 = AnnotationFactory(data={'body': 'baz'}) fts_query = { 'body': { 'query': 'foo bar', 'operator': 'or' } } results = self.search.search(fts=fts_query) assert_equal(results, [anno1, anno2])
def test_search_by_range_lt(self): """Test search by range less than.""" anno_now = AnnotationFactory() now = datetime.utcnow() yesterday = (now - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ') anno_yesterday = AnnotationFactory(created=yesterday) AnnotationFactory() range_query = { 'created': { 'lt': anno_now.created } } results = self.search.search(range=range_query) assert_equal(results, [anno_yesterday])
def test_list_all_collections(self): """Test a list of all Annotation Collections is returned.""" collection1 = CollectionFactory() collection2 = CollectionFactory() AnnotationFactory.create_batch(3, collection=collection1) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.index', _external=True), 'label': 'All collections', 'type': ['AnnotationCollection', 'BasicContainer'], 'total': 2, 'items': [{ 'id': url_for('api.collections', collection_id=collection1.id, _external=True), 'type': collection1.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 3 }, { 'id': url_for('api.collections', collection_id=collection2.id, _external=True), 'type': collection2.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 0 }] } endpoint = '/annotations/' res = self.app_get_json_ld(endpoint) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_410_when_annotation_used_to_exist(self): """Test 410 when Annotation used to exist.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection, deleted=True) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 410, res.data)
def test_collection_exported(self): """Test Collection exported.""" annotation = AnnotationFactory() endpoint = u'/export/{}/'.format(annotation.collection.id) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 200, res.data) data = json.loads(res.data.decode('utf8')) assert_equal(data, [{ 'id': url_for('api.annotations', collection_id=annotation.collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': annotation.data['body'], 'target': annotation.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }])
def test_get_minimal_container_with_iris(self): """Test get Collection with PreferMinimalContainer and IRIs.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, iris=1), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': url_for('api.collections', collection_id=collection.id, **dict(page=0, iris=1)) } endpoint = u'/annotations/{}/'.format(collection.id) prefer = ('return=representation;include=' '"http://www.w3.org/ns/ldp#PreferMinimalContainer' ' http://www.w3.org/ns/oa#PreferContainedIRIs"') headers = dict(prefer=prefer) res = self.app_get_json_ld(endpoint, headers=headers) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_get_iri_container_using_query_string(self): """Test get Collection with PreferContainedIRIs using query.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, iris=1), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': { 'id': url_for('api.collections', collection_id=collection.id, **dict(page=0, iris=1)), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [ url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id) ] } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint + '?iris=1') data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_offset(self): """Test search with offset.""" size = 5 offset = 2 annotations = AnnotationFactory.create_batch(size) results = self.search.search(offset=offset) assert_equal(len(results), size - offset)
def test_404_when_annotation_not_in_collection(self): """Test 404 when Annotation is not in the Collection.""" collection1 = CollectionFactory() collection2 = CollectionFactory() annotation = AnnotationFactory(collection=collection1) endpoint = u'/annotations/{}/{}/'.format(collection2.id, annotation.id) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data)
def test_annotation_updated(self): """Test Annotation updated.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) data = annotation.dictize().copy() data['body'] = "My new body" assert_equal(annotation.modified, None) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_put_json_ld(endpoint, data=data) # Test object updated assert_equal(annotation.modified, '1984-11-19T00:00:00Z') # Test data data = json.loads(res.data.decode('utf8')) assert_equal( data, { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': data['body'], 'target': data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'modified': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }) # Test 200 assert_equal(res.status_code, 200, res.data) # Test Link header link = '<http://www.w3.org/ns/ldp#Resource>; rel="type"' assert_equal(res.headers.get('Link'), link)
def test_annotation_deleted(self): """Test Annotation deleted.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_delete_json_ld(endpoint) assert_equal(annotation.deleted, True) assert_equal(res.status_code, 204, res.data)
def test_deleted_annotations_not_returned(self): """Test deleted Annotation not returned in AnnotationCollection.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) AnnotationFactory(collection=collection, deleted=True) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': { 'id': url_for('api.collections', collection_id=collection.id, page=0), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [{ 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': annotation.data['body'], 'target': annotation.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }] } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_collection_modified_when_annotation_deleted(self): """Test Collection modified time updated when Annotation deleted.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) self.app_delete_json_ld(endpoint) assert_equal(annotation.deleted, True) assert_not_equal(annotation.modified, None) assert_equal(collection.modified, annotation.modified)
def test_non_empty_collection_cannot_be_deleted(self): """Test non-empty Collection cannot be deleted.""" collection = CollectionFactory() CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_delete_json_ld(endpoint) assert_equal(res.status_code, 400, res.data) assert_equal(collection.deleted, False)
def test_search(self): """Test search.""" endpoint = '/search/' anno_data = dict(body='foo', target='bar', type='Annotation') anno = AnnotationFactory(data=anno_data) AnnotationFactory() query = {'contains': {u'body': u'foo'}} res = self.app_get_json_ld(endpoint, data=query) data = json.loads(res.data.decode('utf8')) assert_dict_equal( data, { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.search', **query), 'type': ['AnnotationCollection', 'BasicContainer'], 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': { 'id': url_for('api.search', page=0, **query), 'type': 'AnnotationPage', 'items': [{ 'id': url_for('api.annotations', collection_id=anno.collection.id, annotation_id=anno.id), 'type': 'Annotation', 'body': anno.data['body'], 'target': anno.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }], 'startIndex': 0 } })
def test_search_by_fts_does_not_include_keys(self): """Test search by fts does not include keys.""" anno1 = AnnotationFactory(data={'body': {'source': 'foo'}}) fts_query = { 'body': { 'query': 'source' } } results = self.search.search(fts=fts_query) assert_equal(results, [])
def test_collection_exported_as_zip(self): """Test Collection exported as zip. Testing of this really ought to be improved! """ annotation = AnnotationFactory() endpoint = u'/export/{}/?zip=1'.format(annotation.collection.id) res = self.app.get(endpoint) assert_equal(res.headers['Content-Type'], 'application/zip') content_disposition = 'attachment; filename=collection1.zip' assert_equal(res.headers['Content-Disposition'], content_disposition)
def test_get_annotation_headers(self): """Test Annotation headers.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_get_json_ld(endpoint) link = '<http://www.w3.org/ns/ldp#Resource>; rel="type"' assert_equal(res.headers.get('Link'), link) ct = 'application/ld+json; profile="http://www.w3.org/ns/anno.jsonld"' assert_equal(res.headers.get('Content-Type'), ct) allow = 'GET,PUT,DELETE,OPTIONS,HEAD' assert_equal(res.headers.get('Allow'), allow) assert_not_equal(res.headers.get('ETag'), None)