def test_summarize_params_diff(self): unmodified_param = DictValue("ParamA", "new-param-value", "new-param-value") modified_param = DictValue("ParamB", "param-b-old-value", "param-b-new-value-delta") added_param = DictValue("ParamC", None, "param-c-new-value") removed_param = DictValue("ParamD", "param-d-old-value", None) params_diff = [ unmodified_param, modified_param, added_param, removed_param, ] self.assertEqual(summarize_params_diff([]), "") self.assertEqual( summarize_params_diff(params_diff), '\n'.join([ "Parameters Added: ParamC", "Parameters Removed: ParamD", "Parameters Modified: ParamB\n", ])) only_modified_params_diff = [modified_param] self.assertEqual(summarize_params_diff(only_modified_params_diff), "Parameters Modified: ParamB\n") only_added_params_diff = [added_param] self.assertEqual(summarize_params_diff(only_added_params_diff), "Parameters Added: ParamC\n") only_removed_params_diff = [removed_param] self.assertEqual(summarize_params_diff(only_removed_params_diff), "Parameters Removed: ParamD\n")
def test_diff_dictionaries(self): old_dict = { "a": "Apple", "b": "Banana", "c": "Corn", } new_dict = { "a": "Apple", "b": "Bob", "d": "Doug", } [count, changes] = diff_dictionaries(old_dict, new_dict) self.assertEqual(count, 3) expected_output = [ DictValue("a", "Apple", "Apple"), DictValue("b", "Banana", "Bob"), DictValue("c", "Corn", None), DictValue("d", None, "Doug"), ] expected_output.sort(key=attrgetter("key")) # compare all the outputs to the expected change for expected_change in expected_output: change = changes.pop(0) self.assertEqual(change, expected_change) # No extra output self.assertEqual(len(changes), 0)
def test_status(self): added = DictValue("k0", None, "value_0") self.assertEqual(added.status(), DictValue.ADDED) removed = DictValue("k1", "value_1", None) self.assertEqual(removed.status(), DictValue.REMOVED) modified = DictValue("k2", "value_1", "value_2") self.assertEqual(modified.status(), DictValue.MODIFIED) unmodified = DictValue("k3", "value_1", "value_1") self.assertEqual(unmodified.status(), DictValue.UNMODIFIED)
def test_format(self): added = DictValue("k0", None, "value_0") self.assertEqual(added.changes(), ['+%s = %s' % (added.key, added.new_value)]) removed = DictValue("k1", "value_1", None) self.assertEqual(removed.changes(), ['-%s = %s' % (removed.key, removed.old_value)]) modified = DictValue("k2", "value_1", "value_2") self.assertEqual(modified.changes(), [ '-%s = %s' % (modified.key, modified.old_value), '+%s = %s' % (modified.key, modified.new_value) ]) unmodified = DictValue("k3", "value_1", "value_1") self.assertEqual(unmodified.changes(), [' %s = %s' % ( unmodified.key, unmodified.old_value)]) self.assertEqual(unmodified.changes(), [' %s = %s' % ( unmodified.key, unmodified.new_value)])
def test_ask_for_approval_with_params_diff(self): get_input_path = "stacker.ui.get_raw_input" params_diff = [ DictValue('ParamA', None, 'new-param-value'), DictValue('ParamB', 'param-b-old-value', 'param-b-new-value-delta') ] with patch(get_input_path, return_value="y"): self.assertIsNone(ask_for_approval([], params_diff, None)) for v in ("n", "N", "x", "\n"): with patch(get_input_path, return_value=v): with self.assertRaises(exceptions.CancelExecution): ask_for_approval([], params_diff) with patch(get_input_path, side_effect=["v", "n"]) as mock_get_input: with patch("stacker.providers.aws.default.output_full_changeset" ) as mock_full_changeset: with self.assertRaises(exceptions.CancelExecution): ask_for_approval([], params_diff, True) self.assertEqual(mock_full_changeset.call_count, 1) self.assertEqual(mock_get_input.call_count, 2)
def test_format(self): added = DictValue("k0", None, "value_0") self.assertEqual(added.changes(), ['+%s = %s' % (added.key, added.new_value)]) removed = DictValue("k1", "value_1", None) self.assertEqual(removed.changes(), ['-%s = %s' % (removed.key, removed.old_value)]) modified = DictValue("k2", "value_1", "value_2") self.assertEqual(modified.changes(), [ '-%s = %s' % (modified.key, modified.old_value), '+%s = %s' % (modified.key, modified.new_value) ]) unmodified = DictValue("k3", "value_1", "value_1") self.assertEqual(unmodified.changes(), [' %s = %s' % (unmodified.key, unmodified.old_value)]) self.assertEqual(unmodified.changes(), [' %s = %s' % (unmodified.key, unmodified.new_value)])