mirror of
https://github.com/dtomlinson91/panaetius.git
synced 2025-12-22 04:55:44 +00:00
adding latest tests
This commit is contained in:
@@ -4,3 +4,20 @@ import pytest
|
||||
@pytest.fixture()
|
||||
def header():
|
||||
return "panaetius_testing"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def testing_config_contents():
|
||||
return {
|
||||
"panaetius_testing": {
|
||||
"some_top_string": "some_top_value",
|
||||
"second": {
|
||||
"some_second_string": "some_second_value",
|
||||
"some_second_int": 1,
|
||||
"some_second_float": 1.0,
|
||||
"some_second_list": ["some", "second", "value"],
|
||||
"some_second_table": {"first": ["some", "first", "value"]},
|
||||
"some_second_table_bools": {"bool": [True, False]},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,4 @@ some_second_int = 1
|
||||
some_second_float = 1.0
|
||||
some_second_list = ["some", "second", "value"]
|
||||
some_second_table = { "first" = ["some", "first", "value"] }
|
||||
|
||||
# [panaetius_testing.logging]
|
||||
# path = ""
|
||||
some_second_table_bools = { "bool" = [true, false] }
|
||||
@@ -1,54 +1,57 @@
|
||||
import os
|
||||
|
||||
from panaetius import Config, set_config, set_logger, SimpleLogger
|
||||
# from panaetius.logging import AdvancedLogger
|
||||
|
||||
from panaetius.logging import AdvancedLogger
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ["PANAETIUS_TEST_PATH"] = "/usr/local"
|
||||
os.environ["PANAETIUS_TEST_BOOL"] = "true"
|
||||
print(os.environ.get("PANAETIUS_TEST_PATH"))
|
||||
os.environ["PANAETIUS_TEST_PATH"] = '"/usr/local"'
|
||||
os.environ["PANAETIUS_TEST_BOOL"] = "True"
|
||||
# print(os.environ.get("PANAETIUS_TEST_PATH"))
|
||||
# os.environ[
|
||||
# "PANAETIUS_TEST_TOML_POINTS"
|
||||
# ] = "[ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, { x = 2, y = 4, z = 8 }]"
|
||||
|
||||
os.environ["PANAETIUS_TEST_NOC_PATH"] = "/usr/locals"
|
||||
os.environ["PANAETIUS_TEST_NOC_PATH"] = '"/usr/locals"'
|
||||
os.environ["PANAETIUS_TEST_NOC_FLOAT"] = "2.0"
|
||||
os.environ["PANAETIUS_TEST_NOC_BOOL"] = "true"
|
||||
os.environ["PANAETIUS_TEST_NOC_EMBEDDED_PATH"] = "/usr/local"
|
||||
os.environ["PANAETIUS_TEST_NOC_BOOL"] = "True"
|
||||
os.environ["PANAETIUS_TEST_NOC_EMBEDDED_PATH"] = '"/usr/local"'
|
||||
os.environ["PANAETIUS_TEST_NOC_EMBEDDED_FLOAT"] = "2.0"
|
||||
os.environ["PANAETIUS_TEST_NOC_EMBEDDED_BOOL"] = "true"
|
||||
os.environ["PANAETIUS_TEST_NOC_EMBEDDED_BOOL"] = "True"
|
||||
|
||||
# c = Config("panaetius_test")
|
||||
c = Config("panaetius_test_noc")
|
||||
c = Config("panaetius_test")
|
||||
# c = Config("panaetius_test_noc")
|
||||
|
||||
set_config(c, key="toml.points", coerce=True)
|
||||
set_config(c, key="toml.points")
|
||||
set_config(c, key="path", default="some path")
|
||||
set_config(c, key="top", default="some top")
|
||||
set_config(c, key="logging.path")
|
||||
set_config(c, key="nonexistent.item", default="some nonexistent item")
|
||||
set_config(c, key="nonexistent.item")
|
||||
set_config(c, key="toml.points_config")
|
||||
set_config(c, key="float", coerce=True)
|
||||
set_config(c, key="float")
|
||||
set_config(c, key="float_str", default="2.0")
|
||||
set_config(c, key="bool", coerce=True)
|
||||
set_config(c, key="bool")
|
||||
set_config(c, key="noexistbool", default=False)
|
||||
set_config(c, key="middle.middle")
|
||||
|
||||
# set_config(c, key="path")
|
||||
# set_config(c, key="float", coerce=True)
|
||||
# set_config(c, key="bool", coerce=True)
|
||||
# set_config(c, key="float")
|
||||
# set_config(c, key="bool")
|
||||
# set_config(c, key="noexiststr", default="2.0")
|
||||
# set_config(c, key="noexistfloat", default=2.0)
|
||||
# set_config(c, key="noexistbool", default=False)
|
||||
|
||||
set_config(c, key="embedded.path")
|
||||
set_config(c, key="embedded.float", coerce=True)
|
||||
set_config(c, key="embedded.bool", coerce=True)
|
||||
set_config(c, key="embedded.float")
|
||||
set_config(c, key="embedded.bool")
|
||||
set_config(c, key="embedded.noexiststr", default="2.0")
|
||||
set_config(c, key="embedded.noexistfloat", default=2.0)
|
||||
set_config(c, key="embedded.noexistbool", default=False)
|
||||
|
||||
logger = set_logger(c, SimpleLogger())
|
||||
# logger = set_logger(c, AdvancedLogger(logging_level="INFO"))
|
||||
# logger = set_logger(c, SimpleLogger())
|
||||
logger = set_logger(c, AdvancedLogger(logging_level="DEBUG"))
|
||||
logger.info("test logging message")
|
||||
logger.debug("debugging message")
|
||||
for i in dir(c):
|
||||
logger.debug(i + ": " + str(getattr(c, i)) + " - " + str(type(getattr(c, i))))
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import os
|
||||
import pathlib
|
||||
from uuid import uuid4
|
||||
|
||||
import toml
|
||||
import pytest
|
||||
|
||||
import panaetius
|
||||
from panaetius.exceptions import KeyErrorTooDeepException
|
||||
|
||||
# test config paths
|
||||
|
||||
|
||||
def test_default_config_path_set(header):
|
||||
@@ -13,9 +18,9 @@ def test_default_config_path_set(header):
|
||||
assert str(config.config_path) == str(pathlib.Path.home() / ".config")
|
||||
|
||||
|
||||
def test_user_config_path_set(header, datadir):
|
||||
def test_user_config_path_set(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(datadir / "without_logging")
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
@@ -24,13 +29,182 @@ def test_user_config_path_set(header, datadir):
|
||||
assert str(config.config_path) == config_path
|
||||
|
||||
|
||||
def test_config_file_exists(header, datadir):
|
||||
# test config files
|
||||
|
||||
|
||||
def test_config_file_exists(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(datadir / "without_logging")
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
_ = config.config
|
||||
|
||||
# assert
|
||||
assert config._missing_config is False
|
||||
|
||||
|
||||
def test_config_file_contents_read_success(header, shared_datadir, testing_config_contents):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
config_contents = config.config
|
||||
|
||||
# assert
|
||||
assert config._missing_config == False
|
||||
assert config_contents == testing_config_contents
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"set_config_key,get_config_key,expected_value",
|
||||
[
|
||||
("some_top_string", "some_top_string", "some_top_value"),
|
||||
("second.some_second_string", "second_some_second_string", "some_second_value"),
|
||||
(
|
||||
"second.some_second_list",
|
||||
"second_some_second_list",
|
||||
["some", "second", "value"],
|
||||
),
|
||||
(
|
||||
"second.some_second_table",
|
||||
"second_some_second_table",
|
||||
{"first": ["some", "first", "value"]},
|
||||
),
|
||||
(
|
||||
"second.some_second_table_bools",
|
||||
"second_some_second_table_bools",
|
||||
{"bool": [True, False]},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_get_value_from_key(
|
||||
set_config_key, get_config_key, expected_value, header, shared_datadir
|
||||
):
|
||||
"""
|
||||
Test the following:
|
||||
|
||||
- keys are read from top level key
|
||||
- keys are read from two level key
|
||||
- inline arrays are read correctly
|
||||
- inline tables are read correctly
|
||||
- inline tables & arrays read bools correctly
|
||||
"""
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
config = panaetius.Config(header, config_path)
|
||||
panaetius.set_config(config, set_config_key)
|
||||
|
||||
# act
|
||||
config_value = getattr(config, get_config_key)
|
||||
|
||||
# assert
|
||||
assert config_value == expected_value
|
||||
|
||||
|
||||
def test_key_level_too_deep(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
config = panaetius.Config(header, config_path)
|
||||
key = "a.key.too.deep"
|
||||
|
||||
# act
|
||||
with pytest.raises(KeyErrorTooDeepException) as key_error_too_deep:
|
||||
panaetius.set_config(config, key)
|
||||
|
||||
# assert
|
||||
assert (
|
||||
str(key_error_too_deep.value)
|
||||
== f"Your key of {key} can only be 2 levels deep maximum. "
|
||||
f"You have 4"
|
||||
)
|
||||
|
||||
|
||||
def test_get_value_missing_key_from_default(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
config = panaetius.Config(header, config_path)
|
||||
panaetius.set_config(
|
||||
config,
|
||||
"missing.key_from_default",
|
||||
default=["some", "default", "value", 1.0, True],
|
||||
)
|
||||
|
||||
# act
|
||||
default_value = getattr(config, "missing_key_from_default")
|
||||
|
||||
# assert
|
||||
assert default_value == ["some", "default", "value", 1.0, True]
|
||||
|
||||
|
||||
def test_get_value_missing_key_from_env(header, shared_datadir):
|
||||
# arrange
|
||||
os.environ[f"{header.upper()}_MISSING_KEY"] = '"some missing key"'
|
||||
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
config = panaetius.Config(header, config_path)
|
||||
panaetius.set_config(config, "missing_key")
|
||||
|
||||
# act
|
||||
value_from_key = getattr(config, "missing_key")
|
||||
|
||||
# assert
|
||||
assert value_from_key == "some missing key"
|
||||
|
||||
|
||||
# test env vars
|
||||
|
||||
|
||||
def test_config_file_does_not_exist(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "nonexistent_folder")
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
config_contents = config.config
|
||||
|
||||
# assert
|
||||
assert config._missing_config is True
|
||||
assert config_contents == {}
|
||||
|
||||
|
||||
def test_missing_config_read_from_default(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "nonexistent_folder")
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
panaetius.set_config(config, "missing.key_read_from_default", default=True)
|
||||
|
||||
# assert
|
||||
assert getattr(config, "missing_key_read_from_default") is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"env_value,expected_value",
|
||||
[
|
||||
('"a missing string"', "a missing string"),
|
||||
("1", 1),
|
||||
("1.0", 1.0),
|
||||
("True", True),
|
||||
(
|
||||
'["an", "array", "of", "items", 1, True]',
|
||||
["an", "array", "of", "items", 1, True],
|
||||
),
|
||||
(
|
||||
'{"an": "array", "of": "items", "1": True}',
|
||||
{"an": "array", "of": "items", "1": True},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_missing_config_read_from_env_var(env_value, expected_value, header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / str(uuid4()))
|
||||
os.environ[f"{header.upper()}_MISSING_KEY_READ_FROM_ENV_VAR"] = env_value
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
panaetius.set_config(config, "missing.key_read_from_env_var")
|
||||
|
||||
# assert
|
||||
assert getattr(config, "missing_key_read_from_env_var") == expected_value
|
||||
|
||||
13
tests/test_library.py
Normal file
13
tests/test_library.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import panaetius
|
||||
|
||||
|
||||
def test_set_config(header, shared_datadir):
|
||||
# arrange
|
||||
config_path = str(shared_datadir / "without_logging")
|
||||
|
||||
# act
|
||||
config = panaetius.Config(header, config_path)
|
||||
panaetius.set_config(config, "some_top_string")
|
||||
|
||||
# assert
|
||||
assert getattr(config, "some_top_string") == "some_top_value"
|
||||
34
tests/test_logging.py
Normal file
34
tests/test_logging.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import logging
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from panaetius import set_logger, SimpleLogger, Config, set_config
|
||||
from panaetius.exceptions import LoggingDirectoryDoesNotExistException
|
||||
|
||||
|
||||
def test_logging_directory_does_not_exist(header, shared_datadir):
|
||||
# arrange
|
||||
config = Config(header)
|
||||
logging_path = str(shared_datadir / str(uuid4()))
|
||||
set_config(config, "logging.path", default=str(logging_path))
|
||||
|
||||
# act
|
||||
with pytest.raises(LoggingDirectoryDoesNotExistException) as logging_exception:
|
||||
_ = set_logger(config, SimpleLogger())
|
||||
|
||||
# assert
|
||||
assert str(logging_exception.value) == ""
|
||||
|
||||
|
||||
def test_logging_directory_does_exist(header, shared_datadir):
|
||||
# arrange
|
||||
config = Config(header)
|
||||
logging_path = str(shared_datadir / "without_logging")
|
||||
set_config(config, "logging.path", default=str(logging_path))
|
||||
|
||||
# act
|
||||
logger = set_logger(config, SimpleLogger())
|
||||
|
||||
# assert
|
||||
assert isinstance(logger, logging.Logger)
|
||||
Reference in New Issue
Block a user