From b543c0457236ea3f8d2292e30807acc31e4dc1ad Mon Sep 17 00:00:00 2001 From: jleute Date: Sat, 19 May 2018 01:32:51 +0200 Subject: [PATCH] Updated unittest for saving of server lists in the conf file --- lbrynet/conf.py | 3 ++- lbrynet/tests/unit/test_conf.py | 29 ++++++++++++++++------------- lbrynet/tests/util.py | 4 ++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lbrynet/conf.py b/lbrynet/conf.py index 1d5d8972d..e902192fd 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -538,7 +538,6 @@ class Config(object): path = conf_file else: path = self.get_conf_filename() - ext = os.path.splitext(path)[1] decoder = settings_decoders.get(ext, False) 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(): if len(v) == 3: env_defaults[k] = (v[0], None, v[2]) + elif len(v) == 4: + env_defaults[k] = (v[0], None, v[2], v[3]) else: env_defaults[k] = (v[0], None) return Env(**env_defaults) diff --git a/lbrynet/tests/unit/test_conf.py b/lbrynet/tests/unit/test_conf.py index 3baa5f7ab..87d5c03fc 100644 --- a/lbrynet/tests/unit/test_conf.py +++ b/lbrynet/tests/unit/test_conf.py @@ -4,6 +4,8 @@ import json from twisted.trial import unittest from lbrynet import conf from lbrynet.core.Error import InvalidCurrencyError +from lbrynet.tests import mocks +from lbrynet.tests.util import create_conf_file class SettingsTest(unittest.TestCase): 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_lbryum_dir)) - def test_conversion_reversal(self): - # simulate decoding, conversion, conversion reversal and encoding of - # server list settings from the config file. - settings = self.get_mock_config_instance() - encoder = conf.settings_encoders['.yml'] - decoder = conf.settings_decoders['.yml'] - conf_file_entry = "lbryum_servers: ['localhost:5001', 'localhost:5002']" - decoded = decoder(conf_file_entry) - converted = settings._convert_conf_file_lists(decoded) - converted_reversed = settings._convert_conf_file_lists_reverse(converted) - encoded = encoder(converted_reversed) - self.assertEqual(conf_file_entry, encoded.strip()) - self.assertEqual(decoded, converted_reversed) + def test_load_save_load_config_file(self): + #settings = self.get_mock_config_instance() + conf_entry = 'lbryum_servers: ["localhost:50001", "localhost:50002"]\n' + conf_temp = create_conf_file(conf_entry) + conf.conf_file = conf_temp + adjustable_settings={'data_dir': (str, conf.default_data_dir), + 'lbryum_servers': (list, [('localhost', 5001)], + conf.server_list, conf.server_list_reverse)} + env = conf.Env(**adjustable_settings) + settings = conf.Config({}, adjustable_settings, environment=env) + conf.settings = settings + settings.load_conf_file_settings() + settings.save_conf_file_settings() + settings.load_conf_file_settings() diff --git a/lbrynet/tests/util.py b/lbrynet/tests/util.py index e6ad2005c..45e4f3a15 100644 --- a/lbrynet/tests/util.py +++ b/lbrynet/tests/util.py @@ -19,6 +19,10 @@ DEFAULT_ISO_TIME = time.mktime(DEFAULT_TIMESTAMP.timetuple()) 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(): db_dir = tempfile.mkdtemp()