Skip to content

Commit fe46c6d

Browse files
committed
Merge pull request softlayer#543 from sudorandom/issue-539
Fixes sorting issue with formatted items
2 parents ba7e956 + 80c1098 commit fe46c6d

6 files changed

Lines changed: 65 additions & 6 deletions

File tree

SoftLayer/CLI/formatting.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def format_prettytable(table):
7373
for i, row in enumerate(table.rows):
7474
for j, item in enumerate(row):
7575
table.rows[i][j] = format_output(item)
76+
7677
ptable = table.prettytable()
7778
ptable.hrules = prettytable.FRAME
7879
ptable.horizontal_char = '.'
@@ -83,12 +84,15 @@ def format_prettytable(table):
8384

8485
def format_no_tty(table):
8586
"""Converts SoftLayer.CLI.formatting.Table instance to a prettytable."""
87+
8688
for i, row in enumerate(table.rows):
8789
for j, item in enumerate(row):
8890
table.rows[i][j] = format_output(item, fmt='raw')
8991
ptable = table.prettytable()
92+
9093
for col in table.columns:
9194
ptable.align[col] = 'l'
95+
9296
ptable.hrules = prettytable.NONE
9397
ptable.border = False
9498
ptable.header = False
@@ -304,6 +308,30 @@ def __str__(self):
304308

305309
__repr__ = __str__
306310

311+
# Implement sorting methods.
312+
# NOTE(kmcdonald): functools.total_ordering could be used once support for
313+
# Python 2.6 is dropped
314+
def __eq__(self, other):
315+
return self.original == getattr(other, 'original', other)
316+
317+
def __lt__(self, other):
318+
if self.original is None:
319+
return True
320+
321+
other_val = getattr(other, 'original', other)
322+
if other_val is None:
323+
return False
324+
return self.original < other_val
325+
326+
def __gt__(self, other):
327+
return not (self < other or self == other)
328+
329+
def __le__(self, other):
330+
return self < other or self == other
331+
332+
def __ge__(self, other):
333+
return not self < other
334+
307335

308336
def _format_python_value(value):
309337
"""If the value has to_python() defined then return that."""

SoftLayer/CLI/server/list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@click.command()
1515
@click.option('--sortby',
1616
help='Column to sort by',
17-
type=click.Choice(['guid',
17+
type=click.Choice(['id',
1818
'hostname',
1919
'primary_ip',
2020
'backend_ip',

SoftLayer/testing/fixtures/SoftLayer_Account.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
'friendlyName': 'Friendly Transaction Name',
137137
'id': 6660
138138
}
139-
}
139+
},
140140
}, {
141141
'id': 1001,
142142
'globalIdentifier': '1a2b3c-1702',
@@ -224,6 +224,8 @@
224224
'id': 19082
225225
},
226226
]
227+
}, {
228+
'id': 1003,
227229
}]
228230
getDomains = [{'name': 'example.com',
229231
'id': 12345,

SoftLayer/tests/CLI/helper_tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ def test_blank(self):
131131
self.assertEqual('-', item.formatted)
132132
self.assertEqual('NULL', str(item))
133133

134+
def test_sort_mixed(self):
135+
blank = formatting.blank()
136+
items = [10, blank]
137+
sorted_items = sorted(items)
138+
self.assertEqual(sorted_items, [blank, 10])
139+
140+
items = [blank, 10]
141+
sorted_items = sorted(items)
142+
self.assertEqual(sorted_items, [blank, 10])
143+
144+
items = [blank, "10"]
145+
sorted_items = sorted(items)
146+
self.assertEqual(sorted_items, [blank, "10"])
147+
148+
def test_sort(self):
149+
items = [10, formatting.FormattedItem(20), formatting.FormattedItem(5)]
150+
sorted_items = sorted(items)
151+
self.assertEqual(sorted_items, [formatting.FormattedItem(5),
152+
10,
153+
formatting.FormattedItem(20)])
154+
134155

135156
class FormattedListTests(testing.TestCase):
136157
def test_init(self):

SoftLayer/tests/CLI/modules/server_tests.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,19 @@ def test_list_servers(self):
8484
'id': 1002,
8585
'backend_ip': '10.1.0.4',
8686
'action': None,
87-
}
87+
},
88+
{
89+
'action': None,
90+
'backend_ip': None,
91+
'datacenter': None,
92+
'hostname': None,
93+
'id': 1003,
94+
'primary_ip': None,
95+
},
8896
]
8997

9098
self.assertEqual(result.exit_code, 0)
91-
self.assertEqual(json.loads(result.output), expected)
99+
self.assertEqual(expected, json.loads(result.output))
92100

93101
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
94102
@mock.patch('SoftLayer.HardwareManager.reload')

SoftLayer/tests/managers/hardware_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_list_hardware_with_filters(self):
7878

7979
def test_resolve_ids_ip(self):
8080
_id = self.hardware._get_ids_from_ip('172.16.1.100')
81-
self.assertEqual(_id, [1000, 1001, 1002])
81+
self.assertEqual(_id, [1000, 1001, 1002, 1003])
8282

8383
_id = self.hardware._get_ids_from_ip('nope')
8484
self.assertEqual(_id, [])
@@ -93,7 +93,7 @@ def test_resolve_ids_ip(self):
9393

9494
def test_resolve_ids_hostname(self):
9595
_id = self.hardware._get_ids_from_hostname('hardware-test1')
96-
self.assertEqual(_id, [1000, 1001, 1002])
96+
self.assertEqual(_id, [1000, 1001, 1002, 1003])
9797

9898
def test_get_hardware(self):
9999
result = self.hardware.get_hardware(1000)

0 commit comments

Comments
 (0)