aboutsummaryrefslogtreecommitdiffstats
path: root/test/reader/preview/test_rawpy.py
blob: ed35f53c5f78d9c22aff7a6a2f0d414f6e636668 (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
"""

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

# external imports
import PIL.Image

# bsie imports
from bsie.utils import errors

# objects to test
from bsie.reader.preview._rawpy import RawpyPreviewReader


## code ##

class TestRawpyPreviewReader(unittest.TestCase):
    def setUp(self):
        if __package__ is None or __package__ == '': # direct call or local discovery
            importlib.import_module('load_nef', __package__).get()
        else: # parent discovery
            importlib.import_module('.load_nef', __package__).get()

    def test_call(self):
        rdr = RawpyPreviewReader()
        # raises exception when image cannot be read
        self.assertRaises(errors.ReaderError, rdr,
            os.path.join(os.path.dirname(__file__), 'invalid.nef'))
        # raises exception when image has invalid type
        self.assertRaises(errors.UnsupportedFileFormatError, rdr,
            os.path.join(os.path.dirname(__file__), 'invalid.jpg'))
        self.assertRaises(errors.UnsupportedFileFormatError, rdr,
            os.path.join(os.path.dirname(__file__), 'invalid.foo'))
        # proper file produces a generator
        gen = rdr(os.path.join(os.path.dirname(__file__), 'testimage.nef'))
        self.assertIsInstance(gen, partial)
        # generator produces an image
        img = gen(10)
        self.assertIsInstance(img, PIL.Image.Image)
        self.assertEqual(img.size, (10, 7))
        self.assertEqual(sum(band for pix in img.getdata() for band in pix), 15269)
        # cleanup
        img.close()


## main ##

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

## EOF ##