aboutsummaryrefslogtreecommitdiffstats
path: root/test/extractor/generic/test_constant.py
blob: 7fdb8ac8789b14d55d79cefcd260cb0820e260d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""

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 ;
                owl:maxCardinality "1"^^xsd:number .
            bse:comment rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                owl:maxCardinality "INF"^^xsd:number .
            '''
        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 ;
                owl:maxCardinality "1"^^xsd:number .
            bse:comment rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                owl:maxCardinality "INF"^^xsd:number .
            '''
        # 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())])



## main ##

if __name__ == '__main__':
    unittest.main()

## EOF ##