def test_basics(self): d = Document({ "id": "1", "title": "Annotations: The Missing Manual", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" } ], }) d.save() d = Document.fetch("1") assert_equal(d["title"], "Annotations: The Missing Manual") assert_equal(len(d['link']), 2) assert_equal(d['link'][0]['href'], "https://peerj.com/articles/53/") assert_equal(d['link'][0]['type'], "text/html") assert_equal(d['link'][1]['href'], "https://peerj.com/articles/53.pdf") assert_equal(d['link'][1]['type'], "application/pdf") assert d['created'] assert d['updated']
def includeme(config): app = Flask('annotator') # Create the annotator-store app app.register_blueprint(store.store) # and register the store api. # Set up the models settings = config.get_settings() if 'es.host' in settings: app.config['ELASTICSEARCH_HOST'] = settings['es.host'] if 'es.index' in settings: app.config['ELASTICSEARCH_INDEX'] = settings['es.index'] es.init_app(app) with app.test_request_context(): Annotation.create_all() Document.create_all() # Configure authentication and authorization app.config['AUTHZ_ON'] = True app.before_request(before_request) # Configure the API views -- version 1 is just an annotator.store proxy api_v1 = wsgiapp2(app) config.add_view(api_v1, route_name='api') if not config.registry.queryUtility(interfaces.IStoreClass): config.registry.registerUtility(Store, interfaces.IStoreClass)
def test_delete(self): ann = Document(id=1) ann.save() newdoc = Document.fetch(1) newdoc.delete() nodoc = Document.fetch(1) assert nodoc == None
def test_search(self): # Test search retrieve d = Document({ "id": "1", "title": "document", "link": [peerj["html"], peerj["pdf"]] }) d.save() res = Document.search(query={'title': 'document'}) assert_equal(len(res), 1)
def test_uris(self): d = Document({ "id": "1", "title": "document", "link": [peerj["html"], peerj["pdf"]] }) assert_equal(d.uris(), [ "https://peerj.com/articles/53/", "https://peerj.com/articles/53.pdf" ])
def test_get_all_by_uri(self): # add two documents and make sure we can search for both d = Document({ "id": "1", "title": "document1", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, ] }) d.save() d = Document({ "id": "2", "title": "document2", "link": [{ "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }] }) d.save() docs = Document.get_all_by_uris([ "https://peerj.com/articles/53/", "https://peerj.com/articles/53.pdf" ]) assert_equal(len(docs), 2)
def test_get_by_uri(self): # create 3 documents and make sure get_by_uri works properly d = Document({ "id": "1", "title": "document1", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }, ], }) d.save() d = Document({ "id": "2", "title": "document2", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }, ], }) d.save() d = Document({ "id": "3", "title": "document3", "link": [ { "href": "http://nature.com/123/", "type": "text/html" } ], }) d.save() doc = Document.get_by_uri("https://peerj.com/articles/53/") assert doc assert_equal(doc['title'], "document1")
def test_basics(self): # Creating a single document and verifies the saved attributes d = Document({ "id": "1", "title": "Annotations: The Missing Manual", "link": [peerj["html"], peerj["pdf"]] }) d.save() d = Document.fetch("1") assert_equal(d["title"], "Annotations: The Missing Manual") assert_equal(len(d['link']), 2) assert_equal(d['link'][0]['href'], "https://peerj.com/articles/53/") assert_equal(d['link'][0]['type'], "text/html") assert_equal(d['link'][1]['href'], "https://peerj.com/articles/53.pdf") assert_equal(d['link'][1]['type'], "application/pdf") assert d['created'] assert d['updated']
def test_search(self): d = Document({ "id": "1", "title": "document", "link": [{ "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }], }) d.save() res = Document.search(query={'title': 'document'}) assert_equal(len(res), 1)
def test_get_all_by_uri(self): # add two documents and make sure we can search for both d = Document({ "id": "1", "title": "document1", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, ] }) d.save() d = Document({ "id": "2", "title": "document2", "link": [ { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" } ] }) d.save() docs = Document.get_all_by_uris(["https://peerj.com/articles/53/", "https://peerj.com/articles/53.pdf"]) assert_equal(len(docs), 2)
def test_search(self): d = Document({ "id": "1", "title": "document", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" } ], }) d.save() res = Document.search(title='document') assert_equal(len(res), 1)
def test_uris(self): d = Document({ "id": "1", "title": "document", "link": [{ "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }], }) assert_equal(d.uris(), [ "https://peerj.com/articles/53/", "https://peerj.com/articles/53.pdf" ])
def test_uris(self): d = Document({ "id": "1", "title": "document", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" } ], }) assert_equal(d.uris(), [ "https://peerj.com/articles/53/", "https://peerj.com/articles/53.pdf" ])
def test_merge_links(self): d = Document({ "id": "1", "title": "document", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" } ], }) d.save() d = Document.fetch(1) assert d assert_equal(len(d['link']), 2) d.merge_links([ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "http://peerj.com/articles/53.doc", "type": "application/vnd.ms-word.document" } ]) d.save() assert_equal(len(d['link']), 3) d = Document.fetch(1) assert d assert_equal(len(d['link']), 3) doc = Document.get_by_uri("https://peerj.com/articles/53/") assert doc assert_equal(len(doc['link']), 3)
def test_deficient_links(self): # Test that bad links are not saved d = Document({ "id": "1", "title": "Chaos monkey: The messed up links", "link": [{ "href": "http://cuckoo.baboon/" }, { # I'm an empty link entry }, { "type": "text/html" }, { "href": "http://cuckoo.baboon/", "type": "text/html" }] }) d.save() d = Document.fetch("1") assert_equal(len(d['link']), 2) assert_equal(d['link'][0]['href'], "http://cuckoo.baboon/") assert_equal(d['link'][1]['href'], "http://cuckoo.baboon/") assert_equal(d['link'][1]['type'], "text/html")
def test_basics(self): d = Document({ "id": "1", "title": "Annotations: The Missing Manual", "link": [{ "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }], }) d.save() d = Document.fetch("1") assert_equal(d["title"], "Annotations: The Missing Manual") assert_equal(len(d['link']), 2) assert_equal(d['link'][0]['href'], "https://peerj.com/articles/53/") assert_equal(d['link'][0]['type'], "text/html") assert_equal(d['link'][1]['href'], "https://peerj.com/articles/53.pdf") assert_equal(d['link'][1]['type'], "application/pdf") assert d['created'] assert d['updated']
def test_delete(self): # Test deleting a document ann = Document(id=1) ann.save() newdoc = Document.fetch(1) newdoc.delete() nodoc = Document.fetch(1) assert nodoc is None
def drop_indices(app): from .model import Annotation from annotator.document import Document with app.test_request_context(): Annotation.drop_all() Document.drop_all()
def test_merge_links(self): d = Document({ "id": "1", "title": "document", "link": [{ "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }], }) d.save() d = Document.fetch(1) assert d assert_equal(len(d['link']), 2) d.merge_links([{ "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "http://peerj.com/articles/53.doc", "type": "application/vnd.ms-word.document" }]) d.save() assert_equal(len(d['link']), 3) d = Document.fetch(1) assert d assert_equal(len(d['link']), 3) doc = Document.get_by_uri("https://peerj.com/articles/53/") assert doc assert_equal(len(doc['link']), 3)
def test_get_by_uri(self): # create 3 documents and make sure get_by_uri works properly d = Document({ "id": "1", "title": "document1", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }, ], }) d.save() d = Document({ "id": "2", "title": "document2", "link": [ { "href": "https://peerj.com/articles/53/", "type": "text/html" }, { "href": "https://peerj.com/articles/53.pdf", "type": "application/pdf" }, ], }) d.save() d = Document({ "id": "3", "title": "document3", "link": [{ "href": "http://nature.com/123/", "type": "text/html" }], }) d.save() doc = Document.get_by_uri("https://peerj.com/articles/53/") assert doc assert_equal(doc['title'], "document1")
def test_save_merge_documents(self): d1 = Document({ "id": "1", "title": "document1", "link": [peerj["html"], peerj["pdf"]] }) d1.save() d2 = Document({ "id": "2", "title": "document2", "link": [peerj["doc"], peerj["docx"]] }) d2.save() # They are not merged yet d1 = Document.fetch(1) d2 = Document.fetch(2) assert d1 assert d2 d3 = Document({ "id": "3", "title": "document3", "link": [peerj["doc"], peerj["docx"]] }) d3.save() # d2 is merged into d3 d2 = Document.fetch(2) d3 = Document.fetch(3) assert d2 is None assert d3 d4 = Document({ "id": "4", "title": "document4", "link": [ { "href": "https://totallydifferenturl.com", "type": "text/html" } ] }) # A new document is created for d4 # It is not merged d4.save() d4 = Document.fetch(4) assert d4 d5 = Document({ "id": "5", "title": "document5", "link": [peerj["pdf"], peerj["doc"]] }) d5.save() # The documents have been merged into d5 d1 = Document.fetch(1) d2 = Document.fetch(2) d3 = Document.fetch(3) d4 = Document.fetch(4) d5 = Document.fetch(5) assert d1 is None assert d2 is None assert d3 is None assert d4 assert d5
def test_new(self): d = Document() assert_equal('{}', repr(d))
def test_save(self): d1 = Document({ "id": "1", "title": "document1", "link": [peerj["html"], peerj["pdf"]] }) d1.save() d2 = Document({ "id": "2", "title": "document2", "link": [peerj["pdf"], peerj["doc"]] }) d2.save() d3 = Document({ "id": "3", "title": "document3", "link": [peerj["doc"], peerj["docx"]] }) d3.save() d4 = Document({ "id": "4", "title": "document4", "link": [peerj["docx"]] }) d4.save() d1 = Document.fetch(1) d2 = Document.fetch(2) d3 = Document.fetch(3) d4 = Document.fetch(4) assert d1 is None assert d2 is None assert d3 is None assert d4
def test_merge_links(self): d = Document({ "id": "1", "title": "document", "link": [peerj["html"], peerj["pdf"]] }) d.save() d = Document.fetch(1) assert d assert_equal(len(d['link']), 2) d.merge_links([peerj["html"], peerj["doc"]]) d.save() assert_equal(len(d['link']), 3) d = Document.fetch(1) assert d assert_equal(len(d['link']), 3) doc = Document.get_by_uri("https://peerj.com/articles/53/") assert doc assert_equal(len(doc['link']), 3)
def includeme(config): """Include the annotator-store API backend via http or route embedding. Example INI file: .. code-block:: ini [app:h] api.key: 00000000-0000-0000-0000-000000000000 api.endpoint: https://example.com/api or use a relative path for the endpoint to embed the annotation store directly in the application. .. code-block:: ini [app:h] api.endpoint: /api The default is to embed the store as a route bound to "/api". """ app = flask.Flask('annotator') # Create the annotator-store app app.register_blueprint(store.store) # and register the store api. settings = config.get_settings() if 'es.host' in settings: app.config['ELASTICSEARCH_HOST'] = settings['es.host'] if 'es.index' in settings: app.config['ELASTICSEARCH_INDEX'] = settings['es.index'] es.init_app(app) try: with app.test_request_context(): Annotation.create_all() Document.create_all() except socket.error: raise Exception( "Can not access ElasticSearch at %s! Are you sure it's running?" % (app.config["ELASTICSEARCH_HOST"],) ) # Configure authentication and authorization app.config['AUTHZ_ON'] = True app.before_request(before_request) app.after_request(after_request) # Configure the API routes api_config = {'static': True} api_endpoint = config.registry.settings.get('api.endpoint', None) api_url = config.registry.settings.get('api.url', api_endpoint) if api_endpoint is not None: api_path = api_endpoint.rstrip('/') api_pattern = '/'.join([api_path, '*subpath']) # Configure the API views -- version 1 is just an annotator.store proxy api_v1 = wsgiapp2(app) config.add_route('api_real', api_pattern) config.add_view(api_v1, route_name='api_real') config.add_view(api_v1, name='api_virtual') if api_url is not None: api_url = api_url.strip('/') if urlparse.urlparse(api_url).scheme: def set_app_url(request, elements, kw): kw.setdefault('_app_url', api_url) return (elements, kw) api_config['pregenerator'] = set_app_url config.add_route('api', '/*subpath', **api_config) else: config.add_route('api', api_url + '/*subpath', **api_config) if not config.registry.queryUtility(interfaces.IStoreClass): config.registry.registerUtility(Store, interfaces.IStoreClass)
def test_get_by_uri_not_found(self): assert Document.get_by_uri("bogus") is None
def test_get_by_uri(self): # Make sure that only the document with the given uri is retrieved d = Document({ "id": "1", "title": "document1", "link": [peerj["html"], peerj["pdf"]] }) d.save() d = Document({ "id": "2", "title": "document2", "link": [ { "href": "http://nature.com/123/", "type": "text/html" } ], }) d.save() d = Document({ "id": "3", "title": "document3", "link": [peerj["doc"]] }) d.save() doc = Document.get_by_uri("https://peerj.com/articles/53/") assert doc assert_equal(doc['title'], "document1")
def includeme(config): """Include the annotator-store API backend via http or route embedding. Example INI file: .. code-block:: ini [app:h] api.key: 00000000-0000-0000-0000-000000000000 api.endpoint: https://example.com/api or use a relative path for the endpoint to embed the annotation store directly in the application. .. code-block:: ini [app:h] api.endpoint: /api The default is to embed the store as a route bound to "/api". """ app = flask.Flask('annotator') # Create the annotator-store app app.register_blueprint(store.store) # and register the store api. settings = config.get_settings() if 'es.host' in settings: app.config['ELASTICSEARCH_HOST'] = settings['es.host'] if 'es.index' in settings: app.config['ELASTICSEARCH_INDEX'] = settings['es.index'] es.init_app(app) try: with app.test_request_context(): Annotation.create_all() Document.create_all() except socket.error: raise Exception( "Can not access ElasticSearch at %s! Are you sure it's running?" % (app.config["ELASTICSEARCH_HOST"], )) except: with app.test_request_context(): Annotation.update_settings() Annotation.create_all() Document.create_all() # Configure authentication and authorization app.config['AUTHZ_ON'] = True app.before_request(before_request) app.after_request(after_request) # Configure the API routes api_config = {'static': True} api_endpoint = config.registry.settings.get('api.endpoint', None) api_url = config.registry.settings.get('api.url', api_endpoint) if api_endpoint is not None: api_path = api_endpoint.rstrip('/') api_pattern = '/'.join([api_path, '*subpath']) # Configure the API views -- version 1 is just an annotator.store proxy api_v1 = wsgiapp2(app) config.add_route('api_real', api_pattern) config.add_view(api_v1, route_name='api_real') config.add_view(api_v1, name='api_virtual') if api_url is not None: api_url = api_url.strip('/') if urlparse.urlparse(api_url).scheme: def set_app_url(request, elements, kw): kw.setdefault('_app_url', api_url) return (elements, kw) api_config['pregenerator'] = set_app_url config.add_route('api', '/*subpath', **api_config) else: config.add_route('api', api_url + '/*subpath', **api_config) if not config.registry.queryUtility(interfaces.IStoreClass): config.registry.registerUtility(Store, interfaces.IStoreClass)