4 # Copyright (C) 2011, 2012 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 testing ganeti.ht"""
26 from ganeti import constants
32 class TestTypeChecks(unittest.TestCase):
34 self.assertFalse(ht.TNotNone(None))
35 self.assertTrue(ht.TNone(None))
37 for val in [0, True, "", "Hello World", [], range(5)]:
38 self.assertTrue(ht.TNotNone(val))
39 self.assertFalse(ht.TNone(val))
42 self.assertTrue(ht.TBool(True))
43 self.assertTrue(ht.TBool(False))
45 for val in [0, None, "", [], "Hello"]:
46 self.assertFalse(ht.TBool(val))
48 for val in [True, -449, 1, 3, "x", "abc", [1, 2]]:
49 self.assertTrue(ht.TTrue(val))
51 for val in [False, 0, None, []]:
52 self.assertFalse(ht.TTrue(val))
55 for val in [-100, -3, 0, 16, 128, 923874]:
56 self.assertTrue(ht.TInt(val))
57 self.assertTrue(ht.TNumber(val))
59 for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]:
60 self.assertFalse(ht.TInt(val))
62 for val in range(0, 100, 4):
63 self.assertTrue(ht.TNonNegativeInt(val))
65 self.assertFalse(ht.TNonNegativeInt(neg))
66 self.assertFalse(ht.TPositiveInt(neg))
68 self.assertFalse(ht.TNonNegativeInt(0.1 + val))
69 self.assertFalse(ht.TPositiveInt(0.1 + val))
71 for val in [0, 0.1, 0.9, -0.3]:
72 self.assertFalse(ht.TPositiveInt(val))
74 for val in range(1, 100, 4):
75 self.assertTrue(ht.TPositiveInt(val))
76 self.assertFalse(ht.TPositiveInt(0.1 + val))
79 for val in [-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
80 self.assertTrue(ht.TFloat(val))
81 self.assertTrue(ht.TNumber(val))
83 for val in [False, True, None, "", [], "Hello", 0, 28, -1, -3281]:
84 self.assertFalse(ht.TFloat(val))
87 for val in [-100, -3, 0, 16, 128, 923874,
88 -100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
89 self.assertTrue(ht.TNumber(val))
91 for val in [False, True, None, "", [], "Hello", "1"]:
92 self.assertFalse(ht.TNumber(val))
95 for val in ["", "abc", "Hello World", "123",
96 u"", u"\u272C", u"abc"]:
97 self.assertTrue(ht.TString(val))
99 for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
100 self.assertFalse(ht.TString(val))
102 def testElemOf(self):
103 fn = ht.TElemOf(range(10))
104 self.assertTrue(fn(0))
105 self.assertTrue(fn(3))
106 self.assertTrue(fn(9))
107 self.assertFalse(fn(-1))
108 self.assertFalse(fn(100))
111 self.assertFalse(fn(0))
112 self.assertFalse(fn(100))
113 self.assertFalse(fn(True))
115 fn = ht.TElemOf(["Hello", "World"])
116 self.assertTrue(fn("Hello"))
117 self.assertTrue(fn("World"))
118 self.assertFalse(fn("e"))
121 for val in [[], range(10), ["Hello", "World", "!"]]:
122 self.assertTrue(ht.TList(val))
124 for val in [False, True, None, {}, 0, 1, 5, -193, 93.8582]:
125 self.assertFalse(ht.TList(val))
128 for val in [{}, dict.fromkeys(range(10)), {"Hello": [], "World": "!"}]:
129 self.assertTrue(ht.TDict(val))
131 for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
132 self.assertFalse(ht.TDict(val))
134 def testIsLength(self):
135 fn = ht.TIsLength(10)
136 self.assertTrue(fn(range(10)))
137 self.assertFalse(fn(range(1)))
138 self.assertFalse(fn(range(100)))
141 fn = ht.TAnd(ht.TNotNone, ht.TString)
142 self.assertTrue(fn(""))
143 self.assertFalse(fn(1))
144 self.assertFalse(fn(None))
147 fn = ht.TMaybe(ht.TAnd(ht.TString, ht.TIsLength(5)))
148 self.assertTrue(fn("12345"))
149 self.assertTrue(fn(None))
150 self.assertFalse(fn(1))
151 self.assertFalse(fn(""))
152 self.assertFalse(fn("abc"))
155 self.assertTrue(ht.TMap(str, ht.TString)(123))
156 self.assertTrue(ht.TMap(int, ht.TInt)("9999"))
157 self.assertFalse(ht.TMap(lambda x: x + 100, ht.TString)(123))
159 def testNonEmptyString(self):
160 self.assertTrue(ht.TNonEmptyString("xyz"))
161 self.assertTrue(ht.TNonEmptyString("Hello World"))
162 self.assertFalse(ht.TNonEmptyString(""))
163 self.assertFalse(ht.TNonEmptyString(None))
164 self.assertFalse(ht.TNonEmptyString([]))
166 def testMaybeString(self):
167 self.assertTrue(ht.TMaybeString("xyz"))
168 self.assertTrue(ht.TMaybeString("Hello World"))
169 self.assertTrue(ht.TMaybeString(None))
170 self.assertFalse(ht.TMaybeString(""))
171 self.assertFalse(ht.TMaybeString([]))
173 def testMaybeBool(self):
174 self.assertTrue(ht.TMaybeBool(False))
175 self.assertTrue(ht.TMaybeBool(True))
176 self.assertTrue(ht.TMaybeBool(None))
177 self.assertFalse(ht.TMaybeBool([]))
178 self.assertFalse(ht.TMaybeBool("0"))
179 self.assertFalse(ht.TMaybeBool("False"))
181 def testListOf(self):
182 fn = ht.TListOf(ht.TNonEmptyString)
183 self.assertTrue(fn([]))
184 self.assertTrue(fn(["x"]))
185 self.assertTrue(fn(["Hello", "World"]))
186 self.assertFalse(fn(None))
187 self.assertFalse(fn(False))
188 self.assertFalse(fn(range(3)))
189 self.assertFalse(fn(["x", None]))
191 def testDictOf(self):
192 fn = ht.TDictOf(ht.TNonEmptyString, ht.TInt)
193 self.assertTrue(fn({}))
194 self.assertTrue(fn({"x": 123, "y": 999}))
195 self.assertFalse(fn(None))
196 self.assertFalse(fn({1: "x"}))
197 self.assertFalse(fn({"x": ""}))
198 self.assertFalse(fn({"x": None}))
199 self.assertFalse(fn({"": 8234}))
201 def testStrictDictRequireAllExclusive(self):
202 fn = ht.TStrictDict(True, True, { "a": ht.TInt, })
203 self.assertFalse(fn(1))
204 self.assertFalse(fn(None))
205 self.assertFalse(fn({}))
206 self.assertFalse(fn({"a": "Hello", }))
207 self.assertFalse(fn({"unknown": 999,}))
208 self.assertFalse(fn({"unknown": None,}))
210 self.assertTrue(fn({"a": 123, }))
211 self.assertTrue(fn({"a": -5, }))
213 fn = ht.TStrictDict(True, True, { "a": ht.TInt, "x": ht.TString, })
214 self.assertFalse(fn({}))
215 self.assertFalse(fn({"a": -5, }))
216 self.assertTrue(fn({"a": 123, "x": "", }))
217 self.assertFalse(fn({"a": 123, "x": None, }))
219 def testStrictDictExclusive(self):
220 fn = ht.TStrictDict(False, True, { "a": ht.TInt, "b": ht.TList, })
221 self.assertTrue(fn({}))
222 self.assertTrue(fn({"a": 123, }))
223 self.assertTrue(fn({"b": range(4), }))
224 self.assertFalse(fn({"b": 123, }))
226 self.assertFalse(fn({"foo": {}, }))
227 self.assertFalse(fn({"bar": object(), }))
229 def testStrictDictRequireAll(self):
230 fn = ht.TStrictDict(True, False, { "a": ht.TInt, "m": ht.TInt, })
231 self.assertTrue(fn({"a": 1, "m": 2, "bar": object(), }))
232 self.assertFalse(fn({}))
233 self.assertFalse(fn({"a": 1, "bar": object(), }))
234 self.assertFalse(fn({"a": 1, "m": [], "bar": object(), }))
236 def testStrictDict(self):
237 fn = ht.TStrictDict(False, False, { "a": ht.TInt, })
238 self.assertTrue(fn({}))
239 self.assertFalse(fn({"a": ""}))
240 self.assertTrue(fn({"a": 11}))
241 self.assertTrue(fn({"other": 11}))
242 self.assertTrue(fn({"other": object()}))
245 for i in [0, 1, 4395, 2347625220]:
246 self.assertTrue(ht.TJobId(i))
247 self.assertTrue(ht.TJobId(str(i)))
248 self.assertFalse(ht.TJobId(-(i + 1)))
250 for i in ["", "-", ".", ",", "a", "99j", "job-123", "\t", " 83 ",
251 None, [], {}, object()]:
252 self.assertFalse(ht.TJobId(i))
254 def testRelativeJobId(self):
255 for i in [-1, -93, -4395]:
256 self.assertTrue(ht.TRelativeJobId(i))
257 self.assertFalse(ht.TRelativeJobId(str(i)))
259 for i in [0, 1, 2, 10, 9289, "", "0", "-1", "-999"]:
260 self.assertFalse(ht.TRelativeJobId(i))
261 self.assertFalse(ht.TRelativeJobId(str(i)))
264 self.assertRaises(AssertionError, ht.TItems, [])
266 fn = ht.TItems([ht.TString])
267 self.assertFalse(fn([0]))
268 self.assertFalse(fn([None]))
269 self.assertTrue(fn(["Hello"]))
270 self.assertTrue(fn(["Hello", "World"]))
271 self.assertTrue(fn(["Hello", 0, 1, 2, "anything"]))
273 fn = ht.TItems([ht.TAny, ht.TInt, ht.TAny])
274 self.assertTrue(fn(["Hello", 0, []]))
275 self.assertTrue(fn(["Hello", 893782]))
276 self.assertTrue(fn([{}, -938210858947, None]))
277 self.assertFalse(fn(["Hello", []]))
279 def testInstanceOf(self):
280 fn = ht.TInstanceOf(self.__class__)
281 self.assertTrue(fn(self))
282 self.assertTrue(str(fn).startswith("Instance of "))
284 self.assertFalse(fn(None))
286 def testMaybeValueNone(self):
287 fn = ht.TMaybeValueNone(ht.TInt)
289 self.assertTrue(fn(None))
290 self.assertTrue(fn(0))
291 self.assertTrue(fn(constants.VALUE_NONE))
293 self.assertFalse(fn(""))
294 self.assertFalse(fn([]))
295 self.assertFalse(fn(constants.VALUE_DEFAULT))
298 if __name__ == "__main__":
299 testutils.GanetiTestProgram()