def generate_sub_component(parent,component_name): new_component = [] definition = (Uri(component_name), identifiers.predicates.instance_of, parent) type = (Uri(component_name), identifiers.predicates.rdf_type, identifiers.objects.sub_component) new_component.append(definition) new_component.append(type) return new_component
def setUp(self): self.env = Env() self.v_uri = Uri('http://test.triplepack/#variable') self.env.assign(self.v_uri, Value(42)) self.template = Template(Name('A'), [Name('x'), Name('y')], [ Property(Name('x'), Value(42)), Property(Uri('http://example.eg/predicate'), Name('y')) ]) self.expansion = Expansion(Name('e'), Name('A'), [Value(1), Value(2)], []) self.template.evaluate(self.env) def triple_eval(triple): (s, p, o) = triple s = s.evaluate(self.env) p = p.evaluate(self.env) o = o.evaluate(self.env) return (s, p, o) triples = self.expansion.as_triples(self.env) triples = [triple_eval(triple) for triple in triples] bindings = self.env._symbol_table templates = self.env._template_table self.pack = TriplePack(triples, bindings, templates, [])
def test_assignment_uri_uri(self): name = Identifier(Uri('http://variable/#v')) value = Identifier(Uri('http://variable/#value')) self.assertEqual(Assignment(name, value).evaluate( self.env), value.evaluate(self.env)) self.assertEqual(name.evaluate(self.env), value.evaluate(self.env))
def test_identifier_evaluate_bound_prefix(self): i = Identifier(Name('first'), Name('second')) prefix = Uri('http://first.org/#') self.env.assign(Identifier(Name('first')).evaluate(self.env), prefix) expected = Uri('http://first.org/#second') actually = i.evaluate(self.env) self.assertEqual(expected, actually)
def test_assignment_uri_uri(self): script = '<http://uri.org/> = <http://value.org>' expected = [ Assignment(Identifier(Uri('http://uri.org/')), Identifier(Uri('http://value.org'))) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_prefix_for_uri(self): data = RDFData() data.bind_prefix('test_prefix', Uri('http://prefix.org/#', None)) self.assertEqual(data.prefix_for_uri(Uri('http://prefix.org/#', None)), 'test_prefix')
def generate_component(parent,component_name): new_component = [] definition = (Uri(component_name), identifiers.predicates.definition, parent) access = (Uri(component_name), identifiers.predicates.access, identifiers.predicates.public ) type = (Uri(component_name), identifiers.predicates.rdf_type, identifiers.objects.component) new_component.append(definition) new_component.append(access) new_component.append(type) return new_component
def test_import_pragma_uri(self): script = "@use <import>" expected = [ImportPragma(Identifier(Uri('import')))] actually = self.parser.parse(script) self.assertEqual(expected, actually) script = "use <import>" expected = [ImportPragma(Identifier(Uri('import')))] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_identifier_evaluate_uri_and_symbol_parts(self): i = Identifier(Uri('first'), Name('second')) expected = Value(1) uri = Uri('firstsecond') self.assertEqual(i.evaluate(self.env), uri) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually)
def test_exactly_N_succeeds(self): with self.assertRaises(CardinalityError): ext = ExactlyN(Uri('http://example.eg/predicate'), 2) ext.run(self.pack) ext = ExactlyN(Uri('http://example.eg/predicate'), 2) add = self.pack.search( (None, Uri('http://example.eg/predicate'), None))[0] self.pack.add(add) ext.run(self.pack)
def test_triples_get_triples_by_predicate(self): self.assertEqual( self.pack.search((None, Value(1), None)), [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))]) self.assertEqual( self.pack.search((None, Uri('http://example.eg/predicate'), None)), [(Identifier(Name('e')).evaluate( self.env), Uri('http://example.eg/predicate'), Value(2))]) self.assertEqual(self.pack.search((None, Value(3), None)), [])
def test_equal(self): uri1 = Uri(rdflib.URIRef('exampleuri')) uri2 = Uri('exampleuri') uri3 = Uri(uri2) self.assertEqual(uri1, uri2, uri3) uri4 = Uri('anotherexample') self.assertNotEqual(uri4, uri1) self.assertNotEqual(uri4, uri2) self.assertNotEqual(uri4, uri3)
def test_to_rdf(self): data = RDFData() self.assertEqual(rdflib.URIRef('http://test.org/#'), data.to_rdf(Uri('http://test.org/#', None))) self.assertEqual(rdflib.URIRef(''), data.to_rdf(Uri('', None))) self.assertEqual(rdflib.Literal(42), data.to_rdf(Value(42, None))) self.assertEqual(rdflib.Literal("String"), data.to_rdf(Value("String", None))) self.assertEqual(rdflib.Literal(True), data.to_rdf(Value(True, None))) self.assertEqual(rdflib.Literal(0.12345), data.to_rdf(Value(0.12345, None)))
def test_exactly_one_fails(self): with self.assertRaises(CardinalityError): ext = ExactlyOne(Uri('http://test.eg/#notthere')) ext.run(self.pack) with self.assertRaises(CardinalityError): ext = ExactlyOne(Uri('http://example.eg/predicate')) add = self.pack.search( (None, Uri('http://example.eg/predicate'), None))[0] self.pack.add(add) ext.run(self.pack)
def test_at_least_one(self): triples = list(self.pack.triples) with self.assertRaises(CardinalityError): ext = AtLeastOne(Uri('http://test.eg/#notthere')) ext.run(self.pack) ext = AtLeastOne(Uri('http://example.eg/predicate')) ext.run(self.pack) self.assertEqual(triples, self.pack.triples)
def test_or_false_true(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = Or(false, true) triples = list(self.pack.triples) conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples)
def test_from_rdf(self): data = RDFData() self.assertEqual( Uri('http://namespace.eg/', None), data.from_rdf(rdflib.Namespace('http://namespace.eg/'))) self.assertEqual(Uri('http://uri.org/', None), data.from_rdf(rdflib.URIRef('http://uri.org/'))) self.assertEqual(Value(42, None), data.from_rdf(rdflib.Literal(42))) self.assertEqual(Value(False, None), data.from_rdf(rdflib.Literal(False))) self.assertEqual(Value("String", None), data.from_rdf(rdflib.Literal("String")))
def test_and_false_true(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = And(false, true) triples = list(self.pack.triples) with self.assertRaises(ExtensionError): conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples)
def test_add(self): data = RDFData() (s, p, o) = (Uri('http://subject.com/', None), Uri('http://predicate.com/', None), Value(123, None)) data.add(s, p, o) rdf_triple = (rdflib.URIRef('http://subject.com/'), rdflib.URIRef('http://predicate.com/'), rdflib.Literal(123)) self.assertEqual(next(data._g.triples((None, None, None))), rdf_triple)
def test_assignment_uri_boolean(self): script = '<http://uri.org/> = true' expected = [ Assignment(Identifier(Uri('http://uri.org/')), Value(True)) ] actually = self.parser.parse(script) self.assertEqual(expected, actually) script = '<http://uri.org/> = false' expected = [ Assignment(Identifier(Uri('http://uri.org/')), Value(False)) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_get_and_set_default_prefix(self): prefix = 'x' self.env.bind_prefix(prefix, Uri('http://eg/')) before = Uri(self.env._rdf._g.identifier.toPython()) self.assertEqual(before, self.env.uri) self.assertEqual(self.env.prefix, None) self.env.prefix = prefix self.env.uri = Uri('http://eg/') self.assertEqual(Uri('http://eg/'), self.env.uri) self.assertEqual('x', self.env.prefix)
def test_assignment_uri_double(self): script = '<http://uri.org/> = 0.12345' expected = [ Assignment(Identifier(Uri('http://uri.org/')), Value(0.12345)) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_assignment_uri_string(self): script = '<http://uri.org/> = "hello"' expected = [ Assignment(Identifier(Uri('http://uri.org/')), Value("hello")) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_prefix_pragma_uri(self): script = "@prefix Prefix = <http://example.eg/>" expected = [ PrefixPragma('Prefix', Identifier(Uri('http://example.eg/'))) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_triples_lookup_template(self): self.assertCountEqual( self.pack.lookup_template( self.template.identifier.evaluate(self.env)), self.template.as_triples(self.env)) self.assertCountEqual( self.pack.lookup_template(Uri('http://triplepack.org/#not')), None)
def test_assignment(self): uri = Uri('http://test.variable/#x') value = Value(12345) self.env.assign(uri, value) self.assertEqual(self.env._symbol_table.get(uri), value)
def test_assignment_name_uri(self): script = 'Name = <http://uri.org/>' expected = [ Assignment(Identifier(Name('Name')), Identifier(Uri('http://uri.org/'))) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_identifier_evaluate_bound_prefix_not_uri(self): i = Identifier(Name('first'), Name('second')) value = Value(12345) self.env.assign(Identifier(Name('first')).evaluate(self.env), value) expected = Uri('prefixfirstsecond') actually = i.evaluate(self.env) self.assertEqual(expected, actually)
def test_exactly_one_succeeds(self): triples = list(self.pack.triples) ext = ExactlyOne(Uri('http://example.eg/predicate')) ext.run(self.pack) self.assertEqual(triples, self.pack.triples)
def test_prefix_binding(self): prefix_uri = Uri('http://test.prefix.eg/') prefix = 'prefix' self.env.bind_prefix(prefix, prefix_uri) self.assertTrue('prefix' in [p for (p, n) in self.env._rdf._g.namespaces()])