aboutsummaryrefslogtreecommitdiffstats
path: root/test/extractor/image/test_iptc.py
blob: 7efbdfe4a42c1c8bae127988b677a8db369e4b97 (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

# standard imports
import unittest

# bsie imports
from bsie.extractor import base
from bsie.matcher import nodes
from bsie.utils import bsfs, ns

# objects to test
from bsie.extractor.image.iptc import Iptc


## code ##

class TestIptc(unittest.TestCase):

    def test_eq(self):
        # identical instances are equal
        self.assertEqual(Iptc(), Iptc())
        self.assertEqual(hash(Iptc()), hash(Iptc()))
        # comparison respects type
        class Foo(): pass
        self.assertNotEqual(Iptc(), Foo())
        self.assertNotEqual(hash(Iptc()), hash(Foo()))
        self.assertNotEqual(Iptc(), 1234)
        self.assertNotEqual(hash(Iptc()), hash(1234))
        self.assertNotEqual(Iptc(), None)
        self.assertNotEqual(hash(Iptc()), hash(None))

    def test_schema(self):
        self.assertSetEqual({pred.uri for pred in Iptc().schema.predicates()}, {
            ns.bsfs.Predicate,
            ns.bse.tag,
            ns.bst.label,
            })

    def test_extract(self):
        ext = Iptc()
        subject = nodes.Entity(ucid='abc123')
        content = {
            'Iptc.Application2.Keywords': ['hello', 'world'],
            'Iptc.Application2.RecordVersion': '4',
            }
        # target tags
        t_hello = nodes.Tag(label='hello')
        t_world = nodes.Tag(label='world')

        # invalid principals are ignored
        self.assertSetEqual(set(ext.extract(subject, content, {ns.bse.filename})), set())
        # extract finds all relevant information
        self.assertSetEqual(set(ext.extract(subject, content, {ext.schema.predicate(ns.bse.tag)})), {
            (subject, ext.schema.predicate(ns.bse.tag), t_hello),
            (subject, ext.schema.predicate(ns.bse.tag), t_world),
            (t_hello, ext.schema.predicate(ns.bst.label), 'hello'),
            (t_world, ext.schema.predicate(ns.bst.label), 'world'),
            })

        # empty content is acceptable
        self.assertSetEqual(set(ext.extract(subject, {}, set(ext.principals))), set())
        # no principals is acceptable
        self.assertSetEqual(set(ext.extract(subject, content, set())), set())


## main ##

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

## EOF ##