mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 00:36:11 -06:00
Simplify tests
This commit is contained in:
parent
e7481f3bcc
commit
113de69af7
@ -1,5 +1,7 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from dcim.models import *
|
||||
from utilities.testing.utils import create_test_device
|
||||
|
||||
|
||||
class CountersTest(TestCase):
|
||||
@ -9,53 +11,42 @@ class CountersTest(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
|
||||
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'
|
||||
)
|
||||
device1 = Device.objects.create(
|
||||
device_type=devicetype, device_role=devicerole, name='TestDevice1', site=site
|
||||
)
|
||||
device2 = Device.objects.create(
|
||||
device_type=devicetype, device_role=devicerole, name='TestDevice2', site=site
|
||||
)
|
||||
# Create devices
|
||||
device1 = create_test_device('Device 1')
|
||||
device2 = create_test_device('Device 2')
|
||||
|
||||
# Create interfaces
|
||||
Interface.objects.create(device=device1, name='Interface 1')
|
||||
Interface.objects.create(device=device1, name='Interface 2')
|
||||
Interface.objects.create(device=device2, name='Interface 3')
|
||||
Interface.objects.create(device=device2, name='Interface 4')
|
||||
|
||||
def test_interface_count_addition(self):
|
||||
"""
|
||||
When a tracked object (Interface) is added the tracking counter should be updated.
|
||||
"""
|
||||
device1 = Device.objects.get(name='TestDevice1')
|
||||
device2 = Device.objects.get(name='TestDevice2')
|
||||
self.assertEqual(device1._interface_count, 0)
|
||||
self.assertEqual(device2._interface_count, 0)
|
||||
device1, device2 = Device.objects.all()
|
||||
self.assertEqual(device1._interface_count, 2)
|
||||
self.assertEqual(device2._interface_count, 2)
|
||||
|
||||
interface1 = Interface.objects.create(device=device1, name='eth0')
|
||||
interface2 = Interface.objects.create(device=device2, name='eth0')
|
||||
interface3 = Interface.objects.create(device=device2, name='eth1')
|
||||
Interface.objects.create(device=device1, name='Interface 5')
|
||||
Interface.objects.create(device=device2, name='Interface 6')
|
||||
device1.refresh_from_db()
|
||||
device2.refresh_from_db()
|
||||
self.assertEqual(device1._interface_count, 1)
|
||||
self.assertEqual(device2._interface_count, 2)
|
||||
self.assertEqual(device1._interface_count, 3)
|
||||
self.assertEqual(device2._interface_count, 3)
|
||||
|
||||
def test_interface_count_deletion(self):
|
||||
"""
|
||||
When a tracked object (Interface) is deleted the tracking counter should be updated.
|
||||
"""
|
||||
device1 = Device.objects.get(name='TestDevice1')
|
||||
device2 = Device.objects.get(name='TestDevice2')
|
||||
self.assertEqual(device1._interface_count, 0)
|
||||
self.assertEqual(device2._interface_count, 0)
|
||||
device1, device2 = Device.objects.all()
|
||||
self.assertEqual(device1._interface_count, 2)
|
||||
self.assertEqual(device2._interface_count, 2)
|
||||
|
||||
interface1 = Interface.objects.create(device=device1, name='eth0')
|
||||
interface2 = Interface.objects.create(device=device2, name='eth0')
|
||||
interface3 = Interface.objects.create(device=device2, name='eth1')
|
||||
interface2.delete()
|
||||
interface3.delete()
|
||||
Interface.objects.get(name='Interface 1').delete()
|
||||
Interface.objects.get(name='Interface 3').delete()
|
||||
device1.refresh_from_db()
|
||||
device2.refresh_from_db()
|
||||
self.assertEqual(device1._interface_count, 1)
|
||||
self.assertEqual(device2._interface_count, 0)
|
||||
self.assertEqual(device2._interface_count, 1)
|
||||
|
Loading…
Reference in New Issue
Block a user