Merge "Fix network segment range "_get_ranges" function"

This commit is contained in:
Zuul 2020-03-23 21:41:45 +00:00 committed by Gerrit Code Review
commit 9e8960bad0
2 changed files with 15 additions and 2 deletions

View File

@ -23,7 +23,6 @@ from osc_lib.cli import format_columns
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
import six
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
@ -42,7 +41,7 @@ def _get_columns(item):
def _get_ranges(item):
item = [int(i) if isinstance(i, six.string_types) else i for i in item]
item = sorted([int(i) for i in item])
for a, b in itertools.groupby(enumerate(item), lambda xy: xy[1] - xy[0]):
b = list(b)
yield "%s-%s" % (b[0][1], b[-1][1]) if b[0][1] != b[-1][1] else \

View File

@ -24,6 +24,20 @@ from openstackclient.tests.unit.network.v2 import fakes as network_fakes
from openstackclient.tests.unit import utils as tests_utils
class TestAuxiliaryFunctions(tests_utils.TestCase):
def test__get_ranges(self):
input_reference = [
([1, 2, 3, 4, 5, 6, 7], ['1-7']),
([1, 2, 5, 4, 3, 6, 7], ['1-7']),
([1, 2, 4, 3, 7, 6], ['1-4', '6-7']),
([1, 2, 4, 3, '13', 12, '7', '6'], ['1-4', '6-7', '12-13'])
]
for input, reference in input_reference:
self.assertEqual(reference,
list(network_segment_range._get_ranges(input)))
class TestNetworkSegmentRange(network_fakes.TestNetworkV2):
def setUp(self):