aboutsummaryrefslogtreecommitdiffstats
path: root/test/lib/test_bsie.py
blob: 38e6f5975d0e5deef7845409ac73124bc922335f (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""

Part of the bsie test suite.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# standard imports
import os
import unittest

# bsie imports
from bsie.extractor import ExtractorBuilder
from bsie.extractor.base import SCHEMA_PREAMBLE
from bsie.lib import PipelineBuilder
from bsie.reader import ReaderBuilder
from bsie.utils import bsfs, node, ns

# objects to test
from bsie.lib.bsie import BSIE


## code ##

class TestBSIE(unittest.TestCase):
    def setUp(self):
        # reader builder
        rbuild = ReaderBuilder({})
        # extractor builder
        ebuild = ExtractorBuilder([
            {'bsie.extractor.generic.path.Path': {}},
            {'bsie.extractor.generic.stat.Stat': {}},
            {'bsie.extractor.generic.constant.Constant': dict(
                tuples=[('http://bsfs.ai/schema/Entity#author', 'Me, myself, and I')],
                schema='''
                    bse:author rdfs:subClassOf bsfs:Predicate ;
                        rdfs:domain bsfs:Entity ;
                        rdfs:range xsd:string ;
                        bsfs:unique "true"^^xsd:boolean .
                    ''',
                )},
            ])
        # build pipeline
        self.prefix = bsfs.Namespace('http://example.com/local/')
        pbuild = PipelineBuilder(self.prefix, rbuild, ebuild)
        self.pipeline = pbuild.build()

    def test_construction(self):
        # pipeline only
        lib = BSIE(self.pipeline)
        self.assertSetEqual(set(lib.principals), {
            ns.bse.filename,
            ns.bse.filesize,
            ns.bse.author,
            })
        self.assertEqual(lib.schema, bsfs.schema.from_string(SCHEMA_PREAMBLE + '''
            bse:filename rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:string ;
                bsfs:unique "false"^^xsd:boolean .

            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:integer;
                bsfs:unique "false"^^xsd:boolean .

            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "true"^^xsd:boolean .
            '''))

        # specify collect
        lib = BSIE(self.pipeline, collect={
            ns.bse.filesize,
            ns.bse.author,
            ns.bse.inexistent,
            })
        self.assertSetEqual(set(lib.principals), {
            ns.bse.filesize,
            ns.bse.author,
            })
        self.assertEqual(lib.schema, bsfs.schema.from_string(SCHEMA_PREAMBLE + '''
            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:integer;
                bsfs:unique "false"^^xsd:boolean .

            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "true"^^xsd:boolean .
            '''))
        # empty collect is disregarded
        lib = BSIE(self.pipeline, collect={})
        self.assertSetEqual(set(lib.principals), {
            ns.bse.filename,
            ns.bse.filesize,
            ns.bse.author,
            })
        self.assertEqual(lib.schema, bsfs.schema.from_string(SCHEMA_PREAMBLE + '''
            bse:filename rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:string ;
                bsfs:unique "false"^^xsd:boolean .

            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:integer;
                bsfs:unique "false"^^xsd:boolean .

            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "true"^^xsd:boolean .

            '''))

        # specify discard
        lib = BSIE(self.pipeline, discard={
            ns.bse.filesize,
            ns.bse.filename,
            ns.bse.inexistent,
            })
        self.assertSetEqual(set(lib.principals), {
            ns.bse.author,
            })
        self.assertEqual(lib.schema, bsfs.schema.from_string(SCHEMA_PREAMBLE + '''
            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "true"^^xsd:boolean .
            '''))

        # specify collect and discard
        lib = BSIE(self.pipeline,
            collect={ns.bse.filesize, ns.bse.author, ns.bse.foo, ns.bse.bar},
            discard={ns.bse.author, ns.bse.foo, ns.bse.foobar},
            )
        self.assertSetEqual(set(lib.principals), {
            ns.bse.filesize,
            })
        self.assertEqual(lib.schema, bsfs.schema.from_string(SCHEMA_PREAMBLE + '''
            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:integer;
                bsfs:unique "false"^^xsd:boolean .

            '''))


    def test_from_file(self):
        # setup
        lib = BSIE(self.pipeline)
        self.assertSetEqual(set(lib.principals), {
            ns.bse.filesize,
            ns.bse.filename,
            ns.bse.author,
            })
        content_hash = 'a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447'
        subject = node.Node(ns.bsfs.File, (self.prefix + 'file#')[content_hash])
        testfile = os.path.join(os.path.dirname(__file__), 'testfile.t')

        # from_file extracts all available triples
        self.assertSetEqual(set(lib.from_file(testfile)), {
            (subject, lib.schema.predicate(ns.bse.filename), 'testfile.t'),
            (subject, lib.schema.predicate(ns.bse.filesize), 12),
            (subject, lib.schema.predicate(ns.bse.author), 'Me, myself, and I'),
            })

        # from_file respects predicate argument
        self.assertSetEqual(set(lib.from_file(testfile, {ns.bse.filename, ns.bse.invalid})), {
            (subject, lib.schema.predicate(ns.bse.filename), 'testfile.t'),
            })


## main ##

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

## EOF ##