aboutsummaryrefslogtreecommitdiffstats
path: root/test/lib/test_pipeline.py
blob: 8fecc74cbd36e9e06072835f7d41053e0040f264 (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
"""

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

# bsie imports
from bsie.utils import bsfs, errors, node, ns
import bsie.extractor.generic.constant
import bsie.extractor.generic.path
import bsie.extractor.generic.stat
import bsie.reader.path
import bsie.reader.stat

# objects to test
from bsie.lib.pipeline import Pipeline


## code ##

class TestPipeline(unittest.TestCase):
    def setUp(self):
        # constant A
        csA = '''
            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:string ;
                bsfs:unique "true"^^xsd:boolean .
            '''
        tupA = [('http://bsfs.ai/schema/Entity#author', 'Me, myself, and I')]
        # constant B
        csB = '''
            bse:rating rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:File ;
                rdfs:range xsd:integer ;
                bsfs:unique "true"^^xsd:boolean .
            '''
        tupB = [('http://bsfs.ai/schema/Entity#rating', 123)]
        # extractors/readers
        self.ext2rdr = {
            bsie.extractor.generic.path.Path(): bsie.reader.path.Path(),
            bsie.extractor.generic.stat.Stat(): bsie.reader.stat.Stat(),
            bsie.extractor.generic.constant.Constant(csA, tupA): None,
            bsie.extractor.generic.constant.Constant(csB, tupB): None,
        }
        self.prefix = bsfs.Namespace('http://example.com/local/')

    def test_essentials(self):
        pipeline = Pipeline(self.prefix, self.ext2rdr)
        self.assertEqual(str(pipeline), 'Pipeline')
        self.assertEqual(repr(pipeline), 'Pipeline(...)')

    def test_equality(self):
        pipeline = Pipeline(self.prefix, self.ext2rdr)
        # a pipeline is equivalent to itself
        self.assertEqual(pipeline, pipeline)
        self.assertEqual(hash(pipeline), hash(pipeline))
        # identical builds are equivalent
        self.assertEqual(pipeline, Pipeline(self.prefix, self.ext2rdr))
        self.assertEqual(hash(pipeline), hash(Pipeline(self.prefix, self.ext2rdr)))

        # equivalence respects prefix
        self.assertNotEqual(pipeline, Pipeline(bsfs.URI('http://example.com/global/ent#'), self.ext2rdr))
        self.assertNotEqual(hash(pipeline), hash(Pipeline(bsfs.URI('http://example.com/global/ent#'), self.ext2rdr)))
        # equivalence respects extractors/readers
        ext2rdr = {ext: rdr for idx, (ext, rdr) in enumerate(self.ext2rdr.items()) if idx % 2 == 0}
        self.assertNotEqual(pipeline, Pipeline(self.prefix, ext2rdr))
        self.assertNotEqual(hash(pipeline), hash(Pipeline(self.prefix, ext2rdr)))

        # equivalence respects schema
        p2 = Pipeline(self.prefix, self.ext2rdr)
        p2._schema = bsfs.schema.Schema()
        self.assertNotEqual(pipeline, p2)
        self.assertNotEqual(hash(pipeline), hash(p2))

        # not equal to other types
        class Foo(): pass
        self.assertNotEqual(pipeline, Foo())
        self.assertNotEqual(hash(pipeline), hash(Foo()))
        self.assertNotEqual(pipeline, 123)
        self.assertNotEqual(hash(pipeline), hash(123))
        self.assertNotEqual(pipeline, None)
        self.assertNotEqual(hash(pipeline), hash(None))


    def test_call(self):
        # build pipeline
        pipeline = Pipeline(self.prefix, self.ext2rdr)
        # build objects for tests
        content_hash = 'a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447'
        subject = node.Node(ns.bsfs.File, (self.prefix + 'file#')[content_hash])
        testfile = os.path.join(os.path.dirname(__file__), 'testfile.t')
        p_filename = pipeline.schema.predicate(ns.bse.filename)
        p_filesize = pipeline.schema.predicate(ns.bse.filesize)
        p_author = pipeline.schema.predicate(ns.bse.author)
        p_rating = pipeline.schema.predicate(ns.bse.rating)
        entity = pipeline.schema.node(ns.bsfs.File)
        p_invalid = pipeline.schema.predicate(ns.bsfs.Predicate).child(ns.bse.foo, range=entity)

        # extract given predicates
        self.assertSetEqual(set(pipeline(testfile, {p_filename, p_filesize})), {
            (subject, p_filename, 'testfile.t'),
            (subject, p_filesize, 12),
            })
        self.assertSetEqual(set(pipeline(testfile, {p_author})), {
            (subject, p_author, 'Me, myself, and I'),
            })
        self.assertSetEqual(set(pipeline(testfile, {p_filename})), {
            (subject, p_filename, 'testfile.t'),
            })
        self.assertSetEqual(set(pipeline(testfile, {p_filesize})), {
            (subject, p_filesize, 12),
            })
        # extract all predicates
        self.assertSetEqual(set(pipeline(testfile)), {
            (subject, p_filename, 'testfile.t'),
            (subject, p_filesize, 12),
            (subject, p_author, 'Me, myself, and I'),
            (subject, p_rating, 123),
            })
        # invalid predicate
        self.assertSetEqual(set(pipeline(testfile, {p_invalid})), set())
        # valid/invalid predicates mixed
        self.assertSetEqual(set(pipeline(testfile, {p_filename, p_invalid})), {
            (subject, p_filename, 'testfile.t'),
            })
        # invalid path
        self.assertRaises(FileNotFoundError, list, pipeline('inexistent_file'))
        # FIXME: unreadable file (e.g. permissions error)

    def test_call_reader_err(self):
        class FaultyReader(bsie.reader.path.Path):
            def __call__(self, path):
                raise errors.ReaderError('reader error')

        pipeline = Pipeline(self.prefix, {bsie.extractor.generic.path.Path(): FaultyReader()})
        with self.assertLogs(logging.getLogger('bsie.lib.pipeline'), logging.ERROR):
            testfile = os.path.join(os.path.dirname(__file__), 'testfile.t')
            p_filename = pipeline.schema.predicate(ns.bse.filename)
            self.assertSetEqual(set(pipeline(testfile, {p_filename})), set())

    def test_call_extractor_err(self):
        class FaultyExtractor(bsie.extractor.generic.path.Path):
            def extract(self, subject, content, predicates):
                raise errors.ExtractorError('extractor error')

        pipeline = Pipeline(self.prefix, {FaultyExtractor(): bsie.reader.path.Path()})
        with self.assertLogs(logging.getLogger('bsie.lib.pipeline'), logging.ERROR):
            testfile = os.path.join(os.path.dirname(__file__), 'testfile.t')
            p_filename = pipeline.schema.predicate(ns.bse.filename)
            self.assertSetEqual(set(pipeline(testfile, {p_filename})), set())

    def test_predicates(self):
        # build pipeline
        pipeline = Pipeline(self.prefix, self.ext2rdr)
        #
        self.assertSetEqual(set(pipeline.principals), {
            pipeline.schema.predicate(ns.bse.filename),
            pipeline.schema.predicate(ns.bse.filesize),
            pipeline.schema.predicate(ns.bse.author),
            pipeline.schema.predicate(ns.bse.rating),
            })


## main ##

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

## EOF ##