def test_vocabulary_is_set_of_names_without_datatypes(self): self.assertEqual(self.graph.vocabulary(), {URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), URI('http://xmlns.com/foaf/0.1/name'), URI('http://xmlns.com/foaf/0.1/knows'), Literal("John Lennon", XSD.string)})
def setUp(self): self.triples = {(URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), URI('http://www.example.org/staffid/85740')), (URI('http://www.example.org/staffid/85740'), URI('http://xmlns.com/foaf/0.1/name'), Literal("John Lennon", XSD.string))} self.graph = Graph(self.triples)
def test_names_is_set_of_uris_and_literals(self): self.assertEqual(self.graph.names(), {URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), URI('http://xmlns.com/foaf/0.1/name'), URI('http://xmlns.com/foaf/0.1/knows'), Literal("John Lennon", XSD.string), XSD.string})
def test_not_equal_to_frozenset_with_different_triples(self): triples = {(URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), URI('http://www.example.org/staffid/85740')), (URI('http://www.example.org/staffid/85740'), URI('http://xmlns.com/foaf/0.1/name'), Literal("John Doe", XSD.string))} self.assertNotEqual(self.graph, frozenset(triples))
def test_not_equal_to_graph_with_different_number_of_unground_triples(self): graph = Graph({(URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), URI('john')), (URI('john'), URI('http://xmlns.com/foaf/0.1/name'), Literal("John Lennon", XSD.string))}) self.assertNotEqual(self.graph, graph) self.assertNotEqual(graph, self.graph)
def _init(self): super()._init() self.uri = URI(QName(self.tag)) self.subject = None self.base_uri = URI(self.base) if self.base is not None else None self.language = self.attrib.get(QName(XML, 'lang')) if self.language is None: parent = self.getparent() if parent is not None: self.language = parent.language
def test_not_equal_to_graph_without_valid_bijection(self): graph = Graph({(URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), BlankNode('john')), (BlankNode('john'), URI('http://xmlns.com/foaf/0.1/name'), Literal("John Lennon", XSD.string)), (BlankNode('paul'), URI('http://xmlns.com/foaf/0.1/knows'), BlankNode('john'))}) self.assertNotEqual(self.graph, graph) self.assertNotEqual(graph, self.graph)
def from_element(cls, element): type = URI(QName(element.tag)) uri = URI(element.get(QName(RDF, 'about'))) type_class = cls.TYPE_MAP.get(type, cls) if cls is type_class: test = cls(type, uri) test.status = cls._status(element) test.description = cls._description(element) test.warning = cls._warning(element) else: test = type_class.from_element(element) return test
def _property_attrs(self, element): # 2.5 Property Attributes for attr, value in element.items(): if attr not in _XML_ATTRS: predicate = URI(QName(attr)) if predicate not in self.ILLEGAL_PROPERTY_ATTRS: if predicate != RDF.type: object_ = PlainLiteral(value, element.language) else: object_ = URI(value) yield (element.subject, predicate, object_) elif predicate == RDF.li: raise ParseError("rdf:li is not allowed as attribute")
def test_matches_only_typed_literals(self): self.assertFalse(BlankNode() in self.type) self.assertFalse(URI('test') in self.type) self.assertFalse(PlainLiteral('1.5') in self.type) self.assertFalse(PlainLiteral('1.5', 'en') in self.type) self.assert_(TypedLiteral('1.5', XSD.float) in self.type) self.assert_(TypedLiteral('1.5', XSD.string) in self.type)
def __init__(self, lexical_form, datatype): super().__init__(lexical_form) if isinstance(datatype, str): if not isinstance(datatype, URI): datatype = URI(datatype) self.datatype = datatype else: raise TypeError("datatype must be a string")
def _empty_property(self, element, parent, ids): # 7.2.21 Production emptyPropertyElt id_ = self._id(element, ids) literal_attrs = _XML_ATTRS | {QName(RDF, 'ID')} if all(attr in literal_attrs for attr in element.keys()): object_ = PlainLiteral("", element.language) triple = (parent.subject, element.uri, object_) yield triple if id_ is not None: for triple in self._reify(id_, triple): yield triple else: resource = element.attrib.get(QName(RDF, 'resource')) node_id = element.attrib.get(QName(RDF, 'nodeID')) if resource is not None: if node_id is None: object_ = self._uri(resource, element.base_uri) else: raise ParseError elif node_id is not None: if _NCNAME.match(node_id): object_ = BlankNode(node_id) else: raise ParseError( "rdf:nodeID does not match NCName: {!r}".format( node_id)) else: object_ = BlankNode() triple = (parent.subject, element.uri, object_) yield triple if id_ is not None: for triple in self._reify(id_, triple): yield triple subject = object_ property_attrs = set(element.keys()) property_attrs -= literal_attrs | { QName(RDF, 'resource'), QName(RDF, 'nodeID') } for attr in property_attrs: predicate = URI(QName(attr)) if predicate in self.XML_TERMS: continue elif predicate in self.ILLEGAL_PROPERTY_ATTRS: raise ParseError value = element.get(attr) if predicate != RDF.type: object_ = PlainLiteral(value, element.language) else: object_ = self._uri(value, element.base_uri) yield (subject, predicate, object_)
def test_compares_to_uri_lexicographically(self): self.assert_( URI('http://script.example/Latin') < URI( 'http://script.example/Кириллица') < URI( 'http://script.example/漢字') < self.uri) self.assert_(self.uri > URI('http://script.example/漢字') > URI( 'http://script.example/Кириллица') > URI( 'http://script.example/Latin'))
def _literal_property(self, element, parent, ids): # 7.2.16 Production literalPropertyElt datatype = element.get(QName(RDF, 'datatype')) if datatype is not None: object_ = TypedLiteral(element.text, URI(datatype)) else: object_ = PlainLiteral(element.text, element.language) triple = (parent.subject, element.uri, object_) yield triple id_ = self._id(element, ids) if id_ is not None: # 7.3 Reification Rules for triple in self._reify(id_, triple): yield triple
def setUp(self): self.triples = {(URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), BlankNode('john')), (BlankNode('john'), URI('http://xmlns.com/foaf/0.1/name'), Literal("John Lennon", XSD.string)), (BlankNode('john'), URI('http://xmlns.com/foaf/0.1/knows'), BlankNode('paul'))} self.bijection = {(URI('http://www.example.org/index.html'), URI('http://purl.org/dc/elements/1.1/creator'), BlankNode('lennon')), (BlankNode('lennon'), URI('http://xmlns.com/foaf/0.1/name'), Literal("John Lennon", XSD.string)), (BlankNode('lennon'), URI('http://xmlns.com/foaf/0.1/knows'), BlankNode('starr'))} self.graph = Graph(self.triples)
def __getattr__(self, local_name): return URI(self + local_name)
def __getitem__(self, local_name): if isinstance(local_name, str): return URI(self + local_name) else: return super().__getitem__(local_name)
def test_getitem_returns_new_uri(self): self.assertEqual(self.namespace['test'], URI('http://example.org/test'))
def test_getattr_returns_new_uri(self): self.assertEqual(self.namespace.test, URI('http://example.org/test'))
def __init__(self, type, uri=None): self.type = URI(type) self.uri = URI(uri) if uri is not None else uri
def test_compares_equal_to_uri(self): self.assertEqual(self.namespace, URI('http://example.org/'))
def _datatype_support(cls, element): for child in element.findall(str(QName(TEST, 'datatypeSupport'))): uri = child.get(QName(RDF, 'resource')) if uri is not None: yield URI(uri)
def test_calling_blank_node_with_different_context_returns_different_blank_node(self): context = Context() bnode_1 = self.subjects.blank_node(URI('test'), self.context) bnode_2 = self.subjects.blank_node(URI('test'), context) self.assertNotEqual(bnode_1, bnode_2)
def test_datatype_is_converted_to_uri(self): literal = TypedLiteral("1", "string") self.assert_(isinstance(literal.datatype, URI)) self.assertEqual(literal.datatype, URI("string"))
def _entailment_rules(cls, element): for child in element.findall(str(QName(TEST, 'entailmentRules'))): uri = child.get(QName(RDF, 'resource')) if uri is not None: yield URI(uri)
def test_hash_not_equal_to_plain_literal_with_language(self): literal = TypedLiteral("1", URI('en')) self.assertNotEqual(hash(literal), hash(PlainLiteral("1", 'en')))
def test_compares_greater_than_uri(self): uri = URI('http://example.org/') self.assert_(self.literal > uri) self.assert_(not self.literal < uri)
def _uri(self, uri, base_uri=None): if base_uri and not uri: base_uri = base_uri.rsplit('#', 1)[0] return URI(urllib.parse.urljoin(base_uri or '', uri))
def test_nodes_is_set_of_subjects_and_objects(self): self.assertEqual(self.graph.nodes(), {URI('http://www.example.org/index.html'), BlankNode('john'), BlankNode('paul'), Literal("John Lennon", XSD.string)})
def test_nodes_is_set_of_subjects_and_objects(self): self.assertEqual(self.graph.nodes(), {URI('http://www.example.org/index.html'), URI('http://www.example.org/staffid/85740'), Literal("John Lennon", XSD.string)})