Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.compat_unittest.py @ d01e51a5

History | View | Annotate | Download (3.9 kB)

1 cea881e5 Michael Hanselmann
#!/usr/bin/python
2 cea881e5 Michael Hanselmann
#
3 cea881e5 Michael Hanselmann
4 cea881e5 Michael Hanselmann
# Copyright (C) 2010 Google Inc.
5 cea881e5 Michael Hanselmann
#
6 cea881e5 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 cea881e5 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 cea881e5 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 cea881e5 Michael Hanselmann
# (at your option) any later version.
10 cea881e5 Michael Hanselmann
#
11 cea881e5 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 cea881e5 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 cea881e5 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 cea881e5 Michael Hanselmann
# General Public License for more details.
15 cea881e5 Michael Hanselmann
#
16 cea881e5 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 cea881e5 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 cea881e5 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 cea881e5 Michael Hanselmann
# 02110-1301, USA.
20 cea881e5 Michael Hanselmann
21 cea881e5 Michael Hanselmann
22 cea881e5 Michael Hanselmann
"""Script for unittesting the compat module"""
23 cea881e5 Michael Hanselmann
24 cea881e5 Michael Hanselmann
import unittest
25 cea881e5 Michael Hanselmann
26 cea881e5 Michael Hanselmann
from ganeti import compat
27 cea881e5 Michael Hanselmann
28 cea881e5 Michael Hanselmann
import testutils
29 cea881e5 Michael Hanselmann
30 cea881e5 Michael Hanselmann
31 cea881e5 Michael Hanselmann
class TestPartial(testutils.GanetiTestCase):
32 cea881e5 Michael Hanselmann
  def test(self):
33 cea881e5 Michael Hanselmann
    # Test standard version
34 cea881e5 Michael Hanselmann
    self._Test(compat.partial)
35 cea881e5 Michael Hanselmann
36 cea881e5 Michael Hanselmann
    # Test our version
37 cea881e5 Michael Hanselmann
    self._Test(compat._partial)
38 cea881e5 Michael Hanselmann
39 cea881e5 Michael Hanselmann
  def _Test(self, fn):
40 cea881e5 Michael Hanselmann
    def _TestFunc1(x, power=2):
41 cea881e5 Michael Hanselmann
      return x ** power
42 cea881e5 Michael Hanselmann
43 cea881e5 Michael Hanselmann
    cubic = fn(_TestFunc1, power=3)
44 cea881e5 Michael Hanselmann
    self.assertEqual(cubic(1), 1)
45 cea881e5 Michael Hanselmann
    self.assertEqual(cubic(3), 27)
46 cea881e5 Michael Hanselmann
    self.assertEqual(cubic(4), 64)
47 cea881e5 Michael Hanselmann
48 cea881e5 Michael Hanselmann
    def _TestFunc2(*args, **kwargs):
49 cea881e5 Michael Hanselmann
      return (args, kwargs)
50 cea881e5 Michael Hanselmann
51 cea881e5 Michael Hanselmann
    self.assertEqualValues(fn(_TestFunc2, "Hello", "World")("Foo"),
52 cea881e5 Michael Hanselmann
                           (("Hello", "World", "Foo"), {}))
53 cea881e5 Michael Hanselmann
54 cea881e5 Michael Hanselmann
    self.assertEqualValues(fn(_TestFunc2, "Hello", xyz=123)("Foo"),
55 cea881e5 Michael Hanselmann
                           (("Hello", "Foo"), {"xyz": 123}))
56 cea881e5 Michael Hanselmann
57 cea881e5 Michael Hanselmann
    self.assertEqualValues(fn(_TestFunc2, xyz=123)("Foo", xyz=999),
58 cea881e5 Michael Hanselmann
                           (("Foo", ), {"xyz": 999,}))
59 cea881e5 Michael Hanselmann
60 cea881e5 Michael Hanselmann
61 6396164f Guido Trotter
class TestTryToRoman(testutils.GanetiTestCase):
62 6396164f Guido Trotter
  """test the compat.TryToRoman function"""
63 6396164f Guido Trotter
64 855bffd2 Guido Trotter
  def setUp(self):
65 855bffd2 Guido Trotter
    testutils.GanetiTestCase.setUp(self)
