From 6ea3e740607297a9800a6ef24d6c07dfae1c873d Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Sat, 18 Feb 2017 19:52:37 +0100 Subject: Include missing unit tests to test runner config We were not running those tests in CI, bad :( --- roles/lib_utils/src/test/unit/repoquery.py | 87 -------- roles/lib_utils/src/test/unit/test_repoquery.py | 87 ++++++++ roles/lib_utils/src/test/unit/test_yedit.py | 277 ++++++++++++++++++++++++ roles/lib_utils/src/test/unit/yedit_test.py | 277 ------------------------ 4 files changed, 364 insertions(+), 364 deletions(-) delete mode 100755 roles/lib_utils/src/test/unit/repoquery.py create mode 100755 roles/lib_utils/src/test/unit/test_repoquery.py create mode 100755 roles/lib_utils/src/test/unit/test_yedit.py delete mode 100755 roles/lib_utils/src/test/unit/yedit_test.py (limited to 'roles/lib_utils') diff --git a/roles/lib_utils/src/test/unit/repoquery.py b/roles/lib_utils/src/test/unit/repoquery.py deleted file mode 100755 index c487ab254..000000000 --- a/roles/lib_utils/src/test/unit/repoquery.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python2 -''' - Unit tests for repoquery -''' -# To run: -# ./repoquery.py -# -# . -# Ran 1 test in 0.002s -# -# OK - -import os -import sys -import unittest -import mock - -# Removing invalid variable names for tests so that I can -# keep them brief -# pylint: disable=invalid-name,no-name-in-module -# Disable import-error b/c our libraries aren't loaded in jenkins -# pylint: disable=import-error,wrong-import-position -# place class in our python path -module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 -sys.path.insert(0, module_path) -from repoquery import Repoquery # noqa: E402 - - -class RepoQueryTest(unittest.TestCase): - ''' - Test class for RepoQuery - ''' - - def setUp(self): - ''' setup method for other tests ''' - pass - - @mock.patch('repoquery._run') - def test_querying_a_package(self, mock_cmd): - ''' Testing querying a package ''' - - # Arrange - - # run_ansible input parameters - params = { - 'state': 'list', - 'name': 'bash', - 'query_type': 'repos', - 'verbose': False, - 'show_duplicates': False, - 'match_version': None, - } - - valid_stderr = '''Repo rhel-7-server-extras-rpms forced skip_if_unavailable=True due to: /etc/pki/entitlement/3268107132875399464-key.pem - Repo rhel-7-server-rpms forced skip_if_unavailable=True due to: /etc/pki/entitlement/4128505182875899164-key.pem''' # not real - - # Return values of our mocked function call. These get returned once per call. - mock_cmd.side_effect = [ - (0, '4.2.46|21.el7_3|x86_64|rhel-7-server-rpms|4.2.46-21.el7_3', valid_stderr), # first call to the mock - ] - - # Act - results = Repoquery.run_ansible(params, False) - - # Assert - self.assertEqual(results['state'], 'list') - self.assertFalse(results['changed']) - self.assertTrue(results['results']['package_found']) - self.assertEqual(results['results']['returncode'], 0) - self.assertEqual(results['results']['package_name'], 'bash') - self.assertEqual(results['results']['versions'], {'latest_full': '4.2.46-21.el7_3', - 'available_versions': ['4.2.46'], - 'available_versions_full': ['4.2.46-21.el7_3'], - 'latest': '4.2.46'}) - - # Making sure our mock was called as we expected - mock_cmd.assert_has_calls([ - mock.call(['/usr/bin/repoquery', '--plugins', '--quiet', '--pkgnarrow=repos', '--queryformat=%{version}|%{release}|%{arch}|%{repo}|%{version}-%{release}', 'bash']), - ]) - - def tearDown(self): - '''TearDown method''' - pass - - -if __name__ == "__main__": - unittest.main() diff --git a/roles/lib_utils/src/test/unit/test_repoquery.py b/roles/lib_utils/src/test/unit/test_repoquery.py new file mode 100755 index 000000000..c487ab254 --- /dev/null +++ b/roles/lib_utils/src/test/unit/test_repoquery.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python2 +''' + Unit tests for repoquery +''' +# To run: +# ./repoquery.py +# +# . +# Ran 1 test in 0.002s +# +# OK + +import os +import sys +import unittest +import mock + +# Removing invalid variable names for tests so that I can +# keep them brief +# pylint: disable=invalid-name,no-name-in-module +# Disable import-error b/c our libraries aren't loaded in jenkins +# pylint: disable=import-error,wrong-import-position +# place class in our python path +module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 +sys.path.insert(0, module_path) +from repoquery import Repoquery # noqa: E402 + + +class RepoQueryTest(unittest.TestCase): + ''' + Test class for RepoQuery + ''' + + def setUp(self): + ''' setup method for other tests ''' + pass + + @mock.patch('repoquery._run') + def test_querying_a_package(self, mock_cmd): + ''' Testing querying a package ''' + + # Arrange + + # run_ansible input parameters + params = { + 'state': 'list', + 'name': 'bash', + 'query_type': 'repos', + 'verbose': False, + 'show_duplicates': False, + 'match_version': None, + } + + valid_stderr = '''Repo rhel-7-server-extras-rpms forced skip_if_unavailable=True due to: /etc/pki/entitlement/3268107132875399464-key.pem + Repo rhel-7-server-rpms forced skip_if_unavailable=True due to: /etc/pki/entitlement/4128505182875899164-key.pem''' # not real + + # Return values of our mocked function call. These get returned once per call. + mock_cmd.side_effect = [ + (0, '4.2.46|21.el7_3|x86_64|rhel-7-server-rpms|4.2.46-21.el7_3', valid_stderr), # first call to the mock + ] + + # Act + results = Repoquery.run_ansible(params, False) + + # Assert + self.assertEqual(results['state'], 'list') + self.assertFalse(results['changed']) + self.assertTrue(results['results']['package_found']) + self.assertEqual(results['results']['returncode'], 0) + self.assertEqual(results['results']['package_name'], 'bash') + self.assertEqual(results['results']['versions'], {'latest_full': '4.2.46-21.el7_3', + 'available_versions': ['4.2.46'], + 'available_versions_full': ['4.2.46-21.el7_3'], + 'latest': '4.2.46'}) + + # Making sure our mock was called as we expected + mock_cmd.assert_has_calls([ + mock.call(['/usr/bin/repoquery', '--plugins', '--quiet', '--pkgnarrow=repos', '--queryformat=%{version}|%{release}|%{arch}|%{repo}|%{version}-%{release}', 'bash']), + ]) + + def tearDown(self): + '''TearDown method''' + pass + + +if __name__ == "__main__": + unittest.main() diff --git a/roles/lib_utils/src/test/unit/test_yedit.py b/roles/lib_utils/src/test/unit/test_yedit.py new file mode 100755 index 000000000..ed07ac96e --- /dev/null +++ b/roles/lib_utils/src/test/unit/test_yedit.py @@ -0,0 +1,277 @@ +#!/usr/bin/env python2 +''' + Unit tests for yedit +''' +# To run +# python -m unittest yedit_test +# +# ............................. +# ---------------------------------------------------------------------- +# Ran 29 tests in 0.133s +# OK + +import os +import sys +import unittest + +# Removing invalid variable names for tests so that I can +# keep them brief +# pylint: disable=invalid-name,no-name-in-module +# Disable import-error b/c our libraries aren't loaded in jenkins +# pylint: disable=import-error +# place yedit in our path +yedit_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 +sys.path.insert(0, yedit_path) + +from yedit import Yedit # noqa: E402 + +# pylint: disable=too-many-public-methods +# Silly pylint, moar tests! + + +class YeditTest(unittest.TestCase): + ''' + Test class for yedit + ''' + data = {'a': 'a', + 'b': {'c': {'d': [{'e': 'x'}, 'f', 'g']}}, + } # noqa: E124 + + filename = 'yedit_test.yml' + + def setUp(self): + ''' setup method will create a file and set to known configuration ''' + yed = Yedit(YeditTest.filename) + yed.yaml_dict = YeditTest.data + yed.write() + + def test_load(self): + ''' Testing a get ''' + yed = Yedit('yedit_test.yml') + self.assertEqual(yed.yaml_dict, self.data) + + def test_write(self): + ''' Testing a simple write ''' + yed = Yedit('yedit_test.yml') + yed.put('key1', 1) + yed.write() + self.assertTrue('key1' in yed.yaml_dict) + self.assertEqual(yed.yaml_dict['key1'], 1) + + def test_write_x_y_z(self): + '''Testing a write of multilayer key''' + yed = Yedit('yedit_test.yml') + yed.put('x.y.z', 'modified') + yed.write() + yed.load() + self.assertEqual(yed.get('x.y.z'), 'modified') + + def test_delete_a(self): + '''Testing a simple delete ''' + yed = Yedit('yedit_test.yml') + yed.delete('a') + yed.write() + yed.load() + self.assertTrue('a' not in yed.yaml_dict) + + def test_delete_b_c(self): + '''Testing delete of layered key ''' + yed = Yedit('yedit_test.yml', separator=':') + yed.delete('b:c') + yed.write() + yed.load() + self.assertTrue('b' in yed.yaml_dict) + self.assertFalse('c' in yed.yaml_dict['b']) + + def test_create(self): + '''Testing a create ''' + os.unlink(YeditTest.filename) + yed = Yedit('yedit_test.yml') + yed.create('foo', 'bar') + yed.write() + yed.load() + self.assertTrue('foo' in yed.yaml_dict) + self.assertTrue(yed.yaml_dict['foo'] == 'bar') + + def test_create_content(self): + '''Testing a create with content ''' + content = {"foo": "bar"} + yed = Yedit("yedit_test.yml", content) + yed.write() + yed.load() + self.assertTrue('foo' in yed.yaml_dict) + self.assertTrue(yed.yaml_dict['foo'], 'bar') + + def test_array_insert(self): + '''Testing a create with content ''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', 'inject') + self.assertTrue(yed.get('b:c:d[0]') == 'inject') + + def test_array_insert_first_index(self): + '''Testing a create with content ''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', 'inject') + self.assertTrue(yed.get('b:c:d[1]') == 'f') + + def test_array_insert_second_index(self): + '''Testing a create with content ''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', 'inject') + self.assertTrue(yed.get('b:c:d[2]') == 'g') + + def test_dict_array_dict_access(self): + '''Testing a create with content''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) + self.assertTrue(yed.get('b:c:d[0]:[0]:x:y') == 'inject') + + def test_dict_array_dict_replace(self): + '''Testing multilevel delete''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) + yed.put('b:c:d[0]:[0]:x:y', 'testing') + self.assertTrue('b' in yed.yaml_dict) + self.assertTrue('c' in yed.yaml_dict['b']) + self.assertTrue('d' in yed.yaml_dict['b']['c']) + self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'], list)) + self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0], list)) + self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0][0], dict)) + self.assertTrue('y' in yed.yaml_dict['b']['c']['d'][0][0]['x']) + self.assertTrue(yed.yaml_dict['b']['c']['d'][0][0]['x']['y'] == 'testing') # noqa: E501 + + def test_dict_array_dict_remove(self): + '''Testing multilevel delete''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) + yed.delete('b:c:d[0]:[0]:x:y') + self.assertTrue('b' in yed.yaml_dict) + self.assertTrue('c' in yed.yaml_dict['b']) + self.assertTrue('d' in yed.yaml_dict['b']['c']) + self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'], list)) + self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0], list)) + self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0][0], dict)) + self.assertFalse('y' in yed.yaml_dict['b']['c']['d'][0][0]['x']) + + def test_key_exists_in_dict(self): + '''Testing exist in dict''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) + self.assertTrue(yed.exists('b:c', 'd')) + + def test_key_exists_in_list(self): + '''Testing exist in list''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) + self.assertTrue(yed.exists('b:c:d', [{'x': {'y': 'inject'}}])) + self.assertFalse(yed.exists('b:c:d', [{'x': {'y': 'test'}}])) + + def test_update_to_list_with_index(self): + '''Testing update to list with index''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('x:y:z', [1, 2, 3]) + yed.update('x:y:z', [5, 6], index=2) + self.assertTrue(yed.get('x:y:z') == [1, 2, [5, 6]]) + self.assertTrue(yed.exists('x:y:z', [5, 6])) + self.assertFalse(yed.exists('x:y:z', 4)) + + def test_update_to_list_with_curr_value(self): + '''Testing update to list with index''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('x:y:z', [1, 2, 3]) + yed.update('x:y:z', [5, 6], curr_value=3) + self.assertTrue(yed.get('x:y:z') == [1, 2, [5, 6]]) + self.assertTrue(yed.exists('x:y:z', [5, 6])) + self.assertFalse(yed.exists('x:y:z', 4)) + + def test_update_to_list(self): + '''Testing update to list''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('x:y:z', [1, 2, 3]) + yed.update('x:y:z', [5, 6]) + self.assertTrue(yed.get('x:y:z') == [1, 2, 3, [5, 6]]) + self.assertTrue(yed.exists('x:y:z', [5, 6])) + self.assertFalse(yed.exists('x:y:z', 4)) + + def test_append_twice_to_list(self): + '''Testing append to list''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('x:y:z', [1, 2, 3]) + yed.append('x:y:z', [5, 6]) + yed.append('x:y:z', [5, 6]) + self.assertTrue(yed.get('x:y:z') == [1, 2, 3, [5, 6], [5, 6]]) + self.assertTrue(2 == yed.get('x:y:z').count([5, 6])) + self.assertFalse(yed.exists('x:y:z', 4)) + + def test_add_item_to_dict(self): + '''Testing update to dict''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('x:y:z', {'a': 1, 'b': 2}) + yed.update('x:y:z', {'c': 3, 'd': 4}) + self.assertTrue(yed.get('x:y:z') == {'a': 1, 'b': 2, 'c': 3, 'd': 4}) + self.assertTrue(yed.exists('x:y:z', {'c': 3})) + + def test_first_level_dict_with_none_value(self): + '''test dict value with none value''' + yed = Yedit(content={'a': None}, separator=":") + yed.put('a:b:c', 'test') + self.assertTrue(yed.get('a:b:c') == 'test') + self.assertTrue(yed.get('a:b'), {'c': 'test'}) + + def test_adding_yaml_variable(self): + '''test dict value with none value''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('z:y', '{{test}}') + self.assertTrue(yed.get('z:y') == '{{test}}') + + def test_keys_with_underscore(self): + '''test dict value with none value''' + yed = Yedit("yedit_test.yml", separator=':') + yed.put('z_:y_y', {'test': '{{test}}'}) + self.assertTrue(yed.get('z_:y_y') == {'test': '{{test}}'}) + + def test_first_level_array_update(self): + '''test update on top level array''' + yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}], separator=':') + yed.update('', {'c': 4}) + self.assertTrue({'c': 4} in yed.get('')) + + def test_first_level_array_delete(self): + '''test remove top level key''' + yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}]) + yed.delete('') + self.assertTrue({'b': 3} not in yed.get('')) + + def test_first_level_array_get(self): + '''test dict value with none value''' + yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}]) + yed.get('') + self.assertTrue([{'a': 1}, {'b': 2}, {'b': 3}] == yed.yaml_dict) + + def test_pop_list_item(self): + '''test dict value with none value''' + yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}], separator=':') + yed.pop('', {'b': 2}) + self.assertTrue([{'a': 1}, {'b': 3}] == yed.yaml_dict) + + def test_pop_list_item_2(self): + '''test dict value with none value''' + z = list(range(10)) + yed = Yedit(content=z, separator=':') + yed.pop('', 5) + z.pop(5) + self.assertTrue(z == yed.yaml_dict) + + def test_pop_dict_key(self): + '''test dict value with none value''' + yed = Yedit(content={'a': {'b': {'c': 1, 'd': 2}}}, separator='#') + yed.pop('a#b', 'c') + self.assertTrue({'a': {'b': {'d': 2}}} == yed.yaml_dict) + + def tearDown(self): + '''TearDown method''' + os.unlink(YeditTest.filename) + + +if __name__ == "__main__": + unittest.main() diff --git a/roles/lib_utils/src/test/unit/yedit_test.py b/roles/lib_utils/src/test/unit/yedit_test.py deleted file mode 100755 index ed07ac96e..000000000 --- a/roles/lib_utils/src/test/unit/yedit_test.py +++ /dev/null @@ -1,277 +0,0 @@ -#!/usr/bin/env python2 -''' - Unit tests for yedit -''' -# To run -# python -m unittest yedit_test -# -# ............................. -# ---------------------------------------------------------------------- -# Ran 29 tests in 0.133s -# OK - -import os -import sys -import unittest - -# Removing invalid variable names for tests so that I can -# keep them brief -# pylint: disable=invalid-name,no-name-in-module -# Disable import-error b/c our libraries aren't loaded in jenkins -# pylint: disable=import-error -# place yedit in our path -yedit_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 -sys.path.insert(0, yedit_path) - -from yedit import Yedit # noqa: E402 - -# pylint: disable=too-many-public-methods -# Silly pylint, moar tests! - - -class YeditTest(unittest.TestCase): - ''' - Test class for yedit - ''' - data = {'a': 'a', - 'b': {'c': {'d': [{'e': 'x'}, 'f', 'g']}}, - } # noqa: E124 - - filename = 'yedit_test.yml' - - def setUp(self): - ''' setup method will create a file and set to known configuration ''' - yed = Yedit(YeditTest.filename) - yed.yaml_dict = YeditTest.data - yed.write() - - def test_load(self): - ''' Testing a get ''' - yed = Yedit('yedit_test.yml') - self.assertEqual(yed.yaml_dict, self.data) - - def test_write(self): - ''' Testing a simple write ''' - yed = Yedit('yedit_test.yml') - yed.put('key1', 1) - yed.write() - self.assertTrue('key1' in yed.yaml_dict) - self.assertEqual(yed.yaml_dict['key1'], 1) - - def test_write_x_y_z(self): - '''Testing a write of multilayer key''' - yed = Yedit('yedit_test.yml') - yed.put('x.y.z', 'modified') - yed.write() - yed.load() - self.assertEqual(yed.get('x.y.z'), 'modified') - - def test_delete_a(self): - '''Testing a simple delete ''' - yed = Yedit('yedit_test.yml') - yed.delete('a') - yed.write() - yed.load() - self.assertTrue('a' not in yed.yaml_dict) - - def test_delete_b_c(self): - '''Testing delete of layered key ''' - yed = Yedit('yedit_test.yml', separator=':') - yed.delete('b:c') - yed.write() - yed.load() - self.assertTrue('b' in yed.yaml_dict) - self.assertFalse('c' in yed.yaml_dict['b']) - - def test_create(self): - '''Testing a create ''' - os.unlink(YeditTest.filename) - yed = Yedit('yedit_test.yml') - yed.create('foo', 'bar') - yed.write() - yed.load() - self.assertTrue('foo' in yed.yaml_dict) - self.assertTrue(yed.yaml_dict['foo'] == 'bar') - - def test_create_content(self): - '''Testing a create with content ''' - content = {"foo": "bar"} - yed = Yedit("yedit_test.yml", content) - yed.write() - yed.load() - self.assertTrue('foo' in yed.yaml_dict) - self.assertTrue(yed.yaml_dict['foo'], 'bar') - - def test_array_insert(self): - '''Testing a create with content ''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', 'inject') - self.assertTrue(yed.get('b:c:d[0]') == 'inject') - - def test_array_insert_first_index(self): - '''Testing a create with content ''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', 'inject') - self.assertTrue(yed.get('b:c:d[1]') == 'f') - - def test_array_insert_second_index(self): - '''Testing a create with content ''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', 'inject') - self.assertTrue(yed.get('b:c:d[2]') == 'g') - - def test_dict_array_dict_access(self): - '''Testing a create with content''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) - self.assertTrue(yed.get('b:c:d[0]:[0]:x:y') == 'inject') - - def test_dict_array_dict_replace(self): - '''Testing multilevel delete''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) - yed.put('b:c:d[0]:[0]:x:y', 'testing') - self.assertTrue('b' in yed.yaml_dict) - self.assertTrue('c' in yed.yaml_dict['b']) - self.assertTrue('d' in yed.yaml_dict['b']['c']) - self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'], list)) - self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0], list)) - self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0][0], dict)) - self.assertTrue('y' in yed.yaml_dict['b']['c']['d'][0][0]['x']) - self.assertTrue(yed.yaml_dict['b']['c']['d'][0][0]['x']['y'] == 'testing') # noqa: E501 - - def test_dict_array_dict_remove(self): - '''Testing multilevel delete''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) - yed.delete('b:c:d[0]:[0]:x:y') - self.assertTrue('b' in yed.yaml_dict) - self.assertTrue('c' in yed.yaml_dict['b']) - self.assertTrue('d' in yed.yaml_dict['b']['c']) - self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'], list)) - self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0], list)) - self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0][0], dict)) - self.assertFalse('y' in yed.yaml_dict['b']['c']['d'][0][0]['x']) - - def test_key_exists_in_dict(self): - '''Testing exist in dict''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) - self.assertTrue(yed.exists('b:c', 'd')) - - def test_key_exists_in_list(self): - '''Testing exist in list''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('b:c:d[0]', [{'x': {'y': 'inject'}}]) - self.assertTrue(yed.exists('b:c:d', [{'x': {'y': 'inject'}}])) - self.assertFalse(yed.exists('b:c:d', [{'x': {'y': 'test'}}])) - - def test_update_to_list_with_index(self): - '''Testing update to list with index''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('x:y:z', [1, 2, 3]) - yed.update('x:y:z', [5, 6], index=2) - self.assertTrue(yed.get('x:y:z') == [1, 2, [5, 6]]) - self.assertTrue(yed.exists('x:y:z', [5, 6])) - self.assertFalse(yed.exists('x:y:z', 4)) - - def test_update_to_list_with_curr_value(self): - '''Testing update to list with index''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('x:y:z', [1, 2, 3]) - yed.update('x:y:z', [5, 6], curr_value=3) - self.assertTrue(yed.get('x:y:z') == [1, 2, [5, 6]]) - self.assertTrue(yed.exists('x:y:z', [5, 6])) - self.assertFalse(yed.exists('x:y:z', 4)) - - def test_update_to_list(self): - '''Testing update to list''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('x:y:z', [1, 2, 3]) - yed.update('x:y:z', [5, 6]) - self.assertTrue(yed.get('x:y:z') == [1, 2, 3, [5, 6]]) - self.assertTrue(yed.exists('x:y:z', [5, 6])) - self.assertFalse(yed.exists('x:y:z', 4)) - - def test_append_twice_to_list(self): - '''Testing append to list''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('x:y:z', [1, 2, 3]) - yed.append('x:y:z', [5, 6]) - yed.append('x:y:z', [5, 6]) - self.assertTrue(yed.get('x:y:z') == [1, 2, 3, [5, 6], [5, 6]]) - self.assertTrue(2 == yed.get('x:y:z').count([5, 6])) - self.assertFalse(yed.exists('x:y:z', 4)) - - def test_add_item_to_dict(self): - '''Testing update to dict''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('x:y:z', {'a': 1, 'b': 2}) - yed.update('x:y:z', {'c': 3, 'd': 4}) - self.assertTrue(yed.get('x:y:z') == {'a': 1, 'b': 2, 'c': 3, 'd': 4}) - self.assertTrue(yed.exists('x:y:z', {'c': 3})) - - def test_first_level_dict_with_none_value(self): - '''test dict value with none value''' - yed = Yedit(content={'a': None}, separator=":") - yed.put('a:b:c', 'test') - self.assertTrue(yed.get('a:b:c') == 'test') - self.assertTrue(yed.get('a:b'), {'c': 'test'}) - - def test_adding_yaml_variable(self): - '''test dict value with none value''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('z:y', '{{test}}') - self.assertTrue(yed.get('z:y') == '{{test}}') - - def test_keys_with_underscore(self): - '''test dict value with none value''' - yed = Yedit("yedit_test.yml", separator=':') - yed.put('z_:y_y', {'test': '{{test}}'}) - self.assertTrue(yed.get('z_:y_y') == {'test': '{{test}}'}) - - def test_first_level_array_update(self): - '''test update on top level array''' - yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}], separator=':') - yed.update('', {'c': 4}) - self.assertTrue({'c': 4} in yed.get('')) - - def test_first_level_array_delete(self): - '''test remove top level key''' - yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}]) - yed.delete('') - self.assertTrue({'b': 3} not in yed.get('')) - - def test_first_level_array_get(self): - '''test dict value with none value''' - yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}]) - yed.get('') - self.assertTrue([{'a': 1}, {'b': 2}, {'b': 3}] == yed.yaml_dict) - - def test_pop_list_item(self): - '''test dict value with none value''' - yed = Yedit(content=[{'a': 1}, {'b': 2}, {'b': 3}], separator=':') - yed.pop('', {'b': 2}) - self.assertTrue([{'a': 1}, {'b': 3}] == yed.yaml_dict) - - def test_pop_list_item_2(self): - '''test dict value with none value''' - z = list(range(10)) - yed = Yedit(content=z, separator=':') - yed.pop('', 5) - z.pop(5) - self.assertTrue(z == yed.yaml_dict) - - def test_pop_dict_key(self): - '''test dict value with none value''' - yed = Yedit(content={'a': {'b': {'c': 1, 'd': 2}}}, separator='#') - yed.pop('a#b', 'c') - self.assertTrue({'a': {'b': {'d': 2}}} == yed.yaml_dict) - - def tearDown(self): - '''TearDown method''' - os.unlink(YeditTest.filename) - - -if __name__ == "__main__": - unittest.main() -- cgit v1.2.3