test_ordering.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.test import TestCase
  2. from utilities.ordering import naturalize, naturalize_interface
  3. class NaturalizationTestCase(TestCase):
  4. """
  5. Validate the operation of the functions which generate values suitable for natural ordering.
  6. """
  7. def test_naturalize(self):
  8. # Original, naturalized
  9. data = (
  10. ('abc', 'abc'),
  11. ('123', '00000123'),
  12. ('abc123', 'abc00000123'),
  13. ('123abc', '00000123abc'),
  14. ('123abc456', '00000123abc00000456'),
  15. ('abc123def', 'abc00000123def'),
  16. ('abc123def456', 'abc00000123def00000456'),
  17. )
  18. for origin, naturalized in data:
  19. self.assertEqual(naturalize(origin, max_length=100), naturalized)
  20. def test_naturalize_max_length(self):
  21. self.assertEqual(naturalize('abc123def456', max_length=10), 'abc0000012')
  22. def test_naturalize_interface(self):
  23. # Original, naturalized
  24. data = (
  25. # IOS/JunOS-style
  26. ('Gi', '9999999999999999Gi..................'),
  27. ('Gi1', '9999999999999999Gi000001............'),
  28. ('Gi1.0', '9999999999999999Gi000001......000000'),
  29. ('Gi1.1', '9999999999999999Gi000001......000001'),
  30. ('Gi1:0', '9999999999999999Gi000001000000......'),
  31. ('Gi1:0.0', '9999999999999999Gi000001000000000000'),
  32. ('Gi1:0.1', '9999999999999999Gi000001000000000001'),
  33. ('Gi1:1', '9999999999999999Gi000001000001......'),
  34. ('Gi1:1.0', '9999999999999999Gi000001000001000000'),
  35. ('Gi1:1.1', '9999999999999999Gi000001000001000001'),
  36. ('Gi1/2', '0001999999999999Gi000002............'),
  37. ('Gi1/2/3', '0001000299999999Gi000003............'),
  38. ('Gi1/2/3/4', '0001000200039999Gi000004............'),
  39. ('Gi1/2/3/4/5', '0001000200030004Gi000005............'),
  40. ('Gi1/2/3/4/5:6', '0001000200030004Gi000005000006......'),
  41. ('Gi1/2/3/4/5:6.7', '0001000200030004Gi000005000006000007'),
  42. # Generic
  43. ('Interface 1', '9999999999999999Interface 000001............'),
  44. ('Interface 1 (other)', '9999999999999999Interface 000001............ (other)'),
  45. ('Interface 99', '9999999999999999Interface 000099............'),
  46. ('PCIe1-p1', '9999999999999999PCIe000001............-p00000001'),
  47. ('PCIe1-p99', '9999999999999999PCIe000001............-p00000099'),
  48. )
  49. for origin, naturalized in data:
  50. self.assertEqual(naturalize_interface(origin, max_length=100), naturalized)
  51. def test_naturalize_interface_max_length(self):
  52. self.assertEqual(naturalize_interface('Gi1/2/3', max_length=20), '0001000299999999Gi00')