Пример #1
0
class LogicExtensionsTest(unittest.TestCase):
    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_and_true_true(self):

        true = AtLeastOne(Uri('http://example.eg/predicate', None))

        conjunction = And(true, true)

        triples = list(self.pack.triples)

        conjunction.run(self.pack)

        self.assertEqual(triples, self.pack.triples)

    def test_and_true_false(self):

        true = AtLeastOne(Uri('http://example.eg/predicate', None))
        false = AtLeastOne(Uri('http://test.eg/#notthere', None))

        conjunction = And(true, false)

        triples = list(self.pack.triples)

        with self.assertRaises(ExtensionError):
            conjunction.run(self.pack)

        self.assertEqual(triples, self.pack.triples)

    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_and_false_false(self):

        false = AtLeastOne(Uri('http://test.eg/#notthere', None))

        conjunction = And(false, false)

        triples = list(self.pack.triples)

        with self.assertRaises(ExtensionError):
            conjunction.run(self.pack)

        self.assertEqual(triples, self.pack.triples)

    def test_or_true_true(self):

        true = AtLeastOne(Uri('http://example.eg/predicate', None))

        conjunction = Or(true, true)

        triples = list(self.pack.triples)

        conjunction.run(self.pack)

        self.assertEqual(triples, self.pack.triples)

    def test_or_true_false(self):

        true = AtLeastOne(Uri('http://example.eg/predicate', None))
        false = AtLeastOne(Uri('http://test.eg/#notthere', None))

        conjunction = Or(true, false)

        triples = list(self.pack.triples)

        conjunction.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_or_false_false(self):

        false = AtLeastOne(Uri('http://test.eg/#notthere', None))

        conjunction = Or(false, false)

        triples = list(self.pack.triples)

        with self.assertRaises(ExtensionError):
            conjunction.run(self.pack)

        self.assertEqual(triples, self.pack.triples)
Пример #2
0
class EnvTest(unittest.TestCase):

    def setUp(self):
        self.env = Env()
        self.parser = Parser()

    def tearDown(self):
        None

    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()])

    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_self_uri_set(self):

        uri = Uri('setselfuri')
        self.env.self_uri = uri
        self.assertEqual(self.env.self_uri, uri)

    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_lookup(self):
        uri = Uri('http://test.variable/#x')
        value = Value(12345)

        self.env.assign(uri, value)
        self.assertEqual(self.env.lookup(uri), value)

    def test_template_binding(self):
        template = self.parser.parse('t()(x = 1 y = 2)')[0]
        uri = template.identifier.evaluate(self.env)
        with self.assertRaises(KeyError):
            self.env.lookup_template(uri)

        self.env.assign_template(uri, template.as_triples(self.env))
        self.assertEqual(self.env._template_table.get(uri, False),
                         template.as_triples(self.env))

    def test_template_lookup(self):
        template = self.parser.parse('t()(x = 1 y = 2)')[0]
        uri = template.identifier.evaluate(self.env)
        with self.assertRaises(KeyError):
            self.env.lookup_template(uri)

        self.env._template_table[uri] = template.as_triples(self.env)

        self.assertEqual(self.env.lookup_template(uri),
                         template.as_triples(self.env))

    def test_extension_binding(self):

        t = self.parser.parse('t()(@extension E() @extension F())')[0]
        uri = t.identifier.evaluate(self.env)
        extensions = t.extensions

        self.assertFalse(self.env.lookup_extensions(uri))

        self.env.assign_extensions(uri, extensions)

        self.assertEqual(extensions, self.env._extension_table.get(uri, None))

    def test_extension_lookup(self):

        t = self.parser.parse('t()(@extension E() @extension F())')[0]
        uri = t.identifier.evaluate(self.env)
        extensions = t.extensions

        self.assertFalse(self.env.lookup_extensions(uri))

        self.env._extension_table[uri] = extensions

        self.assertEqual(extensions, self.env.lookup_extensions(uri))
