updated test to compare original and saved config file

This commit is contained in:
jleute 2018-05-21 18:50:25 +02:00
parent 93ee9fcff1
commit 092581bf32
2 changed files with 19 additions and 23 deletions

View file

@ -1,10 +1,10 @@
import os import os
import json import json
import tempfile
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.util import create_conf_file, remove_conf_file
class SettingsTest(unittest.TestCase): class SettingsTest(unittest.TestCase):
def setUp(self): def setUp(self):
@ -79,25 +79,29 @@ 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_load_save_load_config_file(self): def test_load_save_config_file(self):
conf_entry = 'lbryum_servers: ["localhost:50001", "localhost:50002"]\n' # setup settings
conf_temp = create_conf_file(conf_entry)
conf.conf_file = conf_temp
adjustable_settings = {'data_dir': (str, conf.default_data_dir), adjustable_settings = {'data_dir': (str, conf.default_data_dir),
'lbryum_servers': (list, [('localhost', 5001)], 'lbryum_servers': (list, [('localhost', 5001)],
conf.server_list, conf.server_list_reverse)} conf.server_list, conf.server_list_reverse)}
env = conf.Env(**adjustable_settings) env = conf.Env(**adjustable_settings)
settings = conf.Config({}, adjustable_settings, environment=env) settings = conf.Config({}, adjustable_settings, environment=env)
conf.settings = settings conf.settings = settings
settings.load_conf_file_settings() # setup tempfile
first = settings.get('lbryum_servers', data_type=conf.TYPE_PERSISTED) conf_entry = "lbryum_servers: ['localhost:50001', 'localhost:50002']\n"
settings.save_conf_file_settings() with tempfile.NamedTemporaryFile(suffix='.yml') as conf_file:
try: conf_file.write(conf_entry)
conf_file.seek(0)
conf.conf_file = conf_file.name
# load and save settings from conf file
settings.load_conf_file_settings() settings.load_conf_file_settings()
except Exception, e: settings.save_conf_file_settings()
remove_conf_file(conf_temp) # test if overwritten entry equals original entry
raise Exception(e) # use decoded versions, because format might change without
second = settings.get('lbryum_servers', data_type=conf.TYPE_PERSISTED) # changing the interpretation
remove_conf_file(conf_temp) decoder = conf.settings_decoders['.yml']
self.assertEqual(first, second) conf_decoded = decoder(conf_entry)
conf_entry_new = conf_file.read()
conf_decoded_new = decoder(conf_entry_new)
self.assertEqual(conf_decoded, conf_decoded_new)

View file

@ -19,14 +19,6 @@ 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 remove_conf_file(filename):
os.remove(filename)
def mk_db_and_blob_dir(): def mk_db_and_blob_dir():
db_dir = tempfile.mkdtemp() db_dir = tempfile.mkdtemp()
blob_dir = tempfile.mkdtemp() blob_dir = tempfile.mkdtemp()