From f5e8e532832ff1d3bf8ce4e66ca077fcc0d0f6ef Mon Sep 17 00:00:00 2001 From: Alexander Haase Date: Mon, 16 Sep 2024 17:35:25 +0200 Subject: [PATCH] Add test cases for device site/location --- netbox/dcim/tests/test_models.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/netbox/dcim/tests/test_models.py b/netbox/dcim/tests/test_models.py index 1c3dbb90b..3f1fded65 100644 --- a/netbox/dcim/tests/test_models.py +++ b/netbox/dcim/tests/test_models.py @@ -589,6 +589,45 @@ class DeviceTestCase(TestCase): device2.full_clean() device2.save() + def test_device_mismatched_site_location(self): + sites = ( + Site(name='Site 1', slug='site-1'), + Site(name='Site 2', slug='site-2'), + ) + Site.objects.bulk_create(sites) + + locations = ( + Location(name='Location 1', slug='location-1', site=sites[0]), + Location(name='Location 2', slug='location-2', site=sites[1]), + ) + for location in locations: + location.save() + + device_type = DeviceType.objects.first() + device_role = DeviceRole.objects.first() + + # Same site, should pass + Device(name='device1', site=sites[0], location=locations[0], device_type=device_type, role=device_role).full_clean() + + # Mismatched site, should fail + with self.assertRaises(ValidationError): + Device(name='device2', site=sites[0], location=locations[1], device_type=device_type, role=device_role).full_clean() + + def test_device_rack_clone_fields(self): + site = Site.objects.first() + location = Location(name='Location 1', slug='location-1', site=site) + location.save() + + rack = Rack.objects.create(name='Rack 1', site=site, location=location) + + device_type = DeviceType.objects.first() + device_role = DeviceRole.objects.first() + + # Device should use site and location from rack + device = Device.objects.create(name='device1', rack=rack, device_type=device_type, role=device_role) + self.assertEqual(device.site, site) + self.assertEqual(device.location, location) + def test_device_mismatched_site_cluster(self): cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') Cluster.objects.create(name='Cluster 1', type=cluster_type)