Ignore:
Timestamp:
Aug 31, 2018 7:13:23 AM (6 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
c1cfa80
Parents:
aa1db44
Message:

Manual update of test/ directory with changes on master - SASVIEW-996

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/sasdataloader/test/utest_generic_file_reader_class.py

    rbeba407 r3a473ef  
    88import numpy as np 
    99 
    10 from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D 
     10from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D, Data1D 
     11from sas.sascalc.dataloader.loader import Loader 
     12from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 
    1113from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    1214 
     
    1416 
    1517 
     18def find(filename): 
     19    return os.path.join(os.path.dirname(__file__), filename) 
     20 
     21 
    1622class GenericFileReaderTests(unittest.TestCase): 
    1723 
    1824    def setUp(self): 
    19         self.reader = FileReader() 
    20         self.bad_file = "ACB123.txt" 
    21         self.good_file = "123ABC.txt" 
    22         self.msg = "Unable to find file at: {}\n".format(self.bad_file) 
    23         self.msg += "Please check your file path and try again." 
    24         x = np.zeros(0) 
    25         y = np.zeros(0) 
    26         self.reader.current_dataset = plottable_1D(x, y) 
    27         self.reader.current_datainfo = DataInfo() 
    28         self.reader.send_to_output() 
     25        self.reader = TestFileReader() 
     26        self.bad_file = find("ACB123.txt") 
     27        self.good_file = find("123ABC.txt") 
     28        self.generic_reader = Loader() 
     29        self.deprecated_file_type = find("FEB18012.ASC") 
    2930 
    3031    def test_bad_file_path(self): 
    31         output = self.reader.read(self.bad_file) 
    32         self.assertEqual(len(output[0].errors), 1) 
    33         self.assertEqual(output[0].errors[0], self.msg) 
     32        self.assertRaises(NoKnownLoaderException, self.reader.read, 
     33                          self.bad_file) 
    3434 
    3535    def test_good_file_path(self): 
    36         f_open = open(self.good_file, 'w') 
    37         f_open.close() 
     36        f = open(self.good_file, 'w') 
     37        f.write('123ABC exists!') 
     38        f.close() 
    3839        output = self.reader.read(self.good_file) 
    39         self.assertEqual(len(output[0].errors), 1) 
    40         self.assertEqual(output[0].errors[0], self.msg) 
     40        self.assertEqual(len(output), 1) 
     41        self.assertEqual(output[0].meta_data["blah"], '123ABC exists!') 
     42 
     43    def test_old_file_types(self): 
     44        f = self.generic_reader.load(self.deprecated_file_type) 
     45        last_f = f[0] 
     46        if hasattr(last_f, "errors"): 
     47            self.assertEquals(len(last_f.errors), 1) 
     48        else: 
     49            self.fail("Errors did not propogate to the file properly.") 
     50 
     51    def test_same_file_unknown_extensions(self): 
     52        # Five files, all with the same content, but different file extensions 
     53        no_ext = find("test_data//TestExtensions") 
     54        not_xml = find("test_data//TestExtensions.notxml") 
     55        # Deprecated extensions 
     56        asc_dep = find("test_data//TestExtensions.asc") 
     57        nxs_dep = find("test_data//TestExtensions.nxs") 
     58        # Native extension as a baseline 
     59        xml_native = find("test_data//TestExtensions.xml") 
     60        # Load the files and check contents 
     61        no_ext_load = self.generic_reader.load(no_ext) 
     62        asc_load = self.generic_reader.load(asc_dep) 
     63        nxs_load = self.generic_reader.load(nxs_dep) 
     64        not_xml_load = self.generic_reader.load(not_xml) 
     65        xml_load = self.generic_reader.load(xml_native) 
     66        self.check_unknown_extension(no_ext_load[0]) 
     67        self.check_unknown_extension(asc_load[0]) 
     68        self.check_unknown_extension(nxs_load[0]) 
     69        self.check_unknown_extension(not_xml_load[0]) 
     70        self.check_unknown_extension(xml_load[0]) 
     71        # Be sure the deprecation warning is passed with the file 
     72        self.assertEquals(len(asc_load[0].errors), 1) 
     73        self.assertEquals(len(nxs_load[0].errors), 1) 
     74 
     75    def check_unknown_extension(self, data): 
     76        self.assertTrue(isinstance(data, Data1D)) 
     77        self.assertEquals(len(data.x), 138) 
     78        self.assertEquals(data.sample.ID, "TK49 c10_SANS") 
     79        self.assertEquals(data.meta_data["loader"], "CanSAS XML 1D") 
    4180 
    4281    def tearDown(self): 
     
    4584        if os.path.isfile(self.good_file): 
    4685            os.remove(self.good_file) 
     86 
     87class TestFileReader(FileReader): 
     88    def get_file_contents(self): 
     89        """ 
     90        Reader specific class to access the contents of the file 
     91        All reader classes that inherit from FileReader must implement 
     92        """ 
     93        x = np.zeros(0) 
     94        y = np.zeros(0) 
     95        self.current_dataset = plottable_1D(x,y) 
     96        self.current_datainfo = DataInfo() 
     97        self.current_datainfo.meta_data["blah"] = self.nextline() 
     98        self.send_to_output() 
Note: See TracChangeset for help on using the changeset viewer.