2024-08-02 05:00:42 -06:00
|
|
|
import pytest
|
|
|
|
|
2020-07-18 23:02:44 -06:00
|
|
|
from app_skellington import _util
|
2024-08-02 05:00:42 -06:00
|
|
|
from app_skellington.cfg import Config
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sample_configspec_filepath():
|
2024-08-02 05:00:42 -06:00
|
|
|
return _util.get_asset(__name__, "sample_config.spec")
|
|
|
|
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sample_configini_filepath():
|
2024-08-02 05:00:42 -06:00
|
|
|
return _util.get_asset(__name__, "sample_config.ini")
|
|
|
|
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sample_full_configspec_filepath():
|
2024-08-02 05:00:42 -06:00
|
|
|
return _util.get_asset(__name__, "sample_config_full.spec")
|
|
|
|
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sample_full_configini_filepath():
|
2024-08-02 05:00:42 -06:00
|
|
|
return _util.get_asset(__name__, "sample_config_full.ini")
|
|
|
|
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sample_invalid_configspec_filepath():
|
2024-08-02 05:00:42 -06:00
|
|
|
return _util.get_asset(__name__, "sample_config_invalid.spec")
|
|
|
|
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
class TestConfig_e2e:
|
2024-08-02 05:00:42 -06:00
|
|
|
def test_allows_reading_ini_and_no_spec(self, sample_configini_filepath):
|
|
|
|
cfg = Config(configini_filepath=sample_configini_filepath)
|
2024-11-16 00:57:00 -07:00
|
|
|
assert (
|
|
|
|
cfg["root_option"] == "root_option_val"
|
|
|
|
), "expecting default from config.spec (didnt get)"
|
|
|
|
assert (
|
|
|
|
cfg["app"]["sub_option"] == "sub_option_val"
|
|
|
|
), "expecting default for sub option"
|
2024-08-02 05:00:42 -06:00
|
|
|
|
|
|
|
def test_allows_reading_spec_and_no_ini(self, sample_configspec_filepath):
|
|
|
|
cfg = Config(configspec_filepath=sample_configspec_filepath)
|
2024-11-16 00:57:00 -07:00
|
|
|
assert (
|
|
|
|
cfg["root_option"] == "def_string"
|
|
|
|
), "expecting default from config.spec (didnt get)"
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
# NOTE(MG) Changed the functionality to not do it this way.
|
|
|
|
# def test_constructor_fails_with_invalid_spec(
|
|
|
|
# self, sample_invalid_configspec_filepath
|
|
|
|
# ):
|
|
|
|
# with pytest.raises(Exception):
|
|
|
|
# cfg = Config(
|
|
|
|
# configspec_filepath=sample_invalid_configspec_filepath
|
|
|
|
# )
|
|
|
|
|
2024-08-02 05:00:42 -06:00
|
|
|
def test_allows_options_beyond_spec(self, sample_configspec_filepath):
|
|
|
|
cfg = Config(configspec_filepath=sample_configspec_filepath)
|
|
|
|
cfg["foo"] = "test my value"
|
|
|
|
assert cfg["foo"] == "test my value"
|
2020-07-18 23:02:44 -06:00
|
|
|
|
2024-08-02 05:00:42 -06:00
|
|
|
cfg["app"]["bar"] = "another value"
|
|
|
|
assert cfg["app"]["bar"] == "another value"
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
# def test_can_read_config_file_mutiple_times(self):
|
|
|
|
# pass
|
|
|
|
|
2024-08-02 05:00:42 -06:00
|
|
|
def test_can_override_config_file_manually(self, sample_configini_filepath):
|
|
|
|
cfg = Config(configini_filepath=sample_configini_filepath)
|
|
|
|
cfg["root_option"] = "newval"
|
|
|
|
assert cfg["root_option"] == "newval"
|
2020-07-18 23:02:44 -06:00
|
|
|
|
2024-08-02 05:00:42 -06:00
|
|
|
cfg["app"]["sub_option"] = "another_new_val"
|
2024-11-16 00:57:00 -07:00
|
|
|
assert (
|
|
|
|
cfg["app"]["sub_option"] == "another_new_val"
|
|
|
|
), "expecting default for sub option"
|
2020-07-18 23:02:44 -06:00
|
|
|
|
|
|
|
def test_can_set_option_without_config(self):
|
|
|
|
cfg = Config()
|
2024-08-02 05:00:42 -06:00
|
|
|
cfg["foo"] = "test my value"
|
|
|
|
assert cfg["foo"] == "test my value"
|
|
|
|
|
|
|
|
cfg["app"] = {}
|
|
|
|
cfg["app"]["bar"] = "another value"
|
|
|
|
assert cfg["app"]["bar"] == "another value"
|
2020-07-18 23:02:44 -06:00
|
|
|
|
2024-08-02 05:00:42 -06:00
|
|
|
def test_uses_spec_as_defaults(self, sample_configspec_filepath):
|
|
|
|
cfg = Config(configspec_filepath=sample_configspec_filepath)
|
2024-11-16 00:57:00 -07:00
|
|
|
assert (
|
|
|
|
cfg["root_option"] == "def_string"
|
|
|
|
), "expecting default from config.spec (didnt get)"
|
2024-08-02 05:00:42 -06:00
|
|
|
assert cfg["app"]["sub_option"] == "def_sub", "expecting default for sub option"
|