Skip to content

Commit

Permalink
Added full test for annotation_api
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Jun 10, 2016
1 parent f1cb2a8 commit 89f56a7
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 4 deletions.
6 changes: 3 additions & 3 deletions flask_nemo/plugins/annotations_api.py
Expand Up @@ -11,7 +11,7 @@ class AnnotationsApiPlugin(PluginPrototype):
:type queryinterface: QueryInterface
"""

ROUTES = [
ROUTES = [
("/api/annotations/target/<target_urn>", "r_annotations_by_target", ["GET"]),
("/api/annotations/resource/<sha>", "r_annotation_get", ["GET"])
]
Expand Down Expand Up @@ -75,5 +75,5 @@ def r_annotation_get(self, sha):
content = annotation.read()
if isinstance(content, Response):
return content
headers = {"Content-Type": annotation.content_type}
return content, headers
headers = {"Content-Type": annotation.mimetype}
return Response(content, headers=headers)
1 change: 1 addition & 0 deletions tests/test_controller.py
Expand Up @@ -12,6 +12,7 @@

from .resources import NemoResource


class NemoTestControllers(NemoResource):

def test_flask_nemo(self):
Expand Down
127 changes: 126 additions & 1 deletion tests/test_plugins/test_annotations_api.py
Expand Up @@ -3,10 +3,135 @@
from unittest import TestCase
from flask_nemo.plugins.annotations_api import AnnotationsApiPlugin
from flask_nemo.query.proto import QueryPrototype
from flask_nemo.query.annotation import AnnotationResource
from flask import Response


class MockQueryInterface(QueryPrototype):
ANNOTATION = AnnotationResource(
"uri", "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1", "http://foo.bar/treebank",
resolver=None, # Overwriting this one for the purpose of the test
mimetype="application/xml", slug="treebank"
)

def getAnnotations(self,
*urns,
wildcard=".", include=None, exclude=None,
limit=None, start=1,
expand=False, **kwargs
):
return 1, [
type(self).ANNOTATION
]

def getResource(self, sha):
annotation = type(self).ANNOTATION
if sha == "abc":
annotation.read = lambda: Response("a", headers={"Content-Type": "text/plain"})
annotation.read = lambda: Response("a", headers={"Content-Type": "text/plain"})
else:
annotation.__mimetype__ = "application/xml"
annotation.read = lambda: "123"
return annotation


class RetrieverMock(object):
@staticmethod
def match(uri):
return True

@staticmethod
def read(uri):
return "{} has been read".format(uri), "mimetype"


class AnnotationsApiPluginDummyInterfaceTest(TestCase):
""" Test Suite for Annotations Api Plugin with a mock of query interface
"""

def setUp(self):
self.ann_plugin = AnnotationsApiPlugin(name="testplugin", queryinterface=MockQueryInterface(lambda x: x))
self.client = make_client(self.ann_plugin)

def test_route_by_target(self):
""" Check error response for improper urn
"""
response = self.client.get("/api/annotations/target/urn:cts:latinLit:phi1294.phi002.perseus-lat2:1")
data = json.loads(response.data.decode("utf-8"))
self.assertEqual(
data,
{
"annotations": [
{
'slug': 'treebank',
'target': 'urn:cts:latinLit:phi1294.phi002.perseus-lat2:1',
'type': 'http://foo.bar/treebank',
'uri': 'uri',
'url': '/api/annotations/resource/922fffd1bd6fdaa95b4545df7a78754f6d67c2272b2900aa3ccd5e9da3dbb179'
}
],
'count': 1
},
"Response of the api should work with default annotation"
)
self.assertEqual(200, response.status_code, "HTTP Code for valid request is 200")

def test_route_get_content_response(self):
""" Check error response for improper urn
"""
response = self.client.get("/api/annotations/resource/abc")
data = response.data.decode("utf-8")
self.assertEqual(
data,
"a",
"Content of the response should be forwarded"
)
self.assertEqual("text/plain", response.headers["Content-Type"], "Response mimetype should be forwarded")

def test_route_get_content_normal(self):
""" Check error response for improper urn
"""
response = self.client.get("/api/annotations/resource/def")
self.assertEqual(
response.data,
b"123",
"Content of the response should be forwarded"
)
self.assertEqual("application/xml", response.headers["Content-Type"], "Read() mimetype should be forwarded")


class AnnotationsApiPluginTest(TestCase):
""" Test Suite for Annotations Api Plugin in empty case
"""

def setUp(self):
self.ann_plugin = AnnotationsApiPlugin(name="testplugin", queryinterface=QueryPrototype(lambda x: x))
self.client = make_client(self.ann_plugin)

def test_route_by_target_valid_urn(self):
""" Check empty response for valid urn target (given prototype query interface which knows nothing)
"""
response = self.client.get("/api/annotations/target/urn:cts:greekLit:tlg0012.tlg001.grc1")
self.assertEqual({"annotations": [], "count": 0}, json.loads(response.get_data().decode("utf-8")))
self.assertEqual(200, response.status_code)

def test_route_by_target_invalid_urn(self):
""" Check error response for improper urn
"""
response = self.client.get("/api/annotations/target/foo")
self.assertEqual(b"invalid urn", str(response.data))
self.assertEqual(400, response.status_code)

def test_route_get(self):
""" Check not found response for valid urn target (given prototype query interface which knows nothing)
"""
response = self.client.get("/api/annotations/resource/foo")
self.assertEqual("b'invalid resource uri'", str(response.data))
self.assertEqual(404, response.status_code)


class AnnotationsApiPluginTest(TestCase):
""" Test Suite for Annotations Api Plugin
""" Test Suite for Annotations Api Plugin in empty case
"""

def setUp(self):
Expand Down

0 comments on commit 89f56a7

Please sign in to comment.