source: sasview/test/sasdataloader/test/utest_generic_file_reader_class.py @ 3ee0451

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249unittest-saveload
Last change on this file since 3ee0451 was 3ee0451, checked in by krzywon, 6 years ago

Unit test for error message handling with 'deprecated' data sets.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1"""
2    Unit tests for the generic file reader class
3"""
4
5import os
6import unittest
7import logging
8import numpy as np
9
10from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D
11from sas.sascalc.dataloader.loader import Loader
12from sas.sascalc.dataloader.file_reader_base_class import FileReader
13
14logger = logging.getLogger(__name__)
15
16
17def find(filename):
18    return os.path.join(os.path.dirname(__file__), filename)
19
20
21class GenericFileReaderTests(unittest.TestCase):
22
23    def setUp(self):
24        self.reader = TestFileReader()
25        self.bad_file = find("ACB123.txt")
26        self.good_file = find("123ABC.txt")
27        self.generic_reader = Loader()
28        self.deprecated_file_type = find("FEB18012.ASC")
29
30    def test_bad_file_path(self):
31        output = self.reader.read(self.bad_file)
32        self.assertEqual(output, [])
33
34    def test_good_file_path(self):
35        f = open(self.good_file, 'w')
36        f.write('123ABC exists!')
37        f.close()
38        output = self.reader.read(self.good_file)
39        self.assertEqual(len(output), 1)
40        self.assertEqual(output[0].meta_data["blah"], '123ABC exists!')
41
42    def test_old_file_types(self):
43        f = self.generic_reader.load(self.deprecated_file_type)
44        last_f = f[0]
45        if hasattr(last_f, "errors"):
46            self.assertEquals(len(last_f.errors), 1)
47        else:
48            self.fail("Errors did not propogate to the file properly.")
49
50    def tearDown(self):
51        if os.path.isfile(self.bad_file):
52            os.remove(self.bad_file)
53        if os.path.isfile(self.good_file):
54            os.remove(self.good_file)
55
56class TestFileReader(FileReader):
57    def get_file_contents(self):
58        """
59        Reader specific class to access the contents of the file
60        All reader classes that inherit from FileReader must implement
61        """
62        x = np.zeros(0)
63        y = np.zeros(0)
64        self.current_dataset = plottable_1D(x,y)
65        self.current_datainfo = DataInfo()
66        self.current_datainfo.meta_data["blah"] = self.nextline()
67        self.send_to_output()
Note: See TracBrowser for help on using the repository browser.