def test_get_children(self): next_step1 = NextStep() names = ['', 'action1', 'action2'] for name in names: self.assertIsNone(next_step1.get_children([name])) self.assertDictEqual(next_step1.get_children([]), next_step1.as_json(with_children=False)) flags = [Flag(), Flag(action='action1'), Flag(action='action2')] next_step2 = NextStep(flags=flags) for i, name in enumerate(names): self.assertDictEqual(next_step2.get_children([name]), flags[i].as_json()) self.assertDictEqual(next_step2.get_children([]), next_step2.as_json(with_children=False)) filters = [ Filter(action='filter1'), Filter(), Filter(action='filter2') ] flags = [Flag(action='action1', filters=filters)] names = ['filter1', '', 'filter2'] next_step3 = NextStep(flags=flags) ancestries = [[name, 'action1'] for name in names] for i, ancestry in enumerate(ancestries): self.assertDictEqual(next_step3.get_children(ancestry), filters[i].as_json()) self.assertDictEqual(next_step3.get_children([]), next_step3.as_json(with_children=False))
def test_to_from_json(self): filter_params = ['test_filter_action', ''] flags_params = [('', []), ('test_action', []), ('test_action', filter_params)] input_params = [('', '', None, []), ('test_name', '', None, []), ('test_name', 'test_parent', None, []), ('test_name', 'test_parent', ['a', 'b'], []), ('test_name', 'test_parent', ['a', 'b'], flags_params)] for (name, parent_name, ancestry, flag_params) in input_params: next_step = NextStep(name=name, parent_name=parent_name, ancestry=ancestry) if flag_params: flags = [] for flag_action, flag_filter_params in flag_params: flag = Flag(action=flag_action, parent_name=next_step.name, ancestry=next_step.ancestry) if filter_params: flag.filters = [ Filter(action=flag_action, parent_name=flag.name, ancestry=flag.ancestry) for flag_action in flag_filter_params ] flags.append(flag) next_step.flags = flags next_step_json = next_step.as_json() derived_next_step = NextStep.from_json(next_step_json, parent_name=parent_name, ancestry=ancestry) self.assertDictEqual(derived_next_step.as_json(), next_step_json) self.assertEqual(next_step.parent_name, derived_next_step.parent_name) self.assertListEqual(next_step.ancestry, derived_next_step.ancestry) derived_json_without_children = next_step_json derived_json_without_children['flags'] = [ flag['action'] for flag in derived_json_without_children['flags'] ] self.assertDictEqual( derived_next_step.as_json(with_children=False), derived_json_without_children) # check the ancestry of the flags original_flag_ancestries = [ list(flag.ancestry) for flag in next_step.flags ] derived_flag_ancestries = [ list(flag.ancestry) for flag in derived_next_step.flags ] self.assertEqual(len(original_flag_ancestries), len(derived_flag_ancestries)) for original_flag_ancestry, derived_flag_ancestry in zip( original_flag_ancestries, derived_flag_ancestries): self.assertListEqual(derived_flag_ancestry, original_flag_ancestry)
def test_get_children(self): next_step1 = NextStep() names = ['sub1_top_flag', 'mod1_flag1', 'Top Flag'] for name in names: self.assertIsNone(next_step1.get_children([name])) self.assertDictEqual(next_step1.get_children([]), next_step1.as_json(with_children=False)) flags = [ Flag('sub1_top_flag'), Flag(action='mod1_flag1'), Flag(action='Top Flag') ] next_step2 = NextStep(flags=flags) for i, name in enumerate(names): self.assertDictEqual(next_step2.get_children([name]), flags[i].as_json()) self.assertDictEqual(next_step2.get_children([]), next_step2.as_json(with_children=False))
def test_to_from_xml_is_convertible(self): flags = [Flag(action='mod1_flag1'), Flag(action='Top Flag')] inputs = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent'), NextStep(name='name', parent_name='parent', ancestry=['a', 'b']), NextStep(name='name', parent_name='parent', flags=[], ancestry=['a', 'b']), NextStep(name='name', parent_name='parent', flags=flags, ancestry=['a', 'b']) ] for next_step in inputs: original_json = next_step.as_json() new_step = NextStep(xml=next_step.to_xml()) new_json = new_step.as_json() self.assertDictEqual(new_json, original_json)