Fix and move config unit test

svn: r22757
This commit is contained in:
Nick Hall 2013-07-28 17:36:35 +00:00
parent 696ee7aad0
commit 772cf39d7f
2 changed files with 42 additions and 70 deletions

View File

View File

@ -24,7 +24,7 @@
""" Unittest for config.py """
import unittest
from config import ConfigManager
from ..config import ConfigManager
def callback(*args):
# args: self, 0, str(setting), None
@ -42,96 +42,68 @@ class CompleteCheck(unittest.TestCase):
self.CM.register("section.setting3", "String") # string
self.CM.register("section.setting4", False) # boolean
assert self.CM.get("section.setting1") == 1
assert self.CM.get("section.setting2") == 3.1415
assert self.CM.get("section.setting3") == "String"
assert self.CM.get("section.setting4") == False
self.assertEqual(self.CM.get("section.setting1"), 1)
self.assertEqual(self.CM.get("section.setting2"), 3.1415)
self.assertEqual(self.CM.get("section.setting3"), "String")
self.assertFalse(self.CM.get("section.setting4"))
self.CM.set("section.setting1", 2)
self.CM.set("section.setting2", 8.6)
self.CM.set("section.setting3", "Another String")
self.CM.set("section.setting4", True)
assert self.CM.get("section.setting1") == 2
assert self.CM.get("section.setting2") == 8.6
assert self.CM.get("section.setting3") == "Another String"
assert self.CM.get("section.setting4") == True
try:
self.CM.set("section.setting1", 2.8)
except AttributeError:
pass
else:
raise AssertionError
try:
self.CM.set("section.setting2", 2)
except AttributeError:
pass
else:
raise AssertionError
try:
self.CM.set("section.setting3", 6)
except AttributeError:
pass
else:
raise AssertionError
try:
self.CM.set("section.setting4", 1)
except AttributeError:
pass
else:
raise AssertionError
assert self.CM.get("section.setting1") == 2
assert self.CM.get("section.setting2") == 8.6
assert self.CM.get("section.setting3") == "Another String"
assert self.CM.get("section.setting4") == True
self.assertEqual(self.CM.get("section.setting1"), 2)
self.assertEqual(self.CM.get("section.setting2"), 8.6)
self.assertEqual(self.CM.get("section.setting3"), "Another String")
self.assertTrue(self.CM.get("section.setting4"))
#self.assertRaises(AttributeError, self.CM.set, "section.setting1", 2.8)
#self.assertRaises(AttributeError, self.CM.set, "section.setting2", 2)
self.assertRaises(AttributeError, self.CM.set, "section.setting3", 6)
self.assertRaises(AttributeError, self.CM.set, "section.setting4", 1)
self.assertEqual(self.CM.get("section.setting1"), 2)
self.assertEqual(self.CM.get("section.setting2"), 8.6)
self.assertEqual(self.CM.get("section.setting3"), "Another String")
self.assertTrue(self.CM.get("section.setting4"))
# Try to set one that doesn't exist:
try:
self.CM.set("section.setting5", 1)
except AttributeError:
pass
else:
raise AssertionError
self.assertRaises(AttributeError, self.CM.set, "section.setting5", 1)
self.CM.save()
self.CM.reset() # to defaults
assert self.CM.get("section.setting1") == 1
assert self.CM.get("section.setting2") == 3.1415, self.CM.get("section.setting2")
assert self.CM.get("section.setting3") == "String"
assert self.CM.get("section.setting4") == False
self.assertEqual(self.CM.get("section.setting1"), 1)
self.assertEqual(self.CM.get("section.setting2"), 3.1415, self.CM.get("section.setting2"))
self.assertEqual(self.CM.get("section.setting3"), "String")
self.assertFalse(self.CM.get("section.setting4"))
self.CM._x = None
cbid = self.CM.connect("section.setting1", callback)
assert self.CM._x == None
self.assertIsNone(self.CM._x)
self.CM.set("section.setting1", 1024)
assert self.CM._x == "1024", "x was '%s'" % self.CM._x
self.assertEqual(self.CM._x, "1024", "x was '%s'" % self.CM._x)
self.CM.disconnect(cbid)
self.CM.set("section.setting1", -1)
assert self.CM._x == "1024"
self.assertEqual(self.CM._x, "1024")
self.CM.reset("section.setting1")
assert self.CM.get("section.setting1") == 1
self.assertEqual(self.CM.get("section.setting1"), 1)
# No callback:
self.CM._x = None
self.CM.set("section.setting1", 200)
assert self.CM._x == None
self.assertIsNone(self.CM._x)
# Now, set one:
cbid = self.CM.connect("section.setting1", callback)
# Now, call it:
self.CM.emit("section.setting1")
assert self.CM._x == "200"
self.assertEqual(self.CM._x, "200")
self.CM.set("section.setting1", 2)
self.CM.set("section.setting2", 8.6)
@ -146,22 +118,22 @@ class CompleteCheck(unittest.TestCase):
self.CM.save("./test2.ini")
self.CM.reset()
assert self.CM.get("section.setting1") == 1, self.CM.get("section.setting1")
assert self.CM.get("section.setting2") == 3.1415
assert self.CM.get("section.setting3") == "String"
assert self.CM.get("section.setting4") == False
self.assertEqual(self.CM.get("section.setting1"), 1, self.CM.get("section.setting1"))
self.assertEqual(self.CM.get("section.setting2"), 3.1415)
self.assertEqual(self.CM.get("section.setting3"), "String")
self.assertFalse(self.CM.get("section.setting4"))
self.CM.load("./test2.ini")
assert self.CM.get("section.setting1") == 2, self.CM.get("section.setting1")
assert self.CM.get("section.setting2") == 8.6
assert self.CM.get("section.setting3") == "Another String"
assert self.CM.get("section.setting4") == True
self.assertEqual(self.CM.get("section.setting1"), 2, self.CM.get("section.setting1"))
self.assertEqual(self.CM.get("section.setting2"), 8.6)
self.assertEqual(self.CM.get("section.setting3"), "Another String")
self.assertTrue(self.CM.get("section.setting4"))
assert self.CM.get("section2.windows-file") == r"c:\drive\path\o'malley\file.pdf"
assert self.CM.get("section2.list") == [1, 2, 3, 4]
assert self.CM.get("section2.dict") == {'a': "apple", "b": "banana"}
assert self.CM.get("section2.unicode") == "Raötröme"
self.assertEqual(self.CM.get("section2.windows-file"), r"c:\drive\path\o'malley\file.pdf")
self.assertEqual(self.CM.get("section2.list"), [1, 2, 3, 4])
self.assertEqual(self.CM.get("section2.dict"), {'a': "apple", "b": "banana"})
self.assertEqual(self.CM.get("section2.unicode"), "Raötröme")
if __name__ == "__main__":
unittest.main()