49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
import tempfile
|
|
|
|
import pytest
|
|
import yaml
|
|
|
|
from image_prediction.config import Config
|
|
|
|
|
|
@pytest.fixture
|
|
def config_file_content():
|
|
return {"A": [{"B": [1, 2]}, {"C": 3}, 4], "D": {"E": {"F": True}}}
|
|
|
|
|
|
@pytest.fixture
|
|
def config(config_file_content):
|
|
with tempfile.NamedTemporaryFile(suffix=".yaml", mode="w") as f:
|
|
yaml.dump(config_file_content, f, default_flow_style=False)
|
|
yield Config(f.name)
|
|
|
|
|
|
def test_dot_access_key_exists(config):
|
|
assert config.A == [{"B": [1, 2]}, {"C": 3}, 4]
|
|
assert config.D.E["F"]
|
|
|
|
|
|
def test_access_key_exists(config):
|
|
assert config["A"] == [{"B": [1, 2]}, {"C": 3}, 4]
|
|
assert config["A"][0] == {"B": [1, 2]}
|
|
assert config["A"][0]["B"] == [1, 2]
|
|
assert config["A"][0]["B"][0] == 1
|
|
|
|
|
|
def test_dot_access_key_does_not_exists(config):
|
|
assert config.B is None
|
|
|
|
|
|
def test_access_key_does_not_exists(config):
|
|
assert config["B"] is None
|
|
|
|
|
|
def test_get_method_returns_key_if_key_does_exist(config):
|
|
dot_indexable = config.D.E
|
|
assert dot_indexable.get("F", "default_value") is True
|
|
|
|
|
|
def test_get_method_returns_default_if_key_does_not_exist(config):
|
|
dot_indexable = config.D.E
|
|
assert dot_indexable.get("X", "default_value") == "default_value"
|