/usr/share/pyshared/melange/tests/unit/test_utils.py is in python-melange 1:2012.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from melange import tests
from melange.common import utils
class TestUtils(tests.BaseTest):
def test_remove_nones(self):
hash = utils.remove_nones(dict(a=1, b=None, c=3))
self.assertEqual(hash, dict(a=1, c=3))
class ParseIntTest(tests.BaseTest):
def test_converts_invalid_int_to_none(self):
self.assertEqual(utils.parse_int("a2z"), None)
def test_converts_none_to_none(self):
self.assertEqual(utils.parse_int(None), None)
def test_converts_valid_integer_string_to_int(self):
self.assertEqual(utils.parse_int("123"), 123)
class TestExclude(tests.BaseTest):
def test_excludes_given_keys(self):
dictionary = {'key1': "value1", 'key2': "value2", 'key3': "value3"}
self.assertEqual(utils.exclude(dictionary, 'key2', 'key3'),
{'key1': "value1"})
def test_excludes_ignore_non_exsistant_keys(self):
dictionary = {'key1': "value1", 'key2': "value2", 'key3': "value3"}
self.assertEqual(utils.exclude(dictionary, 'key2', 'nonexistant'),
{'key1': "value1", 'key3': "value3"})
def test_returns_none_if_dict_is_none(self):
self.assertIsNone(utils.exclude(None, 'key1'))
class TestFilterDict(tests.BaseTest):
def test_filters_given_keys(self):
dictionary = {'key1': "value1", 'key2': "value2", 'key3': "value3"}
self.assertEqual(utils.filter_dict(dictionary, 'key2', 'key3'),
{'key2': "value2", 'key3': "value3"})
def test_filter_ignore_non_exsistant_keys(self):
dictionary = {'key1': "value1", 'key2': "value2", 'key3': "value3"}
self.assertEqual(utils.filter_dict(dictionary, 'key2', 'nonexistant'),
{'key2': "value2"})
def test_returns_none_if_dict_is_none(self):
self.assertIsNone(utils.filter_dict(None, 'key1'))
class TestStringifyKeys(tests.BaseTest):
def test_converts_keys_to_string(self):
dictionary = {u'key1': "value1", 'key2': u"value2"}
converted_dictionary = utils.stringify_keys(dictionary)
for key in converted_dictionary:
self.assertEqual(type(key), str)
def test_returns_none_if_dict_is_none(self):
self.assertIsNone(utils.stringify_keys(None))
class Foo(object):
method_execution_count = 0
@utils.cached_property
def bar(self):
self.method_execution_count += 1
return 42
class TestCachedProperty(tests.BaseTest):
def test_retrives_the_value_returned_by_method(self):
foo = Foo()
self.assertEqual(foo.bar, 42)
def test_retrives_the_same_value_all_the_time(self):
foo = Foo()
for i in range(1, 5):
self.assertEqual(foo.bar, 42)
def test_value_is_cached_after_first_method_call(self):
foo = Foo()
for i in range(1, 5):
foo.bar
self.assertEqual(foo.method_execution_count, 1)
def test_returns_instance_of_cached_proprty_when_called_on_class(self):
self.assertTrue(isinstance(Foo.bar, utils.cached_property))
class TestFind(tests.BaseTest):
def test_find_returns_first_item_matching_predicate(self):
items = [1, 2, 3, 4]
item = utils.find((lambda item: item == 2), items)
self.assertEqual(item, 2)
def test_find_returns_none_when_no_matching_item_found(self):
items = [1, 2, 3, 4]
item = utils.find((lambda item: item == 8), items)
self.assertEqual(item, None)
class TestMethodInspector(tests.BaseTest):
def test_method_without_optional_args(self):
def foo(bar):
pass
method = utils.MethodInspector(foo)
self.assertEqual(method.required_args, ['bar'])
self.assertEqual(method.optional_args, [])
def test_method_with_optional_args(self):
def foo(bar, baz=1):
pass
method = utils.MethodInspector(foo)
self.assertEqual(method.required_args, ['bar'])
self.assertEqual(method.optional_args, [('baz', 1)])
def test_instance_method_with_optional_args(self):
class Foo():
def bar(self, baz, qux=2):
pass
method = utils.MethodInspector(Foo().bar)
self.assertEqual(method.required_args, ['baz'])
self.assertEqual(method.optional_args, [('qux', 2)])
def test_method_without_args(self):
def foo():
pass
method = utils.MethodInspector(foo)
self.assertEqual(method.required_args, [])
self.assertEqual(method.optional_args, [])
def test_instance_method_without_args(self):
class Foo():
def bar(self):
pass
method = utils.MethodInspector(Foo().bar)
self.assertEqual(method.required_args, [])
self.assertEqual(method.optional_args, [])
def test_method_str(self):
class Foo():
def bar(self, baz, qux=None):
pass
method = utils.MethodInspector(Foo().bar)
self.assertEqual(str(method), "bar baz=<baz> [qux=<qux>]")
|