Skip to content

Commit

Permalink
test Config
Browse files Browse the repository at this point in the history
  • Loading branch information
nihlaeth committed Feb 15, 2017
1 parent 64469ad commit 10555a6
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 3 deletions.
68 changes: 68 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Test Config."""
import sys
from pathlib import Path
import pytest
from user_config import Config, Section, StringOption

class FallbackConfig(Config):

"""Test configuration fallback."""

application = "test"
author = "nobody"

class GeneralSection(Section):

"""General section."""

string = StringOption(default="default")

general = GeneralSection()

def test_default():
config_directory = Path(__file__).parents[0] / 'test_config'
sys.argv = [sys.argv[0]]
config = FallbackConfig(
file_name="default",
global_path=config_directory / 'global',
user_path=config_directory / 'user')
assert config.general.string == "default"

def test_global():
config_directory = Path(__file__).parents[0] / 'test_config'
sys.argv = [sys.argv[0]]
config = FallbackConfig(
file_name="global",
global_path=config_directory / 'global',
user_path=config_directory / 'user')
assert config.general.string == "global"

def test_user():
config_directory = Path(__file__).parents[0] / 'test_config'
sys.argv = [sys.argv[0]]
config = FallbackConfig(
file_name="user",
global_path=config_directory / 'global',
user_path=config_directory / 'user')
assert config.general.string == "user"

def test_cli():
config_directory = Path(__file__).parents[0] / 'test_config'
sys.argv = [sys.argv[0], '--string', 'cli']
config = FallbackConfig(
file_name="user",
global_path=config_directory / 'global',
user_path=config_directory / 'user')
assert config.general.string == "cli"

def test_required_attributes():
class NoApplication(Config):
"""Test missing application attribute."""
author = "nobody"
with pytest.raises(AttributeError):
NoApplication()
class NoAuthor(Config):
"""Test missing author attribute."""
application = "test"
with pytest.raises(AttributeError):
NoAuthor()
2 changes: 2 additions & 0 deletions tests/test_config/global/default.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
# string = default
2 changes: 2 additions & 0 deletions tests/test_config/global/global.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
string = global
2 changes: 2 additions & 0 deletions tests/test_config/global/user.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
string = global
2 changes: 2 additions & 0 deletions tests/test_config/user/default.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
# string = default
2 changes: 2 additions & 0 deletions tests/test_config/user/global.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
# string = default
2 changes: 2 additions & 0 deletions tests/test_config/user/user.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
string = user
3 changes: 0 additions & 3 deletions user_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,3 @@ def __init__(
command_line_arguments, self._data)
# validate _data
self._elements[element].validate_data(self._data)

def __getattr__(self, name):
return self._data[name]

0 comments on commit 10555a6

Please sign in to comment.