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