aboutsummaryrefslogtreecommitdiffstats
path: root/test/apps/test_loader.py
blob: 21752c8d5e668b77a59167a834eb96671bfaa395 (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

# standard imports
import os
import tempfile
import unittest

# external imports
import yaml

# objects to test
from bsie.apps._loader import load_pipeline


## code ##

class TestLoader(unittest.TestCase):
    def test_load_pipeline(self):
        # config file can be empty
        config = {
            'ReaderBuilder': {},
            'ExtractorBuilder': []
            }
        # create config file
        _, path = tempfile.mkstemp(prefix='bsie-test-', suffix='.yaml')
        with open(path, 'wt') as cfile:
            yaml.dump(config, cfile)
        # pipeline contains only default predicates
        pipeline = load_pipeline(path)
        self.assertSetEqual({pred.uri for pred in pipeline.schema.predicates()}, {
            'https://schema.bsfs.io/core/Predicate',
            })

        # pipeline is built according to configured extractors
        config = {
            'ReaderBuilder': {},
            'ExtractorBuilder': [
                {'bsie.extractor.preview.Preview': {
                    'max_sides': [50],
                    }},
                {'bsie.extractor.generic.path.Path': {}},
                {'bsie.extractor.generic.constant.Constant': {
                    'schema': '''
                        bse:author rdfs:subClassOf bsfs:Predicate ;
                            rdfs:domain bsn:Entity ;
                            rdfs:range xsd:string ;
                            bsfs:unique "true"^^xsd:boolean .
                        ''',
                    'tuples': [['https://schema.bsfs.io/ie/Node/Entity#author', 'Me, myself, and I']],
                  }},
                {'bsie.extractor.image.colors_spatial.ColorsSpatial': {
                    'width': 2,
                    'height': 2,
                    'exp': 2,
                    }},
                ]
            }
        # create config file
        _, path = tempfile.mkstemp(prefix='bsie-test-', suffix='.yaml')
        with open(path, 'wt') as cfile:
            yaml.dump(config, cfile)
        # pipeline contains all defined predicates
        pipeline = load_pipeline(path)
        self.assertSetEqual({pred.uri for pred in pipeline.schema.predicates()}, {
            'https://schema.bsfs.io/ie/Node/Entity#author',
            'https://schema.bsfs.io/core/Predicate',
            'https://schema.bsfs.io/ie/Node/Entity#filename',
            'https://schema.bsfs.io/ie/Node/Entity#dirname',
            'https://schema.bsfs.io/ie/Node/Entity#colors_spatial_0658f2234a054e1dd59a14462c89f7733e019160419c796356aa831498bd0a04',
            'https://schema.bsfs.io/ie/Node/Entity#preview',
            'https://schema.bsfs.io/ie/Node/Preview#width',
            'https://schema.bsfs.io/ie/Node/Preview#height',
            'https://schema.bsfs.io/ie/Node/Preview#asset',
            })

        # config file must exist
        self.assertRaises(OSError, load_pipeline, 'invalid.yaml')


## main ##

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

## EOF ##