def test_export_jsonld(self): b = Metadata() m3 = Metadatum("desc", [("fre", "Omelette")]) m4 = Metadatum("title", [("eng", "ttl"), ("fre", "titre")]) m5 = Metadatum("dc:editor", [("eng", "Captain Hook"), ("fre", "Capitaine Crochet")]) b[("desc", "title", "dc:editor")] = (m3, m4, m5) six.assertCountEqual( self, b.export(Mimetypes.JSON.DTS), [ { 'http://chs.harvard.edu/xmlns/cts/desc': 'Omelette', 'http://chs.harvard.edu/xmlns/cts/title': 'titre', 'http://purl.org/dc/elements/1.1/editor': "Capitaine Crochet", '@language': 'fre' }, { 'http://chs.harvard.edu/xmlns/cts/title': 'ttl', 'http://purl.org/dc/elements/1.1/editor': "Captain Hook", '@language': 'eng' } ], "JSON LD Expression should take into account prefixes" )
def test_export_jsonld_withns(self): """ With no namespace """ b = Metadata() m3 = Metadatum("desc", [("fre", "Un bon livre")], namespace=NAMESPACES.CTS) m4 = Metadatum("title", [("eng", "A title"), ("fre", "Un titre")], namespace=NAMESPACES.CTS) m5 = Metadatum("dc:editor", [("eng", "An Editor"), ("fre", "Un Editeur")]) b[("desc", "title", "dc:editor")] = (m3, m4, m5) six.assertCountEqual( self, b.export(Mimetypes.JSON.DTS.Std), [ { 'http://chs.harvard.edu/xmlns/cts/desc': 'Un bon livre', 'http://chs.harvard.edu/xmlns/cts/title': 'Un titre', 'http://purl.org/dc/elements/1.1/editor': "Un Editeur", '@language': 'fre' }, { 'http://chs.harvard.edu/xmlns/cts/title': 'A title', 'http://purl.org/dc/elements/1.1/editor': "An Editor", '@language': 'eng' } ], "JSON LD Expression should take into account prefixes" )
def test_init(self): a = Metadata() self.assertTrue(hasattr(a, "metadata"), True) self.assertTrue(isinstance(a.metadata, defaultdict)) a = Metadata(keys=["title"]) self.assertTrue(isinstance(a.metadata["title"], Metadatum))
def test_set(self): a = Metadata() a["title"] = [("eng", "Epigrams")] self.assertEqual(a["title"]["eng"], "Epigrams") self.assertEqual( a.keys(), ["title"], "Keys should be set" ) a[("desc", "label")] = ([("eng", "desc")], [("eng", "lbl"), ("fre", "label")]) self.assertEqual(a["desc"]["eng"], "desc") self.assertEqual(a["label"][("eng", "fre")], ("lbl", "label"))
def test_init(self): a = Metadata() self.assertTrue(hasattr(a, "metadata"), True) self.assertTrue(isinstance(a.metadata, defaultdict)) a = Metadata(keys=["title"]) self.assertTrue(isinstance(a.metadata["title"], Metadatum)) self.assertEqual( a.keys(), ["title"], "Keys should be set" )
def test_export_xmlRDF_withNs(self): b = Metadata() m3 = Metadatum("desc", [("fre", "Omelette")], namespace=NAMESPACES.CTS) m4 = Metadatum("title", [("eng", "ttl"), ("fre", "titre")], namespace=NAMESPACES.CTS) m5 = Metadatum("dc:editor", [("eng", "Captain Hook"), ("fre", "Capitaine Crochet")]) b[("desc", "title", "dc:editor")] = (m3, m4, m5) self.assertEqual( b.export(Mimetypes.XML.RDF), """<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description> <editor xmlns="http://purl.org/dc/elements/1.1/" xml:lang="eng">Captain Hook</editor><editor xmlns="http://purl.org/dc/elements/1.1/" xml:lang="fre">Capitaine Crochet</editor><desc xmlns="http://chs.harvard.edu/xmlns/cts/" xml:lang="fre">Omelette</desc><title xmlns="http://chs.harvard.edu/xmlns/cts/" xml:lang="eng">ttl</title><title xmlns="http://chs.harvard.edu/xmlns/cts/" xml:lang="fre">titre</title> </rdf:Description> </rdf:RDF>""", "XML/RDF Expression should take into account prefixes" )
def test_get(self): m = Metadata() m.add(RDF_NAMESPACES.CTS.title, "Title") m.add(RDF_NAMESPACES.CTS.title, "Title", lang="eng") m.add(RDF_NAMESPACES.CTS.title, "SubTitle", lang="eng") self.assertCountEqual(list(m.get(RDF_NAMESPACES.CTS.title)), [ rdflib.term.Literal('Title'), rdflib.term.Literal('Title', lang="eng"), rdflib.term.Literal('SubTitle', lang="eng") ]) self.assertCountEqual( list(m.get(RDF_NAMESPACES.CTS.title, lang="eng")), [ rdflib.term.Literal('Title', lang="eng"), rdflib.term.Literal('SubTitle', lang="eng") ])
def test_add(self): """ Test sum of two Metadata objects """ a = Metadata() m1 = Metadatum("desc", [("eng", "desc")]) m2 = Metadatum("label", [("eng", "lbl"), ("fre", "label")]) a["desc"] = m1 a["label"] = m2 b = Metadata() m3 = Metadatum("desc", [("fre", "Omelette")]) m4 = Metadatum("title", [("eng", "ttl"), ("fre", "titre")]) b[("desc", "title")] = (m3, m4) c = a + b self.assertEqual(len(c), 3) self.assertEqual(len(c["desc"]), 2)
def test_export_json(self): b = Metadata() m3 = Metadatum("desc", [("fre", "Omelette")]) m4 = Metadatum("title", [("eng", "ttl"), ("fre", "titre")]) m5 = Metadatum("dc:editor", [("eng", "Captain Hook"), ("fre", "Capitaine Crochet")]) b[("desc", "title", "dc:editor")] = (m3, m4, m5) six.assertCountEqual( self, b.export(Mimetypes.JSON.Std), {'dc:editor': {'default': 'eng', 'langs': [('eng', 'Captain Hook'), ('fre', 'Capitaine Crochet')], 'name': 'dc:editor'}, 'title': {'default': 'eng', 'langs': [('eng', 'ttl'), ('fre', 'titre')], 'name': 'title'}, 'desc': {'default': 'fre', 'langs': [('fre', 'Omelette')], 'name': 'desc'}}, "JSON LD Expression should take into account prefixes" )
def test_iter(self): a = Metadata() m1 = Metadatum("desc", [("eng", "desc")]) m2 = Metadatum("label", [("eng", "lbl"), ("fre", "label")]) a["desc"] = m1 a["label"] = m2 i = 0 d = [] d2 = [] for k, v in a: d.append(k) d2.append(v) if i == 1: break i += 1 self.assertEqual(d, ["desc", "label"]) self.assertEqual(d2, [m1, m2]) i = 0 d = [] d2 = [] for k, v in a: d.append(k) d2.append(v) break self.assertEqual(d, ["desc"]) self.assertEqual(d2, [m1]) self.assertEqual(list(a), [("desc", m1), ("label", m2)])
def __init__(self, resource=None, urn=None, parents=None, subtype="Edition"): self.resource = None self.citation = None self.lang = None self.urn = None self.docname = None self.parents = list() self.subtype = subtype self.validate = None self.metadata = Metadata( keys=["label", "description", "namespaceMapping"]) if urn is not None: self.urn = URN(urn) if parents is not None: self.parents = parents self.lang = self.parents[0].lang if resource is not None: self.setResource(resource)
def test_set(self): a = Metadata() a["title"] = [("eng", "Epigrams")] self.assertEqual(a["title"]["eng"], "Epigrams") a[("desc", "label")] = ([("eng", "desc")], [("eng", "lbl"), ("fre", "label")]) self.assertEqual(a["desc"]["eng"], "desc") self.assertEqual(a["label"][("eng", "fre")], ("lbl", "label"))
def test_export_multiple_values_JSON(self): m = Metadata() m.add(DC.contributor, "Me", lang="eng") m.add(DC.contributor, "You", lang="eng") m.add(DC.contributor, "Ich", lang="deu") m.add(DC.contributor, "Du", lang="deu") self.assertCountEqual( m.export(Mimetypes.JSON.Std), { 'http://purl.org/dc/elements/1.1/contributor': { 'deu': ['Du', 'Ich'], 'eng': ['Me', 'You'] } })
def __init__(self, identifier="", *args, **kwargs): super(Collection, self).__init__(identifier, *args, **kwargs) self.__graph__ = get_graph() self.__node__ = URIRef(identifier) self.__metadata__ = Metadata(node=self.asNode()) self.__capabilities__ = Metadata.getOr(self.asNode(), RDF_NAMESPACES.DTS.capabilities) self.graph.set((self.asNode(), RDF.type, self.TYPE_URI)) self.graph.set( (self.asNode(), RDF_NAMESPACES.DTS.model, self.MODEL_URI)) self.graph.addN([(self.asNode(), RDF_NAMESPACES.DTS.capabilities, self.capabilities.asNode(), self.graph)]) self.__parent__ = None self.__children__ = {}
def test_len(self): a = Metadata() m1 = Metadatum("desc", [("eng", "desc")]) m2 = Metadatum("label", [("eng", "lbl"), ("fre", "label")]) a["desc"] = m1 self.assertEqual(len(a), 1) a["label"] = m2 self.assertEqual(len(a), 2) a["z"] = 1.5 self.assertEqual(len(a), 2)
def __init__(self, identifier: str=None, metadata: Metadata=None): self._graph = get_graph() self._identifier = identifier self._node = BNode() self._metadata = metadata or Metadata(node=self.asNode()) self._graph.addN([ (self._node, RDF_NAMESPACES.DTS.implements, URIRef(identifier), self._graph)#, #(self.__node__, RDF_NAMESPACES.DTS.metadata, self.metadata.asNode(), self.__graph__) ])
def __init__(self, identifier="", *args, **kwargs): super(Collection, self).__init__(identifier, *args, **kwargs) self._graph = get_graph() self._node = URIRef(identifier) self._metadata = Metadata(node=self.asNode()) self.graph.set((self.asNode(), RDF.type, self.TYPE_URI)) self.graph.set((self.asNode(), RDF_NAMESPACES.DTS.model, self.MODEL_URI)) self._parent = None self._children = {}
def test_get_item(self): m = Metadata() m.add(RDF_NAMESPACES.CTS.title, "Title") m.add(RDF_NAMESPACES.CTS.title, "Title", lang="eng") m.add(RDF_NAMESPACES.CTS.title, "SubTitle", lang="eng") self.assertCountEqual(m[RDF_NAMESPACES.CTS.title], [ rdflib.term.Literal('Title'), rdflib.term.Literal('Title', lang="eng"), rdflib.term.Literal('SubTitle', lang="eng") ]) self.assertIn(m[RDF_NAMESPACES.CTS.title, "eng"], [ rdflib.term.Literal('Title', lang="eng"), rdflib.term.Literal('SubTitle', lang="eng") ])
def __init__(self, resource=None, urn=None, parents=None): self.resource = None self.urn = None self.works = defaultdict(Work) self.parents = list() self.metadata = Metadata(keys=["groupname"]) if urn is not None: self.urn = URN(urn) if parents: self.parents = parents if resource is not None: self.setResource(resource)
def __init__(self, resource=None, urn=None, parents=None): self.resource = None self.lang = None self.urn = None self.texts = defaultdict(Text) self.parents = list() self.metadata = Metadata(keys=["title"]) if urn is not None: self.urn = URN(urn) if parents is not None: self.parents = parents if resource is not None: self.setResource(resource)
def test_get(self): a = Metadata() m1 = Metadatum("desc", [("eng", "desc")]) m2 = Metadatum("label", [("eng", "lbl"), ("fre", "label")]) a[("desc", "label")] = (m1, m2) self.assertEqual(a[("desc", "label")], (m1, m2)) self.assertEqual(a[0], m1) with self.assertRaises(KeyError): z = a[2] with self.assertRaises(KeyError): z = a["textgroup"] with six.assertRaisesRegex( self, TypeError, "Only basestring or tuple instances are accepted as key"): a[3.5] = "test" with six.assertRaisesRegex(self, ValueError, "Less values than keys detected"): a[("lat", "grc")] = ["Epigrammata"]
def test_export_exclude(self): m = Metadata() b = rdflib.BNode() m.add(RDF_NAMESPACES.CTS.title, "Title") m.add(RDF_NAMESPACES.CTS.title, "Title", lang="eng") m.add(RDF_NAMESPACES.CTS.title, "SubTitle", lang="eng") m.add(RDF_NAMESPACES.CTS.description, "SubTitle", lang="eng") m.add(RDF_NAMESPACES.CTS.description, "SubTitle", lang="fre") m.add(RDF_NAMESPACES.DTS.description, "SubTitle", lang="eng") m.add(RDF_NAMESPACES.DTS.description, "SubTitle", lang="fre") m.graph.add((b, RDF_NAMESPACES.TEI.nobodycares, m.asNode())) self.assertEqual( m.export(Mimetypes.JSON.Std, exclude=[RDF_NAMESPACES.CTS.title]), { 'https://w3id.org/dts/api#description': { 'fre': 'Subtitle', 'eng': 'Subtitle' }, 'http://chs.harvard.edu/xmlns/cts/description': { 'fre': 'Subtitle', 'eng': 'Subtitle' } }) self.assertEqual( m.export(Mimetypes.JSON.Std, exclude=[RDF_NAMESPACES.CTS]), { 'https://w3id.org/dts/api#description': { 'fre': 'Subtitle', 'eng': 'Subtitle' } })
def test_remove_unlink(self): m = Metadata() b = rdflib.BNode() m.add(RDF_NAMESPACES.CTS.title, "Title") m.add(RDF_NAMESPACES.CTS.title, "Title", lang="eng") m.add(RDF_NAMESPACES.CTS.title, "SubTitle", lang="eng") m.add(RDF_NAMESPACES.CTS.description, "SubTitle", lang="eng") m.add(RDF_NAMESPACES.CTS.description, "SubTitle", lang="fre") m.graph.add((b, RDF_NAMESPACES.TEI.nobodycares, m.asNode())) self.assertCountEqual(m[RDF_NAMESPACES.CTS.title], [ rdflib.term.Literal('Title'), rdflib.term.Literal('Title', lang="eng"), rdflib.term.Literal('SubTitle', lang="eng") ]) m.remove(RDF_NAMESPACES.CTS.title) self.assertEqual(m[RDF_NAMESPACES.CTS.title], []) self.assertCountEqual(m[RDF_NAMESPACES.CTS.description], [ rdflib.term.Literal('SubTitle', lang="fre"), rdflib.term.Literal('SubTitle', lang="eng") ]) self.assertEqual(list(m.graph.subject_predicates(m.asNode())), [(b, RDF_NAMESPACES.TEI.nobodycares)]) m.unlink() self.assertEqual(list(m.graph.subject_predicates(m.asNode())), [])
def __init__(self, resource=None): self.metadata = Metadata() self.resource = None if resource is not None: self.setResource(resource)
def test_add_value(self): m = Metadata() m.add(RDF_NAMESPACES.CTS.title, "Title") self.assertEqual(m.get_single(RDF_NAMESPACES.CTS.title), rdflib.term.Literal('Title')) m.add(RDF_NAMESPACES.CTS.number, 5) self.assertEqual( m.get_single(RDF_NAMESPACES.CTS.number), rdflib.term.Literal('5', datatype=rdflib.term._XSD_INTEGER)) m.add(RDF_NAMESPACES.CTS.description, "Titre", "fre") self.assertEqual(m.get_single(RDF_NAMESPACES.CTS.description, "fre"), rdflib.term.Literal('Titre', 'fre')) self.assertIn(m.get_single(RDF_NAMESPACES.CTS.description, "eng"), [ rdflib.term.Literal('Titre', 'fre'), rdflib.term.Literal('Titre') ])