diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-13 18:26:34 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-13 18:26:34 +0200 |
commit | 9b337be1a6e392923509fa94435164b0da98960d (patch) | |
tree | 1b77c63077c7fc53a4a41c01c01de418cbe24b73 | |
parent | 771ce2a2edff6f8e065b2757066778e3fb1b765a (diff) |
Add test infrastructure
-rw-r--r-- | pyapd/config.py | 37 | ||||
-rw-r--r-- | pyapd_config.yaml | 1 | ||||
-rw-r--r-- | tests/data/simple_config.yaml | 1 | ||||
-rw-r--r-- | tests/test_config.py | 22 |
4 files changed, 61 insertions, 0 deletions
diff --git a/pyapd/config.py b/pyapd/config.py new file mode 100644 index 0000000..7850eab --- /dev/null +++ b/pyapd/config.py @@ -0,0 +1,37 @@ +from contextlib import contextmanager +from typing import Optional + +import ruamel.yaml + +class Config(): + def __init__(self, config_fn: Optional[str] = None, **kw): + self.config_fn = config_fn + self.data = {} + if config_fn: + self.load() + + def __getattr__(self, name): + try: + return self.data[name] + except KeyError as e: + raise NameError(e) + + def load(self, config_fn: Optional[str] = None): + if config_fn: + self.config_fn = config_fn + yaml = ruamel.yaml.YAML() + if self.config_fn: + fnames = [self.config_fn] + else: + fnames = [ + 'pyapd_config.yaml', + '/etc/pyapd/pyapd.yaml', + ] + for fn in fnames: + try: + with open(fn, 'r') as fp: + self.data = yaml.load(fp) + self.config_fn = fn + break + except FileNotFoundError: + continue diff --git a/pyapd_config.yaml b/pyapd_config.yaml new file mode 100644 index 0000000..4a485ff --- /dev/null +++ b/pyapd_config.yaml @@ -0,0 +1 @@ +backend: memory diff --git a/tests/data/simple_config.yaml b/tests/data/simple_config.yaml new file mode 100644 index 0000000..fa44e32 --- /dev/null +++ b/tests/data/simple_config.yaml @@ -0,0 +1 @@ +backend: none diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..37f057e --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,22 @@ +import unittest + +from pyapd import config + + +class TestConfig(unittest.TestCase): + def setUp(self): + self.config = config.Config() + + def test_load_config_no_files(self): + self.config.load() + self.assertEqual(self.config.config_fn, 'pyapd_config.yaml') + self.assertIn('backend', self.config.data) + + def test_load_config_from_file(self): + self.config.load('tests/data/simple_config.yaml') + self.assertEqual( + self.config.config_fn, + 'tests/data/simple_config.yaml' + ) + self.assertIn('backend', self.config.data) + self.assertEqual(self.config.backend, 'none') |