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_extension_in_body_with_arg(self): script = 'A()(@extension ExtensionName(12345))' forms = self.parser.parse(script) a = forms[0] expected_template = Template(Identifier( Name('A')), [], [ExtensionPragma('ExtensionName', [Value(12345)])]) self.assertEqual(expected_template, a)
def test_expansion_in_property_with_args(self): script = 'A()(x = e is a B(12345))' forms = self.parser.parse(script) e = self.parser.parse('e is a B(12345)')[0] expected_template = Template(Identifier(Name('A')), [], [Property(Identifier(Name('x')), e)]) self.assertEqual(expected_template, forms[0])
def test_template_onearg_nobase(self): script = 'DNASequence(x)(encoding = <SBOL:IUPACDNA>)' forms = self.parser.parse(script) expected_template = Template(Identifier(Name('DNASequence')), ['x'], [ Property(Identifier(Name('encoding')), Identifier(Uri('SBOL:IUPACDNA'))) ]) self.assertCountEqual(forms, [expected_template])
def test_empty_template_noargs(self): script = 'DNASequence()(Other())' forms = self.parser.parse(script) expected_template = Template( Identifier(Name('DNASequence')), [], [Expansion(None, Identifier(Name('Other')), [], [])]) self.assertCountEqual(forms, [expected_template])
def test_template_onearg_base(self): script = 'B(x)(x = 42)\nA(x)(B(x) encoding = <SBOL:IUPACDNA>)' forms = self.parser.parse(script) expected_template = Template(Identifier(Name('A')), ['x'], [ Expansion(None, Identifier(Name('B')), [Identifier(Name('x'))], []), Property(Identifier(Name('encoding')), Identifier(Uri('SBOL:IUPACDNA'))) ]) self.assertCountEqual([forms[1]], [expected_template])
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)
def test_as_triples_empty(self): ''' Tests if triples list is empty when no parser call. ''' template = Template(Identifier(Name('x')), [], []) self.assertCountEqual(template.as_triples(self.env), [])
def test_init(self): ''' Simplest check, does Templates Identifier get added to parts list. ''' template = Template(Identifier(Name('x')), [], []) self.assertEqual(template.identifier.parts[0], Name('x'))
def test_empty_template_args_nobase(self): script = 'DNASequence(x, y, z)' forms = self.parser.parse(script) expected_template = Template(Identifier(Name('DNASequence')), ['x', 'y', 'z'], []) self.assertCountEqual(forms, [expected_template])
def test_expansion_in_body(self): script = 'A()(e is a B())' forms = self.parser.parse(script) e = self.parser.parse('e is a B()')[0] expected_template = Template(Identifier(Name('A')), [], [e]) self.assertEqual(expected_template, forms[0])