Skip to content

Commit

Permalink
Added tests for simple GetPassage
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Dec 2, 2016
1 parent 9735b79 commit 51f2ea6
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 90 deletions.
65 changes: 0 additions & 65 deletions MyCapytain/resolvers/cts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,65 +0,0 @@
from MyCapytain.resolvers.prototypes import Resolver
from MyCapytain.resources.texts.api.cts import Text, Passage
from MyCapytain.retrievers.cts5 import CTS


class HttpCTSResolver(Resolver):
def __init__(self, endpoint):
if not isinstance(endpoint, CTS):
raise TypeError("Endpoint should be a CTS Endpoint object")
self.__endpoint__ = endpoint

@property
def endpoint(self):
""" CTS Endpoint of the resolver
:return: CTS Endpoint
:rtype: CTS
"""
return self.__endpoint__

def getPassage(self, textId, subreference=None, prevnext=False, metadata=False):
""" Retrieve a text node from the API
:param textId: Text Identifier
:param subreference: Passage Reference
:param prevnext: Retrieve graph representing previous and next passage
:param metadata: Retrieve metadata about the passage and the text
:return: Passage
:rtype: Passage
"""
text = Text(
urn=textId,
retriever=self.endpoint
)
if metadata or prevnext:
return text.getPassagePlus(reference=subreference)
else:
return text.getPassage(reference=subreference)

def getSiblings(self, textId, subreference):
""" Retrieve the siblings of a textual node
:param textId: Text Identifier
:param subreference: Passage Reference
:return: (str, str)
"""
text = Text(
urn=textId,
retriever=self.endpoint
)
return text.getPrevNextUrn(subreference)

def getSiblings(self, textId, subreference):
""" Retrieve the siblings of a textual node
:param textId: Text Identifier
:param subreference: Passage Reference
:return: (str, str)
"""
text = Text(
urn=textId,
retriever=self.endpoint
)
return text.getPrevNextUrn(subreference)

80 changes: 80 additions & 0 deletions MyCapytain/resolvers/cts/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from MyCapytain.resolvers.prototypes import Resolver
from MyCapytain.resources.texts.api.cts import Text, Passage
from MyCapytain.resources.collections.cts import TextInventory
from MyCapytain.retrievers.cts5 import CTS


class HttpCTSResolver(Resolver):
def __init__(self, endpoint):
if not isinstance(endpoint, CTS):
raise TypeError("Endpoint should be a CTS Endpoint object")
self.__endpoint__ = endpoint

@property
def endpoint(self):
""" CTS Endpoint of the resolver
:return: CTS Endpoint
:rtype: CTS
"""
return self.__endpoint__

def getPassage(self, textId, subreference=None, prevnext=False, metadata=False):
""" Retrieve a text node from the API
:param textId: Text Identifier
:param subreference: Passage Reference
:param prevnext: Retrieve graph representing previous and next passage
:param metadata: Retrieve metadata about the passage and the text
:return: Passage
:rtype: Passage
"""
text = Text(
urn=textId,
retriever=self.endpoint
)
if metadata or prevnext:
return text.getPassagePlus(reference=subreference)
else:
return text.getPassage(reference=subreference)

def getSiblings(self, textId, subreference):
""" Retrieve the siblings of a textual node
:param textId: Text Identifier
:param subreference: Passage Reference
:return: (str, str)
"""
text = Text(
urn=textId,
retriever=self.endpoint
)
return text.getPrevNextUrn(subreference)

def getReffs(self, textId, level=1, subreference=None):
""" Retrieve the siblings of a textual node
:param textId: Text Identifier
:param level: Depth for retrieval
:param subreference: Passage Reference
:return: (str, str)
"""
text = Text(
urn=textId,
retriever=self.endpoint
)
return text.getReffs(level, subreference)

def getMetadata(self, objectId=None, **filters):
""" Request metadata about a text or a collection
:param textId: Object Identifier to filter on
:param filters: Kwargs parameters. URN and Inv are available
:return: Collection
"""
if objectId is not None:
filters["urn"] = objectId

ti = TextInventory()
ti.parse(self.endpoint.getCapabilities(**filters))
return ti
7 changes: 4 additions & 3 deletions MyCapytain/resolvers/prototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ def getSiblings(self, textId, subreference):
"""
raise NotImplementedError()

def getChildren(self, textId, reference=None, depth=None):
def getReffs(self, textId, level=1, subreference=None):
""" Retrieve the siblings of a textual node
:param textId: Text Identifier
:param reference: Passage Reference
:return: Graph
:param level: Depth for retrieval
:param subreference: Passage Reference
:return: (str, str)
"""
raise NotImplementedError()
3 changes: 0 additions & 3 deletions MyCapytain/resources/texts/api/cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ def getReffs(self, level=1, reference=None):
"""
if hasattr(self, "__depth__"):
level = level + self.depth
if not reference:
if hasattr(self, "reference"):
reference = self.reference

