Add a test for saving the config

This commit is contained in:
Andrew Resch 2009-08-03 17:39:12 +00:00
parent abc3964f73
commit e8b04c1542
2 changed files with 32 additions and 17 deletions

View File

@ -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:

View File

@ -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)