Skip to content

Commit

Permalink
Account for some keys not being in rows
Browse files Browse the repository at this point in the history
Fixes aws#198.
  • Loading branch information
jamesls committed Aug 2, 2013
1 parent d15015a commit fdb8ce7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
4 changes: 3 additions & 1 deletion awscli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ def _build_sub_table_from_list(self, current, indent_level, title):
indent_level=indent_level)
self.table.add_row_header(headers)
first = False
self.table.add_row([element[header] for header in headers])
# Use .get() to account for the fact that sometimes an element
# may not have all the keys from the header.
self.table.add_row([element.get(header, '') for header in headers])
for remaining in more:
# Some of the non scalar attributes may not necessarily
# be in every single element of the list, so we need to
Expand Down
51 changes: 45 additions & 6 deletions tests/unit/output/test_table_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,12 @@
"""


# First record has "Tags" scalar, second record does not.
INNER_LIST = {
"Snapshots": [
{
"Description": "TestVolume1",
"Tags": [
{
"Value": "TestVolume",
"Key": "Name"
}
],
"Tags": [{"Value": "TestVolume", "Key": "Name"}],
"VolumeId": "vol-12345",
"State": "completed",
"VolumeSize": 8,
Expand Down Expand Up @@ -221,6 +217,45 @@
|+------------------------------------------------------------+----------+-----------+-------------+---------------------------+------------+---------------+--------------+|
"""

LIST_WITH_MISSING_KEYS = {
"Snapshots": [
{
"Description": "TestVolume1",
"Tags": "foo",
"VolumeId": "vol-12345",
"State": "completed",
"VolumeSize": 8,
"Progress": "100%",
"StartTime": "2012-05-23T21:46:41.000Z",
"SnapshotId": "snap-1234567",
"OwnerId": "12345"
},
{
"Description": "description",
"VolumeId": "vol-e543b98b",
"State": "completed",
"VolumeSize": 8,
"Progress": "100%",
"StartTime": "2012-05-25T00:07:20.000Z",
"SnapshotId": "snap-23456",
"OwnerId": "12345"
}
]
}

LIST_WITH_MISSING_KEYS_TABLE = """\
-----------------------------------------------------------------------------------------------------------------------------------------
| OperationName |
+---------------------------------------------------------------------------------------------------------------------------------------+
|| Snapshots ||
|+-------------+----------+-----------+---------------+---------------------------+------------+-------+----------------+--------------+|
|| Description | OwnerId | Progress | SnapshotId | StartTime | State | Tags | VolumeId | VolumeSize ||
|+-------------+----------+-----------+---------------+---------------------------+------------+-------+----------------+--------------+|
|| TestVolume1| 12345 | 100% | snap-1234567 | 2012-05-23T21:46:41.000Z | completed | foo | vol-12345 | 8 ||
|| description| 12345 | 100% | snap-23456 | 2012-05-25T00:07:20.000Z | completed | | vol-e543b98b | 8 ||
|+-------------+----------+-----------+---------------+---------------------------+------------+-------+----------------+--------------+|
"""


class Object(object):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -262,3 +297,7 @@ def test_inner_table(self):
def test_empty_table(self):
self.assert_data_renders_to(data={},
table='')

def test_missing_keys(self):
self.assert_data_renders_to(data=LIST_WITH_MISSING_KEYS,
table=LIST_WITH_MISSING_KEYS_TABLE)

0 comments on commit fdb8ce7

Please sign in to comment.