diff options
| author | zhiyuan_cai <luckyvega.g@gmail.com> | 2015-01-21 17:19:46 +0800 |
|---|---|---|
| committer | zhiyuan_cai <luckyvega.g@gmail.com> | 2015-02-06 10:57:10 +0800 |
| commit | 42cff388349186b70559650237d2667da1cb903f (patch) | |
| tree | 055ea0522cbb7293dc5d941477aaaa6d9fae86fe /openstackclient/tests/common | |
| parent | 0cc3955f0aedab76313aa09edd3f31bb9d08b55d (diff) | |
| download | python-openstackclient-42cff388349186b70559650237d2667da1cb903f.tar.gz | |
Add sort support to image list
Add sort support to image list by sorting items in the client side.
The parameter syntax follows this spec[1].
[1] https://review.openstack.org/#/c/145544/
Change-Id: I42b487d18f00f937db1938daa46487cea2a896ab
Closes-Bug: #1410251
Diffstat (limited to 'openstackclient/tests/common')
| -rw-r--r-- | openstackclient/tests/common/test_utils.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/openstackclient/tests/common/test_utils.py b/openstackclient/tests/common/test_utils.py index 583ab99c..cda0b135 100644 --- a/openstackclient/tests/common/test_utils.py +++ b/openstackclient/tests/common/test_utils.py @@ -58,6 +58,68 @@ class TestUtils(test_utils.TestCase): utils.get_password, mock_stdin) + def get_test_items(self): + item1 = {'a': 1, 'b': 2} + item2 = {'a': 1, 'b': 3} + item3 = {'a': 2, 'b': 2} + item4 = {'a': 2, 'b': 1} + return [item1, item2, item3, item4] + + def test_sort_items_with_one_key(self): + items = self.get_test_items() + sort_str = 'b' + expect_items = [items[3], items[0], items[2], items[1]] + self.assertEqual(expect_items, utils.sort_items(items, sort_str)) + + def test_sort_items_with_multiple_keys(self): + items = self.get_test_items() + sort_str = 'a,b' + expect_items = [items[0], items[1], items[3], items[2]] + self.assertEqual(expect_items, utils.sort_items(items, sort_str)) + + def test_sort_items_all_with_direction(self): + items = self.get_test_items() + sort_str = 'a:desc,b:desc' + expect_items = [items[2], items[3], items[1], items[0]] + self.assertEqual(expect_items, utils.sort_items(items, sort_str)) + + def test_sort_items_some_with_direction(self): + items = self.get_test_items() + sort_str = 'a,b:desc' + expect_items = [items[1], items[0], items[2], items[3]] + self.assertEqual(expect_items, utils.sort_items(items, sort_str)) + + def test_sort_items_with_object(self): + item1 = mock.Mock(a=1, b=2) + item2 = mock.Mock(a=1, b=3) + item3 = mock.Mock(a=2, b=2) + item4 = mock.Mock(a=2, b=1) + items = [item1, item2, item3, item4] + sort_str = 'b,a' + expect_items = [item4, item1, item3, item2] + self.assertEqual(expect_items, utils.sort_items(items, sort_str)) + + def test_sort_items_with_empty_key(self): + items = self.get_test_items() + sort_srt = '' + self.assertEqual(items, utils.sort_items(items, sort_srt)) + sort_srt = None + self.assertEqual(items, utils.sort_items(items, sort_srt)) + + def test_sort_items_with_invalid_key(self): + items = self.get_test_items() + sort_str = 'c' + self.assertRaises(exceptions.CommandError, + utils.sort_items, + items, sort_str) + + def test_sort_items_with_invalid_direction(self): + items = self.get_test_items() + sort_str = 'a:bad_dir' + self.assertRaises(exceptions.CommandError, + utils.sort_items, + items, sort_str) + class NoUniqueMatch(Exception): pass |
