mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-14 21:44:28 +00:00
Add a test for saving the config
This commit is contained in:
parent
abc3964f73
commit
e8b04c1542
@ -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:
|
if self.__config == loaded_data and self.__version == version:
|
||||||
# The config has not changed so lets just return
|
# The config has not changed so lets just return
|
||||||
self._save_timer = None
|
self._save_timer.cancel()
|
||||||
return
|
return
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
log.warning("Unable to open config file: %s", filename)
|
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
|
# Save the new config and make sure it's written to disk
|
||||||
try:
|
try:
|
||||||
|
@ -2,21 +2,25 @@ from twisted.trial import unittest
|
|||||||
from twisted.python.failure import Failure
|
from twisted.python.failure import Failure
|
||||||
|
|
||||||
import common
|
import common
|
||||||
|
import os
|
||||||
|
|
||||||
from deluge.config import Config
|
from deluge.config import Config
|
||||||
|
|
||||||
|
DEFAULTS = {"string": "foobar", "int": 1, "float": 0.435, "bool": True, "tuple": (1, 2)}
|
||||||
|
|
||||||
class ConfigTestCase(unittest.TestCase):
|
class ConfigTestCase(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.config_dir = common.set_tmp_config_dir()
|
||||||
|
|
||||||
def test_init(self):
|
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=".")
|
config = Config("test.conf", config_dir=self.config_dir)
|
||||||
self.assertEquals(defaults, config.config)
|
|
||||||
|
|
||||||
config = Config("test.conf", config_dir=".")
|
|
||||||
self.assertEquals({}, config.config)
|
self.assertEquals({}, config.config)
|
||||||
|
|
||||||
def test_set_get_item(self):
|
def test_set_get_item(self):
|
||||||
config = Config("test.conf", config_dir=".")
|
config = Config("test.conf", config_dir=self.config_dir)
|
||||||
config["foo"] = 1
|
config["foo"] = 1
|
||||||
self.assertEquals(config["foo"], 1)
|
self.assertEquals(config["foo"], 1)
|
||||||
self.assertRaises(ValueError, config.set_item, "foo", "bar")
|
self.assertRaises(ValueError, config.set_item, "foo", "bar")
|
||||||
@ -26,41 +30,52 @@ class ConfigTestCase(unittest.TestCase):
|
|||||||
config._save_timer.cancel()
|
config._save_timer.cancel()
|
||||||
|
|
||||||
def test_load(self):
|
def test_load(self):
|
||||||
d = {"string": "foobar", "int": 1, "float": 0.435, "bool": True, "tuple": (1, 2)}
|
|
||||||
|
|
||||||
def check_config():
|
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["string"], "foobar")
|
||||||
self.assertEquals(config["float"], 0.435)
|
self.assertEquals(config["float"], 0.435)
|
||||||
|
|
||||||
# Test loading an old config from 1.1.x
|
# Test loading an old config from 1.1.x
|
||||||
import pickle
|
import pickle
|
||||||
pickle.dump(d, open("test.conf", "wb"))
|
pickle.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb"))
|
||||||
|
|
||||||
check_config()
|
check_config()
|
||||||
|
|
||||||
# Test opening a previous 1.2 config file of just a json object
|
# Test opening a previous 1.2 config file of just a json object
|
||||||
import json
|
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()
|
check_config()
|
||||||
|
|
||||||
# Test opening a previous 1.2 config file of having the format versions
|
# Test opening a previous 1.2 config file of having the format versions
|
||||||
# as ints
|
# 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")
|
||||||
f.write(str(1) + "\n")
|
f.write(str(1) + "\n")
|
||||||
json.dump(d, f, indent=2)
|
json.dump(DEFAULTS, f, indent=2)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
check_config()
|
check_config()
|
||||||
|
|
||||||
# Test the 1.2 config format
|
# Test the 1.2 config format
|
||||||
v = {"format": 1, "file": 1}
|
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(v, f, indent=2)
|
||||||
json.dump(d, f, indent=2)
|
json.dump(DEFAULTS, f, indent=2)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
check_config()
|
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)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user