From 2b4905959434301e42259561f75e4548c99de6cf Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Mon, 8 Jan 2018 14:35:54 +0100 Subject: [PATCH] new tests for config class --- boswatch/config.py | 54 ++++++++++++++++++++----------- boswatch/utils/paths.py | 1 + test/test.ini | 6 ++++ test/test_config.py | 71 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 test/test.ini create mode 100644 test/test_config.py diff --git a/boswatch/config.py b/boswatch/config.py index 4238951..a130e90 100644 --- a/boswatch/config.py +++ b/boswatch/config.py @@ -61,18 +61,19 @@ class Config: def __init__(self, configPath="", shareName=""): """!Create a new config object and load the ini file directly - @param configPath: Path to the ini file - @param shareName: Name for the global share point (empty is only local)""" - self._config = None - self._loadConfigFile(configPath) - if shareName: - self._shareConfig(shareName) + @param configPath: If you like to load a ini file + @param shareName: If you like to share the config""" + self._config = configparser.ConfigParser() + if configPath: + self._loadConfigFile(configPath) + if shareName: + self._shareConfig(shareName) def _loadConfigFile(self, configPath): """!loads a given configuration in the class wide config variable @param configPath: Path to the config file - @return status of loading""" + @return True or False""" logging.debug("load config file from: %s", configPath) try: self._config.read(configPath, "utf-8") @@ -85,14 +86,16 @@ class Config: """!Shares the configuration Shares the local _config to teh class wide global _sharedConfig - @param shareName: Name of the global share point""" + @param shareName: Name of the global share point + @return True or False""" try: bool(self._sharedConfig[shareName]) + logging.error("cannot share config - name is always in use: %s", shareName) + return False + except: self._sharedConfig[shareName] = self._config logging.debug("shared configuration as: %s", shareName) - except: - logging.error("cannot share config because the name is always in use: %s", shareName) - + return True def getConfig(self, section, key, shareName=""): """!Method to read a single config entry @@ -100,12 +103,27 @@ class Config: @param section: Section to read from @param key: Value to read @param shareName: Name of the global config share (empty is only local) - @return The value from config file""" - try: - if shareName: + @return The value or None""" + if shareName: + try: return self._sharedConfig[shareName].get(section, key) - else: - return self._config.get(section, key) - except: # pragma: no cover - logging.exception("Error while reading a config entry") + except KeyError: + logging.error("no shared config named: %s", shareName) + except configparser.NoSectionError: + logging.error("no shared config section: %s", section) + except configparser.NoOptionError: + logging.error("no shared config option: %s", key) + except: # pragma: no cover + logging.exception("error while reading shared config") + return None + + else: + try: + return self._config.get(section, key) + except configparser.NoSectionError: + logging.error("no local config section: %s", section) + except configparser.NoOptionError: + logging.error("no local config option: %s", key) + except: # pragma: no cover + logging.exception("error while reading local config") return None diff --git a/boswatch/utils/paths.py b/boswatch/utils/paths.py index 164a6a5..2ed5056 100644 --- a/boswatch/utils/paths.py +++ b/boswatch/utils/paths.py @@ -25,6 +25,7 @@ LOG_PATH = ROOT_PATH + "/log/" CONFIG_PATH = ROOT_PATH + "/config/" CSV_PATH = ROOT_PATH + "/csv/" BIN_PATH = ROOT_PATH + "/_bin/" +TEST_PATH = ROOT_PATH + "/test/" def createIfNotExist(dirPath): diff --git a/test/test.ini b/test/test.ini new file mode 100644 index 0000000..4e5f396 --- /dev/null +++ b/test/test.ini @@ -0,0 +1,6 @@ +[test] +one = 1 +two = two + +[testcase] +test = ok \ No newline at end of file diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 0000000..b3ce673 --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,71 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""! + ____ ____ ______ __ __ __ _____ + / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / + / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < + / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / +/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ + German BOS Information Script + by Bastian Schroll + +@file: test_config.py +@date: 08.01.2017 +@author: Bastian Schroll +@description: Unittests for BOSWatch. File must be run as "pytest" unittest +""" + +# import pytest # import the pytest framework + +from boswatch.utils import paths +from boswatch.config import Config + + +class Test_Config: + """!Unittests for the config""" + + def test_loadLocalConfig(self): + bwConfig = Config(paths.TEST_PATH + "test.ini") + assert bwConfig._config is not None + + def test_getLocalConfig(self): + bwConfig = Config(paths.TEST_PATH + "test.ini") + assert bwConfig.getConfig("test", "one") == "1" + assert bwConfig.getConfig("test", "two") == "two" + assert bwConfig.getConfig("testcase", "test") == "ok" + + def test_getLocalConfigFailed(self): + bwConfig = Config(paths.TEST_PATH + "test.ini") + assert bwConfig.getConfig("test", "abc") is None + assert bwConfig.getConfig("abc", "test") is None + + def test_shareConfig(self): + bwConfig = Config(paths.TEST_PATH + "test.ini", "test_shareConfig") + assert bwConfig._sharedConfig["test_shareConfig"] is not None + + def test_shareConfigUsed(self): + bwConfig1 = Config(paths.TEST_PATH + "test.ini", "test_shareConfigUsed") + assert bwConfig1._sharedConfig["test_shareConfigUsed"] is not None + bwConfig2 = Config(paths.TEST_PATH + "test.ini") + assert bwConfig2._shareConfig("test_shareConfigUsed") is False + + def test_getNotSetSharedConfig(self): + bwConfig = Config(paths.TEST_PATH + "test.ini") + assert bwConfig.getConfig("test", "one") == "1" + assert bwConfig.getConfig("test", "one", "NotSetSharedConfig") is None + + def test_getSharedConfig(self): + bwConfig1 = Config(paths.TEST_PATH + "test.ini", "test_getSharedConfig") + assert bwConfig1._sharedConfig["test_getSharedConfig"] is not None + + bwConfig2 = Config() + assert bwConfig2.getConfig("test", "one") is None + assert bwConfig2.getConfig("test", "one", "test_getSharedConfig") == "1" + + def test_getSharedConfigFailed(self): + bwConfig1 = Config(paths.TEST_PATH + "test.ini", "test_getSharedConfigFailed") + assert bwConfig1._sharedConfig["test_getSharedConfigFailed"] is not None + + bwConfig2 = Config() + assert bwConfig2.getConfig("test", "abc", "test_getSharedConfigFailed") is None + assert bwConfig2.getConfig("abc", "test", "test_getSharedConfigFailed") is None