2017-01-03 05:58:52 +01:00
|
|
|
# Project imports
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
2019-07-04 11:57:10 +02:00
|
|
|
|
2017-01-03 05:58:52 +01:00
|
|
|
from mock import patch
|
2019-07-04 11:57:10 +02:00
|
|
|
from tempfile import gettempdir
|
2017-01-03 05:58:52 +01:00
|
|
|
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
|
|
|
|
|
|
|
|
from elodie import constants
|
2019-07-04 11:57:10 +02:00
|
|
|
from elodie.config import load_config, load_plugin_config
|
2017-01-03 05:58:52 +01:00
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-singleton-success' % gettempdir())
|
2017-01-03 05:58:52 +01:00
|
|
|
def test_load_config_singleton_success():
|
2019-07-04 11:57:10 +02:00
|
|
|
with open('%s/config.ini-singleton-success' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
2021-04-16 20:22:27 +02:00
|
|
|
[Geolocation]
|
2019-07-04 11:57:10 +02:00
|
|
|
key=your-api-key-goes-here
|
|
|
|
prefer_english_names=False
|
|
|
|
""")
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
2021-04-16 20:22:27 +02:00
|
|
|
config = load_config(constants.CONFIG_FILE)
|
|
|
|
assert config['Geolocation']['key'] == 'your-api-key-goes-here', config.get('MapQuest', 'key')
|
2017-01-03 05:58:52 +01:00
|
|
|
config.set('MapQuest', 'key', 'new-value')
|
|
|
|
|
2021-04-16 20:22:27 +02:00
|
|
|
config = load_config(constants.CONFIG_FILE)
|
2017-01-03 05:58:52 +01:00
|
|
|
|
2019-07-04 11:57:10 +02:00
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
2017-01-03 05:58:52 +01:00
|
|
|
|
2019-07-04 11:57:10 +02:00
|
|
|
assert config['MapQuest']['key'] == 'new-value', config.get('MapQuest', 'key')
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-does-not-exist' % gettempdir())
|
2017-01-03 05:58:52 +01:00
|
|
|
def test_load_config_singleton_no_file():
|
2019-07-04 11:57:10 +02:00
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
2021-04-16 20:22:27 +02:00
|
|
|
config = load_config(constants.CONFIG_FILE)
|
2019-07-04 11:57:10 +02:00
|
|
|
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
2017-01-03 05:58:52 +01:00
|
|
|
assert config == {}, config
|
2019-07-04 11:57:10 +02:00
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir())
|
2019-07-04 11:57:10 +02:00
|
|
|
def test_load_plugin_config_unset_backwards_compat():
|
|
|
|
with open('%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
""")
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
plugins = load_plugin_config()
|
|
|
|
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
assert plugins == [], plugins
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-exists-not-set' % gettempdir())
|
2019-07-04 11:57:10 +02:00
|
|
|
def test_load_plugin_config_exists_not_set():
|
|
|
|
with open('%s/config.ini-load-plugin-config-exists-not-set' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
[Plugins]
|
|
|
|
""")
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
plugins = load_plugin_config()
|
|
|
|
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
assert plugins == [], plugins
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-one' % gettempdir())
|
2019-07-04 11:57:10 +02:00
|
|
|
def test_load_plugin_config_one():
|
|
|
|
with open('%s/config.ini-load-plugin-config-one' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
[Plugins]
|
|
|
|
plugins=Dummy
|
|
|
|
""")
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
plugins = load_plugin_config()
|
|
|
|
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
assert plugins == ['Dummy'], plugins
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-one-with-invalid' % gettempdir())
|
2019-07-04 11:57:10 +02:00
|
|
|
def test_load_plugin_config_one_with_invalid():
|
|
|
|
with open('%s/config.ini-load-plugin-config-one' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
[Plugins]
|
|
|
|
plugins=DNE
|
|
|
|
""")
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
plugins = load_plugin_config()
|
|
|
|
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
assert plugins == [], plugins
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-many' % gettempdir())
|
2019-07-04 11:57:10 +02:00
|
|
|
def test_load_plugin_config_many():
|
|
|
|
with open('%s/config.ini-load-plugin-config-many' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
[Plugins]
|
|
|
|
plugins=GooglePhotos,Dummy
|
|
|
|
""")
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
plugins = load_plugin_config()
|
|
|
|
|
|
|
|
if hasattr(load_config, 'config'):
|
|
|
|
del load_config.config
|
|
|
|
|
|
|
|
assert plugins == ['GooglePhotos','Dummy'], plugins
|