| 1 |
# Copyright 2017 Huawei, Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import collections |
| 16 |
|
| 17 |
from osc_lib.cli import format_columns |
| 18 |
from osc_lib.test import base |
| 19 |
|
| 20 |
|
| 21 |
class TestDictColumn(base.TestCase): |
| 22 |
def test_dict_column(self): |
| 23 |
data = { |
| 24 |
'key1': 'value1', |
| 25 |
'key2': 'value2', |
| 26 |
} |
| 27 |
col = format_columns.DictColumn(data) |
| 28 |
self.assertEqual(data, col.machine_readable()) |
| 29 |
self.assertEqual("key1='value1', key2='value2'", col.human_readable()) |
| 30 |
|
| 31 |
def test_complex_object(self): |
| 32 |
"""Non-dict objects should be converted to a dict.""" |
| 33 |
data = collections.OrderedDict( |
| 34 |
[('key1', 'value1'), ('key2', 'value2')] |
| 35 |
) |
| 36 |
col = format_columns.DictColumn(data) |
| 37 |
# we explicitly check type rather than use isinstance since an |
| 38 |
# OrderedDict is a subclass of dict and would inadvertently pass |
| 39 |
self.assertEqual(type(col.machine_readable()), dict) |
| 40 |
|
| 41 |
|
| 42 |
class TestDictListColumn(base.TestCase): |
| 43 |
def test_dict_list_column(self): |
| 44 |
data = { |
| 45 |
'public': ['2001:db8::8', '172.24.4.6'], |
| 46 |
'private': ['2000:db7::7', '192.24.4.6'], |
| 47 |
} |
| 48 |
col = format_columns.DictListColumn(data) |
| 49 |
self.assertEqual(data, col.machine_readable()) |
| 50 |
self.assertEqual( |
| 51 |
'private=192.24.4.6, 2000:db7::7; public=172.24.4.6, 2001:db8::8', |
| 52 |
col.human_readable(), |
| 53 |
) |
| 54 |
|
| 55 |
def test_complex_object(self): |
| 56 |
"""Non-dict-of-list objects should be converted to a dict-of-lists.""" |
| 57 |
data = collections.OrderedDict( |
| 58 |
[('key1', ['value1']), ('key2', ['value2'])], |
| 59 |
) |
| 60 |
col = format_columns.DictListColumn(data) |
| 61 |
# we explicitly check type rather than use isinstance since an |
| 62 |
# OrderedDict is a subclass of dict and would inadvertently pass |
| 63 |
self.assertEqual(type(col.machine_readable()), dict) |
| 64 |
|
| 65 |
|
| 66 |
class TestListColumn(base.TestCase): |
| 67 |
def test_list_column(self): |
| 68 |
data = [ |
| 69 |
'key1', |
| 70 |
'key2', |
| 71 |
] |
| 72 |
col = format_columns.ListColumn(data) |
| 73 |
self.assertEqual(data, col.machine_readable()) |
| 74 |
self.assertEqual("key1, key2", col.human_readable()) |
| 75 |
|
| 76 |
def test_complex_object(self): |
| 77 |
"""Non-list objects should be converted to a list.""" |
| 78 |
data = {'key1', 'key2'} |
| 79 |
col = format_columns.ListColumn(data) |
| 80 |
# we explicitly check type rather than use isinstance since an |
| 81 |
# OrderedDict is a subclass of dict and would inadvertently pass |
| 82 |
self.assertEqual(type(col.machine_readable()), list) |
| 83 |
|
| 84 |
|
| 85 |
class TestListDictColumn(base.TestCase): |
| 86 |
def test_list_dict_column(self): |
| 87 |
data = [ |
| 88 |
{'key1': 'value1'}, |
| 89 |
{'key2': 'value2'}, |
| 90 |
] |
| 91 |
col = format_columns.ListDictColumn(data) |
| 92 |
self.assertEqual(data, col.machine_readable()) |
| 93 |
self.assertEqual("key1='value1'\nkey2='value2'", col.human_readable()) |
| 94 |
|
| 95 |
def test_complex_object(self): |
| 96 |
"""Non-list-of-dict objects should be converted to a list-of-dicts.""" |
| 97 |
# not actually a list (which is the point) |
| 98 |
data = ( |
| 99 |
collections.OrderedDict([('key1', 'value1'), ('key2', 'value2')]), |
| 100 |
) |
| 101 |
col = format_columns.ListDictColumn(data) |
| 102 |
# we explicitly check type rather than use isinstance since an |
| 103 |
# OrderedDict is a subclass of dict and would inadvertently pass |
| 104 |
self.assertEqual(type(col.machine_readable()), list) # noqa: H212 |
| 105 |
for x in col.machine_readable(): |
| 106 |
self.assertEqual(type(x), dict) # noqa: H211 |
| 107 |
|
| 108 |
|
| 109 |
class TestSizeColumn(base.TestCase): |
| 110 |
def test_size_column(self): |
| 111 |
content = 1576395005 |
| 112 |
col = format_columns.SizeColumn(content) |
| 113 |
self.assertEqual(content, col.machine_readable()) |
| 114 |
self.assertEqual('1.6G', col.human_readable()) |