2016-11-30 22:12:14 +01:00
|
|
|
import os
|
2017-07-20 00:15:07 +02:00
|
|
|
import json
|
2016-11-30 22:12:14 +01:00
|
|
|
|
|
|
|
from twisted.trial import unittest
|
|
|
|
from lbrynet import conf
|
2017-07-20 00:15:07 +02:00
|
|
|
from lbrynet.core.Error import InvalidCurrencyError
|
2016-11-30 22:12:14 +01:00
|
|
|
|
|
|
|
class SettingsTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
os.environ['LBRY_TEST'] = 'test_string'
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
del os.environ['LBRY_TEST']
|
|
|
|
|
2017-01-17 04:23:20 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_mock_config_instance():
|
2017-01-17 18:29:09 +01:00
|
|
|
settings = {'test': (str, '')}
|
|
|
|
env = conf.Env(**settings)
|
|
|
|
return conf.Config({}, settings, environment=env)
|
2016-11-30 22:12:14 +01:00
|
|
|
|
2017-01-17 04:23:20 +01:00
|
|
|
def test_envvar_is_read(self):
|
|
|
|
settings = self.get_mock_config_instance()
|
|
|
|
self.assertEqual('test_string', settings['test'])
|
|
|
|
|
|
|
|
def test_setting_can_be_overridden(self):
|
|
|
|
settings = self.get_mock_config_instance()
|
|
|
|
settings['test'] = 'my_override'
|
|
|
|
self.assertEqual('my_override', settings['test'])
|
2016-11-30 22:12:14 +01:00
|
|
|
|
|
|
|
def test_setting_can_be_updated(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
settings = self.get_mock_config_instance()
|
2016-11-30 22:12:14 +01:00
|
|
|
settings.update({'test': 'my_update'})
|
2017-01-17 04:23:20 +01:00
|
|
|
self.assertEqual('my_update', settings['test'])
|
2016-11-30 22:12:14 +01:00
|
|
|
|
|
|
|
def test_setting_is_in_dict(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
settings = self.get_mock_config_instance()
|
|
|
|
setting_dict = settings.get_current_settings_dict()
|
2016-11-30 22:12:14 +01:00
|
|
|
self.assertEqual({'test': 'test_string'}, setting_dict)
|
2017-01-17 18:29:09 +01:00
|
|
|
|
|
|
|
def test_invalid_setting_raises_exception(self):
|
|
|
|
settings = self.get_mock_config_instance()
|
2017-10-11 08:59:30 +02:00
|
|
|
self.assertRaises(KeyError, settings.set, 'invalid_name', 123)
|
2017-01-17 18:29:09 +01:00
|
|
|
|
|
|
|
def test_invalid_data_type_raises_exception(self):
|
|
|
|
settings = self.get_mock_config_instance()
|
|
|
|
self.assertIsNone(settings.set('test', 123))
|
2017-10-11 08:59:30 +02:00
|
|
|
self.assertRaises(KeyError, settings.set, 'test', 123, ('fake_data_type',))
|
2017-01-17 18:29:09 +01:00
|
|
|
|
|
|
|
def test_setting_precedence(self):
|
|
|
|
settings = self.get_mock_config_instance()
|
|
|
|
settings.set('test', 'cli_test_string', data_types=(conf.TYPE_CLI,))
|
|
|
|
self.assertEqual('cli_test_string', settings['test'])
|
|
|
|
settings.set('test', 'this_should_not_take_precedence', data_types=(conf.TYPE_ENV,))
|
|
|
|
self.assertEqual('cli_test_string', settings['test'])
|
|
|
|
settings.set('test', 'runtime_takes_precedence', data_types=(conf.TYPE_RUNTIME,))
|
|
|
|
self.assertEqual('runtime_takes_precedence', settings['test'])
|
2017-06-30 18:39:53 +02:00
|
|
|
|
2017-07-20 00:15:07 +02:00
|
|
|
def test_max_key_fee_set(self):
|
|
|
|
fixed_default = {'CURRENCIES':{'BTC':{'type':'crypto'}}}
|
|
|
|
adjustable_settings = {'max_key_fee': (json.loads, {'currency':'USD', 'amount':1})}
|
|
|
|
env = conf.Env(**adjustable_settings)
|
|
|
|
settings = conf.Config(fixed_default, adjustable_settings, environment=env)
|
|
|
|
|
|
|
|
with self.assertRaises(InvalidCurrencyError):
|
|
|
|
settings.set('max_key_fee', {'currency':'USD', 'amount':1})
|
|
|
|
|
2017-09-29 12:44:22 +02:00
|
|
|
valid_setting = {'currency':'BTC', 'amount':1}
|
|
|
|
settings.set('max_key_fee', valid_setting)
|
2017-07-20 00:15:07 +02:00
|
|
|
out = settings.get('max_key_fee')
|
|
|
|
self.assertEqual(out, valid_setting)
|
|
|
|
|
|
|
|
|
2017-06-30 18:39:53 +02:00
|
|
|
def test_data_dir(self):
|
|
|
|
# check if these directories are returned as string and not unicode
|
|
|
|
# otherwise there will be problems when calling os.path.join on
|
|
|
|
# unicode directory names with string file names
|
2017-07-05 23:39:35 +02:00
|
|
|
self.assertEqual(str, type(conf.default_download_dir))
|
2017-06-30 18:39:53 +02:00
|
|
|
self.assertEqual(str, type(conf.default_data_dir))
|
|
|
|
self.assertEqual(str, type(conf.default_lbryum_dir))
|