Fix and Improve TryToRoman unittest
authorGuido Trotter <ultrotter@google.com>
Tue, 1 Jun 2010 10:45:30 +0000 (11:45 +0100)
committerGuido Trotter <ultrotter@google.com>
Tue, 1 Jun 2010 16:40:48 +0000 (17:40 +0100)
1) Don't break when the roman module is not found
2) Test that not finding the roman module doesn't make TryToRoman fail
(currently that is the case)

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

test/ganeti.compat_unittest.py

index dbe9940..54a1c62 100755 (executable)
@@ -61,11 +61,31 @@ class TestPartial(testutils.GanetiTestCase):
 class TestTryToRoman(testutils.GanetiTestCase):
   """test the compat.TryToRoman function"""
 
+  def setUp(self):
+    testutils.GanetiTestCase.setUp(self)
+    # Save the compat.roman module so we can alter it with a fake...
+    self.compat_roman_module = compat.roman
+
+  def tearDown(self):
+    # ...and restore it at the end of the test
+    compat.roman = self.compat_roman_module
+    testutils.GanetiTestCase.tearDown(self)
+
   def testAFewIntegers(self):
+    # This test only works is the roman module is installed
+    if compat.roman is not None:
+      self.assertEquals(compat.TryToRoman(0), 0)
+      self.assertEquals(compat.TryToRoman(1), "I")
+      self.assertEquals(compat.TryToRoman(4), "IV")
+      self.assertEquals(compat.TryToRoman(5), "V")
+
+  def testWithNoRoman(self):
+    # compat.roman is saved/restored in setUp/tearDown
+    compat.roman = None
     self.assertEquals(compat.TryToRoman(0), 0)
-    self.assertEquals(compat.TryToRoman(1), "I")
-    self.assertEquals(compat.TryToRoman(4), "IV")
-    self.assertEquals(compat.TryToRoman(5), "V")
+    self.assertEquals(compat.TryToRoman(1), 1)
+    self.assertEquals(compat.TryToRoman(4), 4)
+    self.assertEquals(compat.TryToRoman(5), 5)
 
   def testStrings(self):
     self.assertEquals(compat.TryToRoman("astring"), "astring")