66 855bffd2 Guido Trotter
    # Save the compat.roman module so we can alter it with a fake...
67 855bffd2 Guido Trotter
    self.compat_roman_module = compat.roman
68 855bffd2 Guido Trotter
69 855bffd2 Guido Trotter
  def tearDown(self):
70 855bffd2 Guido Trotter
    # ...and restore it at the end of the test
71 855bffd2 Guido Trotter
    compat.roman = self.compat_roman_module
72 855bffd2 Guido Trotter
    testutils.GanetiTestCase.tearDown(self)
73 855bffd2 Guido Trotter
74 6396164f Guido Trotter
  def testAFewIntegers(self):
75 855bffd2 Guido Trotter
    # This test only works is the roman module is installed
76 855bffd2 Guido Trotter
    if compat.roman is not None:
77 855bffd2 Guido Trotter
      self.assertEquals(compat.TryToRoman(0), 0)
78 855bffd2 Guido Trotter
      self.assertEquals(compat.TryToRoman(1), "I")
79 855bffd2 Guido Trotter
      self.assertEquals(compat.TryToRoman(4), "IV")
80 855bffd2 Guido Trotter
      self.assertEquals(compat.TryToRoman(5), "V")
81 855bffd2 Guido Trotter
82 855bffd2 Guido Trotter
  def testWithNoRoman(self):
83 855bffd2 Guido Trotter
    # compat.roman is saved/restored in setUp/tearDown
84 855bffd2 Guido Trotter
    compat.roman = None
85 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman(0), 0)
86 855bffd2 Guido Trotter
    self.assertEquals(compat.TryToRoman(1), 1)
87 855bffd2 Guido Trotter
    self.assertEquals(compat.TryToRoman(4), 4)
88 855bffd2 Guido Trotter
    self.assertEquals(compat.TryToRoman(5), 5)
89 6396164f Guido Trotter
90 6396164f Guido Trotter
  def testStrings(self):
91 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman("astring"), "astring")
92 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman("5"), "5")
93 6396164f Guido Trotter
94 6396164f Guido Trotter
  def testDontConvert(self):
95 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman(0, convert=False), 0)
96 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman(1, convert=False), 1)
97 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman(7, convert=False), 7)
98 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman("astring", convert=False), "astring")
99 6396164f Guido Trotter
    self.assertEquals(compat.TryToRoman("19", convert=False), "19")
100 6396164f Guido Trotter
101 6396164f Guido Trotter
102 4a3dd52d Michael Hanselmann
class TestUniqueFrozenset(unittest.TestCase):
103 4a3dd52d Michael Hanselmann
  def testDuplicates(self):
104 4a3dd52d Michael Hanselmann
    for values in [["", ""], ["Hello", "World", "Hello"]]:
105 4a3dd52d Michael Hanselmann
      self.assertRaises(ValueError, compat.UniqueFrozenset, values)
106 4a3dd52d Michael Hanselmann
107 4a3dd52d Michael Hanselmann
  def testEmpty(self):
108 4a3dd52d Michael Hanselmann
    self.assertEqual(compat.UniqueFrozenset([]), frozenset([]))
109 4a3dd52d Michael Hanselmann
110 4a3dd52d Michael Hanselmann
  def testUnique(self):
111 4a3dd52d Michael Hanselmann
    self.assertEqual(compat.UniqueFrozenset([1, 2, 3]), frozenset([1, 2, 3]))
112 4a3dd52d Michael Hanselmann
113 4a3dd52d Michael Hanselmann
  def testGenerator(self):
114 4a3dd52d Michael Hanselmann
    seq = ("Foo%s" % i for i in range(10))
115 4a3dd52d Michael Hanselmann
    self.assertTrue(callable(seq.next))
116 4a3dd52d Michael Hanselmann
    self.assertFalse(isinstance(seq, (list, tuple)))
117 4a3dd52d Michael Hanselmann
    self.assertEqual(compat.UniqueFrozenset(seq),
118 4a3dd52d Michael Hanselmann
                     frozenset(["Foo%s" % i for i in range(10)]))
119 4a3dd52d Michael Hanselmann
120 4a3dd52d Michael Hanselmann
121 cea881e5 Michael Hanselmann
if __name__ == "__main__":
122 cea881e5 Michael Hanselmann
  testutils.GanetiTestProgram()