return self.getValidReff(level, reference)

Expand Down
19 changes: 0 additions & 19 deletions MyCapytain/retrievers/ahab.py

This file was deleted.

File renamed without changes.
Empty file added tests/resolvers/cts/__init__.py
Empty file.
102 changes: 102 additions & 0 deletions tests/resolvers/cts/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from MyCapytain.resolvers.cts.api import HttpCTSResolver
from MyCapytain.retrievers.cts5 import CTS
from MyCapytain.common.utils import xmlparser, Mimetypes, NS
from MyCapytain.resources.prototypes.text import Passage

from unittest import TestCase
from mock import MagicMock


with open("tests/testing_data/cts/getpassage.xml") as f:
GET_PASSAGE = xmlparser(f)
with open("tests/testing_data/cts/getpassageplus.xml") as f:
GET_PASSAGE_PLUS = xmlparser(f)
with open("tests/testing_data/cts/getprevnexturn.xml") as f:
NEXT_PREV = xmlparser(f)
with open("tests/testing_data/cts/getValidReff.1.1.xml") as f:
GET_VALID_REFF = xmlparser(f)
with open("tests/testing_data/cts/getCapabilities.xml") as f:
GET_CAPABILITIES = xmlparser(f)


class TestHttpCTSResolver(TestCase):
def setUp(self):
self.resolver = HttpCTSResolver(CTS("http://localhost"))
self.resolver.endpoint.getPassagePlus = MagicMock(return_value=GET_PASSAGE_PLUS)
self.resolver.endpoint.getPassage = MagicMock(return_value=GET_PASSAGE)
self.resolver.endpoint.getPrevNextUrn = MagicMock(return_value=NEXT_PREV)
self.resolver.endpoint.getValidReff = MagicMock(return_value=GET_VALID_REFF)
self.resolver.endpoint.getCapabilities = MagicMock(return_value=GET_CAPABILITIES)

def test_getPassage_full(self):
""" Test that we can get a full text """
passage = self.resolver.getPassage("urn:cts:latinLit:phi1294.phi002.perseus-lat2")

# We check we made a reroute to GetPassage request
self.resolver.endpoint.getPassage.assert_called_with(
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2"
)
self.assertIsInstance(
passage, Passage,
"GetPassage should always return passages objects"
)

children = list(passage.getReffs())

# We check the passage is able to perform further requests and is well instantiated
self.resolver.endpoint.getValidReff.assert_called_with(
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2",
level=1
)
self.assertEqual(
children[0], 'urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.1',
"Resource should be string identifiers"
)

self.assertIn(
"Hic est quem legis ille, quem requiris,", passage.export(output=Mimetypes.PLAINTEXT),
"Export Text should work correctly"
)

self.assertEqual(
passage.export(output=Mimetypes.ETREE).xpath(".//tei:l[@n='1']/text()", namespaces=NS, magic_string=False),
["Hic est quem legis ille, quem requiris, "],
"Export to Etree should give an Etree or Etree like object"
)

def test_getPassage_subreference(self):
""" Test that we can get a subreference text passage"""
passage = self.resolver.getPassage("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.1")

# We check we made a reroute to GetPassage request
self.resolver.endpoint.getPassage.assert_called_with(
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1"
)
self.assertIsInstance(
passage, Passage,
"GetPassage should always return passages objects"
)

children = list(passage.getReffs())

# We check the passage is able to perform further requests and is well instantiated
self.resolver.endpoint.getValidReff.assert_called_with(
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1",
level=1
)
self.assertEqual(
children[0], 'urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.1',
"Resource should be string identifiers"
)

self.assertIn(
"Hic est quem legis ille, quem requiris,", passage.export(output=Mimetypes.PLAINTEXT),
"Export Text should work correctly"
)

self.assertEqual(
passage.export(output=Mimetypes.ETREE).xpath(".//tei:l[@n='1']/text()", namespaces=NS, magic_string=False),
["Hic est quem legis ille, quem requiris, "],
"Export to Etree should give an Etree or Etree like object"
)

Empty file added tests/retrievers/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.

0 comments on commit 51f2ea6

Please sign in to comment.