File: utest_generic_file_reader_class.py

package info (click to toggle)
sasdata 0.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,420 kB
  • sloc: xml: 11,476; python: 8,268; makefile: 48; sh: 7
file content (103 lines) | stat: -rw-r--r-- 3,762 bytes parent folder | download | duplicates (2)
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
"""
    Unit tests for the generic file reader class
"""

import os
import unittest
import logging
import numpy as np

from sasdata.dataloader.data_info import DataInfo, plottable_1D, Data1D
from sasdata.dataloader.loader import Loader
from sasdata.dataloader.filereader import FileReader

logger = logging.getLogger(__name__)


def find(filename):
    return os.path.join(os.path.dirname(__file__), 'data', filename)


class GenericFileReaderTests(unittest.TestCase):

    def setUp(self):
        self.reader = TestFileReader()
        self.bad_file = find("ACB123.txt")
        self.good_file = find("123ABC.txt")
        self.generic_reader = Loader()
        self.deprecated_file_type = find("FEB18012.ASC")

    def test_bad_file_path(self):
        self.assertRaises(FileNotFoundError, self.reader.read,
                          self.bad_file)

    def test_good_file_path(self):
        f = open(self.good_file, 'w')
        f.write('123ABC exists!')
        f.close()
        output = self.reader.read(self.good_file)
        self.assertEqual(len(output), 1)
        self.assertEqual(output[0].meta_data["blah"], '123ABC exists!')

    def test_old_file_types(self):
        f = self.generic_reader.load(self.deprecated_file_type)
        last_f = f[0]
        if hasattr(last_f, "errors"):
            self.assertEqual(len(last_f.errors), 1)
        else:
            self.fail("Errors did not propogate to the file properly.")

    def test_same_file_unknown_extensions(self):
        # Five files, all with the same content, but different file extensions
        no_ext = find("TestExtensions")
        not_xml = find("TestExtensions.notxml")
        # Deprecated extensions
        asc_dep = find("TestExtensions.asc")
        nxs_dep = find("TestExtensions.nxs")
        # Native extension as a baseline
        xml_native = find("TestExtensions.xml")
        # Load the files and check contents
        no_ext_load = self.generic_reader.load(no_ext)
        asc_load = self.generic_reader.load(asc_dep)
        nxs_load = self.generic_reader.load(nxs_dep)
        not_xml_load = self.generic_reader.load(not_xml)
        xml_load = self.generic_reader.load(xml_native)
        self.check_unknown_extension(no_ext_load[0])
        self.check_unknown_extension(asc_load[0])
        self.check_unknown_extension(nxs_load[0])
        self.check_unknown_extension(not_xml_load[0])
        self.check_unknown_extension(xml_load[0])
        # Be sure the deprecation warning is passed with the file
        self.assertEqual(len(asc_load[0].errors), 1)
        self.assertEqual(len(nxs_load[0].errors), 0)

    def test_caller_method(self):
        xml_native = find("TestExtensions.xml")
        f_load = self.generic_reader.load(xml_native)[0]
        f_call = Loader()([xml_native])[0]
        self.assertEqual(str(f_call), str(f_load))

    def check_unknown_extension(self, data):
        self.assertTrue(isinstance(data, Data1D))
        self.assertEqual(len(data.x), 138)
        self.assertEqual(data.sample.ID, "TK49 c10_SANS")
        self.assertEqual(data.meta_data["loader"], "CanSAS XML 1D")

    def tearDown(self):
        if os.path.isfile(self.bad_file):
            os.remove(self.bad_file)
        if os.path.isfile(self.good_file):
            os.remove(self.good_file)

class TestFileReader(FileReader):
    def get_file_contents(self):
        """
        Reader specific class to access the contents of the file
        All reader classes that inherit from FileReader must implement
        """
        x = np.zeros(0)
        y = np.zeros(0)
        self.current_dataset = plottable_1D(x,y)
        self.current_datainfo = DataInfo()
        self.current_datainfo.meta_data["blah"] = self.nextline()
        self.send_to_output()