Some pytests for config.py
This commit is contained in:
parent
1753821846
commit
7345177195
6 changed files with 151 additions and 10 deletions
18
.idea/runConfigurations/Tests.xml
Normal file
18
.idea/runConfigurations/Tests.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Tests" type="tests" factoryName="py.test">
|
||||
<module name="kiwi-scp" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<option name="SDK_HOME" value="$USER_HOME$/.cache/pypoetry/virtualenvs/kiwi-scp-rwgzy9um-py3.6/bin/python" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="IS_MODULE_SDK" value="false" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<option name="_new_keywords" value="""" />
|
||||
<option name="_new_parameters" value="""" />
|
||||
<option name="_new_additionalArguments" value="""" />
|
||||
<option name="_new_target" value=""tests"" />
|
||||
<option name="_new_targetType" value=""PATH"" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
|
@ -1,5 +1,5 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="kiwi_next" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||
<configuration default="false" name="kiwi_next" type="PythonConfigurationType" factoryName="Python">
|
||||
<module name="kiwi-scp" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
|
@ -11,11 +11,11 @@
|
|||
<option name="IS_MODULE_SDK" value="true" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/kiwi_scp/scripts/kiwi_next.py" />
|
||||
<option name="SCRIPT_NAME" value="kiwi_scp.scripts.kiwi_next" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="MODULE_MODE" value="true" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
|
|
|
@ -181,7 +181,7 @@ class Config(BaseModel):
|
|||
|
||||
result: Dict[str, Optional[str]] = {}
|
||||
for item in value:
|
||||
key, value = parse_str(item)
|
||||
key, value = parse_str(str(item))
|
||||
result[key] = value
|
||||
|
||||
return result
|
||||
|
@ -193,10 +193,12 @@ class Config(BaseModel):
|
|||
key, value = parse_str(value)
|
||||
return {key: value}
|
||||
|
||||
elif isinstance(value, int):
|
||||
# integer format (just define single oddly named variable)
|
||||
return {str(value): None}
|
||||
|
||||
else:
|
||||
# undefined format
|
||||
raise ValueError
|
||||
# any other format (try to coerce to str first)
|
||||
try:
|
||||
key, value = parse_str(str(value))
|
||||
return {key: value}
|
||||
|
||||
except Exception as e:
|
||||
# undefined format
|
||||
raise ValueError("Invalid Environment Format")
|
||||
|
|
|
@ -11,9 +11,12 @@ pydantic = "^1.8.2"
|
|||
|
||||
[tool.poetry.dev-dependencies]
|
||||
virtualenv = "^20.8.1"
|
||||
pytest = "^6.2.5"
|
||||
toml = "^0.10.2"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
kiwi = "kiwi_scp.scripts.kiwi:main"
|
||||
kiwi_next = "kiwi_scp.scripts.kiwi_next:main"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core>=1.0.0"]
|
||||
|
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
118
tests/test_config.py
Normal file
118
tests/test_config.py
Normal file
|
@ -0,0 +1,118 @@
|
|||
from ipaddress import IPv4Network
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import toml
|
||||
from pydantic import ValidationError
|
||||
|
||||
from kiwi_scp.config import Config
|
||||
|
||||
|
||||
def test_default():
|
||||
c = Config()
|
||||
version = toml.load("./pyproject.toml")["tool"]["poetry"]["version"]
|
||||
|
||||
assert c.version == version
|
||||
assert len(c.shells) == 1
|
||||
assert c.shells[0] == Path("/bin/bash")
|
||||
assert c.projects is None
|
||||
assert c.environment == {}
|
||||
assert c.storage.directory == Path("/var/local/kiwi")
|
||||
assert c.network.name == "kiwi_hub"
|
||||
assert c.network.cidr == IPv4Network("10.22.46.0/24")
|
||||
|
||||
|
||||
#############
|
||||
# ENVIRONMENT
|
||||
#############
|
||||
|
||||
def test_env_empty():
|
||||
c = Config(environment=None)
|
||||
|
||||
assert c.environment == {}
|
||||
|
||||
|
||||
def test_env_dict():
|
||||
c = Config(environment={})
|
||||
|
||||
assert c.environment == {}
|
||||
|
||||
c = Config(environment={
|
||||
"variable": "value"
|
||||
})
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "variable" in c.environment
|
||||
assert c.environment["variable"] == "value"
|
||||
|
||||
|
||||
def test_env_list():
|
||||
c = Config(environment=[])
|
||||
|
||||
assert c.environment == {}
|
||||
|
||||
c = Config(environment=[
|
||||
"variable=value",
|
||||
])
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "variable" in c.environment
|
||||
assert c.environment["variable"] == "value"
|
||||
|
||||
c = Config(environment=[
|
||||
"variable",
|
||||
])
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "variable" in c.environment
|
||||
assert c.environment["variable"] is None
|
||||
|
||||
c = Config(environment=[
|
||||
123,
|
||||
])
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "123" in c.environment
|
||||
assert c.environment["123"] is None
|
||||
|
||||
|
||||
def test_env_str():
|
||||
c = Config(environment="variable=value")
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "variable" in c.environment
|
||||
assert c.environment["variable"] == "value"
|
||||
|
||||
c = Config(environment="variable")
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "variable" in c.environment
|
||||
assert c.environment["variable"] is None
|
||||
|
||||
|
||||
def test_env_coercible():
|
||||
c = Config(environment=123)
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "123" in c.environment
|
||||
assert c.environment["123"] is None
|
||||
|
||||
c = Config(environment=123.4)
|
||||
|
||||
assert len(c.environment) == 1
|
||||
assert "123.4" in c.environment
|
||||
assert c.environment["123.4"] is None
|
||||
|
||||
|
||||
def test_env_undef():
|
||||
class UnCoercible:
|
||||
def __str__(self):
|
||||
raise ValueError
|
||||
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Config(environment=UnCoercible())
|
||||
|
||||
assert len(exc_info.value.errors()) == 1
|
||||
error = exc_info.value.errors()[0]
|
||||
assert error["msg"] == "Invalid Environment Format"
|
||||
assert error["type"] == "value_error"
|
Loading…
Reference in a new issue