Пример #3
0
class TestCoreIdentifier(unittest.TestCase):

    def setUp(self):
        self.env = Env()
        self.env.current_self = Uri('')
        self.env.bind_prefix('p', Uri('prefix'))
        self.env.prefix = 'p'

    def test_identifier_equal(self):
        i1 = Identifier(Name('n1'), Name('n2'), Name('n3'))
        i2 = Identifier(Name('n1'), Name('n2'), Name('n3'))
        self.assertEqual(i1, i2)

        i3 = Identifier(Name('n1'), Name('n2'))
        self.assertNotEqual(i1, i3)

    def test_identifier_evaluate_unbound_single_name(self):
        i = Identifier(Name('first'))
        expected = Uri('prefixfirst')
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_evaluate_unbound_two_names(self):
        i = Identifier(Name('first'), Name('second'))
        expected = Uri('prefixfirstsecond')
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_evaluate_unbound_self_as_prefix(self):
        i = Identifier(Self(), Name('n'))
        expected = Identifier(Self(), Name('n'))
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_evaluate_multiple_unbound(self):
        i = Identifier(Name('n1'), Name('n2'), Name('n3'), Name('n4'))
        expected = Uri('prefixn1n2n3n4')
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_evaluate_bound_local(self):
        i = Identifier(Name('first'))
        expected = Value(1)
        self.env.assign(i.evaluate(self.env), expected)
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_evaluate_bound_prefixed(self):
        i = Identifier(Name('first'), Name('second'))
        expected = Value(1)
        self.env.assign(i.evaluate(self.env), expected)
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_evaluate_bound_multiple_parts(self):
        i = Identifier(Name('first'), Name('second'), Name('third'), Name('fourth'))
        expected = Value(1)
        self.env.assign(i.evaluate(self.env), expected)
        actually = i.evaluate(self.env)
        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_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_identifier_evaluate_bound_double_prefix(self):
        i = Identifier(Name('first'), Name('second'), Name('third'))
        prefix = Uri('http://first.org/#second#')
        uri = Identifier(Name('first'), Name('second')).evaluate(self.env)

        self.env.assign(uri, prefix)
        expected = Uri('http://first.org/#second#third')
        actually = i.evaluate(self.env)
        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_identifier_evaluate_double_bound_prefix_not_uri(self):
        i = Identifier(Name('first'), Name('second'), Name('third'))
        value = Value(12345)

        prefix_part = Identifier(Name('first'), Name('second'))
        self.env.assign(prefix_part.evaluate(self.env), value)
        expected = Uri('prefixfirstsecondthird')
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)

    def test_identifier_uri_first_unbound(self):
        i = Identifier(Uri('http://literal.eg/'), Name('literal'))
        expected = Uri('http://literal.eg/literal')
        actually = i.evaluate(self.env)
        self.assertEqual(expected, actually)
                   
Пример #4
0
class CardinalityExtensionsTest(unittest.TestCase):
    def setUp(self):
        self.env = Env()

        self.v_uri = Uri('http://test.triplepack/#variable', None)
        self.env.assign(self.v_uri, Value(42, None))

        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_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_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_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_exactly_N_fails(self):

        with self.assertRaises(CardinalityError):
            ext = ExactlyN(Uri('http://example.eg/predicate'), 2)
            ext.run(self.pack)

        with self.assertRaises(CardinalityError):
            ext = ExactlyN(Uri('http://example.eg/predicate'), 2)
            add = self.pack.search(
                (None, Uri('http://example.eg/predicate'), None))[0]
            self.pack.add(add)
            self.pack.add(add)
            ext.run(self.pack)

    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)
