From 0d1ea09ed10a5796c71f59cc44d0c2a92f27ee54 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 16 Jan 2018 10:52:41 +0100 Subject: [PATCH] fix tests --- lib/tests/test_simple_config.py | 107 +------------------------------- 1 file changed, 3 insertions(+), 104 deletions(-) diff --git a/lib/tests/test_simple_config.py b/lib/tests/test_simple_config.py index c9c648dc8..ad7fca2b9 100644 --- a/lib/tests/test_simple_config.py +++ b/lib/tests/test_simple_config.py @@ -6,8 +6,7 @@ import tempfile import shutil from io import StringIO -from lib.simple_config import (SimpleConfig, read_system_config, - read_user_config) +from lib.simple_config import (SimpleConfig, read_user_config) class Test_SimpleConfig(unittest.TestCase): @@ -37,18 +36,15 @@ class Test_SimpleConfig(unittest.TestCase): def test_simple_config_key_rename(self): """auto_cycle was renamed auto_connect""" - fake_read_system = lambda : {} fake_read_user = lambda _: {"auto_cycle": True} read_user_dir = lambda : self.user_dir config = SimpleConfig(options=self.options, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) self.assertEqual(config.get("auto_connect"), True) self.assertEqual(config.get("auto_cycle"), None) fake_read_user = lambda _: {"auto_connect": False, "auto_cycle": True} config = SimpleConfig(options=self.options, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) self.assertEqual(config.get("auto_connect"), False) @@ -57,110 +53,51 @@ class Test_SimpleConfig(unittest.TestCase): def test_simple_config_command_line_overrides_everything(self): """Options passed by command line override all other configuration sources""" - fake_read_system = lambda : {"electrum_path": "a"} fake_read_user = lambda _: {"electrum_path": "b"} read_user_dir = lambda : self.user_dir config = SimpleConfig(options=self.options, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) self.assertEqual(self.options.get("electrum_path"), config.get("electrum_path")) - def test_simple_config_user_config_overrides_system_config(self): - """Options passed in user config override system config.""" - fake_read_system = lambda : {"electrum_path": self.electrum_dir} - fake_read_user = lambda _: {"electrum_path": "b"} - read_user_dir = lambda : self.user_dir - config = SimpleConfig(options={}, - read_system_config_function=fake_read_system, - read_user_config_function=fake_read_user, - read_user_dir_function=read_user_dir) - self.assertEqual("b", config.get("electrum_path")) - - def test_simple_config_system_config_ignored_if_portable(self): - """If electrum is started with the "portable" flag, system - configuration is completely ignored.""" - fake_read_system = lambda : {"some_key": "some_value"} - fake_read_user = lambda _: {} - read_user_dir = lambda : self.user_dir - config = SimpleConfig(options={"portable": True}, - read_system_config_function=fake_read_system, - read_user_config_function=fake_read_user, - read_user_dir_function=read_user_dir) - self.assertEqual(config.get("some_key"), None) - def test_simple_config_user_config_is_used_if_others_arent_specified(self): """If no system-wide configuration and no command-line options are specified, the user configuration is used instead.""" - fake_read_system = lambda : {} fake_read_user = lambda _: {"electrum_path": self.electrum_dir} read_user_dir = lambda : self.user_dir config = SimpleConfig(options={}, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) self.assertEqual(self.options.get("electrum_path"), config.get("electrum_path")) def test_cannot_set_options_passed_by_command_line(self): - fake_read_system = lambda : {} fake_read_user = lambda _: {"electrum_path": "b"} read_user_dir = lambda : self.user_dir config = SimpleConfig(options=self.options, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) config.set_key("electrum_path", "c") self.assertEqual(self.options.get("electrum_path"), config.get("electrum_path")) - def test_can_set_options_from_system_config(self): - fake_read_system = lambda : {"electrum_path": self.electrum_dir} - fake_read_user = lambda _: {} - read_user_dir = lambda : self.user_dir - config = SimpleConfig(options={}, - read_system_config_function=fake_read_system, - read_user_config_function=fake_read_user, - read_user_dir_function=read_user_dir) - config.set_key("electrum_path", "c") - self.assertEqual("c", config.get("electrum_path")) - def test_can_set_options_set_in_user_config(self): another_path = tempfile.mkdtemp() - fake_read_system = lambda : {} fake_read_user = lambda _: {"electrum_path": self.electrum_dir} read_user_dir = lambda : self.user_dir config = SimpleConfig(options={}, - read_system_config_function=fake_read_system, - read_user_config_function=fake_read_user, - read_user_dir_function=read_user_dir) - config.set_key("electrum_path", another_path) - self.assertEqual(another_path, config.get("electrum_path")) - - def test_can_set_options_from_system_config_if_portable(self): - """If the "portable" flag is set, the user can overwrite system - configuration options.""" - another_path = tempfile.mkdtemp() - fake_read_system = lambda : {"electrum_path": self.electrum_dir} - fake_read_user = lambda _: {} - read_user_dir = lambda : self.user_dir - config = SimpleConfig(options={"portable": True}, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) config.set_key("electrum_path", another_path) self.assertEqual(another_path, config.get("electrum_path")) def test_user_config_is_not_written_with_read_only_config(self): - """The user config does not contain command-line options or system - options when saved.""" - fake_read_system = lambda : {"something": "b"} + """The user config does not contain command-line options when saved.""" fake_read_user = lambda _: {"something": "a"} read_user_dir = lambda : self.user_dir self.options.update({"something": "c"}) config = SimpleConfig(options=self.options, - read_system_config_function=fake_read_system, read_user_config_function=fake_read_user, read_user_dir_function=read_user_dir) config.save_user_config() @@ -168,48 +105,10 @@ class Test_SimpleConfig(unittest.TestCase): with open(os.path.join(self.electrum_dir, "config"), "r") as f: contents = f.read() result = ast.literal_eval(contents) + result.pop('config_version', None) self.assertEqual({"something": "a"}, result) -class TestSystemConfig(unittest.TestCase): - - sample_conf = """ -[client] -gap_limit = 5 - -[something_else] -everything = 42 -""" - - def setUp(self): - super(TestSystemConfig, self).setUp() - self.thefile = tempfile.mkstemp(suffix=".electrum.test.conf")[1] - - def tearDown(self): - super(TestSystemConfig, self).tearDown() - os.remove(self.thefile) - - def test_read_system_config_file_does_not_exist(self): - somefile = "/foo/I/do/not/exist/electrum.conf" - result = read_system_config(somefile) - self.assertEqual({}, result) - - def test_read_system_config_file_returns_file_options(self): - with open(self.thefile, "w") as f: - f.write(self.sample_conf) - - result = read_system_config(self.thefile) - self.assertEqual({"gap_limit": "5"}, result) - - def test_read_system_config_file_no_sections(self): - - with open(self.thefile, "w") as f: - f.write("gap_limit = 5") # The file has no sections at all - - result = read_system_config(self.thefile) - self.assertEqual({}, result) - - class TestUserConfig(unittest.TestCase): def setUp(self):