23from AnalysisAlgorithmsConfig.ConfigPropertySubstitution
import (
24 substituteComponentProperties,
30 """Pure tests of `substituteValue` — no configurables involved."""
32 SUBS = [(
'Jets_STEP3',
'Jets')]
35 self.assertEqual (substituteValue (
'Jets_STEP3', self.
SUBS),
'Jets')
38 self.assertEqual (substituteValue (
'Electrons_STEP1', self.
SUBS),
44 self.assertEqual (substituteValue (
'Jets_STEP3_%SYS%', self.
SUBS),
48 self.assertEqual (substituteValue (
'Jets_STEP3.flag', self.
SUBS),
54 self.assertEqual (substituteValue (
'MyJets_STEP3', self.
SUBS),
60 self.assertEqual (substituteValue (
'Jets_STEP3.a && Jets_STEP3.b',
66 self.assertEqual (substituteValue (
'Jets_STEP3 && MyJets_STEP3',
68 'Jets && MyJets_STEP3')
71 subs = [(
'Jets_STEP3',
'Jets'), (
'Electrons_STEP2',
'Electrons')]
72 self.assertEqual (substituteValue (
'Jets_STEP3 && Electrons_STEP2',
77 result = substituteValue ([
'Jets_STEP3',
'MyJets_STEP3',
'other'],
79 self.assertEqual (result, [
'Jets',
'MyJets_STEP3',
'other'])
80 self.assertIsInstance (result, list)
83 result = substituteValue ({
'Jets_STEP3':
'Jets_STEP3.flag',
84 'MyJets_STEP3':
'unrelated'},
86 self.assertEqual (result, {
'Jets':
'Jets.flag',
87 'MyJets_STEP3':
'unrelated'})
88 self.assertIsInstance (result, dict)
91 result = substituteValue ({
'Jets_STEP3',
'MyJets_STEP3'}, self.
SUBS)
92 self.assertEqual (result, {
'Jets',
'MyJets_STEP3'})
93 self.assertIsInstance (result, set)
96 result = substituteValue ((
'Jets_STEP3',
'MyJets_STEP3'), self.
SUBS)
97 self.assertEqual (result, (
'Jets',
'MyJets_STEP3'))
98 self.assertIsInstance (result, tuple)
101 value = {
'a': [
'Jets_STEP3',
'MyJets_STEP3'],
102 'Jets_STEP3.b': {
'c':
'Jets_STEP3'}}
103 self.assertEqual (substituteValue (value, self.
SUBS),
104 {
'a': [
'Jets',
'MyJets_STEP3'],
105 'Jets.b': {
'c':
'Jets'}})
108 self.assertEqual (substituteValue (42, self.
SUBS), 42)
109 self.assertEqual (substituteValue (3.14, self.
SUBS), 3.14)
110 self.assertEqual (substituteValue (
True, self.
SUBS),
True)
111 self.assertIsNone (substituteValue (
None, self.
SUBS))
114 self.assertEqual (substituteValue (
'Jets_STEP3', []),
'Jets_STEP3')
115 self.assertEqual (substituteValue ([
'Jets_STEP3'], []), [
'Jets_STEP3'])
119 """Build real configurables via the dual-use interface — the same
120 primitives `ConfigAccumulator` uses internally — and verify the
121 substitution walks into algorithm properties and a private tool."""
123 SUBS = [(
'AnaJets_STEP3',
'AnaJets')]
126 import AnaAlgorithm.DualUseConfig
as DualUseConfig
127 alg = DualUseConfig.createAlgorithm (
128 'CP::AsgSelectionAlg',
'TestSelectionAlg')
129 DualUseConfig.addPrivateTool (
130 alg,
'selectionTool',
'CP::AsgFlagSelectionTool')
131 alg.particles =
'AnaJets_STEP3_%SYS%'
132 alg.preselection =
'preselect_STEP3,as_char'
133 alg.selectionDecoration =
'pass_AnaJets_STEP3,as_char'
134 alg.selectionTool.selectionFlags = [
135 'AnaJets_STEP3.flag',
136 'MyAnaJets_STEP3.flag',
143 substituteComponentProperties (alg, self.
SUBS)
146 self.assertEqual (alg.particles,
'AnaJets_%SYS%')
147 self.assertEqual (alg.preselection,
'preselect_STEP3,as_char')
148 self.assertEqual (alg.selectionDecoration,
'pass_AnaJets_STEP3,as_char')
151 self.assertEqual (list (alg.selectionTool.selectionFlags),
153 'MyAnaJets_STEP3.flag',
158 substituteComponentProperties (alg, self.
SUBS)
159 before_particles = alg.particles
160 before_flags = list (alg.selectionTool.selectionFlags)
161 substituteComponentProperties (alg, self.
SUBS)
162 self.assertEqual (alg.particles, before_particles)
163 self.assertEqual (list (alg.selectionTool.selectionFlags), before_flags)
167 before_particles = alg.particles
168 before_preselection = alg.preselection
169 before_decoration = alg.selectionDecoration
170 before_flags = list (alg.selectionTool.selectionFlags)
171 substituteComponentProperties (alg, [(
'NonExistentContainer',
'Foo')])
172 self.assertEqual (alg.particles, before_particles)
173 self.assertEqual (alg.preselection, before_preselection)
174 self.assertEqual (alg.selectionDecoration, before_decoration)
175 self.assertEqual (list (alg.selectionTool.selectionFlags), before_flags)
179 before_particles = alg.particles
180 before_flags = list (alg.selectionTool.selectionFlags)
181 substituteComponentProperties (alg, [])
182 self.assertEqual (alg.particles, before_particles)
183 self.assertEqual (list (alg.selectionTool.selectionFlags), before_flags)
186if __name__ ==
'__main__' :
test_substitution_is_idempotent(self)
test_no_matching_substitution_leaves_properties_unchanged(self)
test_substitution_rewrites_algorithm_and_private_tool(self)
test_empty_substitutions_leaves_properties_unchanged(self)
test_empty_substitutions_is_identity(self)
test_dict_recurses_on_keys_and_values(self)
test_nested_dict_with_list(self)
test_string_with_suffix(self)
test_anchored_inside_expression_with_trap(self)
test_anchored_inside_expression(self)
test_plain_string_match(self)
test_plain_string_no_match(self)
test_multiple_substitutions_applied_in_order(self)
test_tuple_recurses(self)
test_string_with_trailing_dot(self)
test_anchored_boundary_left(self)
test_non_string_scalars_unchanged(self)