Esempio n. 1
0
    def test_deep_update(self):
        """
        Test updating a (sub)dict.
        """
        mdict = copy.deepcopy(self.dict1)
        res = dictupdate.update_dict_key_value(mdict, "C:F", {
            "foo": "bar",
            "qux": "quux"
        })
        self.assertEqual(
            {
                "A": "B",
                "C": {
                    "D": "E",
                    "F": {
                        "G": "H",
                        "I": "J",
                        "foo": "bar",
                        "qux": "quux"
                    }
                },
            },
            res,
        )

        # Test updating a non-existing subkey
        res = dictupdate.update_dict_key_value({}, "foo:bar:baz",
                                               {"qux": "quux"})
        self.assertEqual({"foo": {"bar": {"baz": {"qux": "quux"}}}}, res)
        # Test updating a non-existing subkey, with a different delimiter
        res = dictupdate.update_dict_key_value({},
                                               "foo bar baz", {"qux": "quux"},
                                               delimiter=" ")
        self.assertEqual({"foo": {"bar": {"baz": {"qux": "quux"}}}}, res)
Esempio n. 2
0
    def test_deep_update(self):
        '''
        Test updating a (sub)dict.
        '''
        mdict = copy.deepcopy(self.dict1)
        res = dictupdate.update_dict_key_value(mdict, 'C:F', {
            'foo': 'bar',
            'qux': 'quux'
        })
        self.assertEqual(
            {
                'A': 'B',
                'C': {
                    'D': 'E',
                    'F': {
                        'G': 'H',
                        'I': 'J',
                        'foo': 'bar',
                        'qux': 'quux'
                    }
                }
            }, res)

        # Test updating a non-existing subkey
        res = dictupdate.update_dict_key_value({}, 'foo:bar:baz',
                                               {'qux': 'quux'})
        self.assertEqual({'foo': {'bar': {'baz': {'qux': 'quux'}}}}, res)
        # Test updating a non-existing subkey, with a different delimiter
        res = dictupdate.update_dict_key_value({},
                                               'foo bar baz', {'qux': 'quux'},
                                               delimiter=' ')
        self.assertEqual({'foo': {'bar': {'baz': {'qux': 'quux'}}}}, res)
Esempio n. 3
0
 def test_deep_update_illegal_update(self):
     '''
     Test errorhandling updating a (sub)dict with illegal types.
     '''
     # Update with an illegal type
     for update_with in [42, None, [42], 'bar']:
         with self.assertRaisesRegex(SaltInvocationError,
                                     r"Cannot update {} with a {}."
                                     "".format(type({}), type(update_with))):
             dictupdate.update_dict_key_value({}, 'foo', update_with)
     # Again, but now using OrderedDicts
     for update_with in [42, None, [42], 'bar']:
         with self.assertRaisesRegex(SaltInvocationError,
                                     r"Cannot update {} with a {}."
                                     "".format(type(OrderedDict()), type(update_with))):
             dictupdate.update_dict_key_value({}, 'foo', update_with, ordered_dict=True)