Newer
Older
from __future__ import (absolute_import, division, print_function)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import mantid
import unittest
import warnings
from isis_powder.routines import InstrumentSettings, ParamMapEntry
class ISISPowderInstrumentSettingsTest(unittest.TestCase):
def test_user_missing_attribute_is_detected(self):
param_entry = ParamMapEntry.ParamMapEntry(ext_name="user_facing_name", int_name="script_facing_name")
inst_settings_obj = InstrumentSettings.InstrumentSettings(param_map=[param_entry])
with self.assertRaisesRegexp(AttributeError, "is required but was not set or passed"):
foo = inst_settings_obj.script_facing_name
del foo
def test_developer_missing_attribute_is_detected(self):
param_entry = ParamMapEntry.ParamMapEntry(ext_name="user_facing_name", int_name="script_facing_name")
inst_settings_obj = InstrumentSettings.InstrumentSettings(param_map=[param_entry])
with self.assertRaisesRegexp(AttributeError, "Please contact the development team"):
foo = inst_settings_obj.not_known
del foo
def test_set_attribute_is_found(self):
expected_value = 100
param_entry = ParamMapEntry.ParamMapEntry(ext_name="user_facing_name", int_name="script_facing_name")
keyword_args = {"user_facing_name": expected_value}
inst_settings_obj = InstrumentSettings.InstrumentSettings(param_map=[param_entry], kwargs=keyword_args)
self.assertEqual(inst_settings_obj.script_facing_name, expected_value)
def test_updating_attributes_produces_warning_on_init(self):
original_value = 123
new_value = 456
param_entry = ParamMapEntry.ParamMapEntry(ext_name="user_facing_name", int_name="script_facing_name")
# First check this works on init
adv_config = {"user_facing_name": original_value}
keyword_args = {"user_facing_name": new_value}
with warnings.catch_warnings(record=True) as warning_capture:
warnings.simplefilter("always")
inst_settings_obj = InstrumentSettings.InstrumentSettings(param_map=[param_entry], kwargs=keyword_args,
adv_conf_dict=adv_config)
self.assertRegexpMatches(str(warning_capture[-1].message), "which was previously set to")
self.assertRegexpMatches(str(warning_capture[-1].message), str(original_value))
self.assertRegexpMatches(str(warning_capture[-1].message), str(new_value))
self.assertEqual(inst_settings_obj.script_facing_name, new_value)
def test_updating_attributes_produces_warning(self):
original_value = 123
new_value = 456
second_value = 567
param_entry = ParamMapEntry.ParamMapEntry(ext_name="user_facing_name", int_name="script_facing_name")
# First check this works on init
adv_config = {"user_facing_name": original_value}
config_dict = {"user_facing_name": new_value}
keyword_args = {"user_facing_name": second_value}
inst_settings_obj = InstrumentSettings.InstrumentSettings(param_map=[param_entry], adv_conf_dict=adv_config)
self.assertEqual(inst_settings_obj.script_facing_name, original_value)
# Next try to update the attribute and check it gives a warning
with warnings.catch_warnings(record=True) as warning_capture:
warnings.simplefilter("always")
inst_settings_obj.update_attributes(basic_config=config_dict)
self.assertRegexpMatches(str(warning_capture[-1].message), "which was previously set to")
self.assertRegexpMatches(str(warning_capture[-1].message), str(original_value))
self.assertRegexpMatches(str(warning_capture[-1].message), str(new_value))
warnings_current_length = len(warning_capture)
# Then check that we only get one additional warning when replacing values again not two
inst_settings_obj.update_attributes(kwargs=keyword_args)
self.assertEqual(warnings_current_length + 1, len(warning_capture))
warnings_current_length = len(warning_capture)
# Finally check that the suppress field works by setting it back to second value
inst_settings_obj.update_attributes(kwargs=config_dict, suppress_warnings=True)
self.assertEqual(warnings_current_length, len(warning_capture))
# Finally check it has took the new value (most recently set)
self.assertEqual(inst_settings_obj.script_facing_name, new_value)
if __name__ == "__main__":
unittest.main()