[dcb91cf] | 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 | |
---|
[f53d684] | 15 | |
---|
| 16 | def find(filename): |
---|
| 17 | return os.path.join(os.path.dirname(__file__), filename) |
---|
| 18 | |
---|
| 19 | |
---|
[dcb91cf] | 20 | class ExtensionRegistryTests(unittest.TestCase): |
---|
| 21 | |
---|
| 22 | def setUp(self): |
---|
[f53d684] | 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") |
---|
[dcb91cf] | 26 | shutil.copyfile(self.valid_file, self.valid_file_wrong_known_ext) |
---|
| 27 | shutil.copyfile(self.valid_file, self.valid_file_wrong_unknown_ext) |
---|
[f53d684] | 28 | self.invalid_file = find("cansas1d_notitle.xml") |
---|
[dcb91cf] | 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) |
---|