ordigi/tests/test_config.py

61 lines
1.8 KiB
Python
Raw Normal View History

2021-08-08 15:33:47 +02:00
from pathlib import Path
import pytest
import shutil
import tempfile
from unittest import mock
from ordigi.config import Config
2021-08-08 15:33:47 +02:00
# Helpers
import random
import string
def random_char(y):
return ''.join(random.choice(string.printable) for x in range(y))
def write_random_file(file_path):
with open(file_path, 'w') as conf_file:
conf_file.write(random_char(20))
class TestConfig:
@pytest.fixture(scope="module")
def conf(self, conf_path):
config = Config(conf_path)
return config.load_config()
2021-08-08 15:33:47 +02:00
def test_write(self, conf_path):
assert conf_path.is_file()
def test_load_config(self, conf):
"""
Read files from config and return variables
"""
# test valid config file
assert conf['Path']['dirs_path'] == '%u{%Y-%m}/{city}|{city}-{%Y}/{folders[:1]}/{folder}'
assert conf['Path']['name'] == '{%Y-%m-%b-%H-%M-%S}-{basename}.%l{ext}'
assert conf['Path']['day_begins'] == '4'
assert conf['Geolocation']['geocoder'] == 'Nominatium'
def test_load_config_no_exist(self):
# test file not exist
2021-10-17 20:04:39 +02:00
config = Config()
config.conf_path = Path('filename')
2021-10-17 20:04:39 +02:00
assert config.load_config() == {}
2021-08-08 15:33:47 +02:00
def test_load_config_invalid(self, conf_path):
# test invalid config
write_random_file(conf_path)
with pytest.raises(Exception) as e:
config = Config(conf_path)
2021-08-08 15:33:47 +02:00
assert e.typename == 'MissingSectionHeaderError'
def test_get_path_definition(self, conf):
"""
Get path definition from config
"""
config = Config(conf=conf)
path = config.get_path_definition()
2021-08-08 15:33:47 +02:00
assert path == '%u{%Y-%m}/{city}|{city}-{%Y}/{folders[:1]}/{folder}/{%Y-%m-%b-%H-%M-%S}-{basename}.%l{ext}'