Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.ht_unittest.py @ 7578ab0a

History | View | Annotate | Download (5.7 kB)

1 ecdf0398 Michael Hanselmann
#!/usr/bin/python
2 ecdf0398 Michael Hanselmann
#
3 ecdf0398 Michael Hanselmann
4 ecdf0398 Michael Hanselmann
# Copyright (C) 2011 Google Inc.
5 ecdf0398 Michael Hanselmann
#
6 ecdf0398 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 ecdf0398 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 ecdf0398 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 ecdf0398 Michael Hanselmann
# (at your option) any later version.
10 ecdf0398 Michael Hanselmann
#
11 ecdf0398 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 ecdf0398 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 ecdf0398 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ecdf0398 Michael Hanselmann
# General Public License for more details.
15 ecdf0398 Michael Hanselmann
#
16 ecdf0398 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 ecdf0398 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 ecdf0398 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 ecdf0398 Michael Hanselmann
# 02110-1301, USA.
20 ecdf0398 Michael Hanselmann
21 ecdf0398 Michael Hanselmann
22 ecdf0398 Michael Hanselmann
"""Script for testing ganeti.ht"""
23 ecdf0398 Michael Hanselmann
24 ecdf0398 Michael Hanselmann
import unittest
25 ecdf0398 Michael Hanselmann
26 ecdf0398 Michael Hanselmann
from ganeti import ht
27 ecdf0398 Michael Hanselmann
28 ecdf0398 Michael Hanselmann
import testutils
29 ecdf0398 Michael Hanselmann
30 ecdf0398 Michael Hanselmann
31 ecdf0398 Michael Hanselmann
class TestTypeChecks(unittest.TestCase):
32 ecdf0398 Michael Hanselmann
  def testNone(self):
33 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TNotNone(None))
34 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TNone(None))
35 ecdf0398 Michael Hanselmann
36 ecdf0398 Michael Hanselmann
    for val in [0, True, "", "Hello World", [], range(5)]:
37 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TNotNone(val))
38 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TNone(val))
39 ecdf0398 Michael Hanselmann
40 ecdf0398 Michael Hanselmann
  def testBool(self):
41 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TBool(True))
42 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TBool(False))
43 ecdf0398 Michael Hanselmann
44 ecdf0398 Michael Hanselmann
    for val in [0, None, "", [], "Hello"]:
45 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TBool(val))
46 ecdf0398 Michael Hanselmann
47 ecdf0398 Michael Hanselmann
    for val in [True, -449, 1, 3, "x", "abc", [1, 2]]:
48 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TTrue(val))
49 ecdf0398 Michael Hanselmann
50 ecdf0398 Michael Hanselmann
    for val in [False, 0, None, []]:
51 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TTrue(val))
52 ecdf0398 Michael Hanselmann
53 ecdf0398 Michael Hanselmann
  def testInt(self):
54 ecdf0398 Michael Hanselmann
    for val in [-100, -3, 0, 16, 128, 923874]:
55 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TInt(val))
56 ecdf0398 Michael Hanselmann
57 ecdf0398 Michael Hanselmann
    for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]:
58 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TInt(val))
59 ecdf0398 Michael Hanselmann
60 ecdf0398 Michael Hanselmann
    for val in range(0, 100, 4):
61 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TPositiveInt(val))
62 ecdf0398 Michael Hanselmann
      neg = -(val + 1)
63 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TPositiveInt(neg))
64 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TStrictPositiveInt(neg))
65 ecdf0398 Michael Hanselmann
66 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TPositiveInt(0.1 + val))
67 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TStrictPositiveInt(0.1 + val))
68 ecdf0398 Michael Hanselmann
69 ecdf0398 Michael Hanselmann
    for val in [0, 0.1, 0.9, -0.3]:
70 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TStrictPositiveInt(val))
71 ecdf0398 Michael Hanselmann
72 ecdf0398 Michael Hanselmann
    for val in range(1, 100, 4):
73 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TStrictPositiveInt(val))
74 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TStrictPositiveInt(0.1 + val))
75 ecdf0398 Michael Hanselmann
76 ecdf0398 Michael Hanselmann
  def testFloat(self):
77 ecdf0398 Michael Hanselmann
    for val in [-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
78 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TFloat(val))
79 ecdf0398 Michael Hanselmann
80 ecdf0398 Michael Hanselmann
    for val in [False, True, None, "", [], "Hello", 0, 28, -1, -3281]:
81 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TFloat(val))
82 ecdf0398 Michael Hanselmann
83 ecdf0398 Michael Hanselmann
  def testString(self):
84 ecdf0398 Michael Hanselmann
    for val in ["", "abc", "Hello World", "123",
85 ecdf0398 Michael Hanselmann
                u"", u"\u272C", u"abc"]:
86 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TString(val))
87 ecdf0398 Michael Hanselmann
88 ecdf0398 Michael Hanselmann
    for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
89 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TString(val))
90 ecdf0398 Michael Hanselmann
91 ecdf0398 Michael Hanselmann
  def testElemOf(self):
92 ecdf0398 Michael Hanselmann
    fn = ht.TElemOf(range(10))
93 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(0))
94 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(3))
95 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(9))
96 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(-1))
97 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(100))
98 ecdf0398 Michael Hanselmann
99 ecdf0398 Michael Hanselmann
    fn = ht.TElemOf([])
100 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(0))
101 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(100))
102 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(True))
103 ecdf0398 Michael Hanselmann
104 ecdf0398 Michael Hanselmann
    fn = ht.TElemOf(["Hello", "World"])
