File: test_failing_files.py

package info (click to toggle)
python-fabio 0.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,092 kB
  • sloc: python: 19,244; ansic: 1,085; makefile: 219; sh: 215
file content (115 lines) | stat: -rw-r--r-- 3,782 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Project: Fable Input Output
#             https://github.com/silx-kit/fabio
#
#    Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program 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.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Test failing files
"""

import unittest
import os
import io
import fabio
import shutil

from .utilstest import UtilsTest


class TestFailingFiles(unittest.TestCase):
    """Test failing files"""

    @classmethod
    def setUpClass(cls):
        cls.tmp_directory = os.path.join(UtilsTest.tempdir, cls.__name__)
        os.makedirs(cls.tmp_directory)
        cls.createResources(cls.tmp_directory)

    @classmethod
    def createResources(cls, directory):

        cls.txt_filename = os.path.join(directory, "test.txt")
        with io.open(cls.txt_filename, "w+t") as f:
            f.write(u"Kikoo")

        cls.bad_edf_filename = os.path.join(directory, "bad_edf.edf")
        with io.open(cls.bad_edf_filename, "w+b") as f:
            f.write(b"\r{")
            f.write(b"\x00\xFF\x99" * 10)

        cls.bad_edf2_filename = os.path.join(directory, "bad_edf2.edf")
        with io.open(cls.bad_edf2_filename, "w+b") as f:
            f.write(b"\n{\n\n}\n")
            f.write(b"\xFF\x00\x99" * 10)

        cls.bad_msk_filename = os.path.join(directory, "bad_msk.msk")
        with io.open(cls.bad_msk_filename, "w+b") as f:
            f.write(b'M\x00\x00\x00A\x00\x00\x00S\x00\x00\x00K\x00\x00\x00')
            f.write(b"\x00\xFF\x99" * 10)

        cls.bad_dm3_filename = os.path.join(directory, "bad_dm3.dm3")
        with io.open(cls.bad_dm3_filename, "w+b") as f:
            f.write(b'\x00\x00\x00\x03')
            f.write(b"\x00\xFF\x99" * 10)

        cls.bad_npy_filename = os.path.join(directory, "bad_numpy.npy")
        with io.open(cls.bad_npy_filename, "w+b") as f:
            f.write(b"\x93NUMPY")
            f.write(b"\x00\xFF\x99" * 10)

        cls.missing_filename = os.path.join(directory, "test.missing")

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.tmp_directory)

    def test_missing_file(self):
        self.assertRaises(IOError, fabio.open, self.missing_filename)

    def test_wrong_format(self):
        self.assertRaises(IOError, fabio.open, self.txt_filename)

    def test_wrong_edf(self):
        self.assertRaises(IOError, fabio.open, self.bad_edf_filename)

    def test_wrong_edf2(self):
        self.assertRaises(IOError, fabio.open, self.bad_edf_filename)

    def test_wrong_msk(self):
        self.assertRaises(ValueError, fabio.open, self.bad_msk_filename)

    def test_wrong_dm3(self):
        self.assertRaises(ValueError, fabio.open, self.bad_dm3_filename)

    def test_wrong_numpy(self):
        self.assertRaises(ValueError, fabio.open, self.bad_npy_filename)


def suite():
    loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
    testsuite = unittest.TestSuite()
    testsuite.addTest(loadTests(TestFailingFiles))
    return testsuite


if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite())