From e8b04c15429e29254d035878b53ef7bd6fabc791 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Mon, 3 Aug 2009 17:39:12 +0000 Subject: [PATCH] Add a test for saving the config --- deluge/config.py | 4 ++-- tests/test_config.py | 45 +++++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/deluge/config.py b/deluge/config.py index e3588f178..a8f4a6ba4 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -396,12 +396,12 @@ what is currently in the config and it could not convert the value if self.__config == loaded_data and self.__version == version: # The config has not changed so lets just return - self._save_timer = None + self._save_timer.cancel() return except Exception, e: log.warning("Unable to open config file: %s", filename) - self._save_timer = None + self._save_timer.cancel() # Save the new config and make sure it's written to disk try: diff --git a/tests/test_config.py b/tests/test_config.py index 7574596a8..e58e0d3be 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -2,21 +2,25 @@ from twisted.trial import unittest from twisted.python.failure import Failure import common +import os from deluge.config import Config +DEFAULTS = {"string": "foobar", "int": 1, "float": 0.435, "bool": True, "tuple": (1, 2)} + class ConfigTestCase(unittest.TestCase): + def setUp(self): + self.config_dir = common.set_tmp_config_dir() + def test_init(self): - defaults = {"string": "foobar", "int": 1, "float": 0.435, "bool": True, "tuple": (1, 2)} + config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) + self.assertEquals(DEFAULTS, config.config) - config = Config("test.conf", defaults=defaults, config_dir=".") - self.assertEquals(defaults, config.config) - - config = Config("test.conf", config_dir=".") + config = Config("test.conf", config_dir=self.config_dir) self.assertEquals({}, config.config) def test_set_get_item(self): - config = Config("test.conf", config_dir=".") + config = Config("test.conf", config_dir=self.config_dir) config["foo"] = 1 self.assertEquals(config["foo"], 1) self.assertRaises(ValueError, config.set_item, "foo", "bar") @@ -26,41 +30,52 @@ class ConfigTestCase(unittest.TestCase): config._save_timer.cancel() def test_load(self): - d = {"string": "foobar", "int": 1, "float": 0.435, "bool": True, "tuple": (1, 2)} - def check_config(): - config = Config("test.conf", config_dir=".") + config = Config("test.conf", config_dir=self.config_dir) self.assertEquals(config["string"], "foobar") self.assertEquals(config["float"], 0.435) # Test loading an old config from 1.1.x import pickle - pickle.dump(d, open("test.conf", "wb")) + pickle.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb")) check_config() # Test opening a previous 1.2 config file of just a json object import json - json.dump(d, open("test.conf", "wb"), indent=2) + json.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb"), indent=2) check_config() # Test opening a previous 1.2 config file of having the format versions # as ints - f = open("test.conf", "wb") + f = open(os.path.join(self.config_dir, "test.conf"), "wb") f.write(str(1) + "\n") f.write(str(1) + "\n") - json.dump(d, f, indent=2) + json.dump(DEFAULTS, f, indent=2) f.close() check_config() # Test the 1.2 config format v = {"format": 1, "file": 1} - f = open("test.conf", "wb") + f = open(os.path.join(self.config_dir, "test.conf"), "wb") json.dump(v, f, indent=2) - json.dump(d, f, indent=2) + json.dump(DEFAULTS, f, indent=2) f.close() check_config() + + def test_save(self): + config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) + config["string"] = "baz" + config["int"] = 2 + ret = config.save() + self.assertTrue(ret) + del config + + config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) + self.assertEquals(config["string"], "baz") + self.assertEquals(config["int"], 2) +