adds tests
This commit is contained in:
@@ -10,34 +10,42 @@ class BadConfiguration(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def load_config():
|
||||
CONFIG = configparser.ConfigParser()
|
||||
wd = Path(__file__).parent
|
||||
CONFIG.read(wd / "config.ini")
|
||||
def read_config(path):
|
||||
config = configparser.ConfigParser()
|
||||
config.read(path)
|
||||
return config
|
||||
|
||||
|
||||
def parse_config(config):
|
||||
try:
|
||||
camera = CONFIG.defaults()['camera']
|
||||
camera = config.defaults()['camera']
|
||||
except KeyError:
|
||||
raise BadConfiguration("No camera path set !")
|
||||
|
||||
backups = {}
|
||||
imports = {}
|
||||
for ext in CONFIG.sections():
|
||||
for ext in config.sections():
|
||||
try:
|
||||
backup_path = CONFIG.get(ext, 'Backup')
|
||||
backup_path = config.get(ext, 'Backup')
|
||||
backups[ext] = Backup(backup_path)
|
||||
except KeyError: # A section without backup is useless
|
||||
except configparser.NoOptionError:
|
||||
# A section without backup is useless
|
||||
raise BadConfiguration(f"{ext} has no backup path set !")
|
||||
except Exception as err:
|
||||
raise BadConfiguration(
|
||||
f"Could not create a Backup at {backup_path}:\n{err}")
|
||||
try:
|
||||
import_path = CONFIG.get(ext, 'Import')
|
||||
import_path = config.get(ext, 'Import')
|
||||
imports[ext] = Store(import_path)
|
||||
except KeyError: # Could be wanted by user
|
||||
print("Warning : {ext} will not be set to import.")
|
||||
except configparser.NoOptionError: # Could be wanted by user
|
||||
print(f"Warning : {ext} will not be set to import.")
|
||||
except Exception as err:
|
||||
raise BadConfiguration(
|
||||
f"Could not create import Store at {import_path}:\n{err}")
|
||||
|
||||
return camera, backups, imports
|
||||
|
||||
|
||||
def load_config():
|
||||
# TODO: find first existing file on paths
|
||||
parse_config(read_config(Path(__file__).parent / "config.ini"))
|
||||
|
||||
@@ -1,21 +1,63 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from configparser import ConfigParser
|
||||
from ..config import parse_config, BadConfiguration
|
||||
|
||||
|
||||
class TestLoadConfig(unittest.TestCase):
|
||||
class TestParseConfig(unittest.TestCase):
|
||||
|
||||
def test_default_load_paths(self):
|
||||
pass
|
||||
_paths = ['sandbox',
|
||||
'sandbox/camera',
|
||||
'sandbox/jpg',
|
||||
'sandbox/jpg/.trash',
|
||||
'sandbox/mp4',
|
||||
'sandbox/mp4/.trash',
|
||||
'sandbox/import',
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
for p in cls._paths:
|
||||
if not os.path.exists(p):
|
||||
os.mkdir(p)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
for p in reversed(cls._paths):
|
||||
if os.path.exists(p):
|
||||
os.rmdir(p)
|
||||
|
||||
def setUp(self):
|
||||
self.test_config = ConfigParser(defaults={'Camera': 'sandbox/camera'})
|
||||
self.test_config.add_section('jpg')
|
||||
self.test_config.add_section('mp4')
|
||||
self.test_config.set('jpg', 'Backup', 'sandbox/jpg')
|
||||
self.test_config.set('mp4', 'Backup', 'sandbox/mp4')
|
||||
self.test_config.set('jpg', 'Import', 'sandbox/import')
|
||||
|
||||
def test_returns_config(self):
|
||||
camera, backups, imports = parse_config(self.test_config)
|
||||
self.assertEqual(camera, 'sandbox/camera')
|
||||
for s in ('jpg', 'mp4'):
|
||||
self.assertIn(s, backups)
|
||||
self.assertEqual(backups[s].path, Path(f'sandbox/{s}'))
|
||||
self.assertIn('jpg', imports)
|
||||
self.assertEqual(imports['jpg'].path, Path('sandbox/import'))
|
||||
self.assertNotIn('mp4', imports)
|
||||
|
||||
def test_raises_exception_on_missing_backup_path(self):
|
||||
pass
|
||||
self.test_config.remove_option('jpg', 'Backup')
|
||||
with self.assertRaises(BadConfiguration):
|
||||
parse_config(self.test_config)
|
||||
|
||||
def test_warns_on_missing_import_path(self):
|
||||
pass
|
||||
raise NotImplementedError
|
||||
|
||||
def test_raises_exception_on_store_init_failure(self):
|
||||
pass
|
||||
raise NotImplementedError
|
||||
|
||||
def test_asserts_camera_path_is_present(self):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user