adding latest tests

This commit is contained in:
2021-10-18 00:12:20 +01:00
parent 4ae4eb085c
commit f73a6d2441
17 changed files with 402 additions and 95 deletions

View File

@@ -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