""" Part of the bsie test suite. A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ # imports import unittest # bsie imports from bsie.utils import ns from bsie.utils.node import Node # objects to test from bsie.extractor.generic.constant import Constant ## code ## class TestConstant(unittest.TestCase): def test_extract(self): schema = ''' bse:author rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; bsfs:unique "true"^^xsd:boolean . bse:comment rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; bsfs:unique "false"^^xsd:boolean . ''' tuples = [ (ns.bse.author, 'Me, myself, and I'), (ns.bse.comment, 'the quick brown fox jumps over the lazy dog.'), ] ext = Constant(schema, tuples) node = Node(ns.bsfs.Entity, '') # Blank node p_author = ext.schema.predicate(ns.bse.author) p_comment = ext.schema.predicate(ns.bse.comment) entity = ext.schema.node(ns.bsfs.Node).get_child(ns.bsfs.Entity) string = ext.schema.literal(ns.bsfs.Literal).get_child(ns.xsd.string) # baseline self.assertSetEqual(set(ext.extract(node, None, (p_author, p_comment))), {(node, p_author, 'Me, myself, and I'), (node, p_comment, 'the quick brown fox jumps over the lazy dog.')}) # predicates is respected p_foobar = ext.schema.predicate(ns.bsfs.Predicate).get_child(ns.bse.foobar, domain=entity, range=entity) self.assertSetEqual(set(ext.extract(node, None, (p_author, p_foobar))), {(node, p_author, 'Me, myself, and I')}) self.assertSetEqual(set(ext.extract(node, None, (p_comment, p_foobar))), {(node, p_comment, 'the quick brown fox jumps over the lazy dog.')}) p_barfoo = ext.schema.predicate(ns.bse.author).get_child(ns.bse.comment, domain=entity, range=string) self.assertSetEqual(set(ext.extract(node, None, (p_foobar, p_barfoo))), set()) def test_construct(self): # schema compliance schema = ''' bse:author rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; bsfs:unique "true"^^xsd:boolean . bse:comment rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; bsfs:unique "false"^^xsd:boolean . ''' # can create a schema self.assertIsInstance(Constant(schema, [ (ns.bse.author, 'Me, myself, and I'), (ns.bse.comment, 'the quick brown fox jumps over the lazy dog.'), ]), Constant) # predicates are validated self.assertRaises(KeyError, Constant, schema, [ (ns.bse.author, 'Me, myself, and I'), (ns.bse.foobar, 'foobar!')]) # FIXME: values are validated #class Foo(): pass # not string compatible #self.assertRaises(ValueError, Constant, schema, [ # (ns.bse.author, Foo())]) def test_eq(self): schema_a = ''' bse:author rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; bsfs:unique "true"^^xsd:boolean . ''' schema_b = ''' bse:comment rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; bsfs:unique "false"^^xsd:boolean . ''' tuples_a = [(ns.bse.author, 'Me, myself, and I')] tuples_b = [(ns.bse.comment, 'the quick brown fox jumps over the lazy dog.') ] # distinct instances, same data self.assertEqual( Constant(schema_a, tuples_a), Constant(schema_a, tuples_a)) self.assertEqual( hash(Constant(schema_a, tuples_a)), hash(Constant(schema_a, tuples_a))) # different data self.assertNotEqual( Constant(schema_a, tuples_a), Constant(schema_b, tuples_b)) self.assertNotEqual( hash(Constant(schema_a, tuples_a)), hash(Constant(schema_b, tuples_b))) # different objects class Foo(): pass self.assertNotEqual(Constant(schema_a, tuples_a), Foo()) self.assertNotEqual(hash(Constant(schema_a, tuples_a)), hash(Foo())) self.assertNotEqual(Constant(schema_a, tuples_a), 123) self.assertNotEqual(hash(Constant(schema_a, tuples_a)), hash(123)) self.assertNotEqual(Constant(schema_a, tuples_a), None) self.assertNotEqual(hash(Constant(schema_a, tuples_a)), hash(None)) ## main ## if __name__ == '__main__': unittest.main() ## EOF ##