Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.ht_unittest.py @ a464ce71

History | View | Annotate | Download (7.3 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2011 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 testing ganeti.ht"""
23

    
24
import unittest
25

    
26
from ganeti import ht
27

    
28
import testutils
29

    
30

    
31
class TestTypeChecks(unittest.TestCase):
32
  def testNone(self):
33
    self.assertFalse(ht.TNotNone(None))
34
    self.assertTrue(ht.TNone(None))
35

    
36
    for val in [0, True, "", "Hello World", [], range(5)]:
37
      self.assertTrue(ht.TNotNone(val))
38
      self.assertFalse(ht.TNone(val))
39

    
40
  def testBool(self):
41
    self.assertTrue(ht.TBool(True))
42
    self.assertTrue(ht.TBool(False))
43

    
44
    for val in [0, None, "", [], "Hello"]:
45
      self.assertFalse(ht.TBool(val))
46

    
47
    for val in [True, -449, 1, 3, "x", "abc", [1, 2]]:
48
      self.assertTrue(ht.TTrue(val))
49

    
50
    for val in [False, 0, None, []]:
51
      self.assertFalse(ht.TTrue(val))
52

    
53
  def testInt(self):
54
    for val in [-100, -3, 0, 16, 128, 923874]:
55
      self.assertTrue(ht.TInt(val))
56

    
57
    for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]:
58
      self.assertFalse(ht.TInt(val))
59

    
60
    for val in range(0, 100, 4):
61
      self.assertTrue(ht.TPositiveInt(val))
62
      neg = -(val + 1)
63
      self.assertFalse(ht.TPositiveInt(neg))
64
      self.assertFalse(ht.TStrictPositiveInt(neg))
65

    
66
      self.assertFalse(ht.TPositiveInt(0.1 + val))
67
      self.assertFalse(ht.TStrictPositiveInt(0.1 + val))
68

    
69
    for val in [0, 0.1, 0.9, -0.3]:
70
      self.assertFalse(ht.TStrictPositiveInt(val))
71

    
72
    for val in range(1, 100, 4):
73
      self.assertTrue(ht.TStrictPositiveInt(val))
74
      self.assertFalse(ht.TStrictPositiveInt(0.1 + val))
75

    
76
  def testFloat(self):
77
    for val in [-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
78
      self.assertTrue(ht.TFloat(val))
79

    
80
    for val in [False, True, None, "", [], "Hello", 0, 28, -1, -3281]:
81
      self.assertFalse(ht.TFloat(val))
82

    
83
  def testString(self):
84
    for val in ["", "abc", "Hello World", "123",
85
                u"", u"\u272C", u"abc"]:
86
      self.assertTrue(ht.TString(val))
87

    
88
    for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
89
      self.assertFalse(ht.TString(val))
90

    
91
  def testElemOf(self):
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))
98

    
99
    fn = ht.TElemOf([])
100
    self.assertFalse(fn(0))
101
    self.assertFalse(fn(100))
102
    self.assertFalse(fn(True))
103

    
104
    fn = ht.TElemOf(["Hello", "World"])
105
    self.assertTrue(fn("Hello"))
106
    self.assertTrue(fn("World"))
107
    self.assertFalse(fn("e"))
108

    
109
  def testList(self):
110
    for val in [[], range(10), ["Hello", "World", "!"]]:
111
      self.assertTrue(ht.TList(val))
112

    
113
    for val in [False, True, None, {}, 0, 1, 5, -193, 93.8582]:
114
      self.assertFalse(ht.TList(val))
115

    
116
  def testDict(self):
117
    for val in [{}, dict.fromkeys(range(10)), {"Hello": [], "World": "!"}]:
118
      self.assertTrue(ht.TDict(val))
119

    
120
    for val in [False, True, None, [], 0, 1, 5, -193, 93.8582]:
121
      self.assertFalse(ht.TDict(val))
122

    
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)))
128

    
129
  def testAnd(self):
130
    fn = ht.TAnd(ht.TNotNone, ht.TString)
131
    self.assertTrue(fn(""))
132
    self.assertFalse(fn(1))
133
    self.assertFalse(fn(None))
134

    
135
  def testOr(self):
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"))
142

    
143
  def testMap(self):
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))
147

    
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([]))
154

    
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([]))
161

    
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"))
169

    
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]))
179

    
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}))
189

    
190
  def testStrictDictRequireAllExclusive(self):
191
    fn = ht.TStrictDict(True, True, { "a": ht.TInt, })
192
    self.assertFalse(fn(1))
193
    self.assertFalse(fn(None))
194
    self.assertFalse(fn({}))
195
    self.assertFalse(fn({"a": "Hello", }))
196
    self.assertFalse(fn({"unknown": 999,}))
197
    self.assertFalse(fn({"unknown": None,}))
198

    
199
    self.assertTrue(fn({"a": 123, }))
200
    self.assertTrue(fn({"a": -5, }))
201

    
202
    fn = ht.TStrictDict(True, True, { "a": ht.TInt, "x": ht.TString, })
203
    self.assertFalse(fn({}))
204
    self.assertFalse(fn({"a": -5, }))
205
    self.assertTrue(fn({"a": 123, "x": "", }))
206
    self.assertFalse(fn({"a": 123, "x": None, }))
207

    
208
  def testStrictDictExclusive(self):
209
    fn = ht.TStrictDict(False, True, { "a": ht.TInt, "b": ht.TList, })
210
    self.assertTrue(fn({}))
211
    self.assertTrue(fn({"a": 123, }))
212
    self.assertTrue(fn({"b": range(4), }))
213
    self.assertFalse(fn({"b": 123, }))
214

    
215
    self.assertFalse(fn({"foo": {}, }))
216
    self.assertFalse(fn({"bar": object(), }))
217

    
218
  def testStrictDictRequireAll(self):
219
    fn = ht.TStrictDict(True, False, { "a": ht.TInt, "m": ht.TInt, })
220
    self.assertTrue(fn({"a": 1, "m": 2, "bar": object(), }))
221
    self.assertFalse(fn({}))
222
    self.assertFalse(fn({"a": 1, "bar": object(), }))
223
    self.assertFalse(fn({"a": 1, "m": [], "bar": object(), }))
224

    
225
  def testStrictDict(self):
226
    fn = ht.TStrictDict(False, False, { "a": ht.TInt, })
227
    self.assertTrue(fn({}))
228
    self.assertFalse(fn({"a": ""}))
229
    self.assertTrue(fn({"a": 11}))
230
    self.assertTrue(fn({"other": 11}))
231
    self.assertTrue(fn({"other": object()}))
232

    
233

    
234
if __name__ == "__main__":
235
  testutils.GanetiTestProgram()