From f87ed6dd9143526980e4b7514a07ba3091ad4c51 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Thu, 22 Jan 2015 16:22:29 +0100 Subject: [PATCH] Add tests for templatetags Change-Id: Ie6839b24e94d1ddb90527672635325c52502b277 --- .../templatetags/icon_helpers.py | 6 +- .../infrastructure/templatetags/tests.py | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 tuskar_ui/infrastructure/templatetags/tests.py diff --git a/tuskar_ui/infrastructure/templatetags/icon_helpers.py b/tuskar_ui/infrastructure/templatetags/icon_helpers.py index c4e9fb516..b3a88476a 100644 --- a/tuskar_ui/infrastructure/templatetags/icon_helpers.py +++ b/tuskar_ui/infrastructure/templatetags/icon_helpers.py @@ -51,8 +51,8 @@ IRONIC_NODE_STATE_ICON_DICT = { @register.filter(is_safe=True) def iconized_ironic_node_state(node_power_state): state = IRONIC_NODE_STATE_STRING_DICT.get(node_power_state, "—") - icon = IRONIC_NODE_STATE_ICON_DICT.get(node_power_state, None) - html_string = """ - %s """ % (icon, state) + icon = IRONIC_NODE_STATE_ICON_DICT.get(node_power_state, 'fa-question') + html_string = (u"""""" + u"""%s """) % (icon, unicode(state)) return html_string diff --git a/tuskar_ui/infrastructure/templatetags/tests.py b/tuskar_ui/infrastructure/templatetags/tests.py new file mode 100644 index 000000000..3d338c47d --- /dev/null +++ b/tuskar_ui/infrastructure/templatetags/tests.py @@ -0,0 +1,73 @@ +# -*- 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 +import json + +from tuskar_ui.infrastructure.templatetags import chart_helpers +from tuskar_ui.infrastructure.templatetags import icon_helpers +from tuskar_ui.test import helpers + + +Flavor = collections.namedtuple('Flavor', 'name used_instances') +Flavors = collections.namedtuple('Flavors', 'list_flavors') + + +class ChartHelpersTest(helpers.TestCase): + def test_remaining_capacity_by_flavors(self): + flavors = Flavors([ + Flavor('a', 0), + Flavor('b', 1), + ]) + ret = chart_helpers.remaining_capacity_by_flavors(flavors) + self.assertEqual( + ret, + '

Capacity remaining by flavors:

' + '

0 a

' + '

1 b

' + ) + + def test_all_used_instances(self): + flavors = Flavors([ + Flavor('a', 0), + Flavor('b', 1), + ]) + ret = chart_helpers.all_used_instances(flavors) + self.assertEqual(ret, json.dumps([ + { + 'popup_used': '

0% total, ' + ' 0 instances of a

', + 'used_instances': '0', + }, { + 'popup_used': '

1% total, ' + ' 1 instances of b

', + 'used_instances': '1', + }, + ])) + + +class IconHelpersTest(helpers.TestCase): + def test_iconized_ironic_node_state(self): + ret = icon_helpers.iconized_ironic_node_state('active') + self.assertEqual( + ret, + u'' + 'powered on ', + ) + ret = icon_helpers.iconized_ironic_node_state('') + self.assertEqual( + ret, + u'' + ' ', + )