unit tests for grains.append module function

- separate test functions for every return
- add tests for nested lists
- add test for appending to an element of a list (fails silently)
This commit is contained in:
Christophe Drevet-Droguet 2015-05-09 20:44:40 +02:00
parent 0c9a32326c
commit c4954046ad

View file

@ -112,9 +112,37 @@ class GrainsModuleTestCase(TestCase):
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append(self):
# grains {'a_list': ['a', 'b', 'c'], 'a': {'nested': {'list': ['1', '2', '3']}, 'aa': 'val'}}
def test_append_not_a_list(self):
# Failing append to an existing string, without convert
grainsmod.__grains__ = {'b': 'bval'}
res = grainsmod.append('b', 'd')
# check the result
self.assertEqual(res, 'The key b is not a valid list')
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'b': 'bval'})
# Failing append to an existing dict
grainsmod.__grains__ = {'b': {'b1': 'bval1'}}
res = grainsmod.append('b', 'd')
# check the result
self.assertEqual(res, 'The key b is not a valid list')
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'b': {'b1': 'bval1'}})
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append_already_in_list(self):
# Append an existing value
grainsmod.__grains__ = {'a_list': ['a', 'b', 'c'], 'b': 'bval'}
res = grainsmod.append('a_list', 'b')
# check the result
self.assertEqual(res, 'The val b was already in the list a_list')
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a_list': ['a', 'b', 'c'], 'b': 'bval'})
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append_ok(self):
# Append to an existing list
grainsmod.__grains__ = {'a_list': ['a', 'b', 'c'], 'b': 'bval'}
res = grainsmod.append('a_list', 'd')
@ -128,28 +156,101 @@ class GrainsModuleTestCase(TestCase):
res = grainsmod.append('a_list', 'd')
# check the result
self.assertEqual(res, {'a_list': ['d']})
# the whole grains should now be
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a_list': ['d'], 'b': 'bval'})
# Append to an existing string, without convert
grainsmod.__grains__ = {'b': 'bval'}
res = grainsmod.append('b', 'd')
# check the result
self.assertEqual(res, 'The key b is not a valid list')
# the whole grains should now be
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'b': 'bval'})
# Append to an existing string, with convert
grainsmod.__grains__ = {'b': 'bval'}
res = grainsmod.append('b', 'd', convert=True)
# check the result
self.assertEqual(res, {'b': ['bval', 'd']})
# the whole grains should now be
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'b': ['bval', 'd']})
# Append to an existing dict, with convert
grainsmod.__grains__ = {'b': {'b1': 'bval1'}}
res = grainsmod.append('b', 'd', convert=True)
# check the result
self.assertEqual(res, {'b': [{'b1': 'bval1'}, 'd']})
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'b': [{'b1': 'bval1'}, 'd']})
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append_nested_not_a_list(self):
# Failing append to an existing string, without convert
grainsmod.__grains__ = {'a': {'b': 'bval'}}
res = grainsmod.append('a:b', 'd')
# check the result
self.assertEqual(res, 'The key a:b is not a valid list')
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'b': 'bval'}})
# Failing append to an existing dict
grainsmod.__grains__ = {'a': {'b': {'b1': 'bval1'}}}
res = grainsmod.append('a:b', 'd')
# check the result
self.assertEqual(res, 'The key a:b is not a valid list')
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'b': {'b1': 'bval1'}}})
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append_nested_already_in_list(self):
# Append an existing value
grainsmod.__grains__ = {'a': {'a_list': ['a', 'b', 'c'], 'b': 'bval'}}
res = grainsmod.append('a:a_list', 'b')
# check the result
self.assertEqual(res, 'The val b was already in the list a:a_list')
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'a_list': ['a', 'b', 'c'], 'b': 'bval'}})
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append_nested_ok(self):
# Append to an existing list
grainsmod.__grains__ = {'a': {'a_list': ['a', 'b', 'c'], 'b': 'bval'}}
res = grainsmod.append('a:a_list', 'd')
# check the result
self.assertEqual(res, {'a': {'a_list': ['a', 'b', 'c', 'd'], 'b': 'bval'}})
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'a_list': ['a', 'b', 'c', 'd'], 'b': 'bval'}})
# Append to an non existing list
grainsmod.__grains__ = {'a': {'b': 'bval'}}
res = grainsmod.append('a:a_list', 'd')
# check the result
self.assertEqual(res, {'a': {'a_list': ['d'], 'b': 'bval'}})
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'a_list': ['d'], 'b': 'bval'}})
# Append to an existing string, with convert
grainsmod.__grains__ = {'a': {'b': 'bval'}}
res = grainsmod.append('a:b', 'd', convert=True)
# check the result
self.assertEqual(res, {'a': {'b': ['bval', 'd']}})
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'b': ['bval', 'd']}})
# Append to an existing dict, with convert
grainsmod.__grains__ = {'a': {'b': {'b1': 'bval1'}}}
res = grainsmod.append('a:b', 'd', convert=True)
# check the result
self.assertEqual(res, {'a': {'b': [{'b1': 'bval1'}, 'd']}})
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': {'b': [{'b1': 'bval1'}, 'd']}})
@patch.dict(grainsmod.__salt__, {'saltutil.sync_grains': MagicMock()})
def test_append_to_an_element_of_a_list(self):
# Append to an element in a list
# It currently fails silently
grainsmod.__grains__ = {'a': ['b', 'c']}
res = grainsmod.append('a:b', 'd')
# check the result
self.assertEqual(res, {'a': ['b', 'c']})
# check the whole grains
self.assertEqual(grainsmod.__grains__, {'a': ['b', 'c']})
if __name__ == '__main__':