source: sasview/test/sasdataloader/test/utest_extension_registry.py @ f53d684

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f53d684 was f53d684, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

Make tests work from any directory and functional without special runner script (#124)

  • Property mode set to 100644
File size: 2.9 KB
Line 
1"""
2    Unit tests for loading data files using the extension registry
3"""
4
5import logging
6import unittest
7import os
8import shutil
9import numpy as np
10
11from sas.sascalc.dataloader.loader import Registry as Loader
12
13logger = logging.getLogger(__name__)
14
15
16def find(filename):
17    return os.path.join(os.path.dirname(__file__), filename)
18
19
20class ExtensionRegistryTests(unittest.TestCase):
21
22    def setUp(self):
23        self.valid_file = find("valid_cansas_xml.xml")
24        self.valid_file_wrong_known_ext = find("valid_cansas_xml.txt")
25        self.valid_file_wrong_unknown_ext = find("valid_cansas_xml.xyz")
26        shutil.copyfile(self.valid_file, self.valid_file_wrong_known_ext)
27        shutil.copyfile(self.valid_file, self.valid_file_wrong_unknown_ext)
28        self.invalid_file = find("cansas1d_notitle.xml")
29
30        self.loader = Loader()
31
32    def test_wrong_known_ext(self):
33        """
34        Load a valid CanSAS XML file that has the extension '.txt', which is in
35        the extension registry. Compare the results to loading the same file
36        with the extension '.xml'
37        """
38        correct = self.loader.load(self.valid_file)
39        wrong_ext = self.loader.load(self.valid_file_wrong_known_ext)
40        self.assertEqual(len(correct), 1)
41        self.assertEqual(len(wrong_ext), 1)
42        correct = correct[0]
43        wrong_ext = wrong_ext[0]
44
45        self.assertTrue(np.all(correct.x == wrong_ext.x))
46        self.assertTrue(np.all(correct.y == wrong_ext.y))
47        self.assertTrue(np.all(correct.dy == wrong_ext.dy))
48
49    def test_wrong_unknown_ext(self):
50        """
51        Load a valid CanSAS XML file that has the extension '.xyz', which isn't
52        in the extension registry. Compare the results to loading the same file
53        with the extension '.xml'
54        """
55        correct = self.loader.load(self.valid_file)
56        wrong_ext = self.loader.load(self.valid_file_wrong_unknown_ext)
57        self.assertEqual(len(correct), 1)
58        self.assertEqual(len(wrong_ext), 1)
59        correct = correct[0]
60        wrong_ext = wrong_ext[0]
61
62        self.assertTrue(np.all(correct.x == wrong_ext.x))
63        self.assertTrue(np.all(correct.y == wrong_ext.y))
64        self.assertTrue(np.all(correct.dy == wrong_ext.dy))
65
66    def test_data_reader_exception(self):
67        """
68        Load a CanSAS XML file that doesn't meet the schema, and check errors
69        are set correctly
70        """
71        data = self.loader.load(self.invalid_file)
72        self.assertEqual(len(data), 1)
73        data = data[0]
74        self.assertEqual(len(data.errors), 1)
75
76        err_msg = data.errors[0]
77        self.assertTrue("does not fully meet the CanSAS v1.x specification" in err_msg)
78
79    def tearDown(self):
80        if os.path.isfile(self.valid_file_wrong_known_ext):
81            os.remove(self.valid_file_wrong_known_ext)
82        if os.path.isfile(self.valid_file_wrong_unknown_ext):
83            os.remove(self.valid_file_wrong_unknown_ext)
Note: See TracBrowser for help on using the repository browser.