4 # Copyright (C) 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for unittesting the compat module"""
26 from ganeti import compat
31 class TestPartial(testutils.GanetiTestCase):
33 # Test standard version
34 self._Test(compat.partial)
37 self._Test(compat._partial)
40 def _TestFunc1(x, power=2):
43 cubic = fn(_TestFunc1, power=3)
44 self.assertEqual(cubic(1), 1)
45 self.assertEqual(cubic(3), 27)
46 self.assertEqual(cubic(4), 64)
48 def _TestFunc2(*args, **kwargs):
51 self.assertEqualValues(fn(_TestFunc2, "Hello", "World")("Foo"),
52 (("Hello", "World", "Foo"), {}))
54 self.assertEqualValues(fn(_TestFunc2, "Hello", xyz=123)("Foo"),
55 (("Hello", "Foo"), {"xyz": 123}))
57 self.assertEqualValues(fn(_TestFunc2, xyz=123)("Foo", xyz=999),
58 (("Foo", ), {"xyz": 999,}))
61 class TestTryToRoman(testutils.GanetiTestCase):
62 """test the compat.TryToRoman function"""
65 testutils.GanetiTestCase.setUp(self)
66 # Save the compat.roman module so we can alter it with a fake...
67 self.compat_roman_module = compat.roman
70 # ...and restore it at the end of the test
71 compat.roman = self.compat_roman_module
72 testutils.GanetiTestCase.tearDown(self)
74 def testAFewIntegers(self):
75 # This test only works is the roman module is installed
76 if compat.roman is not None:
77 self.assertEquals(compat.TryToRoman(0), 0)
78 self.assertEquals(compat.TryToRoman(1), "I")
79 self.assertEquals(compat.TryToRoman(4), "IV")
80 self.assertEquals(compat.TryToRoman(5), "V")
82 def testWithNoRoman(self):
83 # compat.roman is saved/restored in setUp/tearDown
85 self.assertEquals(compat.TryToRoman(0), 0)
86 self.assertEquals(compat.TryToRoman(1), 1)
87 self.assertEquals(compat.TryToRoman(4), 4)
88 self.assertEquals(compat.TryToRoman(5), 5)
90 def testStrings(self):
91 self.assertEquals(compat.TryToRoman("astring"), "astring")
92 self.assertEquals(compat.TryToRoman("5"), "5")
94 def testDontConvert(self):
95 self.assertEquals(compat.TryToRoman(0, convert=False), 0)
96 self.assertEquals(compat.TryToRoman(1, convert=False), 1)
97 self.assertEquals(compat.TryToRoman(7, convert=False), 7)
98 self.assertEquals(compat.TryToRoman("astring", convert=False), "astring")
99 self.assertEquals(compat.TryToRoman("19", convert=False), "19")
102 if __name__ == "__main__":
103 testutils.GanetiTestProgram()