Debian Codegraph @

osc_lib/tests/utils/test_columns.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from osc_lib.test import base
14 from osc_lib.utils import columns as column_utils
15
16
17 class TestColumnUtils(base.TestCase):
18 def test_get_column_definitions(self):
19 attr_map = (
20 ('id', 'ID', column_utils.LIST_BOTH),
21 ('tenant_id', 'Project', column_utils.LIST_LONG_ONLY),
22 ('name', 'Name', column_utils.LIST_BOTH),
23 ('summary', 'Summary', column_utils.LIST_SHORT_ONLY),
24 )
25 headers, columns = column_utils.get_column_definitions(
26 attr_map, long_listing=False
27 )
28 self.assertEqual(['id', 'name', 'summary'], columns)
29 self.assertEqual(['ID', 'Name', 'Summary'], headers)
30
31 def test_get_column_definitions_long(self):
32 attr_map = (
33 ('id', 'ID', column_utils.LIST_BOTH),
34 ('tenant_id', 'Project', column_utils.LIST_LONG_ONLY),
35 ('name', 'Name', column_utils.LIST_BOTH),
36 ('summary', 'Summary', column_utils.LIST_SHORT_ONLY),
37 )
38 headers, columns = column_utils.get_column_definitions(
39 attr_map, long_listing=True
40 )
41 self.assertEqual(['id', 'tenant_id', 'name'], columns)
42 self.assertEqual(['ID', 'Project', 'Name'], headers)
43
44 def test_get_columns(self):
45 item = {
46 'id': 'test-id',
47 'tenant_id': 'test-tenant_id',
48 # 'name' is not included
49 'foo': 'bar', # unknown attribute
50 }
51 attr_map = (
52 ('id', 'ID', column_utils.LIST_BOTH),
53 ('tenant_id', 'Project', column_utils.LIST_LONG_ONLY),
54 ('name', 'Name', column_utils.LIST_BOTH),
55 )
56 columns, display_names = column_utils.get_columns(item, attr_map)
57 self.assertEqual(tuple(['id', 'tenant_id', 'foo']), columns)
58 self.assertEqual(tuple(['ID', 'Project', 'foo']), display_names)