Merge "Add test methods to compare formattable column values"
This commit is contained in:
commit
7e7cecbde3
@ -16,8 +16,10 @@
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from cliff import columns as cliff_columns
|
||||
import mock
|
||||
|
||||
from osc_lib.cli import format_columns
|
||||
from osc_lib import exceptions
|
||||
from osc_lib.tests import fakes
|
||||
from osc_lib.tests import utils as test_utils
|
||||
@ -628,3 +630,59 @@ class TestFindResource(test_utils.TestCase):
|
||||
actual_unsorted = utils.format_list(['c', 'b', 'a'], separator='\n')
|
||||
self.assertEqual(expected, actual_pre_sorted)
|
||||
self.assertEqual(expected, actual_unsorted)
|
||||
|
||||
|
||||
class TestAssertItemEqual(test_utils.TestCommand):
|
||||
|
||||
def test_assert_normal_item(self):
|
||||
expected = ['a', 'b', 'c']
|
||||
actual = ['a', 'b', 'c']
|
||||
self.assertItemEqual(expected, actual)
|
||||
|
||||
def test_assert_item_with_formattable_columns(self):
|
||||
expected = [format_columns.DictColumn({'a': 1, 'b': 2}),
|
||||
format_columns.ListColumn(['x', 'y', 'z'])]
|
||||
actual = [format_columns.DictColumn({'a': 1, 'b': 2}),
|
||||
format_columns.ListColumn(['x', 'y', 'z'])]
|
||||
self.assertItemEqual(expected, actual)
|
||||
|
||||
def test_assert_item_different_length(self):
|
||||
expected = ['a', 'b', 'c']
|
||||
actual = ['a', 'b']
|
||||
self.assertRaises(AssertionError,
|
||||
self.assertItemEqual, expected, actual)
|
||||
|
||||
def test_assert_item_formattable_columns_vs_legacy_formatter(self):
|
||||
expected = [format_columns.DictColumn({'a': 1, 'b': 2}),
|
||||
format_columns.ListColumn(['x', 'y', 'z'])]
|
||||
actual = [utils.format_dict({'a': 1, 'b': 2}),
|
||||
utils.format_list(['x', 'y', 'z'])]
|
||||
self.assertRaises(AssertionError,
|
||||
self.assertItemEqual, expected, actual)
|
||||
|
||||
def test_assert_item_different_formattable_columns(self):
|
||||
|
||||
class ExceptionColumn(cliff_columns.FormattableColumn):
|
||||
def human_readable(self):
|
||||
raise Exception('always fail')
|
||||
|
||||
expected = [format_columns.DictColumn({'a': 1, 'b': 2})]
|
||||
actual = [ExceptionColumn({'a': 1, 'b': 2})]
|
||||
# AssertionError is a subclass of Exception
|
||||
# so raising AssertionError ensures ExceptionColumn.human_readable()
|
||||
# is not called.
|
||||
self.assertRaises(AssertionError,
|
||||
self.assertItemEqual, expected, actual)
|
||||
|
||||
def test_assert_list_item(self):
|
||||
expected = [
|
||||
['a', 'b', 'c'],
|
||||
[format_columns.DictColumn({'a': 1, 'b': 2}),
|
||||
format_columns.ListColumn(['x', 'y', 'z'])]
|
||||
]
|
||||
actual = [
|
||||
['a', 'b', 'c'],
|
||||
[format_columns.DictColumn({'a': 1, 'b': 2}),
|
||||
format_columns.ListColumn(['x', 'y', 'z'])]
|
||||
]
|
||||
self.assertListItemEqual(expected, actual)
|
||||
|
@ -19,6 +19,7 @@ import json as jsonutils
|
||||
import mock
|
||||
import os
|
||||
|
||||
from cliff import columns as cliff_columns
|
||||
import fixtures
|
||||
from keystoneauth1 import loading
|
||||
from os_client_config import cloud_config
|
||||
@ -130,6 +131,34 @@ class TestCommand(TestCase):
|
||||
self.assertEqual(value, getattr(parsed_args, attr))
|
||||
return parsed_args
|
||||
|
||||
def assertItemEqual(self, expected, actual):
|
||||
"""Compare item considering formattable columns.
|
||||
|
||||
This method compares an observed item to an expected item column by
|
||||
column. If a column is a formattable column, observed and expected
|
||||
columns are compared using human_readable() and machine_readable().
|
||||
"""
|
||||
self.assertEqual(len(expected), len(actual))
|
||||
for col_expected, col_actual in zip(expected, actual):
|
||||
if isinstance(col_expected, cliff_columns.FormattableColumn):
|
||||
self.assertIsInstance(col_actual, col_expected.__class__)
|
||||
self.assertEqual(col_expected.human_readable(),
|
||||
col_actual.human_readable())
|
||||
self.assertEqual(col_expected.machine_readable(),
|
||||
col_actual.machine_readable())
|
||||
else:
|
||||
self.assertEqual(col_expected, col_actual)
|
||||
|
||||
def assertListItemEqual(self, expected, actual):
|
||||
"""Compare a list of items considering formattable columns.
|
||||
|
||||
Each pair of observed and expected items are compared
|
||||
using assertItemEqual() method.
|
||||
"""
|
||||
self.assertEqual(len(expected), len(actual))
|
||||
for item_expected, item_actual in zip(expected, actual):
|
||||
self.assertItemEqual(item_expected, item_actual)
|
||||
|
||||
|
||||
class TestClientManager(TestCase):
|
||||
"""ClientManager class test framework"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user