From 5f4564d8580264ebc80e5657eb080384aaa245dc Mon Sep 17 00:00:00 2001 From: Kilian Soltermann Date: Thu, 19 Oct 2023 15:12:31 +0200 Subject: [PATCH] added nested tests --- plugins/module_utils/xml_utils.py | 2 +- .../plugins/module_utils/test_config_utils.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/plugins/module_utils/xml_utils.py b/plugins/module_utils/xml_utils.py index 53e526f4..8d5d88d3 100644 --- a/plugins/module_utils/xml_utils.py +++ b/plugins/module_utils/xml_utils.py @@ -5,7 +5,7 @@ from __future__ import (absolute_import, division, print_function) -from typing import Union, Optional, List, Set +from typing import Union, Optional, List from xml.etree.ElementTree import Element __metaclass__ = type diff --git a/tests/unit/plugins/module_utils/test_config_utils.py b/tests/unit/plugins/module_utils/test_config_utils.py index 2e107fe8..42a14fd9 100644 --- a/tests/unit/plugins/module_utils/test_config_utils.py +++ b/tests/unit/plugins/module_utils/test_config_utils.py @@ -21,6 +21,9 @@ def sample_config_path(): config_content = """ test_value + + test_value + """ with NamedTemporaryFile(delete=False) as temp_file: temp_file.write(config_content.encode()) @@ -116,7 +119,7 @@ def test_save(sample_config_path): config["test_key"] = "modified_value" assert config.save() # Reload the saved config and assert the changes were saved - reloaded_config = xml_utils.etree_to_dict(ElementTree.parse(sample_config_path).getroot())["opnsense"] + reloaded_config = xml_utils.etree_to_dict(ElementTree.parse(sample_config_path).getroot()) assert reloaded_config["test_key"] == "modified_value" with OPNsenseConfig(path=sample_config_path) as new_config: @@ -153,3 +156,17 @@ def test_exit_without_saving(sample_config_path): with OPNsenseConfig(path=sample_config_path) as config: config["test_key"] = "modified_value" # The RuntimeError should be raised upon exiting the context without saving + + +def test_get_nested_item(sample_config_path): + """ + Test retrieving a value from the config. + + Given a sample OPNsense configuration file, the test verifies that a specific key-value pair + can be retrieved using the OPNsenseConfig object. + + The expected behavior is that the retrieved value matches the original value in the config file. + """ + with OPNsenseConfig(path=sample_config_path) as config: + assert config["test_nested_key_1"]["test_nested_key_2"] == "test_value" + assert not config.save()