Convert test_ipautil from unittest to nose

This commit is contained in:
Petr Viktorin 2012-06-15 05:17:49 -04:00
parent 3e505fe532
commit 468e5e40cc

View File

@ -21,7 +21,6 @@ Test the `ipapython/ipautil.py` module.
""" """
import nose import nose
import unittest
from ipapython import ipautil from ipapython import ipautil
@ -70,280 +69,265 @@ def test_ip_address():
yield (CheckIPAddress(addr[0]),) + addr yield (CheckIPAddress(addr[0]),) + addr
class TestCIDict(unittest.TestCase): class TestCIDict(object):
def setUp(self): def setup(self):
self.cidict = ipautil.CIDict() self.cidict = ipautil.CIDict()
self.cidict["Key1"] = "val1" self.cidict["Key1"] = "val1"
self.cidict["key2"] = "val2" self.cidict["key2"] = "val2"
self.cidict["KEY3"] = "VAL3" self.cidict["KEY3"] = "VAL3"
def tearDown(self): def test_len(self):
pass nose.tools.assert_equal(3, len(self.cidict))
def testLen(self): def test_getitem(self):
self.assertEqual(3, len(self.cidict)) nose.tools.assert_equal("val1", self.cidict["Key1"])
nose.tools.assert_equal("val1", self.cidict["key1"])
def test__GetItem(self): nose.tools.assert_equal("val2", self.cidict["KEY2"])
self.assertEqual("val1", self.cidict["Key1"]) nose.tools.assert_equal("VAL3", self.cidict["key3"])
self.assertEqual("val1", self.cidict["key1"]) nose.tools.assert_equal("VAL3", self.cidict["KEY3"])
self.assertEqual("val2", self.cidict["KEY2"]) with nose.tools.assert_raises(KeyError):
self.assertEqual("VAL3", self.cidict["key3"])
self.assertEqual("VAL3", self.cidict["KEY3"])
try:
self.cidict["key4"] self.cidict["key4"]
fail("should have raised KeyError")
except KeyError:
pass
def testGet(self): def test_get(self):
self.assertEqual("val1", self.cidict.get("Key1")) nose.tools.assert_equal("val1", self.cidict.get("Key1"))
self.assertEqual("val1", self.cidict.get("key1")) nose.tools.assert_equal("val1", self.cidict.get("key1"))
self.assertEqual("val2", self.cidict.get("KEY2")) nose.tools.assert_equal("val2", self.cidict.get("KEY2"))
self.assertEqual("VAL3", self.cidict.get("key3")) nose.tools.assert_equal("VAL3", self.cidict.get("key3"))
self.assertEqual("VAL3", self.cidict.get("KEY3")) nose.tools.assert_equal("VAL3", self.cidict.get("KEY3"))
self.assertEqual("default", self.cidict.get("key4", "default")) nose.tools.assert_equal("default", self.cidict.get("key4", "default"))
def test__SetItem(self): def test_setitem(self):
self.cidict["key4"] = "val4" self.cidict["key4"] = "val4"
self.assertEqual("val4", self.cidict["key4"]) nose.tools.assert_equal("val4", self.cidict["key4"])
self.cidict["KEY4"] = "newval4" self.cidict["KEY4"] = "newval4"
self.assertEqual("newval4", self.cidict["key4"]) nose.tools.assert_equal("newval4", self.cidict["key4"])
def testDel(self): def test_del(self):
self.assert_(self.cidict.has_key("Key1")) assert self.cidict.has_key("Key1")
del(self.cidict["Key1"]) del(self.cidict["Key1"])
self.failIf(self.cidict.has_key("Key1")) assert not self.cidict.has_key("Key1")
self.assert_(self.cidict.has_key("key2")) assert self.cidict.has_key("key2")
del(self.cidict["KEY2"]) del(self.cidict["KEY2"])
self.failIf(self.cidict.has_key("key2")) assert not self.cidict.has_key("key2")
def testClear(self): def test_clear(self):
self.assertEqual(3, len(self.cidict)) nose.tools.assert_equal(3, len(self.cidict))
self.cidict.clear() self.cidict.clear()
self.assertEqual(0, len(self.cidict)) nose.tools.assert_equal(0, len(self.cidict))
def testCopy(self): def test_copy(self):
"""A copy is no longer a CIDict, but should preserve the case of """A copy is no longer a CIDict, but should preserve the case of
the keys as they were inserted.""" the keys as they were inserted."""
copy = self.cidict.copy() copy = self.cidict.copy()
self.assertEqual(3, len(copy)) nose.tools.assert_equal(3, len(copy))
self.assert_(copy.has_key("Key1")) assert copy.has_key("Key1")
self.assertEqual("val1", copy["Key1"]) nose.tools.assert_equal("val1", copy["Key1"])
self.failIf(copy.has_key("key1")) assert not copy.has_key("key1")
def testHasKey(self): def test_haskey(self):
self.assert_(self.cidict.has_key("KEY1")) assert self.cidict.has_key("KEY1")
self.assert_(self.cidict.has_key("key2")) assert self.cidict.has_key("key2")
self.assert_(self.cidict.has_key("key3")) assert self.cidict.has_key("key3")
def testItems(self): def test_items(self):
items = self.cidict.items() items = self.cidict.items()
self.assertEqual(3, len(items)) nose.tools.assert_equal(3, len(items))
items_set = set(items) items_set = set(items)
self.assert_(("Key1", "val1") in items_set) assert ("Key1", "val1") in items_set
self.assert_(("key2", "val2") in items_set) assert ("key2", "val2") in items_set
self.assert_(("KEY3", "VAL3") in items_set) assert ("KEY3", "VAL3") in items_set
def testIterItems(self): def test_iteritems(self):
items = [] items = []
for (k,v) in self.cidict.iteritems(): for (k,v) in self.cidict.iteritems():
items.append((k,v)) items.append((k,v))
self.assertEqual(3, len(items)) nose.tools.assert_equal(3, len(items))
items_set = set(items) items_set = set(items)
self.assert_(("Key1", "val1") in items_set) assert ("Key1", "val1") in items_set
self.assert_(("key2", "val2") in items_set) assert ("key2", "val2") in items_set
self.assert_(("KEY3", "VAL3") in items_set) assert ("KEY3", "VAL3") in items_set
def testIterKeys(self): def test_iterkeys(self):
keys = [] keys = []
for k in self.cidict.iterkeys(): for k in self.cidict.iterkeys():
keys.append(k) keys.append(k)
self.assertEqual(3, len(keys)) nose.tools.assert_equal(3, len(keys))
keys_set = set(keys) keys_set = set(keys)
self.assert_("Key1" in keys_set) assert "Key1" in keys_set
self.assert_("key2" in keys_set) assert "key2" in keys_set
self.assert_("KEY3" in keys_set) assert "KEY3" in keys_set
def testIterValues(self): def test_itervalues(self):
values = [] values = []
for k in self.cidict.itervalues(): for k in self.cidict.itervalues():
values.append(k) values.append(k)
self.assertEqual(3, len(values)) nose.tools.assert_equal(3, len(values))
values_set = set(values) values_set = set(values)
self.assert_("val1" in values_set) assert "val1" in values_set
self.assert_("val2" in values_set) assert "val2" in values_set
self.assert_("VAL3" in values_set) assert "VAL3" in values_set
def testKeys(self): def test_keys(self):
keys = self.cidict.keys() keys = self.cidict.keys()
self.assertEqual(3, len(keys)) nose.tools.assert_equal(3, len(keys))
keys_set = set(keys) keys_set = set(keys)
self.assert_("Key1" in keys_set) assert "Key1" in keys_set
self.assert_("key2" in keys_set) assert "key2" in keys_set
self.assert_("KEY3" in keys_set) assert "KEY3" in keys_set
def testValues(self): def test_values(self):
values = self.cidict.values() values = self.cidict.values()
self.assertEqual(3, len(values)) nose.tools.assert_equal(3, len(values))
values_set = set(values) values_set = set(values)
self.assert_("val1" in values_set) assert "val1" in values_set
self.assert_("val2" in values_set) assert "val2" in values_set
self.assert_("VAL3" in values_set) assert "VAL3" in values_set
def testUpdate(self): def test_update(self):
newdict = { "KEY2": "newval2", newdict = { "KEY2": "newval2",
"key4": "val4" } "key4": "val4" }
self.cidict.update(newdict) self.cidict.update(newdict)
self.assertEqual(4, len(self.cidict)) nose.tools.assert_equal(4, len(self.cidict))
items = self.cidict.items() items = self.cidict.items()
self.assertEqual(4, len(items)) nose.tools.assert_equal(4, len(items))
items_set = set(items) items_set = set(items)
self.assert_(("Key1", "val1") in items_set) assert ("Key1", "val1") in items_set
# note the update "overwrites" the case of the key2 # note the update "overwrites" the case of the key2
self.assert_(("KEY2", "newval2") in items_set) assert ("KEY2", "newval2") in items_set
self.assert_(("KEY3", "VAL3") in items_set) assert ("KEY3", "VAL3") in items_set
self.assert_(("key4", "val4") in items_set) assert ("key4", "val4") in items_set
def testSetDefault(self): def test_setdefault(self):
self.assertEqual("val1", self.cidict.setdefault("KEY1", "default")) nose.tools.assert_equal("val1", self.cidict.setdefault("KEY1", "default"))
self.failIf(self.cidict.has_key("KEY4")) assert not self.cidict.has_key("KEY4")
self.assertEqual("default", self.cidict.setdefault("KEY4", "default")) nose.tools.assert_equal("default", self.cidict.setdefault("KEY4", "default"))
self.assert_(self.cidict.has_key("KEY4")) assert self.cidict.has_key("KEY4")
self.assertEqual("default", self.cidict["key4"]) nose.tools.assert_equal("default", self.cidict["key4"])
self.failIf(self.cidict.has_key("KEY5")) assert not self.cidict.has_key("KEY5")
self.assertEqual(None, self.cidict.setdefault("KEY5")) nose.tools.assert_equal(None, self.cidict.setdefault("KEY5"))
self.assert_(self.cidict.has_key("KEY5")) assert self.cidict.has_key("KEY5")
self.assertEqual(None, self.cidict["key5"]) nose.tools.assert_equal(None, self.cidict["key5"])
def testPop(self): def test_pop(self):
self.assertEqual("val1", self.cidict.pop("KEY1", "default")) nose.tools.assert_equal("val1", self.cidict.pop("KEY1", "default"))
self.failIf(self.cidict.has_key("key1")) assert not self.cidict.has_key("key1")
self.assertEqual("val2", self.cidict.pop("KEY2")) nose.tools.assert_equal("val2", self.cidict.pop("KEY2"))
self.failIf(self.cidict.has_key("key2")) assert not self.cidict.has_key("key2")
self.assertEqual("default", self.cidict.pop("key4", "default")) nose.tools.assert_equal("default", self.cidict.pop("key4", "default"))
try: with nose.tools.assert_raises(KeyError):
self.cidict.pop("key4") self.cidict.pop("key4")
fail("should have raised KeyError")
except KeyError:
pass
def testPopItem(self): def test_popitem(self):
items = set(self.cidict.items()) items = set(self.cidict.items())
self.assertEqual(3, len(self.cidict)) nose.tools.assert_equal(3, len(self.cidict))
item = self.cidict.popitem() item = self.cidict.popitem()
self.assertEqual(2, len(self.cidict)) nose.tools.assert_equal(2, len(self.cidict))
self.assert_(item in items) assert item in items
items.discard(item) items.discard(item)
item = self.cidict.popitem() item = self.cidict.popitem()
self.assertEqual(1, len(self.cidict)) nose.tools.assert_equal(1, len(self.cidict))
self.assert_(item in items) assert item in items
items.discard(item) items.discard(item)
item = self.cidict.popitem() item = self.cidict.popitem()
self.assertEqual(0, len(self.cidict)) nose.tools.assert_equal(0, len(self.cidict))
self.assert_(item in items) assert item in items
items.discard(item) items.discard(item)
class TestTimeParser(unittest.TestCase): class TestTimeParser(object):
def setUp(self): def test_simple(self):
pass
def tearDown(self):
pass
def testSimple(self):
timestr = "20070803" timestr = "20070803"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(2007, time.year) nose.tools.assert_equal(2007, time.year)
self.assertEqual(8, time.month) nose.tools.assert_equal(8, time.month)
self.assertEqual(3, time.day) nose.tools.assert_equal(3, time.day)
self.assertEqual(0, time.hour) nose.tools.assert_equal(0, time.hour)
self.assertEqual(0, time.minute) nose.tools.assert_equal(0, time.minute)
self.assertEqual(0, time.second) nose.tools.assert_equal(0, time.second)
def testHourMinSec(self): def test_hour_min_sec(self):
timestr = "20051213141205" timestr = "20051213141205"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(2005, time.year) nose.tools.assert_equal(2005, time.year)
self.assertEqual(12, time.month) nose.tools.assert_equal(12, time.month)
self.assertEqual(13, time.day) nose.tools.assert_equal(13, time.day)
self.assertEqual(14, time.hour) nose.tools.assert_equal(14, time.hour)
self.assertEqual(12, time.minute) nose.tools.assert_equal(12, time.minute)
self.assertEqual(5, time.second) nose.tools.assert_equal(5, time.second)
def testFractions(self): def test_fractions(self):
timestr = "2003092208.5" timestr = "2003092208.5"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(2003, time.year) nose.tools.assert_equal(2003, time.year)
self.assertEqual(9, time.month) nose.tools.assert_equal(9, time.month)
self.assertEqual(22, time.day) nose.tools.assert_equal(22, time.day)
self.assertEqual(8, time.hour) nose.tools.assert_equal(8, time.hour)
self.assertEqual(30, time.minute) nose.tools.assert_equal(30, time.minute)
self.assertEqual(0, time.second) nose.tools.assert_equal(0, time.second)
timestr = "199203301544,25" timestr = "199203301544,25"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(1992, time.year) nose.tools.assert_equal(1992, time.year)
self.assertEqual(3, time.month) nose.tools.assert_equal(3, time.month)
self.assertEqual(30, time.day) nose.tools.assert_equal(30, time.day)
self.assertEqual(15, time.hour) nose.tools.assert_equal(15, time.hour)
self.assertEqual(44, time.minute) nose.tools.assert_equal(44, time.minute)
self.assertEqual(15, time.second) nose.tools.assert_equal(15, time.second)
timestr = "20060401185912,8" timestr = "20060401185912,8"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(2006, time.year) nose.tools.assert_equal(2006, time.year)
self.assertEqual(4, time.month) nose.tools.assert_equal(4, time.month)
self.assertEqual(1, time.day) nose.tools.assert_equal(1, time.day)
self.assertEqual(18, time.hour) nose.tools.assert_equal(18, time.hour)
self.assertEqual(59, time.minute) nose.tools.assert_equal(59, time.minute)
self.assertEqual(12, time.second) nose.tools.assert_equal(12, time.second)
self.assertEqual(800000, time.microsecond) nose.tools.assert_equal(800000, time.microsecond)
def testTimeZones(self): def test_time_zones(self):
timestr = "20051213141205Z" timestr = "20051213141205Z"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(0, time.tzinfo.houroffset) nose.tools.assert_equal(0, time.tzinfo.houroffset)
self.assertEqual(0, time.tzinfo.minoffset) nose.tools.assert_equal(0, time.tzinfo.minoffset)
offset = time.tzinfo.utcoffset(time.tzinfo.dst()) offset = time.tzinfo.utcoffset(time.tzinfo.dst())
self.assertEqual(0, offset.seconds) nose.tools.assert_equal(0, offset.seconds)
timestr = "20051213141205+0500" timestr = "20051213141205+0500"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(5, time.tzinfo.houroffset) nose.tools.assert_equal(5, time.tzinfo.houroffset)
self.assertEqual(0, time.tzinfo.minoffset) nose.tools.assert_equal(0, time.tzinfo.minoffset)
offset = time.tzinfo.utcoffset(time.tzinfo.dst()) offset = time.tzinfo.utcoffset(time.tzinfo.dst())
self.assertEqual(5 * 60 * 60, offset.seconds) nose.tools.assert_equal(5 * 60 * 60, offset.seconds)
timestr = "20051213141205-0500" timestr = "20051213141205-0500"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(-5, time.tzinfo.houroffset) nose.tools.assert_equal(-5, time.tzinfo.houroffset)
self.assertEqual(0, time.tzinfo.minoffset) nose.tools.assert_equal(0, time.tzinfo.minoffset)
# NOTE - the offset is always positive - it's minutes # NOTE - the offset is always positive - it's minutes
# _east_ of UTC # _east_ of UTC
offset = time.tzinfo.utcoffset(time.tzinfo.dst()) offset = time.tzinfo.utcoffset(time.tzinfo.dst())
self.assertEqual((24 - 5) * 60 * 60, offset.seconds) nose.tools.assert_equal((24 - 5) * 60 * 60, offset.seconds)
timestr = "20051213141205-0930" timestr = "20051213141205-0930"
time = ipautil.parse_generalized_time(timestr) time = ipautil.parse_generalized_time(timestr)
self.assertEqual(-9, time.tzinfo.houroffset) nose.tools.assert_equal(-9, time.tzinfo.houroffset)
self.assertEqual(-30, time.tzinfo.minoffset) nose.tools.assert_equal(-30, time.tzinfo.minoffset)
offset = time.tzinfo.utcoffset(time.tzinfo.dst()) offset = time.tzinfo.utcoffset(time.tzinfo.dst())
self.assertEqual(((24 - 9) * 60 * 60) - (30 * 60), offset.seconds) nose.tools.assert_equal(((24 - 9) * 60 * 60) - (30 * 60), offset.seconds)