|
|
@@ -1017,6 +1017,57 @@ class DeviceTestCase(TestCase):
|
|
|
)
|
|
|
self.assertEqual(inventoryitem.cf['cf1'], 'foo')
|
|
|
|
|
|
+ def test_device_creation_nested_inventory_items(self):
|
|
|
+ """
|
|
|
+ Inventory items are instantiated from nested templates beneath the correct parents, including when names
|
|
|
+ repeat under different parents.
|
|
|
+ """
|
|
|
+ device_type = DeviceType.objects.create(
|
|
|
+ manufacturer=Manufacturer.objects.first(),
|
|
|
+ model='Chassis Type',
|
|
|
+ slug='chassis-type'
|
|
|
+ )
|
|
|
+ # The default template ordering lists child templates ahead of their parents
|
|
|
+ for chassis_name in ('Chassis A', 'Chassis B'):
|
|
|
+ chassis = InventoryItemTemplate.objects.create(device_type=device_type, name=chassis_name)
|
|
|
+ psu = InventoryItemTemplate.objects.create(device_type=device_type, name='PSU', parent=chassis)
|
|
|
+ InventoryItemTemplate.objects.create(device_type=device_type, name='Fan', parent=psu)
|
|
|
+
|
|
|
+ device = Device.objects.create(
|
|
|
+ site=Site.objects.first(),
|
|
|
+ device_type=device_type,
|
|
|
+ role=DeviceRole.objects.first(),
|
|
|
+ name='Test Device 1'
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(InventoryItem.objects.filter(device=device).count(), 6)
|
|
|
+ for chassis_name in ('Chassis A', 'Chassis B'):
|
|
|
+ chassis = InventoryItem.objects.get(device=device, name=chassis_name, parent__isnull=True)
|
|
|
+ psu = InventoryItem.objects.get(device=device, name='PSU', parent=chassis)
|
|
|
+ fan = InventoryItem.objects.get(device=device, name='Fan', parent=psu)
|
|
|
+ self.assertEqual(fan.cf['cf1'], 'foo')
|
|
|
+
|
|
|
+ def test_device_creation_duplicate_root_inventory_items(self):
|
|
|
+ """
|
|
|
+ Root inventory item templates with the same name abort device creation.
|
|
|
+ """
|
|
|
+ device_type = DeviceType.objects.create(
|
|
|
+ manufacturer=Manufacturer.objects.first(),
|
|
|
+ model='Chassis Type',
|
|
|
+ slug='chassis-type'
|
|
|
+ )
|
|
|
+ # The unique constraint treats NULL parents as distinct, so these can both be saved
|
|
|
+ InventoryItemTemplate.objects.create(device_type=device_type, name='PSU')
|
|
|
+ InventoryItemTemplate.objects.create(device_type=device_type, name='PSU')
|
|
|
+
|
|
|
+ with self.assertRaises(AbortRequest):
|
|
|
+ Device.objects.create(
|
|
|
+ site=Site.objects.first(),
|
|
|
+ device_type=device_type,
|
|
|
+ role=DeviceRole.objects.first(),
|
|
|
+ name='Test Device 1'
|
|
|
+ )
|
|
|
+
|
|
|
def test_multiple_unnamed_devices(self):
|
|
|
|
|
|
device1 = Device(
|