diff --git a/__deprecated/test_descriptor.py b/__deprecated/test_descriptor.py index ca6f53e..3b1e0b8 100644 --- a/__deprecated/test_descriptor.py +++ b/__deprecated/test_descriptor.py @@ -17,8 +17,8 @@ import logging import pytest -from module.descriptor import Descriptor -from module.descriptor import DescriptionList +# from module.descriptor import Descriptor +# from module.descriptor import DescriptionList from boswatch.packet import Packet diff --git a/boswatch/configYaml.py b/boswatch/configYaml.py index 625acff..d045de0 100644 --- a/boswatch/configYaml.py +++ b/boswatch/configYaml.py @@ -16,6 +16,7 @@ """ import logging import yaml +import yaml.parser logging.debug("- %s loaded", __name__) @@ -46,9 +47,11 @@ class ConfigYAML: # use safe_load instead load self._config = yaml.safe_load(file) return True - except: # pragma: no cover - logging.exception("cannot load config file") - return False + except FileNotFoundError: + logging.error("config file not found: %s", configPath) + except yaml.parser.ParserError: + logging.exception("error in config file") + return False def get(self, *args, default=None): tmp = self._config diff --git a/test/boswatch/__init__.py b/test/boswatch/__init__.py new file mode 100644 index 0000000..9bad579 --- /dev/null +++ b/test/boswatch/__init__.py @@ -0,0 +1 @@ +# coding=utf-8 diff --git a/test/test_ServerClient.py b/test/boswatch/test_ServerClient.py similarity index 100% rename from test/test_ServerClient.py rename to test/boswatch/test_ServerClient.py diff --git a/test/test_broadcast.py b/test/boswatch/test_broadcast.py similarity index 100% rename from test/test_broadcast.py rename to test/boswatch/test_broadcast.py diff --git a/test/boswatch/test_config.py b/test/boswatch/test_config.py new file mode 100644 index 0000000..cbbc9f4 --- /dev/null +++ b/test/boswatch/test_config.py @@ -0,0 +1,75 @@ +#!/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 have to run as "pytest" unittest +""" +import logging +from typing import Dict + +import pytest + +from boswatch.configYaml import ConfigYAML + + +def setup_method(method): + logging.debug("[TEST] %s.%s", method.__module__, method.__name__) + + +@pytest.fixture +def getConfig(): + return ConfigYAML() + + +@pytest.fixture +def getFilledConfig(): + filledConfig = ConfigYAML() + filledConfig.loadConfigFile("test_config.yaml") + return filledConfig + + +def test_loadConfigFile(getConfig): + """!load a local config file""" + assert getConfig.loadConfigFile("test_config.yaml") is True + + +def test_loadConfigFileFailed(getConfig): + """!load a local config file with syntax error""" + assert getConfig.loadConfigFile("test_configFailed.yaml") is False + + +def test_loadConfigFileNotFound(getConfig): + """!load a local config file where is not available""" + assert getConfig.loadConfigFile("test_configNotFound.yaml") is False + + +def test_getTypes(getFilledConfig): + assert type(getFilledConfig.get("types")) is ConfigYAML + assert type(getFilledConfig.get("types", "string")) is str + assert type(getFilledConfig.get("types", "bool")) is bool + assert type(getFilledConfig.get("types", "integer")) is int + assert type(getFilledConfig.get("types", "float")) is float + + +def test_getNestedConfig(getFilledConfig): + nestedConfig = getFilledConfig.get("types") + assert type(nestedConfig) is ConfigYAML + assert nestedConfig.get("string") == "Hello World" + + +def test_configIterationList(getFilledConfig): + counter = 0 + for item in getFilledConfig.get("list"): + assert type(item) is str + counter += 1 + assert counter == 3 diff --git a/test/boswatch/test_config.yaml b/test/boswatch/test_config.yaml new file mode 100644 index 0000000..abdbd6b --- /dev/null +++ b/test/boswatch/test_config.yaml @@ -0,0 +1,10 @@ +types: + string: Hello World + bool: true + integer: 11 + float: 3.14 + +list: + - one + - two + - three \ No newline at end of file diff --git a/test/test.yaml b/test/boswatch/test_configFailed.yaml similarity index 78% rename from test/test.yaml rename to test/boswatch/test_configFailed.yaml index 5e9ea8f..e113d1e 100644 --- a/test/test.yaml +++ b/test/boswatch/test_configFailed.yaml @@ -1,5 +1,5 @@ types: string: Hello World - boolean: true + boolean: true integer: 11 float: 3.14 diff --git a/test/test_decoder.py b/test/boswatch/test_decoder.py similarity index 100% rename from test/test_decoder.py rename to test/boswatch/test_decoder.py diff --git a/test/test_header.py b/test/boswatch/test_header.py similarity index 100% rename from test/test_header.py rename to test/boswatch/test_header.py diff --git a/test/test_packet.py b/test/boswatch/test_packet.py similarity index 100% rename from test/test_packet.py rename to test/boswatch/test_packet.py diff --git a/test/test_paths.py b/test/boswatch/test_paths.py similarity index 100% rename from test/test_paths.py rename to test/boswatch/test_paths.py diff --git a/test/test_timer.py b/test/boswatch/test_timer.py similarity index 100% rename from test/test_timer.py rename to test/boswatch/test_timer.py diff --git a/test/module/__init__.py b/test/module/__init__.py new file mode 100644 index 0000000..9bad579 --- /dev/null +++ b/test/module/__init__.py @@ -0,0 +1 @@ +# coding=utf-8 diff --git a/test/test_doubleFilter.py b/test/module/test_doubleFilter.py similarity index 100% rename from test/test_doubleFilter.py rename to test/module/test_doubleFilter.py diff --git a/test/plugin/__init__.py b/test/plugin/__init__.py new file mode 100644 index 0000000..9bad579 --- /dev/null +++ b/test/plugin/__init__.py @@ -0,0 +1 @@ +# coding=utf-8 diff --git a/test/test_config.py b/test/test_config.py deleted file mode 100644 index f8dbc68..0000000 --- a/test/test_config.py +++ /dev/null @@ -1,104 +0,0 @@ -#!/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 have to run as "pytest" unittest -""" -import logging -import pytest - -from boswatch.utils import paths -from boswatch import configYaml - -# FIXME complete tests - -@pytest.mark.skip -class Test_Config: - """!Unittests for the config""" - - def setup_method(self, method): - logging.debug("[TEST] %s.%s", method.__module__, method.__name__) - - def test_loadLocalConfig(self): - """!load a local config file""" - bwConfig = configYaml.loadConfigFile(paths.TEST_PATH + "test.yaml") - assert bwConfig is not None - - def test_getLocalConfig(self): - """!get values from local config file""" - bwConfig = configYaml.loadConfigFile(paths.TEST_PATH + "test.yaml") - assert bwConfig["types"]["integer"] == 11 - assert bwConfig["types"]["boolean"] is True - - def test_getLocalConfigFailed(self): - """!fail while get values from local config file""" - bwConfig = configYaml.loadConfigFile(paths.TEST_PATH + "test.yaml") - assert bwConfig["test"]["abc"] is None - assert bwConfig["abc"]["test"] is None - - def test_shareConfig(self): - """!load local config file and share it""" - configYaml.loadConfigFile(paths.TEST_PATH + "test.yaml", "test_shareConfig") - assert configYaml.loadConfigSharepoint("test_shareConfig") is not None - - # def test_shareConfigUsed(self): - # """!load local config file and tr to share it twice with same name""" - # bwConfig1 = Config() - # bwConfig1.loadConfigFile(paths.TEST_PATH + "test.yaml", "test_shareConfigUsed") - # assert bwConfig1._sharePoints["test_shareConfigUsed"] is not None - # bwConfig2 = Config() - # bwConfig2.loadConfigFile(paths.TEST_PATH + "test.yaml") - # assert not bwConfig2._shareConfig("test_shareConfigUsed") - # - # def test_getNotSetSharedConfig(self): - # """!try to get values from shared config where not exists""" - # bwConfig = Config() - # bwConfig.loadConfigFile(paths.TEST_PATH + "test.yaml") - # assert bwConfig.getInt("test", "one") == 1 - # assert bwConfig.getInt("test", "one", "NotSetSharedConfig") is None - # - # def test_getSharedConfig(self): - # """!get values from shared config file""" - # bwConfig1 = Config() - # bwConfig1.loadConfigFile(paths.TEST_PATH + "test.yaml", "test_getSharedConfig") - # assert bwConfig1._sharePoints["test_getSharedConfig"] is not None - # - # bwConfig2 = Config() - # assert bwConfig2.getStr("test", "one") is None - # assert bwConfig2.getInt("test", "one", "test_getSharedConfig") == 1 - # - # def test_getSharedConfigFailed(self): - # """!fail while get values from shared config file""" - # bwConfig1 = Config() - # bwConfig1.loadConfigFile(paths.TEST_PATH + "test.yaml", "test_getSharedConfigFailed") - # assert bwConfig1._sharePoints["test_getSharedConfigFailed"] is not None - # - # bwConfig2 = Config() - # assert bwConfig2.getStr("test", "abc", "test_getSharedConfigFailed") is None - # assert bwConfig2.getStr("abc", "test", "test_getSharedConfigFailed") is None - # - # def test_getDataTypes(self): - # bwConfig = Config() - # bwConfig.loadConfigFile(paths.TEST_PATH + "test.yaml") - # assert bwConfig.getStr("testcase", "test") == "ok" - # assert bwConfig.getInt("test", "one") == 1 - # assert bwConfig.getInt("boolTest", "three") == 0 - # assert bwConfig.getBool("boolTest", "one") - # assert bwConfig.getBool("boolTest", "two") - # assert not bwConfig.getBool("boolTest", "three") - # assert not bwConfig.getBool("boolTest", "four") - # - # def test_getAllSharepoints(self): - # bwConfig = Config() - # bwConfig.loadConfigFile(paths.TEST_PATH + "test.yaml", "test_shareConfig") - # assert bwConfig.getAllSharepoints() is not None