From 454e2ca93568c189a7365190de29100b0f92a3ae Mon Sep 17 00:00:00 2001 From: Ashwini Mhatre Date: Mon, 31 May 2021 16:24:07 +0530 Subject: [PATCH] Improve coverage for from_xml and to_xml (#76) Improve coverage for from_xml and to_xml Reviewed-by: https://github.com/apps/ansible-zuul --- .../improve_coverage_for_filter_plugins.yaml | 3 ++ tests/unit/plugins/filter/test_from_xml.py | 33 +++++++++++++---- tests/unit/plugins/filter/test_to_xml.py | 36 ++++++++++++++----- 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 changelogs/fragments/improve_coverage_for_filter_plugins.yaml diff --git a/changelogs/fragments/improve_coverage_for_filter_plugins.yaml b/changelogs/fragments/improve_coverage_for_filter_plugins.yaml new file mode 100644 index 0000000..8782945 --- /dev/null +++ b/changelogs/fragments/improve_coverage_for_filter_plugins.yaml @@ -0,0 +1,3 @@ +--- +trivial: + - Improve coverage of uts related to to_xml and from_xml filter plugin. diff --git a/tests/unit/plugins/filter/test_from_xml.py b/tests/unit/plugins/filter/test_from_xml.py index 0ff51fe..37108e2 100644 --- a/tests/unit/plugins/filter/test_from_xml.py +++ b/tests/unit/plugins/filter/test_from_xml.py @@ -9,7 +9,8 @@ __metaclass__ = type import unittest from ansible.errors import AnsibleError -from ansible_collections.ansible.utils.plugins.filter.from_xml import from_xml +from ansible.errors import AnsibleFilterError +from ansible_collections.ansible.utils.plugins.filter.from_xml import _from_xml INVALID_DATA = '' @@ -30,11 +31,10 @@ class TestFromXml(unittest.TestCase): """Check passing invalid argspec""" # missing required arguments - args = [INVALID_DATA, "xmltodict"] + args = ["", INVALID_DATA, "xmltodict"] kwargs = {} with self.assertRaises(AnsibleError) as error: - from_xml(*args, **kwargs) - print(str(error.exception)) + _from_xml(*args, **kwargs) self.assertIn( "Error when using plugin 'from_xml': Input Xml is not valid", str(error.exception), @@ -43,7 +43,26 @@ class TestFromXml(unittest.TestCase): def test_valid_data(self): """Check passing valid data as per criteria""" self.maxDiff = None - args = [VALID_DATA, "xmltodict"] - result = from_xml(*args) - print(result) + args = ["", VALID_DATA, "xmltodict"] + result = _from_xml(*args) self.assertEqual(result, OUTPUT) + + def test_args(self): + """Check passing invalid argspec""" + + # missing required arguments + args = [] + kwargs = {} + with self.assertRaises(AnsibleFilterError) as error: + _from_xml(*args, **kwargs) + self.assertIn("missing required arguments: data", str(error.exception)) + + def test_invalid_engine(self): + """Check passing invalid argspec""" + + # missing required arguments + args = ["", INVALID_DATA, "test"] + kwargs = {} + with self.assertRaises(AnsibleError) as error: + _from_xml(*args, **kwargs) + self.assertIn("engine: test is not supported", str(error.exception)) diff --git a/tests/unit/plugins/filter/test_to_xml.py b/tests/unit/plugins/filter/test_to_xml.py index 48cf7e8..ddccada 100644 --- a/tests/unit/plugins/filter/test_to_xml.py +++ b/tests/unit/plugins/filter/test_to_xml.py @@ -9,7 +9,8 @@ __metaclass__ = type import unittest from ansible.errors import AnsibleError -from ansible_collections.ansible.utils.plugins.filter.to_xml import to_xml +from ansible.errors import AnsibleFilterError +from ansible_collections.ansible.utils.plugins.filter.to_xml import _to_xml INVALID_DATA = '' @@ -31,20 +32,37 @@ class TestToXml(unittest.TestCase): """Check passing invalid argspec""" # missing required arguments - args = [INVALID_DATA, "xmltodict"] + args = ["", INVALID_DATA, "xmltodict"] kwargs = {} with self.assertRaises(AnsibleError) as error: - to_xml(*args, **kwargs) - print(str(error.exception)) + _to_xml(*args, **kwargs) self.assertIn( - "Error when using plugin 'to_xml': Input json is not valid", - str(error.exception), + "we were unable to convert to dict", str(error.exception) ) def test_valid_data(self): """Check passing valid data as per criteria""" self.maxDiff = None - args = [VALID_DATA, "xmltodict"] - result = to_xml(*args) - print(result) + args = ["", VALID_DATA, "xmltodict"] + result = _to_xml(*args) self.assertEqual(result, OUTPUT) + + def test_args(self): + """Check passing invalid argspec""" + + # missing required arguments + args = [] + kwargs = {} + with self.assertRaises(AnsibleFilterError) as error: + _to_xml(*args, **kwargs) + self.assertIn("missing required arguments: data", str(error.exception)) + + def test_invalid_engine(self): + """Check passing invalid argspec""" + + # missing required arguments + args = ["", VALID_DATA, "test"] + kwargs = {} + with self.assertRaises(AnsibleError) as error: + _to_xml(*args, **kwargs) + self.assertIn("engine: test is not supported", str(error.exception))