108 lines
3.4 KiB
Python
108 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
This program has been developed by students from the bachelor Computer Science at Utrecht
|
|
University within the Software Project course.
|
|
© Copyright Utrecht University (Department of Information and Computing Sciences)
|
|
"""
|
|
|
|
from robot_interface.utils.get_config import get_config
|
|
|
|
|
|
def test_get_config_prefers_explicit_value(monkeypatch):
|
|
"""
|
|
When a direct value is provided it should be returned without reading the environment.
|
|
"""
|
|
monkeypatch.setenv("GET_CONFIG_TEST", "from-env")
|
|
|
|
result = get_config("explicit", "GET_CONFIG_TEST", "default")
|
|
|
|
assert result == "explicit"
|
|
|
|
|
|
def test_get_config_returns_env_value(monkeypatch):
|
|
"""
|
|
If value is None the environment variable should be used.
|
|
"""
|
|
monkeypatch.setenv("GET_CONFIG_TEST", "from-env")
|
|
|
|
result = get_config(None, "GET_CONFIG_TEST", "default")
|
|
|
|
assert result == "from-env"
|
|
|
|
|
|
def test_get_config_casts_env_value(monkeypatch):
|
|
"""
|
|
The env value should be cast when a cast function is provided.
|
|
"""
|
|
monkeypatch.setenv("GET_CONFIG_PORT", "1234")
|
|
|
|
result = get_config(None, "GET_CONFIG_PORT", 0, int)
|
|
|
|
assert result == 1234
|
|
|
|
|
|
def test_get_config_casts_default_when_env_missing(monkeypatch):
|
|
"""
|
|
When the env var is missing it should fall back to the default and still apply the cast.
|
|
"""
|
|
monkeypatch.delenv("GET_CONFIG_MISSING", raising=False)
|
|
|
|
result = get_config(None, "GET_CONFIG_MISSING", "42", int)
|
|
|
|
assert result == 42
|
|
|
|
|
|
def test_get_config_unset_boolean_default(monkeypatch):
|
|
"""
|
|
When the env var is a boolean, and it's not set, ensure it uses the default value.
|
|
"""
|
|
monkeypatch.delenv("SOME_BOOLEAN_VARIABLE", raising=False)
|
|
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", False, bool)
|
|
assert result == False
|
|
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", True, bool)
|
|
assert result == True
|
|
|
|
|
|
def test_get_config_true_boolean(monkeypatch):
|
|
"""
|
|
When the env var is a boolean, and its value is "true", "TRUE", "yes", etc., it should return true.
|
|
"""
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "TRUE")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", False, bool)
|
|
assert result == True
|
|
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "true")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", False, bool)
|
|
assert result == True
|
|
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "yes")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", False, bool)
|
|
assert result == True
|
|
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "YES")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", False, bool)
|
|
assert result == True
|
|
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "TrUE")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", False, bool)
|
|
assert result == True
|
|
|
|
|
|
def test_get_config_false_boolean(monkeypatch):
|
|
"""
|
|
When the env var is a boolean, and its value is not "true", "TRUE", "yes", etc., it should return False.
|
|
"""
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "FALSE")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", True, bool)
|
|
assert result == False
|
|
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "false")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", True, bool)
|
|
assert result == False
|
|
|
|
monkeypatch.setenv("SOME_BOOLEAN_VARIABLE", "anything, tbh")
|
|
result = get_config(None, "SOME_BOOLEAN_VARIABLE", True, bool)
|
|
assert result == False
|