source: sasview/test/sasdataloader/test/utest_generic_file_reader_class.py @ 425feff

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

Move the deprecation check so it is always called, regardless of file loading success.

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[beba407]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
[3ee0451]11from sas.sascalc.dataloader.loader import Loader
[425feff]12from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException
[beba407]13from sas.sascalc.dataloader.file_reader_base_class import FileReader
14
15logger = logging.getLogger(__name__)
16
17
[f53d684]18def find(filename):
19    return os.path.join(os.path.dirname(__file__), filename)
20
21
[beba407]22class GenericFileReaderTests(unittest.TestCase):
23
24    def setUp(self):
[3648cbf]25        self.reader = TestFileReader()
[f53d684]26        self.bad_file = find("ACB123.txt")
27        self.good_file = find("123ABC.txt")
[3ee0451]28        self.generic_reader = Loader()
29        self.deprecated_file_type = find("FEB18012.ASC")
[beba407]30
31    def test_bad_file_path(self):
[425feff]32        self.assertRaises(NoKnownLoaderException, self.reader.read,
33                          self.bad_file)
[beba407]34
35    def test_good_file_path(self):
[3648cbf]36        f = open(self.good_file, 'w')
37        f.write('123ABC exists!')
38        f.close()
[beba407]39        output = self.reader.read(self.good_file)
[3648cbf]40        self.assertEqual(len(output), 1)
41        self.assertEqual(output[0].meta_data["blah"], '123ABC exists!')
[beba407]42
[3ee0451]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
[beba407]51    def tearDown(self):
52        if os.path.isfile(self.bad_file):
53            os.remove(self.bad_file)
54        if os.path.isfile(self.good_file):
55            os.remove(self.good_file)
[3648cbf]56
57class TestFileReader(FileReader):
58    def get_file_contents(self):
59        """
60        Reader specific class to access the contents of the file
61        All reader classes that inherit from FileReader must implement
62        """
63        x = np.zeros(0)
64        y = np.zeros(0)
65        self.current_dataset = plottable_1D(x,y)
66        self.current_datainfo = DataInfo()
67        self.current_datainfo.meta_data["blah"] = self.nextline()
[f53d684]68        self.send_to_output()
Note: See TracBrowser for help on using the repository browser.