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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# standard imports
import unittest
# bsie imports
from bsie.matcher import nodes
from bsie.utils import ns
# 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 bsn:Entity ;
rdfs:range xsd:string ;
bsfs:unique "true"^^xsd:boolean .
bse:comment rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn: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 = nodes.Entity(ucid='abc123') # 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).child(ns.bsn.Entity)
string = ext.schema.literal(ns.bsfs.Literal).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).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).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 bsn:Entity ;
rdfs:range xsd:string ;
bsfs:unique "true"^^xsd:boolean .
bse:comment rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn: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 bsn:Entity ;
rdfs:range xsd:string ;
bsfs:unique "true"^^xsd:boolean .
'''
schema_b = '''
bse:comment rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn: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 ##
|