From fdb8ce71acda93d1931c8572305833c06c25b1ec Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Fri, 2 Aug 2013 14:54:56 -0700 Subject: [PATCH] Account for some keys not being in rows Fixes #198. --- awscli/formatter.py | 4 +- tests/unit/output/test_table_formatter.py | 51 ++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/awscli/formatter.py b/awscli/formatter.py index af44b227375a..35029679b994 100644 --- a/awscli/formatter.py +++ b/awscli/formatter.py @@ -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 diff --git a/tests/unit/output/test_table_formatter.py b/tests/unit/output/test_table_formatter.py index ba9f8c67682e..5a788b60f67d 100644 --- a/tests/unit/output/test_table_formatter.py +++ b/tests/unit/output/test_table_formatter.py @@ -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, @@ -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): @@ -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)