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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since dcb91cf was dcb91cf, checked in by lewis, 7 years ago

Make suggested changes

  • Property mode set to 100644
File size: 2.8 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
15class ExtensionRegistryTests(unittest.TestCase):
16
17    def setUp(self):
18        self.valid_file = "valid_cansas_xml.xml"
19        self.valid_file_wrong_known_ext = "valid_cansas_xml.txt"
20        self.valid_file_wrong_unknown_ext = "valid_cansas_xml.xyz"
21        shutil.copyfile(self.valid_file, self.valid_file_wrong_known_ext)
22        shutil.copyfile(self.valid_file, self.valid_file_wrong_unknown_ext)
23        self.invalid_file = "cansas1d_notitle.xml"
24
25        self.loader = Loader()
26
27    def test_wrong_known_ext(self):
28        """
29        Load a valid CanSAS XML file that has the extension '.txt', which is in
30        the extension registry. Compare the results to loading the same file
31        with the extension '.xml'
32        """
33        correct = self.loader.load(self.valid_file)
34        wrong_ext = self.loader.load(self.valid_file_wrong_known_ext)
35        self.assertEqual(len(correct), 1)
36        self.assertEqual(len(wrong_ext), 1)
37        correct = correct[0]
38        wrong_ext = wrong_ext[0]
39
40        self.assertTrue(np.all(correct.x == wrong_ext.x))
41        self.assertTrue(np.all(correct.y == wrong_ext.y))
42        self.assertTrue(np.all(correct.dy == wrong_ext.dy))
43
44    def test_wrong_unknown_ext(self):
45        """
46        Load a valid CanSAS XML file that has the extension '.xyz', which isn't
47        in the extension registry. Compare the results to loading the same file
48        with the extension '.xml'
49        """
50        correct = self.loader.load(self.valid_file)
51        wrong_ext = self.loader.load(self.valid_file_wrong_unknown_ext)
52        self.assertEqual(len(correct), 1)
53        self.assertEqual(len(wrong_ext), 1)
54        correct = correct[0]
55        wrong_ext = wrong_ext[0]
56
57        self.assertTrue(np.all(correct.x == wrong_ext.x))
58        self.assertTrue(np.all(correct.y == wrong_ext.y))
59        self.assertTrue(np.all(correct.dy == wrong_ext.dy))
60
61    def test_data_reader_exception(self):
62        """
63        Load a CanSAS XML file that doesn't meet the schema, and check errors
64        are set correctly
65        """
66        data = self.loader.load(self.invalid_file)
67        self.assertEqual(len(data), 1)
68        data = data[0]
69        self.assertEqual(len(data.errors), 1)
70
71        err_msg = data.errors[0]
72        self.assertTrue("does not fully meet the CanSAS v1.x specification" in err_msg)
73
74    def tearDown(self):
75        if os.path.isfile(self.valid_file_wrong_known_ext):
76            os.remove(self.valid_file_wrong_known_ext)
77        if os.path.isfile(self.valid_file_wrong_unknown_ext):
78            os.remove(self.valid_file_wrong_unknown_ext)
Note: See TracBrowser for help on using the repository browser.