mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 12:12:53 -06:00
Adapt form tests to work without fixture data
This commit is contained in:
parent
eb4c2e5d7f
commit
47962ea732
@ -2,6 +2,7 @@ from django.test import TestCase
|
|||||||
|
|
||||||
from dcim.forms import *
|
from dcim.forms import *
|
||||||
from dcim.models import *
|
from dcim.models import *
|
||||||
|
from virtualization.models import Cluster, ClusterGroup, ClusterType
|
||||||
|
|
||||||
|
|
||||||
def get_id(model, slug):
|
def get_id(model, slug):
|
||||||
@ -10,83 +11,108 @@ def get_id(model, slug):
|
|||||||
|
|
||||||
class DeviceTestCase(TestCase):
|
class DeviceTestCase(TestCase):
|
||||||
|
|
||||||
fixtures = ['dcim', 'ipam', 'virtualization']
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
|
||||||
|
site = Site.objects.create(name='Site 1', slug='site-1')
|
||||||
|
rack = Rack.objects.create(name='Rack 1', site=site)
|
||||||
|
manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
|
||||||
|
device_type = DeviceType.objects.create(
|
||||||
|
manufacturer=manufacturer, model='Device Type 1', slug='device-type-1', u_height=1
|
||||||
|
)
|
||||||
|
device_role = DeviceRole.objects.create(
|
||||||
|
name='Device Role 1', slug='device-role-1', color='ff0000'
|
||||||
|
)
|
||||||
|
Platform.objects.create(name='Platform 1', slug='platform-1')
|
||||||
|
Device.objects.create(
|
||||||
|
name='Device 1', device_type=device_type, device_role=device_role, site=site, rack=rack, position=1
|
||||||
|
)
|
||||||
|
cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
|
||||||
|
cluster_group = ClusterGroup.objects.create(name='Cluster Group 1', slug='cluster-group-1')
|
||||||
|
Cluster.objects.create(name='Cluster 1', type=cluster_type, group=cluster_group)
|
||||||
|
|
||||||
def test_racked_device(self):
|
def test_racked_device(self):
|
||||||
test = DeviceForm(data={
|
form = DeviceForm(data={
|
||||||
'name': 'test',
|
'name': 'New Device',
|
||||||
'device_role': get_id(DeviceRole, 'leaf-switch'),
|
'device_role': DeviceRole.objects.first().pk,
|
||||||
'tenant': None,
|
'tenant': None,
|
||||||
'manufacturer': get_id(Manufacturer, 'juniper'),
|
'manufacturer': Manufacturer.objects.first().pk,
|
||||||
'device_type': get_id(DeviceType, 'qfx5100-48s'),
|
'device_type': DeviceType.objects.first().pk,
|
||||||
'site': get_id(Site, 'test1'),
|
'site': Site.objects.first().pk,
|
||||||
'rack': '1',
|
'rack': Rack.objects.first().pk,
|
||||||
'face': DeviceFaceChoices.FACE_FRONT,
|
'face': DeviceFaceChoices.FACE_FRONT,
|
||||||
'position': 41,
|
'position': 2,
|
||||||
'platform': get_id(Platform, 'juniper-junos'),
|
'platform': Platform.objects.first().pk,
|
||||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
||||||
})
|
})
|
||||||
self.assertTrue(test.is_valid(), test.fields['position'].choices)
|
self.assertTrue(form.is_valid())
|
||||||
self.assertTrue(test.save())
|
self.assertTrue(form.save())
|
||||||
|
|
||||||
def test_racked_device_occupied(self):
|
def test_racked_device_occupied(self):
|
||||||
test = DeviceForm(data={
|
form = DeviceForm(data={
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
'device_role': get_id(DeviceRole, 'leaf-switch'),
|
'device_role': DeviceRole.objects.first().pk,
|
||||||
'tenant': None,
|
'tenant': None,
|
||||||
'manufacturer': get_id(Manufacturer, 'juniper'),
|
'manufacturer': Manufacturer.objects.first().pk,
|
||||||
'device_type': get_id(DeviceType, 'qfx5100-48s'),
|
'device_type': DeviceType.objects.first().pk,
|
||||||
'site': get_id(Site, 'test1'),
|
'site': Site.objects.first().pk,
|
||||||
'rack': '1',
|
'rack': Rack.objects.first().pk,
|
||||||
'face': DeviceFaceChoices.FACE_FRONT,
|
'face': DeviceFaceChoices.FACE_FRONT,
|
||||||
'position': 1,
|
'position': 1,
|
||||||
'platform': get_id(Platform, 'juniper-junos'),
|
'platform': Platform.objects.first().pk,
|
||||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
||||||
})
|
})
|
||||||
self.assertFalse(test.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
|
self.assertIn('position', form.errors)
|
||||||
|
|
||||||
def test_non_racked_device(self):
|
def test_non_racked_device(self):
|
||||||
test = DeviceForm(data={
|
form = DeviceForm(data={
|
||||||
'name': 'test',
|
'name': 'New Device',
|
||||||
'device_role': get_id(DeviceRole, 'pdu'),
|
'device_role': DeviceRole.objects.first().pk,
|
||||||
'tenant': None,
|
'tenant': None,
|
||||||
'manufacturer': get_id(Manufacturer, 'servertech'),
|
'manufacturer': Manufacturer.objects.first().pk,
|
||||||
'device_type': get_id(DeviceType, 'cwg-24vym415c9'),
|
'device_type': DeviceType.objects.first().pk,
|
||||||
'site': get_id(Site, 'test1'),
|
'site': Site.objects.first().pk,
|
||||||
'rack': '1',
|
'rack': None,
|
||||||
'face': '',
|
'face': None,
|
||||||
'position': None,
|
'position': None,
|
||||||
'platform': None,
|
'platform': Platform.objects.first().pk,
|
||||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
||||||
})
|
})
|
||||||
self.assertTrue(test.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
self.assertTrue(test.save())
|
self.assertTrue(form.save())
|
||||||
|
|
||||||
def test_non_racked_device_with_face(self):
|
def test_non_racked_device_with_face_position(self):
|
||||||
test = DeviceForm(data={
|
form = DeviceForm(data={
|
||||||
'name': 'test',
|
'name': 'New Device',
|
||||||
'device_role': get_id(DeviceRole, 'pdu'),
|
'device_role': DeviceRole.objects.first().pk,
|
||||||
'tenant': None,
|
'tenant': None,
|
||||||
'manufacturer': get_id(Manufacturer, 'servertech'),
|
'manufacturer': Manufacturer.objects.first().pk,
|
||||||
'device_type': get_id(DeviceType, 'cwg-24vym415c9'),
|
'device_type': DeviceType.objects.first().pk,
|
||||||
'site': get_id(Site, 'test1'),
|
'site': Site.objects.first().pk,
|
||||||
'rack': '1',
|
'rack': None,
|
||||||
'face': DeviceFaceChoices.FACE_REAR,
|
'face': DeviceFaceChoices.FACE_REAR,
|
||||||
'position': None,
|
'position': 10,
|
||||||
'platform': None,
|
'platform': None,
|
||||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
||||||
})
|
})
|
||||||
self.assertTrue(test.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertTrue(test.save())
|
self.assertIn('face', form.errors)
|
||||||
|
self.assertIn('position', form.errors)
|
||||||
|
|
||||||
def test_cloned_cluster_device_initial_data(self):
|
def test_initial_data_population(self):
|
||||||
|
device_type = DeviceType.objects.first()
|
||||||
|
cluster = Cluster.objects.first()
|
||||||
test = DeviceForm(initial={
|
test = DeviceForm(initial={
|
||||||
'device_type': get_id(DeviceType, 'poweredge-r640'),
|
'device_type': device_type.pk,
|
||||||
'device_role': get_id(DeviceRole, 'server'),
|
'device_role': DeviceRole.objects.first().pk,
|
||||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
||||||
'site': get_id(Site, 'test1'),
|
'site': Site.objects.first().pk,
|
||||||
"cluster": Cluster.objects.get(id=4).id,
|
'cluster': cluster.pk,
|
||||||
})
|
})
|
||||||
self.assertEqual(test.initial['manufacturer'], get_id(Manufacturer, 'dell'))
|
|
||||||
self.assertIn('cluster_group', test.initial)
|
# Check that the initial value for the manufacturer is set automatically when assigning the device type
|
||||||
self.assertEqual(test.initial['cluster_group'], get_id(ClusterGroup, 'vm-host'))
|
self.assertEqual(test.initial['manufacturer'], device_type.manufacturer.pk)
|
||||||
|
|
||||||
|
# Check that the initial value for the cluster group is set automatically when assigning the cluster
|
||||||
|
self.assertEqual(test.initial['cluster_group'], cluster.group.pk)
|
||||||
|
Loading…
Reference in New Issue
Block a user