Updated unittest for saving of server lists in the conf file

This commit is contained in:
jleute 2018-05-19 01:32:51 +02:00
parent 348544428f
commit b543c04572
3 changed files with 22 additions and 14 deletions

View file

@ -538,7 +538,6 @@ class Config(object):
path = conf_file path = conf_file
else: else:
path = self.get_conf_filename() path = self.get_conf_filename()
ext = os.path.splitext(path)[1] ext = os.path.splitext(path)[1]
decoder = settings_decoders.get(ext, False) decoder = settings_decoders.get(ext, False)
assert decoder is not False, 'Unknown settings format %s' % ext assert decoder is not False, 'Unknown settings format %s' % ext
@ -645,6 +644,8 @@ def get_default_env():
for k, v in ADJUSTABLE_SETTINGS.iteritems(): for k, v in ADJUSTABLE_SETTINGS.iteritems():
if len(v) == 3: if len(v) == 3:
env_defaults[k] = (v[0], None, v[2]) env_defaults[k] = (v[0], None, v[2])
elif len(v) == 4:
env_defaults[k] = (v[0], None, v[2], v[3])
else: else:
env_defaults[k] = (v[0], None) env_defaults[k] = (v[0], None)
return Env(**env_defaults) return Env(**env_defaults)

View file

@ -4,6 +4,8 @@ import json
from twisted.trial import unittest from twisted.trial import unittest
from lbrynet import conf from lbrynet import conf
from lbrynet.core.Error import InvalidCurrencyError from lbrynet.core.Error import InvalidCurrencyError
from lbrynet.tests import mocks
from lbrynet.tests.util import create_conf_file
class SettingsTest(unittest.TestCase): class SettingsTest(unittest.TestCase):
def setUp(self): def setUp(self):
@ -78,17 +80,18 @@ class SettingsTest(unittest.TestCase):
self.assertEqual(str, type(conf.default_data_dir)) self.assertEqual(str, type(conf.default_data_dir))
self.assertEqual(str, type(conf.default_lbryum_dir)) self.assertEqual(str, type(conf.default_lbryum_dir))
def test_conversion_reversal(self): def test_load_save_load_config_file(self):
# simulate decoding, conversion, conversion reversal and encoding of #settings = self.get_mock_config_instance()
# server list settings from the config file. conf_entry = 'lbryum_servers: ["localhost:50001", "localhost:50002"]\n'
settings = self.get_mock_config_instance() conf_temp = create_conf_file(conf_entry)
encoder = conf.settings_encoders['.yml'] conf.conf_file = conf_temp
decoder = conf.settings_decoders['.yml'] adjustable_settings={'data_dir': (str, conf.default_data_dir),
conf_file_entry = "lbryum_servers: ['localhost:5001', 'localhost:5002']" 'lbryum_servers': (list, [('localhost', 5001)],
decoded = decoder(conf_file_entry) conf.server_list, conf.server_list_reverse)}
converted = settings._convert_conf_file_lists(decoded) env = conf.Env(**adjustable_settings)
converted_reversed = settings._convert_conf_file_lists_reverse(converted) settings = conf.Config({}, adjustable_settings, environment=env)
encoded = encoder(converted_reversed) conf.settings = settings
self.assertEqual(conf_file_entry, encoded.strip()) settings.load_conf_file_settings()
self.assertEqual(decoded, converted_reversed) settings.save_conf_file_settings()
settings.load_conf_file_settings()

View file

@ -19,6 +19,10 @@ DEFAULT_ISO_TIME = time.mktime(DEFAULT_TIMESTAMP.timetuple())
log = logging.getLogger("lbrynet.tests.util") log = logging.getLogger("lbrynet.tests.util")
def create_conf_file(entry):
with tempfile.NamedTemporaryFile(delete=False, suffix='.yml') as conf:
conf.write(entry)
return conf.name
def mk_db_and_blob_dir(): def mk_db_and_blob_dir():
db_dir = tempfile.mkdtemp() db_dir = tempfile.mkdtemp()