source: sasview/test/sasdataloader/test/utest_generic_file_reader_class.py @ 0675e6b

Last change on this file since 0675e6b was 0675e6b, checked in by Stuart Prescott <stuart@…>, 7 years ago

Fix tests to find resources without changing dir

Test resources are in the same directory (or a child directory) of the test
code so the code can locate the resources itself rather than relying on the
tests being run from a certain location. This allows the tests to be used to
test the installed code rather than the current checkout and also allows
automated test discovery with tools like py.test to be used, running all
tests at once without a runner.

  • Property mode set to 100644
File size: 1.6 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.file_reader_base_class import FileReader
12
13logger = logging.getLogger(__name__)
14
15
16def find(filename):
17    return os.path.join(os.path.dirname(__file__), filename)
18
19
20class GenericFileReaderTests(unittest.TestCase):
21
22    def setUp(self):
23        self.reader = TestFileReader()
24        self.bad_file = find("ACB123.txt")
25        self.good_file = find("123ABC.txt")
26
27    def test_bad_file_path(self):
28        output = self.reader.read(self.bad_file)
29        self.assertEqual(output, [])
30
31    def test_good_file_path(self):
32        f = open(self.good_file, 'w')
33        f.write('123ABC exists!')
34        f.close()
35        output = self.reader.read(self.good_file)
36        self.assertEqual(len(output), 1)
37        self.assertEqual(output[0].meta_data["blah"], '123ABC exists!')
38
39    def tearDown(self):
40        if os.path.isfile(self.bad_file):
41            os.remove(self.bad_file)
42        if os.path.isfile(self.good_file):
43            os.remove(self.good_file)
44
45class TestFileReader(FileReader):
46    def get_file_contents(self):
47        """
48        Reader specific class to access the contents of the file
49        All reader classes that inherit from FileReader must implement
50        """
51        x = np.zeros(0)
52        y = np.zeros(0)
53        self.current_dataset = plottable_1D(x,y)
54        self.current_datainfo = DataInfo()
55        self.current_datainfo.meta_data["blah"] = self.nextline()
56        self.send_to_output()
Note: See TracBrowser for help on using the repository browser.