File: test_djvu2png.py

package info (click to toggle)
python-djvulibre 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: python: 2,449; makefile: 38; sh: 25
file content (54 lines) | stat: -rw-r--r-- 1,764 bytes parent folder | download
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
# Copyright © 2024 FriedrichFroebel
#
# This file is part of djvulibre-python.
#
# djvulibre-python is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# djvulibre-python is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.

import os
import subprocess
from importlib.util import find_spec
from tempfile import NamedTemporaryFile
from unittest import SkipTest

from tests.tools import EXAMPLES, IMAGES, TestCase


class Djvu2PngTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        if find_spec('numpy') is None:
            raise SkipTest('Package numpy not found.')
        if find_spec('cairocffi') is None and find_spec('cairo') is None:
            raise SkipTest('Cairo bindings not found.')

    def check(self, mode: str):
        with NamedTemporaryFile(suffix='.png') as outfile:
            subprocess.run(
                [
                    os.path.join(EXAMPLES, 'djvu2png'),
                    f'--{mode}',
                    os.path.join(IMAGES, 'test1.djvu'),
                    outfile.name
                ]
            )
            with open(os.path.join(IMAGES, f'test1_{mode}.png'), mode='rb') as fd:
                expected = fd.read()
            outfile.seek(0)
            self.assertEqual(expected, outfile.read())

    def test_foreground(self):
        self.check('foreground')

    def test_background(self):
        # Sample files have no background.
        pass

    def test_mask(self):
        self.check('mask')