1 | """ |
---|
2 | Unit tests for loading data files using the extension registry |
---|
3 | """ |
---|
4 | |
---|
5 | import logging |
---|
6 | import unittest |
---|
7 | import os |
---|
8 | import shutil |
---|
9 | import numpy as np |
---|
10 | |
---|
11 | from sas.sascalc.dataloader.loader import Registry as Loader |
---|
12 | |
---|
13 | logger = logging.getLogger(__name__) |
---|
14 | |
---|
15 | class 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) |
---|