Merge "Add tests for tuskar_ui.infrastructure.utils.utils"

This commit is contained in:
Jenkins 2015-01-23 14:03:05 +00:00 committed by Gerrit Code Review
commit 2760124674
3 changed files with 64 additions and 36 deletions

View File

@ -1,34 +0,0 @@
# -*- coding: utf8 -*-
#
# 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 tuskar_ui.test import helpers as test
from tuskar_ui.utils import utils
class TestItem(object):
def __init__(self, index):
self.index = index
class UtilsTests(test.TestCase):
def test_filter_items(self):
items = [TestItem(i) for i in range(7)]
first = utils.filter_items(items, index=0)
even = utils.filter_items(items, index__in=(0, 2, 4, 6))
last_two = utils.filter_items(items, index__not_in=range(5))
self.assertEqual(utils.length(first), 1)
self.assertEqual(utils.length(even), 4)
self.assertEqual(utils.length(last_two), 2)

62
tuskar_ui/utils/tests.py Normal file
View File

@ -0,0 +1,62 @@
# -*- coding: utf8 -*-
#
# 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.
import collections
from tuskar_ui.test import helpers
from tuskar_ui.utils import utils
class UtilsTests(helpers.TestCase):
def test_de_camel_case(self):
ret = utils.de_camel_case('CamelCaseString')
self.assertEqual(ret, 'Camel Case String')
ret = utils.de_camel_case('SecureSSLConnection')
self.assertEqual(ret, 'Secure SSL Connection')
ret = utils.de_camel_case('xxXXxx')
self.assertEqual(ret, 'xx X Xxx')
ret = utils.de_camel_case('XXX')
self.assertEqual(ret, 'XXX')
ret = utils.de_camel_case('NON Camel Case')
self.assertEqual(ret, 'NON Camel Case')
def test_list_to_dict(self):
Item = collections.namedtuple('Item', 'id')
ret = utils.list_to_dict([Item('foo'), Item('bar'), Item('bar')])
self.assertEqual(ret, {'foo': Item('foo'), 'bar': Item('bar')})
def test_length(self):
ret = utils.length(iter([]))
self.assertEqual(ret, 0)
ret = utils.length(iter([1, 2, 3]))
self.assertEqual(ret, 3)
def test_check_image_type(self):
Image = collections.namedtuple('Image', 'properties')
ret = utils.check_image_type(Image({'type': 'Picasso'}), 'Picasso')
self.assertTrue(ret)
ret = utils.check_image_type(Image({'type': 'Picasso'}), 'Van Gogh')
self.assertFalse(ret)
ret = utils.check_image_type(Image({}), 'Van Gogh')
self.assertTrue(ret)
def test_filter_items(self):
Item = collections.namedtuple('Item', 'index')
items = [Item(i) for i in range(7)]
ret = list(utils.filter_items(items, index=1))
self.assertEqual(ret, [Item(1)])
ret = list(utils.filter_items(items, index__in=(1, 2, 3)))
self.assertEqual(ret, [Item(1), Item(2), Item(3)])
ret = list(utils.filter_items(items, index__not_in=(1, 2, 3)))
self.assertEqual(ret, [Item(0), Item(4), Item(5), Item(6)])

View File

@ -13,12 +13,12 @@
# under the License. # under the License.
import re import re
CAMEL_RE = re.compile(r'([a-z]|SSL)([A-Z])') CAMEL_RE = re.compile(r'([A-Z][a-z]+|[A-Z]+(?=[A-Z\s]|$))')
def de_camel_case(text): def de_camel_case(text):
"""Convert CamelCase names to human-readable format.""" """Convert CamelCase names to human-readable format."""
return CAMEL_RE.sub(lambda m: m.group(1) + ' ' + m.group(2), text) return ' '.join(w.strip() for w in CAMEL_RE.split(text) if w.strip())
def list_to_dict(object_list, key_attribute='id'): def list_to_dict(object_list, key_attribute='id'):