Пример #5
0
class TriplePackTest(unittest.TestCase):
    def setUp(self):
        self.env = Env()
        self.parser = Parser()
        self.vuri = self.parser.parse(
            '<http://test.triplepack/#variable>')[0].parts[0]
        self.env.assign(self.vuri, Value(42))

        self.template = self.parser.parse(
            ('A(x, y)(x = 42 '
             '<http://example.eg/predicate> = y)'))[0]

        self.expansion = self.parser.parse('e is a A(1, 2)')[0]
        self.template.evaluate(self.env)

        triples = self.expansion.as_triples(self.env)
        triples = [triple_eval(triple, self.env) for triple in triples]

        bindings = self.env._symbol_table
        templates = self.env._template_table

        self.pack = TriplePack(triples, bindings, templates, self.env._paths)

    def test_triples_init(self):
        exp_uri = Identifier(Name('e')).evaluate(self.env)
        triples = [(exp_uri, Value(1), Value(42)),
                   (exp_uri, Uri('http://example.eg/predicate'), Value(2))]

        self.assertCountEqual(self.pack.triples, triples)
        self.assertCountEqual(self.pack.bindings, self.env._symbol_table)
        self.assertCountEqual(self.pack.templates, self.env._template_table)

    def test_triples_lookup(self):
        self.assertEqual(self.pack.lookup(self.vuri), Value(42))
        self.assertEqual(self.pack.lookup(Uri('http://triplepack.org/#not')),
                         None)

    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_triples_get_subjects_1(self):
        self.assertEqual(self.pack.subjects,
                         set([self.expansion.identifier.evaluate(self.env)]))

    def test_triples_get_subjects_2(self):
        expansion = Expansion(Identifier(Name('f')), Identifier(Name('A')),
                              [Value(1), Value(2)], [])

        triples = expansion.as_triples(self.env)
        triples = [triple_eval(triple, self.env) for triple in triples]
        doublePack = TriplePack(self.pack.triples + triples,
                                self.pack.bindings, self.pack.templates,
                                self.pack._paths)

        self.assertEqual(
            doublePack.subjects,
            set([
                self.expansion.identifier.evaluate(self.env),
                expansion.identifier.evaluate(self.env)
            ]))

    def test_triples_get_subjects_empty(self):
        emptyPack = TriplePack([], {}, {}, [])
        self.assertEqual(emptyPack.subjects, set())

    def test_triples_get_predicates(self):
        self.assertEqual(self.pack.predicates,
                         set([Value(1),
                              Uri('http://example.eg/predicate')]))

    def test_triples_get_predicates_empty(self):
        emptyPack = TriplePack([], {}, {}, [])
        self.assertEqual(emptyPack.predicates, set())

    def test_triples_get_objects(self):
        self.assertEqual(self.pack.objects, set([Value(2), Value(42)]))

    def test_triples_get_objects_empty(self):
        emptyPack = TriplePack([], {}, {}, [])
        self.assertEqual(emptyPack.objects, set())

    def test_triples_get_triples_by_subject(self):
        expected_result = [(Identifier(Name('e')).evaluate(self.env), Value(1),
                            Value(42)),
                           (Identifier(Name('e')).evaluate(self.env),
                            Uri('http://example.eg/predicate'), Value(2))]

        self.assertEqual(
            self.pack.search(
                (Identifier(Name('e')).evaluate(self.env), None, None)),
            expected_result)

        self.assertEqual(
            self.pack.search(
                (Identifier(Name('f')).evaluate(self.env), None, None)), [])

    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_triples_get_triples_by_object(self):
        self.assertEqual(
            self.pack.search((None, None, Value(42))),
            [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))])

        self.assertEqual(self.pack.search(
            (None, None, Value(2))), [(Identifier(Name('e')).evaluate(
                self.env), Uri('http://example.eg/predicate'), Value(2))])

        self.assertEqual(self.pack.search((None, None, Value(3))), [])

    def test_triples_get_triples_exact(self):
        self.assertEqual(
            self.pack.search((Identifier(Name('e')).evaluate(self.env),
                              Value(1), Value(42))),
            [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))])

        self.assertEqual(
            self.pack.search((Value(1), Value(2, None), Value(3, None))), [])

    def test_triples_get_triples_subject_predicate(self):
        self.assertEqual(
            self.pack.search(
                (Identifier(Name('e')).evaluate(self.env), Value(1,
                                                                 None), None)),
            [(Identifier(Name('e')).evaluate(self.env), Value(
                1, None), Value(42, None))])

        self.assertEqual(
            self.pack.search((Identifier(Name('f')).evaluate(self.env),
                              Value(1, None), None)), [])
        self.assertEqual(
            self.pack.search((Identifier(Name('e')).evaluate(self.env),
                              Value(2, None), None)), [])

    def test_triples_get_triples_subject_object(self):
        self.assertEqual(
            self.pack.search((Identifier(Name('e')).evaluate(self.env), None,
                              Value(42, None))),
            [(Identifier(Name('e')).evaluate(self.env), Value(
                1, None), Value(42, None))])

        self.assertEqual(
            self.pack.search((Identifier(Name('f')).evaluate(self.env), None,
                              Value(42, None))), [])
        self.assertEqual(
            self.pack.search((Identifier(Name('e')).evaluate(self.env), None,
                              Value(41, None))), [])

    def test_triples_get_triples_predicate_object(self):
        self.assertEqual(
            self.pack.search((None, Value(1, None), Value(42, None))),
            [(Identifier(Name('e')).evaluate(self.env), Value(
                1, None), Value(42, None))])

        self.assertEqual(
            self.pack.search((None, Value(2, None), Value(42, None))), [])
        self.assertEqual(
            self.pack.search((None, Value(1, None), Value(41, None))), [])

    def test_triples_subject_has_property(self):
        self.assertTrue(
            self.pack.has(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)))
        self.assertFalse(
            self.pack.has(
                Identifier(Name('e')).evaluate(self.env), Value(2, None)))
        self.assertFalse(
            self.pack.has(
                Identifier(Name('f')).evaluate(self.env), Value(1, None)))

    def test_triples_subject_has_unique_property(self):
        self.assertTrue(
            self.pack.has_unique(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)))
        self.assertFalse(
            self.pack.has_unique(
                Identifier(Name('e')).evaluate(self.env), Value(2, None)))
        self.assertFalse(
            self.pack.has_unique(
                Identifier(Name('f')).evaluate(self.env), Value(1, None)))

        duplicatePack = TriplePack(
            self.pack.triples + [(Identifier(Name('e')).evaluate(
                self.env), Value(1, None), Value(42, None))],
            self.pack.bindings, self.pack.templates, self.pack._paths)

        self.assertFalse(
            duplicatePack.has_unique(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)))

    def test_triples_get_values_for(self):
        self.assertEqual(
            self.pack.value(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)),
            Value(42, None))
        self.assertEqual(
            self.pack.value(
                Identifier(Name('e')).evaluate(self.env), Value(2, None)),
            None)
        self.assertEqual(
            self.pack.value(
                Identifier(Name('f')).evaluate(self.env), Value(1, None)),
            None)

        duplicatePack = TriplePack(
            self.pack.triples + [(Identifier(Name('e')).evaluate(
                self.env), Value(1, None), Value(41, None))],
            self.pack.bindings, self.pack.templates, self.pack._paths)

        self.assertEqual(
            duplicatePack.value(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)),
            [Value(42, None), Value(41, None)])

    def test_triples_add(self):
        self.assertFalse(
            self.pack.has(
                Identifier(Name('e')).evaluate(self.env), Value('fake', None)))
        self.pack.add((Identifier(Name('e')).evaluate(self.env),
                       Value('fake', None), Value('added', None)))
        self.assertTrue(
            self.pack.has(
                Identifier(Name('e')).evaluate(self.env), Value('fake', None)))
        self.assertEqual(
            self.pack.value(
                Identifier(Name('e')).evaluate(self.env), Value('fake', None)),
            Value('added', None))

    def test_triples_set(self):
        self.assertEqual(
            self.pack.value(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)),
            Value(42, None))

        self.pack.set(
            Identifier(Name('e')).evaluate(self.env), Value(1, None),
            Value('set', None))

        self.assertTrue(
            self.pack.has_unique(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)))
        self.assertEqual(
            self.pack.value(
                Identifier(Name('e')).evaluate(self.env), Value(1, None)),
            Value('set', None))

        self.pack.set(
            Identifier(Name('e')).evaluate(self.env), Value('fake', None),
            Value('set', None))
        self.assertTrue(
            self.pack.has(
                Identifier(Name('e')).evaluate(self.env), Value('fake', None)))