utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from django.test import TestCase
  2. from circuits.models import *
  3. from dcim.models import *
  4. from dcim.utils import object_to_path_node
  5. __all__ = (
  6. 'BaseCablePathTestCase',
  7. )
  8. class BaseCablePathTestCase(TestCase):
  9. """
  10. Base class for test cases for cable paths.
  11. """
  12. @classmethod
  13. def setUpTestData(cls):
  14. manufacturer = Manufacturer.objects.create(name='Generic', slug='generic')
  15. device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Test Device')
  16. role = DeviceRole.objects.create(name='Device Role', slug='device-role')
  17. provider = Provider.objects.create(name='Provider', slug='provider')
  18. circuit_type = CircuitType.objects.create(name='Circuit Type', slug='circuit-type')
  19. # Create reusable test objects
  20. cls.site = Site.objects.create(name='Site', slug='site')
  21. cls.device = Device.objects.create(site=cls.site, device_type=device_type, role=role, name='Test Device')
  22. cls.powerpanel = PowerPanel.objects.create(site=cls.site, name='Power Panel')
  23. cls.circuit = Circuit.objects.create(provider=provider, type=circuit_type, cid='Circuit 1')
  24. def _get_cablepath(self, nodes, **kwargs):
  25. """
  26. Return a given cable path
  27. :param nodes: Iterable of steps, with each step being either a single node or a list of nodes
  28. :return: The matching CablePath (if any)
  29. """
  30. path = []
  31. for step in nodes:
  32. if type(step) in (list, tuple):
  33. path.append([object_to_path_node(node) for node in step])
  34. else:
  35. path.append([object_to_path_node(step)])
  36. return CablePath.objects.filter(path=path, **kwargs).first()
  37. def assertPathExists(self, nodes, **kwargs):
  38. """
  39. Assert that a CablePath from origin to destination with a specific intermediate path exists. Returns the
  40. first matching CablePath, if found.
  41. :param nodes: Iterable of steps, with each step being either a single node or a list of nodes
  42. """
  43. cablepath = self._get_cablepath(nodes, **kwargs)
  44. self.assertIsNotNone(cablepath, msg='CablePath not found')
  45. return cablepath
  46. def assertPathDoesNotExist(self, nodes, **kwargs):
  47. """
  48. Assert that a specific CablePath does *not* exist.
  49. :param nodes: Iterable of steps, with each step being either a single node or a list of nodes
  50. """
  51. cablepath = self._get_cablepath(nodes, **kwargs)
  52. self.assertIsNone(cablepath, msg='Unexpected CablePath found')
  53. def assertCurrentPathExists(self, nodes, **kwargs):
  54. """
  55. Assert that the first node references a CablePath with the given route via _path, and return it.
  56. :param nodes: Iterable of steps, the first being the originating path endpoint object
  57. """
  58. origin = type(nodes[0]).objects.get(pk=nodes[0].pk)
  59. self.assertIsNotNone(origin._path_id, msg=f'No path set on originating endpoint {origin}')
  60. # Matched on the route alone, so a flag mismatch does not report itself as a wrong route
  61. cablepath = self._get_cablepath(nodes, pk=origin._path_id)
  62. self.assertIsNotNone(cablepath, msg=f'Path #{origin._path_id} on {origin} does not match the expected route')
  63. for attr, expected in kwargs.items():
  64. self.assertEqual(getattr(cablepath, attr), expected, msg=f'Path #{cablepath.pk} on {origin}: {attr}')
  65. return cablepath
  66. def assertPathIsSet(self, origin, cablepath, msg=None):
  67. """
  68. Assert that a specific CablePath instance is set as the path on the origin.
  69. :param origin: The originating path endpoint
  70. :param cablepath: The CablePath instance originating from this endpoint
  71. :param msg: Custom failure message (optional)
  72. """
  73. if msg is None:
  74. msg = f"Path #{cablepath.pk} not set on originating endpoint {origin}"
  75. self.assertEqual(origin._path_id, cablepath.pk, msg=msg)
  76. def assertPathIsNotSet(self, origin, msg=None):
  77. """
  78. Assert that a specific CablePath instance is set as the path on the origin.
  79. :param origin: The originating path endpoint
  80. :param msg: Custom failure message (optional)
  81. """
  82. if msg is None:
  83. msg = f"Path #{origin._path_id} set as origin on {origin}; should be None!"
  84. self.assertIsNone(origin._path_id, msg=msg)