Update config.py with tests
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
from collections import OrderedDict
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dotenv import dotenv_values, set_key
|
||||
|
||||
from src.config import Config
|
||||
|
||||
CONFIG_PATH = Path(__file__).parent.resolve()
|
||||
TEST_DOT_ENV_PATH = Path(CONFIG_PATH, ".env_test")
|
||||
|
||||
EXPECTED_ENV_DICT: OrderedDict[str, str] = OrderedDict(
|
||||
{
|
||||
"KEY_1": "VALUE_1",
|
||||
"KEY_2": "VALUE_2",
|
||||
"NOTION_TOKEN": "1234454_234324",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def _prepare_test_dot_env() -> any:
|
||||
TEST_DOT_ENV_PATH.touch(mode=0o600, exist_ok=True)
|
||||
for key, value in EXPECTED_ENV_DICT.items():
|
||||
set_key(TEST_DOT_ENV_PATH, key, value)
|
||||
yield
|
||||
TEST_DOT_ENV_PATH.unlink()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def _load_test_dot_env(_prepare_test_dot_env: any) -> None:
|
||||
Config.init(dotenv_path=TEST_DOT_ENV_PATH)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_prepare_test_dot_env")
|
||||
def test_init_config() -> None:
|
||||
assert Config.env_dict == {}
|
||||
Config.init(dotenv_path=TEST_DOT_ENV_PATH)
|
||||
assert Config.env_dict == EXPECTED_ENV_DICT
|
||||
dict_from_file = dotenv_values(dotenv_path=TEST_DOT_ENV_PATH)
|
||||
assert dict_from_file == EXPECTED_ENV_DICT
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_load_test_dot_env")
|
||||
def test_get_config() -> None:
|
||||
assert Config.get_env("NON_EXISTING_KEY") is None
|
||||
key_1 = "KEY_1"
|
||||
assert Config.get_env(key_1) == EXPECTED_ENV_DICT[key_1]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_load_test_dot_env")
|
||||
def test_update_config() -> None:
|
||||
key = "KEY_1"
|
||||
value = EXPECTED_ENV_DICT[key]
|
||||
new_value = "NEW_VALUE"
|
||||
assert Config.get_env(key) == value
|
||||
Config.update_env(key, new_value)
|
||||
assert Config.get_env(key) == new_value
|
||||
dict_from_file = dotenv_values(dotenv_path=TEST_DOT_ENV_PATH)
|
||||
assert dict_from_file[key] == new_value
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_load_test_dot_env")
|
||||
def test_remove_config() -> None:
|
||||
key = "KEY_1"
|
||||
assert Config.get_env(key) == EXPECTED_ENV_DICT[key]
|
||||
Config.remove_env(key)
|
||||
assert Config.get_env(key) is None
|
||||
dict_from_file = dotenv_values(dotenv_path=TEST_DOT_ENV_PATH)
|
||||
assert key not in dict_from_file
|
||||
Reference in New Issue
Block a user