Skip to content

Commit f42a06e

Browse files
author
Tatyana Leontovich
committed
Improve unit tests for python-quantumclient
Partially fixes bug 1137783. Add tests for: quantumclient.common.utils Partially Fixes: bug #1137783 Change-Id: I6491eb881d3f465d11a649d55cde7caba77ed19b
1 parent 3c193c9 commit f42a06e

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

tests/unit/test_utils.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Copyright (C) 2013 Yahoo! Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
16+
17+
import datetime
18+
import sys
19+
20+
import testtools
21+
22+
from quantumclient.common import exceptions
23+
from quantumclient.common import utils
24+
25+
26+
class TestUtils(testtools.TestCase):
27+
def test_string_to_bool_true(self):
28+
self.assertTrue(utils.str2bool('true'))
29+
30+
def test_string_to_bool_false(self):
31+
self.assertFalse(utils.str2bool('false'))
32+
33+
def test_string_to_bool_None(self):
34+
self.assertIsNone(utils.str2bool(None))
35+
36+
def test_string_to_dictionary(self):
37+
input_str = 'key1=value1,key2=value2'
38+
expected = {'key1': 'value1', 'key2': 'value2'}
39+
self.assertEqual(expected, utils.str2dict(input_str))
40+
41+
def test_get_dict_item_properties(self):
42+
item = {'name': 'test_name', 'id': 'test_id'}
43+
fields = ('name', 'id')
44+
actual = utils.get_item_properties(item=item, fields=fields)
45+
self.assertEqual(('test_name', 'test_id'), actual)
46+
47+
def test_get_object_item_properties_mixed_case_fields(self):
48+
class Fake(object):
49+
def __init__(self):
50+
self.id = 'test_id'
51+
self.name = 'test_name'
52+
self.test_user = 'test'
53+
54+
fields = ('name', 'id', 'test user')
55+
mixed_fields = ('test user', 'ID')
56+
item = Fake()
57+
actual = utils.get_item_properties(item, fields, mixed_fields)
58+
self.assertEqual(('test_name', 'test_id', 'test'), actual)
59+
60+
def test_get_object_item_desired_fields_differ_from_item(self):
61+
class Fake(object):
62+
def __init__(self):
63+
self.id = 'test_id_1'
64+
self.name = 'test_name'
65+
self.test_user = 'test'
66+
67+
fields = ('name', 'id', 'test user')
68+
item = Fake()
69+
actual = utils.get_item_properties(item, fields)
70+
self.assertNotEqual(('test_name', 'test_id', 'test'), actual)
71+
72+
def test_get_object_item_desired_fields_is_empty(self):
73+
class Fake(object):
74+
def __init__(self):
75+
self.id = 'test_id_1'
76+
self.name = 'test_name'
77+
self.test_user = 'test'
78+
79+
fields = []
80+
item = Fake()
81+
actual = utils.get_item_properties(item, fields)
82+
self.assertEqual((), actual)
83+
84+
def test_get_object_item_with_formatters(self):
85+
class Fake(object):
86+
def __init__(self):
87+
self.id = 'test_id'
88+
self.name = 'test_name'
89+
self.test_user = 'test'
90+
91+
class FakeCallable(object):
92+
def __call__(self, *args, **kwargs):
93+
return 'pass'
94+
95+
fields = ('name', 'id', 'test user', 'is_public')
96+
formatters = {'is_public': FakeCallable()}
97+
item = Fake()
98+
act = utils.get_item_properties(item, fields, formatters=formatters)
99+
self.assertEqual(('test_name', 'test_id', 'test', 'pass'), act)
100+
101+
102+
class JSONUtilsTestCase(testtools.TestCase):
103+
def test_dumps(self):
104+
self.assertEqual(utils.dumps({'a': 'b'}), '{"a": "b"}')
105+
106+
def test_dumps_dict_with_date_value(self):
107+
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
108+
res = utils.dumps({1: 'a', 2: x})
109+
expected = '{"1": "a", "2": "1920-02-03 04:05:06.000007"}'
110+
self.assertEqual(expected, res)
111+
112+
def test_dumps_dict_with_spaces(self):
113+
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
114+
res = utils.dumps({1: 'a ', 2: x})
115+
expected = '{"1": "a ", "2": "1920-02-03 04:05:06.000007"}'
116+
self.assertEqual(expected, res)
117+
118+
def test_loads(self):
119+
self.assertEqual(utils.loads('{"a": "b"}'), {'a': 'b'})
120+
121+
122+
class ToPrimitiveTestCase(testtools.TestCase):
123+
def test_list(self):
124+
self.assertEqual(utils.to_primitive([1, 2, 3]), [1, 2, 3])
125+
126+
def test_empty_list(self):
127+
self.assertEqual(utils.to_primitive([]), [])
128+
129+
def test_tuple(self):
130+
self.assertEqual(utils.to_primitive((1, 2, 3)), [1, 2, 3])
131+
132+
def test_empty_tuple(self):
133+
self.assertEqual(utils.to_primitive(()), [])
134+
135+
def test_dict(self):
136+
self.assertEqual(
137+
utils.to_primitive(dict(a=1, b=2, c=3)),
138+
dict(a=1, b=2, c=3))
139+
140+
def test_empty_dict(self):
141+
self.assertEqual(utils.to_primitive({}), {})
142+
143+
def test_datetime(self):
144+
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
145+
self.assertEqual(
146+
utils.to_primitive(x),
147+
'1920-02-03 04:05:06.000007')
148+
149+
def test_iter(self):
150+
x = xrange(1, 6)
151+
self.assertEqual(utils.to_primitive(x), [1, 2, 3, 4, 5])
152+
153+
def test_iteritems(self):
154+
d = {'a': 1, 'b': 2, 'c': 3}
155+
156+
class IterItemsClass(object):
157+
def iteritems(self):
158+
return d.iteritems()
159+
160+
x = IterItemsClass()
161+
p = utils.to_primitive(x)
162+
self.assertEqual(p, {'a': 1, 'b': 2, 'c': 3})
163+
164+
def test_nasties(self):
165+
def foo():
166+
pass
167+
x = [datetime, foo, dir]
168+
ret = utils.to_primitive(x)
169+
self.assertEqual(len(ret), 3)
170+
171+
def test_to_primitive_dict_with_date_value(self):
172+
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
173+
res = utils.to_primitive({'a': x})
174+
self.assertEqual({'a': '1920-02-03 04:05:06.000007'}, res)
175+
176+
177+
class ImportClassTestCase(testtools.TestCase):
178+
def test_import_class(self):
179+
dt = utils.import_class('datetime.datetime')
180+
self.assertTrue(sys.modules['datetime'].datetime is dt)
181+
182+
def test_import_bad_class(self):
183+
self.assertRaises(
184+
ImportError, utils.import_class,
185+
'lol.u_mad.brah')
186+
187+
def test_get_client_class_invalid_version(self):
188+
self.assertRaises(
189+
exceptions.UnsupportedVersion,
190+
utils.get_client_class, 'image', '2', {'image': '2'})

0 commit comments

Comments
 (0)