Exemplo n.º 1
0
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"
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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 3 levels deep maximum."
Exemplo n.º 5
0
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) == ""
Exemplo n.º 6
0
def test_missing_config_read_from_env_var_invalid_python(header):
    # arrange
    os.environ[f"{header.upper()}_INVALID_PYTHON"] = "a string without quotes"
    config = panaetius.Config(header)

    # act
    with pytest.raises(InvalidPythonException) as invalid_python_exception:
        panaetius.set_config(config, "invalid_python")

    # assert
    assert str(invalid_python_exception.value) == "a string without quotes is not valid Python."

    # cleanup
    del os.environ[f"{header.upper()}_INVALID_PYTHON"]
Exemplo n.º 7
0
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

    # cleanup
    del os.environ[f"{header.upper()}_MISSING_KEY_READ_FROM_ENV_VAR"]
Exemplo n.º 8
0
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]
Exemplo n.º 9
0
def test_get_value_environment_var_override(header, shared_datadir):
    # arrange
    os.environ[f"{header.upper()}_SOME_TOP_STRING"] = "some_overridden_value"
    config_path = str(shared_datadir / "without_logging")
    config = panaetius.Config(header, config_path)
    panaetius.set_config(config, "some_top_string")

    # act
    config_value = getattr(config, "some_top_string")

    # assert
    assert config_value == "some_overridden_value"

    # cleanup
    del os.environ[f"{header.upper()}_SOME_TOP_STRING"]
Exemplo n.º 10
0
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"

    # cleanup
    del os.environ[f"{header.upper()}_MISSING_KEY"]
Exemplo n.º 11
0
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
Exemplo n.º 12
0
"""Island code extractor for Animal Crossing."""
import os

import panaetius
from panaetius.config import Config

CONFIG = Config(path="~/.config/island-code-extractor",
                header="island-code-extractor")

panaetius.set_config(CONFIG, "reddit.secret")
panaetius.set_config(CONFIG, "reddit.id")
panaetius.set_config(CONFIG, "reddit.user_agent")
panaetius.set_config(CONFIG, "reddit.thread_id")
panaetius.set_config(CONFIG, "reddit.output_path")
Exemplo n.º 13
0
    # 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_FLOAT"] = "2.0"
    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"

    c = Config("panaetius_test")
    # c = Config("panaetius_test_noc")

    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")
    set_config(c, key="float_str", default="2.0")
    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")
    # set_config(c, key="bool")