105 ecdf0398 Michael Hanselmann
    self.assertTrue(fn("Hello"))
106 ecdf0398 Michael Hanselmann
    self.assertTrue(fn("World"))
107 ecdf0398 Michael Hanselmann
    self.assertFalse(fn("e"))
108 ecdf0398 Michael Hanselmann
109 ecdf0398 Michael Hanselmann
  def testList(self):
110 ecdf0398 Michael Hanselmann
    for val in [[], range(10), ["Hello", "World", "!"]]:
111 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TList(val))
112 ecdf0398 Michael Hanselmann
113 ecdf0398 Michael Hanselmann
    for val in [False, True, None, {}, 0, 1, 5, -193, 93.8582]:
114 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TList(val))
115 ecdf0398 Michael Hanselmann
116 ecdf0398 Michael Hanselmann
  def testDict(self):
117 ecdf0398 Michael Hanselmann
    for val in [{}, dict.fromkeys(range(10)), {"Hello": [], "World": "!"}]:
118 ecdf0398 Michael Hanselmann
      self.assertTrue(ht.TDict(val))
119 ecdf0398 Michael Hanselmann
120 ecdf0398 Michael Hanselmann
    for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
121 ecdf0398 Michael Hanselmann
      self.assertFalse(ht.TDict(val))
122 ecdf0398 Michael Hanselmann
123 ecdf0398 Michael Hanselmann
  def testIsLength(self):
124 ecdf0398 Michael Hanselmann
    fn = ht.TIsLength(10)
125 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(range(10)))
126 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(range(1)))
127 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(range(100)))
128 ecdf0398 Michael Hanselmann
129 ecdf0398 Michael Hanselmann
  def testAnd(self):
130 ecdf0398 Michael Hanselmann
    fn = ht.TAnd(ht.TNotNone, ht.TString)
131 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(""))
132 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(1))
133 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(None))
134 ecdf0398 Michael Hanselmann
135 ecdf0398 Michael Hanselmann
  def testOr(self):
136 ecdf0398 Michael Hanselmann
    fn = ht.TOr(ht.TNone, ht.TAnd(ht.TString, ht.TIsLength(5)))
137 ecdf0398 Michael Hanselmann
    self.assertTrue(fn("12345"))
138 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(None))
139 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(1))
140 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(""))
141 ecdf0398 Michael Hanselmann
    self.assertFalse(fn("abc"))
142 ecdf0398 Michael Hanselmann
143 ecdf0398 Michael Hanselmann
  def testMap(self):
144 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMap(str, ht.TString)(123))
145 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMap(int, ht.TInt)("9999"))
146 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TMap(lambda x: x + 100, ht.TString)(123))
147 ecdf0398 Michael Hanselmann
148 ecdf0398 Michael Hanselmann
  def testNonEmptyString(self):
149 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TNonEmptyString("xyz"))
150 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TNonEmptyString("Hello World"))
151 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TNonEmptyString(""))
152 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TNonEmptyString(None))
153 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TNonEmptyString([]))
154 ecdf0398 Michael Hanselmann
155 ecdf0398 Michael Hanselmann
  def testMaybeString(self):
156 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMaybeString("xyz"))
157 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMaybeString("Hello World"))
158 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMaybeString(None))
159 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TMaybeString(""))
160 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TMaybeString([]))
161 ecdf0398 Michael Hanselmann
162 ecdf0398 Michael Hanselmann
  def testMaybeBool(self):
163 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMaybeBool(False))
164 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMaybeBool(True))
165 ecdf0398 Michael Hanselmann
    self.assertTrue(ht.TMaybeBool(None))
166 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TMaybeBool([]))
167 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TMaybeBool("0"))
168 ecdf0398 Michael Hanselmann
    self.assertFalse(ht.TMaybeBool("False"))
169 ecdf0398 Michael Hanselmann
170 ecdf0398 Michael Hanselmann
  def testListOf(self):
171 ecdf0398 Michael Hanselmann
    fn = ht.TListOf(ht.TNonEmptyString)
172 ecdf0398 Michael Hanselmann
    self.assertTrue(fn([]))
173 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(["x"]))
174 ecdf0398 Michael Hanselmann
    self.assertTrue(fn(["Hello", "World"]))
175 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(None))
176 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(False))
177 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(range(3)))
178 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(["x", None]))
179 ecdf0398 Michael Hanselmann
180 ecdf0398 Michael Hanselmann
  def testDictOf(self):
181 ecdf0398 Michael Hanselmann
    fn = ht.TDictOf(ht.TNonEmptyString, ht.TInt)
182 ecdf0398 Michael Hanselmann
    self.assertTrue(fn({}))
183 ecdf0398 Michael Hanselmann
    self.assertTrue(fn({"x": 123, "y": 999}))
184 ecdf0398 Michael Hanselmann
    self.assertFalse(fn(None))
185 ecdf0398 Michael Hanselmann
    self.assertFalse(fn({1: "x"}))
186 ecdf0398 Michael Hanselmann
    self.assertFalse(fn({"x": ""}))
187 ecdf0398 Michael Hanselmann
    self.assertFalse(fn({"x": None}))
188 ecdf0398 Michael Hanselmann
    self.assertFalse(fn({"": 8234}))
189 ecdf0398 Michael Hanselmann
190 ecdf0398 Michael Hanselmann
191 ecdf0398 Michael Hanselmann
if __name__ == "__main__":
192 ecdf0398 Michael Hanselmann
  testutils.GanetiTestProgram()