Add ConfigFile
This commit is contained in:
parent
e01db3ead5
commit
9b8b288ba6
9 changed files with 153 additions and 0 deletions
3
great_ai/tests/utilities/data/bad.conf
Normal file
3
great_ai/tests/utilities/data/bad.conf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
zeroth_key="test"
|
||||
zeroth_key="test"
|
||||
5
great_ai/tests/utilities/data/env.conf
Normal file
5
great_ai/tests/utilities/data/env.conf
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
first_key=test
|
||||
|
||||
second_key="ENV:alma"
|
||||
third_key = ENV:alma
|
||||
15
great_ai/tests/utilities/data/good.conf
Normal file
15
great_ai/tests/utilities/data/good.conf
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
zeroth_key="test"
|
||||
first_key=test
|
||||
second_key = test 2 # comment is ignored
|
||||
third_key= "test= 2=="
|
||||
|
||||
fourth_key = "
|
||||
this#
|
||||
is
|
||||
multiline
|
||||
" # cannot write comments inside multiline
|
||||
|
||||
whitespace = hardly matters
|
||||
|
||||
|
||||
56
great_ai/tests/utilities/test_config_file.py
Normal file
56
great_ai/tests/utilities/test_config_file.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import unittest
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from src.great_ai.utilities.config_file import ConfigFile
|
||||
|
||||
DATA_PATH = Path(__file__).parent.resolve() / "data"
|
||||
|
||||
|
||||
class TestConfigFile(unittest.TestCase):
|
||||
def test_simple(self) -> None:
|
||||
c = ConfigFile(DATA_PATH / "good.conf")
|
||||
assert c.zeroth_key == 'test'
|
||||
assert c.first_key == 'test'
|
||||
assert c.second_key == 'test 2'
|
||||
assert c.third_key == 'test= 2=='
|
||||
assert c.fourth_key == '''
|
||||
this#
|
||||
is
|
||||
multiline
|
||||
'''
|
||||
assert c.whitespace == 'hardly matters'
|
||||
|
||||
|
||||
def test_string_path(self) -> None:
|
||||
c = ConfigFile(str(DATA_PATH / "good.conf"))
|
||||
assert c.zeroth_key == 'test'
|
||||
assert c.first_key == 'test'
|
||||
assert c.second_key == 'test 2'
|
||||
assert c.third_key == 'test= 2=='
|
||||
assert c.fourth_key == '''
|
||||
this#
|
||||
is
|
||||
multiline
|
||||
'''
|
||||
assert c.whitespace == 'hardly matters'
|
||||
|
||||
def test_env(self) -> None:
|
||||
import os
|
||||
os.environ['alma'] = '12'
|
||||
c = ConfigFile(DATA_PATH / "env.conf")
|
||||
del os.environ['alma']
|
||||
assert c.first_key == 'test'
|
||||
assert c.second_key == '12'
|
||||
|
||||
def test_env_not_exists(self) -> None:
|
||||
with pytest.raises(KeyError):
|
||||
ConfigFile(DATA_PATH / "env.conf")
|
||||
|
||||
def test_env_not_exists_but_ignored(self) -> None:
|
||||
with pytest.raises(KeyError):
|
||||
ConfigFile(DATA_PATH / "env.conf", ignore_missing_environment_variables=True)
|
||||
|
||||
def test_duplicate_key(self) -> None:
|
||||
with pytest.raises(KeyError):
|
||||
ConfigFile(DATA_PATH / "bad.conf")
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue