4 # Copyright (C) 2011 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"""
31 class TestTypeChecks(unittest.TestCase):
33 self.assertFalse(ht.TNotNone(None))
34 self.assertTrue(ht.TNone(None))
36 for val in [0, True, "", "Hello World", [], range(5)]:
37 self.assertTrue(ht.TNotNone(val))
38 self.assertFalse(ht.TNone(val))
41 self.assertTrue(ht.TBool(True))
42 self.assertTrue(ht.TBool(False))
44 for val in [0, None, "", [], "Hello"]:
45 self.assertFalse(ht.TBool(val))
47 for val in [True, -449, 1, 3, "x", "abc", [1, 2]]:
48 self.assertTrue(ht.TTrue(val))
50 for val in [False, 0, None, []]:
51 self.assertFalse(ht.TTrue(val))
54 for val in [-100, -3, 0, 16, 128, 923874]:
55 self.assertTrue(ht.TInt(val))
57 for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]:
58 self.assertFalse(ht.TInt(val))
60 for val in range(0, 100, 4):
61 self.assertTrue(ht.TPositiveInt(val))
63 self.assertFalse(ht.TPositiveInt(neg))
64 self.assertFalse(ht.TStrictPositiveInt(neg))
66 self.assertFalse(ht.TPositiveInt(0.1 + val))
67 self.assertFalse(ht.TStrictPositiveInt(0.1 + val))
69 for val in [0, 0.1, 0.9, -0.3]:
70 self.assertFalse(ht.TStrictPositiveInt(val))
72 for val in range(1, 100, 4):
73 self.assertTrue(ht.TStrictPositiveInt(val))
74 self.assertFalse(ht.TStrictPositiveInt(0.1 + val))
77 for val in [-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
78 self.assertTrue(ht.TFloat(val))
80 for val in [False, True, None, "", [], "Hello", 0, 28, -1, -3281]:
81 self.assertFalse(ht.TFloat(val))
84 for val in ["", "abc", "Hello World", "123",
85 u"", u"\u272C", u"abc"]:
86 self.assertTrue(ht.TString(val))
88 for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
89 self.assertFalse(ht.TString(val))
92 fn = ht.TElemOf(range(10))
93 self.assertTrue(fn(0))
94 self.assertTrue(fn(3))
95 self.assertTrue(fn(9))
96 self.assertFalse(fn(-1))
97 self.assertFalse(fn(100))
100 self.assertFalse(fn(0))
101 self.assertFalse(fn(100))
102 self.assertFalse(fn(True))
104 fn = ht.TElemOf(["Hello", "World"])
105 self.assertTrue(fn("Hello"))
106 self.assertTrue(fn("World"))
107 self.assertFalse(fn("e"))
110 for val in [[], range(10), ["Hello", "World", "!"]]:
111 self.assertTrue(ht.TList(val))
113 for val in [False, True, None, {}, 0, 1, 5, -193, 93.8582]:
114 self.assertFalse(ht.TList(val))
117 for val in [{}, dict.fromkeys(range(10)), {"Hello": [], "World": "!"}]:
118 self.assertTrue(ht.TDict(val))
120 for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
121 self.assertFalse(ht.TDict(val))
123 def testIsLength(self):
124 fn = ht.TIsLength(10)
125 self.assertTrue(fn(range(10)))
126 self.assertFalse(fn(range(1)))
127 self.assertFalse(fn(range(100)))
130 fn = ht.TAnd(ht.TNotNone, ht.TString)
131 self.assertTrue(fn(""))
132 self.assertFalse(fn(1))
133 self.assertFalse(fn(None))
136 fn = ht.TOr(ht.TNone, ht.TAnd(ht.TString, ht.TIsLength(5)))
137 self.assertTrue(fn("12345"))
138 self.assertTrue(fn(None))
139 self.assertFalse(fn(1))
140 self.assertFalse(fn(""))
141 self.assertFalse(fn("abc"))
144 self.assertTrue(ht.TMap(str, ht.TString)(123))
145 self.assertTrue(ht.TMap(int, ht.TInt)("9999"))
146 self.assertFalse(ht.TMap(lambda x: x + 100, ht.TString)(123))
148 def testNonEmptyString(self):
149 self.assertTrue(ht.TNonEmptyString("xyz"))
150 self.assertTrue(ht.TNonEmptyString("Hello World"))
151 self.assertFalse(ht.TNonEmptyString(""))
152 self.assertFalse(ht.TNonEmptyString(None))
153 self.assertFalse(ht.TNonEmptyString([]))
155 def testMaybeString(self):
156 self.assertTrue(ht.TMaybeString("xyz"))
157 self.assertTrue(ht.TMaybeString("Hello World"))
158 self.assertTrue(ht.TMaybeString(None))
159 self.assertFalse(ht.TMaybeString(""))
160 self.assertFalse(ht.TMaybeString([]))
162 def testMaybeBool(self):
163 self.assertTrue(ht.TMaybeBool(False))
164 self.assertTrue(ht.TMaybeBool(True))
165 self.assertTrue(ht.TMaybeBool(None))
166 self.assertFalse(ht.TMaybeBool([]))
167 self.assertFalse(ht.TMaybeBool("0"))
168 self.assertFalse(ht.TMaybeBool("False"))
170 def testListOf(self):
171 fn = ht.TListOf(ht.TNonEmptyString)
172 self.assertTrue(fn([]))
173 self.assertTrue(fn(["x"]))
174 self.assertTrue(fn(["Hello", "World"]))
175 self.assertFalse(fn(None))
176 self.assertFalse(fn(False))
177 self.assertFalse(fn(range(3)))
178 self.assertFalse(fn(["x", None]))
180 def testDictOf(self):
181 fn = ht.TDictOf(ht.TNonEmptyString, ht.TInt)
182 self.assertTrue(fn({}))
183 self.assertTrue(fn({"x": 123, "y": 999}))
184 self.assertFalse(fn(None))
185 self.assertFalse(fn({1: "x"}))
186 self.assertFalse(fn({"x": ""}))
187 self.assertFalse(fn({"x": None}))
188 self.assertFalse(fn({"": 8234}))
191 if __name__ == "__main__":
192 testutils.GanetiTestProgram()