root / test / py / ganeti.compat_unittest.py @ 8f096849
History | View | Annotate | Download (3.9 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2010 Google Inc.
|
5 |
#
|
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.
|
10 |
#
|
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.
|
15 |
#
|
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
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for unittesting the compat module"""
|
23 |
|
24 |
import unittest |
25 |
|
26 |
from ganeti import compat |
27 |
|
28 |
import testutils |
29 |
|
30 |
|
31 |
class TestPartial(testutils.GanetiTestCase): |
32 |
def test(self): |
33 |
# Test standard version
|
34 |
self._Test(compat.partial)
|
35 |
|
36 |
# Test our version
|
37 |
self._Test(compat._partial)
|
38 |
|
39 |
def _Test(self, fn): |
40 |
def _TestFunc1(x, power=2): |
41 |
return x ** power
|
42 |
|
43 |
cubic = fn(_TestFunc1, power=3)
|
44 |
self.assertEqual(cubic(1), 1) |
45 |
self.assertEqual(cubic(3), 27) |
46 |
self.assertEqual(cubic(4), 64) |
47 |
|
48 |
def _TestFunc2(*args, **kwargs): |
49 |
return (args, kwargs)
|
50 |
|
51 |
self.assertEqualValues(fn(_TestFunc2, "Hello", "World")("Foo"), |
52 |
(("Hello", "World", "Foo"), {})) |
53 |
|
54 |
self.assertEqualValues(fn(_TestFunc2, "Hello", xyz=123)("Foo"), |
55 |
(("Hello", "Foo"), {"xyz": 123})) |
56 |
|
57 |
self.assertEqualValues(fn(_TestFunc2, xyz=123)("Foo", xyz=999), |
58 |
(("Foo", ), {"xyz": 999,})) |
59 |
|
60 |
|
61 |
class TestTryToRoman(testutils.GanetiTestCase): |
62 |
"""test the compat.TryToRoman function"""
|
63 |
|
64 |
def setUp(self): |
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
|
68 |
|
69 |
def tearDown(self): |
70 |
# ...and restore it at the end of the test
|
71 |
compat.roman = self.compat_roman_module
|
72 |
testutils.GanetiTestCase.tearDown(self)
|
73 |
|
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") |
81 |
|
82 |
def testWithNoRoman(self): |
83 |
# compat.roman is saved/restored in setUp/tearDown
|
84 |
compat.roman = None
|
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) |
89 |
|
90 |
def testStrings(self): |
91 |
self.assertEquals(compat.TryToRoman("astring"), "astring") |
92 |
self.assertEquals(compat.TryToRoman("5"), "5") |
93 |
|
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") |
100 |
|
101 |
|
102 |
class TestUniqueFrozenset(unittest.TestCase): |
103 |
def testDuplicates(self): |
104 |
for values in [["", ""], ["Hello", "World", "Hello"]]: |
105 |
self.assertRaises(ValueError, compat.UniqueFrozenset, values) |
106 |
|
107 |
def testEmpty(self): |
108 |
self.assertEqual(compat.UniqueFrozenset([]), frozenset([])) |
109 |
|
110 |
def testUnique(self): |
111 |
self.assertEqual(compat.UniqueFrozenset([1, 2, 3]), frozenset([1, 2, 3])) |
112 |
|
113 |
def testGenerator(self): |
114 |
seq = ("Foo%s" % i for i in range(10)) |
115 |
self.assertTrue(callable(seq.next)) |
116 |
self.assertFalse(isinstance(seq, (list, tuple))) |
117 |
self.assertEqual(compat.UniqueFrozenset(seq),
|
118 |
frozenset(["Foo%s" % i for i in range(10)])) |
119 |
|
120 |
|
121 |
if __name__ == "__main__": |
122 |
testutils.GanetiTestProgram() |