test_factory.py 4.12 KB
Newer Older
payno's avatar
payno committed
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "24/01/2017"


import unittest
32
import os
payno's avatar
payno committed
33
from ..esrf.edfscan import EDFTomoScan
34
from ..esrf.hdf5scan import HDF5TomoScan
payno's avatar
payno committed
35
36
37
38
39
40
41
42
43
44
from ..scanfactory import ScanFactory
from .utils import UtilsTest
import tempfile


class TestScanFactory(unittest.TestCase):
    """Make sure the Scan factory is correctly working. Able to detect the
    valid scan type for a given file / directory
    """
    def test_no_scan(self):
45
46
        """Make sure an error is raised if we try yo create a scan from an
        empty dir"""
payno's avatar
payno committed
47
48
49
50
51
        scan_dir = tempfile.mkdtemp()
        with self.assertRaises(ValueError):
            ScanFactory.create_scan_object(scan_dir)

    def test_scan_edf(self):
52
53
        """can we create a TomoScanBase object from a folder containing a
        valid .edf acquisition"""
payno's avatar
payno committed
54
55
56
57
        scan_dir = UtilsTest.getDataset('test10')
        scan = ScanFactory.create_scan_object(scan_dir)
        self.assertTrue(isinstance(scan, EDFTomoScan))

58
59
60
    def test_one_nx(self):
        """Can we create a TomoScanBase from a .nx master file containing
        one acquisition"""
61
62
63
        file_name = 'frm_edftomomill_oneentry.nx'
        master_file = UtilsTest.getH5Dataset(file_name)
        scan = ScanFactory.create_scan_object(master_file)
64
        self.assertTrue(isinstance(scan, HDF5TomoScan))
65
66
        self.assertEqual(scan.path, os.path.dirname(master_file))
        self.assertEqual(scan.master_file, master_file)
payno's avatar
payno committed
67
        self.assertEqual(scan.entry, 'entry')
68

69
70
71
72
73
74
75
76
    def test_one_two_nx(self):
        """Can we create a TomoScanBase from a .nx master file containing
        two acquisitions"""
        master_file = UtilsTest.getH5Dataset('frm_edftomomill_twoentries.nx')
        scan = ScanFactory.create_scan_object(master_file)
        self.assertTrue(isinstance(scan, HDF5TomoScan))
        self.assertEqual(scan.path, os.path.dirname(master_file))
        self.assertEqual(scan.master_file, master_file)
payno's avatar
payno committed
77
        self.assertEqual(scan.entry, 'entry0000')
78

79
80
81
82
83
84
    def test_two_nx(self):
        """Can we create two TomoScanBase from a .nx master file containing
        two acquisitions using the ScanFactory"""
        master_file = UtilsTest.getH5Dataset('frm_edftomomill_twoentries.nx')
        scans = ScanFactory.create_scan_objects(master_file)
        self.assertEqual(len(scans), 2)
payno's avatar
payno committed
85
        for scan, scan_entry in zip(scans, ('entry0000', 'entry0001')):
86
87
88
89
            self.assertTrue(isinstance(scan, HDF5TomoScan))
            self.assertEqual(scan.path, os.path.dirname(master_file))
            self.assertEqual(scan.master_file, master_file)
            self.assertEqual(scan.entry, scan_entry)
90

payno's avatar
payno committed
91
92
93
94
95
96
97
98
99
100

def suite():
    test_suite = unittest.TestSuite()
    for ui in (TestScanFactory, ):
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


if __name__ == '__main__':
    unittest.main(defaultTest="suite")