def test_getpassage_variabletypes(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint, citation=self.citation) requests.return_value.text = GET_PASSAGE # Test with -1 _ = text.getTextualNode(subreference=Reference("1.1")) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassage", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1" }) # Test with -1 _ = text.getTextualNode(subreference=URN( "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.2")) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassage", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.2" }) # Test with -1 _ = text.getTextualNode(subreference=["1", "1", "1"]) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassage", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.1" })
def test_getpassageplus(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint) requests.return_value.text = GET_PASSAGE_PLUS # Test with -1 passage = text.getPassagePlus(reference="1.1") requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassagePlus", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1" }) self.assertIsInstance(passage, CtsPassage) self.assertEqual(str(passage.urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1") self.assertEqual( passage.xml.findall(".//{http://www.tei-c.org/ns/1.0}l[@n='1']") [0].text, "Hic est quem legis ille, quem requiris, ") self.assertEqual(text.citation.name, "book") self.assertEqual(len(text.citation), 3) # Test without reference text.getPassagePlus() requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassagePlus", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2" })
def test_reffs(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint, citation=self.citation) requests.side_effect = [ mock.Mock(text=GET_VALID_REFF), mock.Mock(text=GET_VALID_REFF), mock.Mock(text=GET_VALID_REFF) ] reffs = text.reffs self.assertEqual(len(requests.mock_calls), 3) requests.assert_called_with( 'http://services.perseids.org/remote/cts', params={'urn': 'urn:cts:latinLit:phi1294.phi002.perseus-lat2', 'request': 'GetValidReff', 'level': '3'} ) # And when no citation length text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat3", retriever=self.endpoint) requests.side_effect = [ mock.Mock(text=GET_LABEL), mock.Mock(text=GET_VALID_REFF), mock.Mock(text=GET_VALID_REFF), mock.Mock(text=GET_VALID_REFF) ] reffs = text.reffs self.assertEqual(len(requests.mock_calls), 7) requests.assert_called_with( 'http://services.perseids.org/remote/cts', params={'urn': 'urn:cts:latinLit:phi1294.phi002.perseus-lat3', 'request': 'GetValidReff', 'level': '3'} )
def test_get_prev_next_urn(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) requests.return_value.text = NEXT_PREV _prev, _next = text.getPrevNextUrn("1.1") self.assertEqual(_prev, "1.pr", "Endpoint should be called and URN should be parsed") self.assertEqual(_next, "1.2", "Endpoint should be called and URN should be parsed")
def test_getLabel(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) requests.return_value.text = GET_LABEL metadata = text.getLabel() self.assertEqual( str(metadata.get_single(RDF_NAMESPACES.CTS.label, "eng")), "Epigrammata")
def test_init(self): """ Test the __init__ parameters of CtsTextMetadata """ text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint, citation=self.citation) with self.assertRaises(MissingAttribute): CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=None) self.assertEqual(str(text.urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2") self.assertEqual(text.retriever, self.endpoint) self.assertEqual(text.citation, self.citation) text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint, citation=self.citation) self.assertIsInstance(text.metadata, Metadata)
def getSiblings(self, textId, subreference): """ Retrieve the siblings of a textual node :param textId: CtsTextMetadata Identifier :type textId: str :param subreference: CapitainsCtsPassage Reference :type subreference: str :return: Tuple of references :rtype: (str, str) """ text = CtsText(urn=textId, retriever=self.endpoint) return text.getPrevNextUrn(subreference)
def test_first_urn(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) requests.return_value.text = Get_FIRST first = text.getFirstUrn() self.assertEqual(str(first), "1.pr", "Endpoint should be called and URN should be parsed") requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetFirstUrn", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2" })
def getReffs(self, textId, level=1, subreference=None): """ Retrieve the siblings of a textual node :param textId: CtsTextMetadata Identifier :type textId: str :param level: Depth for retrieval :type level: int :param subreference: CapitainsCtsPassage Reference :type subreference: str :return: List of references :rtype: [str] """ text = CtsText(urn=textId, retriever=self.endpoint) return text.getReffs(level, subreference)
def test_init_without_citation(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) requests.return_value.text = GET_PASSAGE # Test with -1 text.getTextualNode(subreference="1.1") requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassage", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1" }) self.assertEqual(text.citation.name, "book")
def test_get_siblings(self): self.endpoint.getPassage = mock.MagicMock(return_value=GET_PASSAGE) self.endpoint.getPrevNextUrn = mock.MagicMock(return_value=NEXT_PREV) self.endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) self.endpoint.getValidReff = mock.MagicMock( return_value=GET_VALID_REFF_1_1) text = CtsText(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) passage = text.getTextualNode("1.1") self.assertEqual(passage.siblingsId, ("1.pr", "1.2"), "SiblingsId should resolve") # When next does not exist from the original resource self.endpoint.getPrevNextUrn.assert_called_with( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1")
def test_first_urn_when_empty(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) requests.return_value.text = Get_FIRST_EMPTY first = text.getFirstUrn() self.assertEqual( first, None, "Endpoint should be called and none should be returned if there is none" ) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetFirstUrn", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2" })
def test_children(self): """ Test next property, given that next information already exists or not) """ self.endpoint.getPassage = mock.MagicMock(return_value=GET_PASSAGE) self.endpoint.getPrevNextUrn = mock.MagicMock(return_value=NEXT_PREV) self.endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) self.endpoint.getValidReff = mock.MagicMock(return_value=GET_VALID_REFF_1_1) passage = CtsText( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1", retriever=self.endpoint ) self.assertEqual(len(list(passage.children)), 6) self.assertEqual( [str(x.urn) for x in passage.children], [ "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.1", "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.2", "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.3", "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.4", "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.5", "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.6" ], "CapitainsCtsPassage should be retrieved and have the correct URN" )
def test_export_fulltext(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint, citation=self.citation) requests.return_value.text = GET_PASSAGE # Test with -1 export = text.export(Mimetypes.PYTHON.ETREE) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetPassage", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2" }) self.assertEqual( export.xpath(".//tei[@n='1']/text()", magic_string=False), [], "Export should be used correctly")
def test_first_urn(self): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) passage = CtsPassage( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1", resource=GET_PASSAGE, retriever=self.endpoint) self.assertEqual(passage.firstId, "1.pr", "Endpoint should be called and URN should be parsed") self.endpoint.getFirstUrn.assert_called_with( "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1")
def test_get_last_id(self): self.endpoint.getPassage = mock.MagicMock(return_value=GET_PASSAGE) self.endpoint.getPrevNextUrn = mock.MagicMock(return_value=NEXT_PREV) self.endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) self.endpoint.getValidReff = mock.MagicMock( return_value=GET_VALID_REFF_1_1) passage = CtsText(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) # When next does not exist from the original resource self.assertEqual(str(passage.lastId), "1.1.6", "FirstId should resolve")
def test_get_reffs_contextual(self): """ Ensure getReffs works with context """ endpoint = HttpCtsRetriever(self.url) endpoint.getValidReff = mock.MagicMock(return_value=GET_VALID_REFF) text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=endpoint) passage = CtsPassage( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1", resource=GET_PASSAGE, retriever=endpoint) passage.getReffs() endpoint.getValidReff.assert_called_with( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1", level=2)
def test_siblings_raise(self): text = CtsText(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint) with self.assertRaises(NotImplementedError): text.prev with self.assertRaises(NotImplementedError): text.prevId with self.assertRaises(NotImplementedError): text.siblingsId with self.assertRaises(NotImplementedError): text.next with self.assertRaises(NotImplementedError): text.nextId
def getTextualNode(self, textId, subreference=None, prevnext=False, metadata=False): """ Retrieve a text node from the API :param textId: CtsTextMetadata Identifier :type textId: str :param subreference: CapitainsCtsPassage Reference :type subreference: str :param prevnext: Retrieve graph representing previous and next passage :type prevnext: boolean :param metadata: Retrieve metadata about the passage and the text :type metadata: boolean :return: CapitainsCtsPassage :rtype: CapitainsCtsPassage """ text = CtsText(urn=textId, retriever=self.endpoint) if metadata or prevnext: return text.getPassagePlus(reference=subreference) else: return text.getTextualNode(subreference=subreference)
def setUp(self): a = Citation(name="line") b = Citation(name="poem", child=a) self.citation = Citation(name="book", child=b) self.url = "http://services.perseids.org/remote/cts" self.endpoint = HttpCtsRetriever(self.url) self.endpoint.getPassage = mock.MagicMock(return_value=GET_PASSAGE) self.endpoint.getPrevNextUrn = mock.MagicMock(return_value=NEXT_PREV) self.endpoint.getValidReff = mock.MagicMock( return_value=GET_VALID_REFF) self.endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) self.text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint, citation=self.citation)
def test_get_first(self): self.endpoint.getPassage = mock.MagicMock(return_value=GET_PASSAGE) self.endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) passage = CtsText( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=self.endpoint ) # When next does not exist from the original resource first = passage.first self.endpoint.getFirstUrn.assert_called_with("urn:cts:latinLit:phi1294.phi002.perseus-lat2") self.endpoint.getPassage.assert_called_with(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr") self.assertEqual(first.xml, GET_PASSAGE.xpath("//tei:TEI", namespaces=XPATH_NAMESPACES)[0]) self.assertIsInstance(first, CtsPassage)
def test_first_urn_whenreference(self): endpoint = HttpCtsRetriever(self.url) endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=endpoint) passage = CtsPassage( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1", resource=GET_PASSAGE, retriever=endpoint) first = passage.getFirstUrn("1.1") endpoint.getFirstUrn.assert_called_with( "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1") self.assertEqual( first, "1.pr", "Parsing should be done and getFirstUrn should treat correctly full urn" )
def test_child_id(self): """ Test next property, given that next information already exists or not) """ self.endpoint.getPassage = mock.MagicMock(return_value=GET_PASSAGE) self.endpoint.getPrevNextUrn = mock.MagicMock(return_value=NEXT_PREV) self.endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST) self.endpoint.getValidReff = mock.MagicMock( return_value=GET_VALID_REFF_1_1) passage = CtsText( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1", retriever=self.endpoint) self.assertEqual( passage.childIds, ["1.1.1", "1.1.2", "1.1.3", "1.1.4", "1.1.5", "1.1.6"], "ChildIds should resolve")
def test_first_urn_when_empty(self): endpoint = HttpCtsRetriever(self.url) endpoint.getFirstUrn = mock.MagicMock(return_value=Get_FIRST_EMPTY) text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", retriever=endpoint) passage = CtsPassage( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1", resource=GET_PASSAGE, retriever=endpoint) first = passage.firstId endpoint.getFirstUrn.assert_called_with( "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1") self.assertEqual( first, None, "Endpoint should be called and none should be returned if there is none" )
from MyCapytain.retrievers.cts5 import HttpCtsRetriever from MyCapytain.resources.texts.remote.cts import CtsText # We set up a retriever which communicates with an API available in Leipzig retriever = HttpCtsRetriever("http://cts.dh.uni-leipzig.de/api/cts") # Given that we have other examples that shows how to work with text, # we will focus here on playing with the graph functionality of texts implementations. # We are gonna retrieve a text passage and the retrieve all its siblings in different fashion# # The main point is to find all children of the same parent. # The use case could be the following : some one want to retrieve the full text around a citation # To enhance the display a little. # We will work with the line 7 of poem 39 of book 4 of Martial's Epigrammata # The text is urn:cts:latinLit:phi1294.phi002.perseus-lat2 text = CtsText(retriever=retriever, urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2") # We retrieve up the passage target = text.getTextualNode(subreference="4.39.7") print(target.text) """ Nec quae Callaico linuntur auro, """ # The parent way : # - get to the parent, # - retrieve each node, # - print only the one which are not target parent = target.parent for node in parent.children:
def test_getvalidreff(self, requests): text = CtsText("urn:cts:latinLit:phi1294.phi002.perseus-lat2", self.endpoint, citation=self.citation) requests.return_value.text = GET_VALID_REFF # Test with -1 text.getValidReff(level=-1) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetValidReff", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "level": "3" }) # Test with level 2 text.getValidReff(level=2) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetValidReff", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "level": "2" }) # Test with no level text.getValidReff() requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetValidReff", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "level": "1" }) # Test with a ref as str text.getValidReff(reference="1.pr") requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetValidReff", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr", "level": "1" }) # Test with a ref as subreference reffs = text.getValidReff(reference=Reference("1.pr")) requests.assert_called_with( "http://services.perseids.org/remote/cts", params={ "request": "GetValidReff", "urn": "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr", "level": "1" }) # Test the parsing self.assertEqual(reffs[0], "1.pr.1")