From 14f3eda2b1831b6ac1aa90f47a19dd27a998c2cc Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Fri, 4 Nov 2022 08:20:50 -0500 Subject: [PATCH] Add tests Add missing search() --- netbox/dcim/filtersets.py | 14 ++++ netbox/dcim/tests/test_api.py | 2 + netbox/dcim/tests/test_filtersets.py | 99 ++++++++++++++++++++++++++++ netbox/dcim/tests/test_models.py | 23 +++++++ netbox/dcim/tests/test_views.py | 2 + 5 files changed, 140 insertions(+) diff --git a/netbox/dcim/filtersets.py b/netbox/dcim/filtersets.py index 410bdb161..88d84a7ab 100644 --- a/netbox/dcim/filtersets.py +++ b/netbox/dcim/filtersets.py @@ -1033,6 +1033,20 @@ class VirtualDeviceContextFilterSet(NetBoxModelFilterSet, TenancyFilterSet): model = VirtualDeviceContext fields = ['id', 'device', 'name', ] + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(name__icontains=value) | + Q(identifier=value.strip()) + ).distinct() + + def _has_primary_ip(self, queryset, name, value): + params = Q(primary_ip4__isnull=False) | Q(primary_ip6__isnull=False) + if value: + return queryset.filter(params) + return queryset.exclude(params) + class ModuleFilterSet(NetBoxModelFilterSet): manufacturer_id = django_filters.ModelMultipleChoiceFilter( diff --git a/netbox/dcim/tests/test_api.py b/netbox/dcim/tests/test_api.py index 2697c29b2..82b4889d6 100644 --- a/netbox/dcim/tests/test_api.py +++ b/netbox/dcim/tests/test_api.py @@ -2163,3 +2163,5 @@ class PowerFeedTest(APIViewTestCases.APIViewTestCase): 'type': REDUNDANT, }, ] + +# TODO: VDC Test Cases diff --git a/netbox/dcim/tests/test_filtersets.py b/netbox/dcim/tests/test_filtersets.py index 92298bd73..1537fe538 100644 --- a/netbox/dcim/tests/test_filtersets.py +++ b/netbox/dcim/tests/test_filtersets.py @@ -4253,5 +4253,104 @@ class PowerFeedTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'connected': False} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) +class VirtualDeviceContextBaseTestCase(TestCase, ChangeLoggedFilterSetTests): + queryset = VirtualDeviceContext.objects.all() + filterset = InterfaceFilterSet + + @classmethod + def setUpTestData(cls): + + sites = ( + Site(name='Site 1', slug='site-1'), + Site(name='Site 2', slug='site-2'), + Site(name='Site 3', slug='site-3'), + ) + Site.objects.bulk_create(sites) + + locations = ( + Location(name='Location 1', site=sites[0], slug='location-1'), + Location(name='Location 2', site=sites[1], slug='location-1'), + Location(name='Location 3', site=sites[2], slug='location-1'), + ) + for location in locations: + location.save() + + racks = ( + Rack(name='Rack 1', site=sites[0], location=locations[0]), + Rack(name='Rack 2', site=sites[1], location=locations[1]), + Rack(name='Rack 3', site=sites[2], location=locations[2]), + ) + Rack.objects.bulk_create(racks) + + tenants = ( + Tenant(name='Tenant 1', slug='tenant-1'), + Tenant(name='Tenant 2', slug='tenant-2'), + Tenant(name='Tenant 3', slug='tenant-3'), + ) + Tenant.objects.bulk_create(tenants) + + manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1') + device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Model 1', slug='model-1') + device_role = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1') + + devices = ( + Device(name='Device 1', device_type=device_type, device_role=device_role, site=sites[0], rack=racks[0], location=locations[0], position=1), + Device(name='Device 2', device_type=device_type, device_role=device_role, site=sites[1], rack=racks[1], location=locations[1], position=1), + Device(name='Device 3', device_type=device_type, device_role=device_role, site=sites[2], rack=racks[2], location=locations[2], position=1), + ) + Device.objects.bulk_create(devices) + + vdcs = ( + VirtualDeviceContext(device=devices[0], name='VDC 1', identifier=1, status=VirtualDeviceContextStatusChoices.STATUS_ACTIVE), + VirtualDeviceContext(device=devices[0], name='VDC 2', identifier=2, status=VirtualDeviceContextStatusChoices.STATUS_PLANNED), + VirtualDeviceContext(device=devices[1], name='VDC 1', status=VirtualDeviceContextStatusChoices.STATUS_OFFLINE), + VirtualDeviceContext(device=devices[1], name='VDC 2', status=VirtualDeviceContextStatusChoices.STATUS_PLANNED), + VirtualDeviceContext(device=devices[2], name='VDC 1', status=VirtualDeviceContextStatusChoices.STATUS_ACTIVE), + VirtualDeviceContext(device=devices[2], name='VDC 2', status=VirtualDeviceContextStatusChoices.STATUS_ACTIVE), + ) + VirtualDeviceContext.objects.bulk_create(vdcs) + + interfaces = ( + Interface(device=devices[0], vdc=vdcs[0], name='Interface 1', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], vdc=vdcs[1], name='Interface 2', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], vdc=vdcs[2], name='Interface 3', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], vdc=vdcs[3], name='Interface 4', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], vdc=vdcs[4], name='Interface 5', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], vdc=vdcs[5], name='Interface 6', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], vdc=vdcs[0], name='Interface 7', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[0], vdc=vdcs[1], name='Interface 8', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], vdc=vdcs[2], name='Interface 9', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[1], vdc=vdcs[3], name='Interface 10', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], vdc=vdcs[4], name='Interface 11', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + Interface(device=devices[2], vdc=vdcs[5], name='Interface 12', type=InterfaceTypeChoices.TYPE_1GE_FIXED), + ) + Interface.objects.bulk_create(interfaces) + + +class VirtualDeviceContextTestCase(VirtualDeviceContextBaseTestCase): + + def test_device(self): + params = {'device': ['Device 1', 'Device 2']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + + def test_status(self): + params = {'status': ['active']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3) + + +class VirtualDeviceContextInterfaceTestCase(VirtualDeviceContextBaseTestCase): + def test_vdc(self): + devices = Device.objects.first() + vdc = VirtualDeviceContext.objects.filter(device=devices) + params = {'vdc': ['VDC 1']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6) + params = {'vdc_id': vdc.values_list('pk')} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + + def test_vdc_identifier(self): + devices = Device.objects.first() + vdc = VirtualDeviceContext.objects.filter(device=devices) + params = {'vdc_identifier': vdc.values_list('identifier')} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) # TODO: Connection filters diff --git a/netbox/dcim/tests/test_models.py b/netbox/dcim/tests/test_models.py index 460a5e252..646738977 100644 --- a/netbox/dcim/tests/test_models.py +++ b/netbox/dcim/tests/test_models.py @@ -588,3 +588,26 @@ class CableTestCase(TestCase): cable = Cable(a_terminations=[self.interface2], b_terminations=[wireless_interface]) with self.assertRaises(ValidationError): cable.clean() + +class CableTestCase(TestCase): + + def setUp(self): + + site = Site.objects.create(name='Test Site 1', slug='test-site-1') + manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1') + devicetype = DeviceType.objects.create( + manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1' + ) + devicerole = DeviceRole.objects.create( + name='Test Device Role 1', slug='test-device-role-1', color='ff0000' + ) + self.device = Device.objects.create( + device_type=devicetype, device_role=devicerole, name='TestDevice1', site=site + ) + + self.vdc1 = VirtualDeviceContext.objects.create(device=self.device, name="VDC 1", identifier=1) + self.vdc2 = VirtualDeviceContext.objects.create(device=self.device, name="VDC 2", identifier=1) + + self.interface1 = Interface.objects.create(device=self.device1, name='Eth1/1', type='10gbase-t') + self.interface2 = Interface.objects.create(device=self.device2, name='Eth1/2', vdc=self.vdc1, type='10gbase-t') + self.interface3 = Interface.objects.create(device=self.device2, name='Eth1/3', vdc=self.vdc2, type='10gbase-t') diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index 8bf1c1948..184b95a5a 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -3073,3 +3073,5 @@ class PowerFeedTestCase(ViewTestCases.PrimaryObjectViewTestCase): response = self.client.get(reverse('dcim:powerfeed_trace', kwargs={'pk': powerfeed.pk})) self.assertHttpStatus(response, 200) + +# TODO: VDC Test Cases