|
5 | 5 | # & Institut Laue - Langevin
|
6 | 6 | # SPDX - License - Identifier: GPL - 3.0 +
|
7 | 7 | import unittest
|
8 |
| -from typing import List, Dict |
9 | 8 | from unittest import mock
|
10 | 9 |
|
11 | 10 | from sans.common.enums import (
|
12 | 11 | SANSInstrument,
|
13 |
| - SANSFacility, |
14 | 12 | DetectorType,
|
15 | 13 | ReductionMode,
|
16 | 14 | RangeStepType,
|
|
19 | 17 | FitType,
|
20 | 18 | RebinType,
|
21 | 19 | )
|
22 |
| -from sans.state.StateObjects.StateData import get_data_builder |
23 | 20 | from sans.state.StateObjects.StateMaskDetectors import StateMaskDetectors, StateMask
|
24 |
| -from sans.state.StateObjects.StatePolarization import StatePolarization |
25 |
| -from sans.test_helper.file_information_mock import SANSFileInformationMock |
26 | 21 | from sans.user_file.parser_helpers.toml_parser_impl_base import MissingMandatoryParam
|
27 | 22 | from sans.user_file.toml_parsers.toml_v1_parser import TomlV1Parser
|
| 23 | +from sans.test_helper.toml_parser_test_helpers import setup_parser_dict |
28 | 24 |
|
29 | 25 |
|
30 | 26 | class TomlV1ParserTest(unittest.TestCase):
|
31 |
| - @staticmethod |
32 |
| - def _get_mock_data_info(): |
33 |
| - # TODO I really really dislike having to do this in a test, but |
34 |
| - # TODO de-coupling StateData is required to avoid it |
35 |
| - file_information = SANSFileInformationMock(instrument=SANSInstrument.SANS2D, run_number=22024) |
36 |
| - data_builder = get_data_builder(SANSFacility.ISIS, file_information) |
37 |
| - data_builder.set_sample_scatter("SANS2D00022024") |
38 |
| - data_builder.set_sample_scatter_period(3) |
39 |
| - return data_builder.build() |
40 |
| - |
41 |
| - def _setup_parser(self, dict_vals) -> TomlV1Parser: |
42 |
| - def _add_missing_mandatory_key(dict_to_check: Dict, key_path: List[str], replacement_val): |
43 |
| - _dict = dict_to_check |
44 |
| - for key in key_path[0:-1]: |
45 |
| - if key not in _dict: |
46 |
| - _dict[key] = {} |
47 |
| - _dict = _dict[key] |
48 |
| - |
49 |
| - if key_path[-1] not in _dict: |
50 |
| - _dict[key_path[-1]] = replacement_val # Add in child value |
51 |
| - return dict_to_check |
52 |
| - |
53 |
| - self._mocked_data_info = self._get_mock_data_info() |
54 |
| - # instrument key needs to generally be present |
55 |
| - dict_vals = _add_missing_mandatory_key(dict_vals, ["instrument", "name"], "LOQ") |
56 |
| - dict_vals = _add_missing_mandatory_key(dict_vals, ["detector", "configuration", "selected_detector"], "rear") |
57 |
| - |
58 |
| - return TomlV1Parser(dict_vals, file_information=None) |
| 27 | + def _setup_parser(self, dict_vals): |
| 28 | + setup_dict, mocked_data_info = setup_parser_dict(dict_vals) |
| 29 | + self._mocked_data_info = mocked_data_info |
| 30 | + return TomlV1Parser(setup_dict, file_information=None) |
59 | 31 |
|
60 | 32 | def test_instrument(self):
|
61 | 33 | parser = self._setup_parser(dict_vals={"instrument": {"name": SANSInstrument.SANS2D.value}})
|
@@ -696,125 +668,6 @@ def test_parse_mask(self):
|
696 | 668 | self.assertEqual(102, norm_state.prompt_peak_correction_max)
|
697 | 669 | self.assertTrue(norm_state.prompt_peak_correction_enabled)
|
698 | 670 |
|
699 |
| - def test_parse_polarization(self): |
700 |
| - top_level_dict = {"polarization": {"flipper_configuration": "00,11,01,10", "spin_configuration": "-1-1,-1+1,+1-1,+1+1"}} |
701 |
| - parser_result = self._setup_parser(top_level_dict) |
702 |
| - polarization_state = parser_result.get_state_polarization() |
703 |
| - |
704 |
| - self.assertIsInstance(polarization_state, StatePolarization) |
705 |
| - self.assertEqual("00,11,01,10", polarization_state.flipper_configuration) |
706 |
| - self.assertEqual("-1-1,-1+1,+1-1,+1+1", polarization_state.spin_configuration) |
707 |
| - |
708 |
| - def test_parse_flippers(self): |
709 |
| - top_level_dict = { |
710 |
| - "polarization": { |
711 |
| - "flipper": { |
712 |
| - "polarizing": { |
713 |
| - "idf_component_name": "name_in_IDF", |
714 |
| - "device_name": "flipper1", |
715 |
| - "device_type": "coil", |
716 |
| - "location": {"x": 1.17, "y": 0.05, "z": 0.045}, |
717 |
| - "transmission": "trans_ws", |
718 |
| - "efficiency": "eff_ws", |
719 |
| - }, |
720 |
| - "analyzing": { |
721 |
| - "idf_component_name": "name_in_IDF_a", |
722 |
| - "device_name": "flipper2", |
723 |
| - "device_type": "coil", |
724 |
| - "location": {"x": 2.17, "y": 0.05, "z": 0.045}, |
725 |
| - "transmission": "trans_ws", |
726 |
| - "efficiency": "eff_ws", |
727 |
| - }, |
728 |
| - } |
729 |
| - } |
730 |
| - } |
731 |
| - parser_result = self._setup_parser(top_level_dict) |
732 |
| - polarization_state = parser_result.get_state_polarization() |
733 |
| - flippers = polarization_state.flippers |
734 |
| - self.assertEqual(2, len(flippers)) |
735 |
| - self.assertEqual("flipper1", flippers[0].device_name) |
736 |
| - self.assertEqual("flipper2", flippers[1].device_name) |
737 |
| - self.assertEqual(1.17, flippers[0].location_x) |
738 |
| - self.assertEqual(0.05, flippers[0].location_y) |
739 |
| - self.assertEqual(0.045, flippers[0].location_z) |
740 |
| - self.assertEqual(2.17, flippers[1].location_x) |
741 |
| - self.assertEqual("name_in_IDF", flippers[0].idf_component_name) |
742 |
| - self.assertEqual("coil", flippers[0].device_type) |
743 |
| - self.assertEqual("trans_ws", flippers[0].transmission) |
744 |
| - self.assertEqual("eff_ws", flippers[0].efficiency) |
745 |
| - |
746 |
| - def test_parse_polarizer_and_analyzer(self): |
747 |
| - top_level_dict = { |
748 |
| - "polarization": { |
749 |
| - "polarizer": { |
750 |
| - "idf_component_name": "name_in_IDF_pol", |
751 |
| - "device_name": "sm-polarizer", |
752 |
| - "device_type": "coil", |
753 |
| - "location": {"x": 1.17, "y": 0.05, "z": 0.045}, |
754 |
| - "transmission": "trans_ws", |
755 |
| - "efficiency": "eff_ws", |
756 |
| - "cell_length": 0.005, |
757 |
| - "gas_pressure": 5, |
758 |
| - }, |
759 |
| - "analyzer": { |
760 |
| - "idf_component_name": "name_in_IDF_ana", |
761 |
| - "device_name": "3He-analyzer", |
762 |
| - "device_type": "coil", |
763 |
| - "location": {"x": 2.17, "y": 0.05, "z": 0.045}, |
764 |
| - "cell_length": 0.006, |
765 |
| - "gas_pressure": 6, |
766 |
| - "transmission": "trans_ws", |
767 |
| - "efficiency": "eff_ws", |
768 |
| - }, |
769 |
| - } |
770 |
| - } |
771 |
| - parser_result = self._setup_parser(top_level_dict) |
772 |
| - polarization_state = parser_result.get_state_polarization() |
773 |
| - polarizer_state = polarization_state.polarizer |
774 |
| - analyzer_state = polarization_state.analyzer |
775 |
| - self.assertEqual(0.006, analyzer_state.cell_length) |
776 |
| - self.assertEqual(0.005, polarizer_state.cell_length) |
777 |
| - self.assertEqual(6, analyzer_state.gas_pressure) |
778 |
| - self.assertEqual(5, polarizer_state.gas_pressure) |
779 |
| - self.assertEqual("sm-polarizer", polarizer_state.device_name) |
780 |
| - self.assertEqual("3He-analyzer", analyzer_state.device_name) |
781 |
| - self.assertEqual(1.17, polarizer_state.location_x) |
782 |
| - self.assertEqual(0.05, polarizer_state.location_y) |
783 |
| - self.assertEqual(0.045, polarizer_state.location_z) |
784 |
| - self.assertEqual(2.17, analyzer_state.location_x) |
785 |
| - self.assertEqual("name_in_IDF_pol", polarizer_state.idf_component_name) |
786 |
| - self.assertEqual("coil", polarizer_state.device_type) |
787 |
| - self.assertEqual("trans_ws", polarizer_state.transmission) |
788 |
| - self.assertEqual("eff_ws", polarizer_state.efficiency) |
789 |
| - |
790 |
| - def test_parse_fields(self): |
791 |
| - top_level_dict = { |
792 |
| - "polarization": { |
793 |
| - "magnetic_field": { |
794 |
| - "sample_strength_log": "nameoflog", |
795 |
| - "sample_direction": {"a": 0, "p": 2.3, "d": 0.002}, |
796 |
| - }, |
797 |
| - "electric_field": { |
798 |
| - "sample_strength_log": "nameofotherlog", |
799 |
| - "sample_direction_log": "nameofanotherlog", |
800 |
| - }, |
801 |
| - } |
802 |
| - } |
803 |
| - parser_result = self._setup_parser(top_level_dict) |
804 |
| - polarization_state = parser_result.get_state_polarization() |
805 |
| - electric_state = polarization_state.electric_field |
806 |
| - magnetic_state = polarization_state.magnetic_field |
807 |
| - self.assertEqual("nameoflog", magnetic_state.sample_strength_log) |
808 |
| - self.assertEqual(0, magnetic_state.sample_direction_a) |
809 |
| - self.assertEqual(2.3, magnetic_state.sample_direction_p) |
810 |
| - self.assertEqual(0.002, magnetic_state.sample_direction_d) |
811 |
| - self.assertIsNone(magnetic_state.sample_direction_log) |
812 |
| - self.assertEqual("nameofotherlog", electric_state.sample_strength_log) |
813 |
| - self.assertEqual("nameofanotherlog", electric_state.sample_direction_log) |
814 |
| - self.assertIsNone(electric_state.sample_direction_a) |
815 |
| - self.assertIsNone(electric_state.sample_direction_p) |
816 |
| - self.assertIsNone(electric_state.sample_direction_d) |
817 |
| - |
818 | 671 |
|
819 | 672 | if __name__ == "__main__":
|
820 | 673 | unittest.main()
|
0 commit comments