From 375bec96988ec7cedcab241575f9505394cd4852 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 26 Oct 2022 11:03:59 -0700 Subject: [PATCH] 7961 make test cases more explicit --- netbox/ipam/tests/test_views.py | 207 +++++++++++++++++--------------- 1 file changed, 109 insertions(+), 98 deletions(-) diff --git a/netbox/ipam/tests/test_views.py b/netbox/ipam/tests/test_views.py index f8dba55e7..f9b03b64a 100644 --- a/netbox/ipam/tests/test_views.py +++ b/netbox/ipam/tests/test_views.py @@ -61,10 +61,10 @@ class ASNTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "description", - "New description1", - "New description2", - "New description3", + "id,description", + f"{asns[0].pk},New description1", + f"{asns[1].pk},New description2", + f"{asns[2].pk},New description3", ) cls.bulk_edit_data = { @@ -85,11 +85,12 @@ class VRFTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) Tenant.objects.bulk_create(tenants) - VRF.objects.bulk_create([ + vrfs = ( VRF(name='VRF 1', rd='65000:1'), VRF(name='VRF 2', rd='65000:2'), VRF(name='VRF 3', rd='65000:3'), - ]) + ) + VRF.objects.bulk_create(vrfs) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -110,10 +111,10 @@ class VRFTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "name", - "VRF 7", - "VRF 8", - "VRF 9", + "id,name", + f"{vrfs[0].pk},VRF 7", + f"{vrfs[1].pk},VRF 8", + f"{vrfs[2].pk},VRF 9", ) cls.bulk_edit_data = { @@ -158,10 +159,10 @@ class RouteTargetTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "65000:1007,New description1", - "65000:1008,New description2", - "65000:1009,New description3", + "id,name,description", + f"{route_targets[0].pk},65000:1007,New description1", + f"{route_targets[1].pk},65000:1008,New description2", + f"{route_targets[2].pk},65000:1009,New description3", ) cls.bulk_edit_data = { @@ -176,11 +177,12 @@ class RIRTestCase(ViewTestCases.OrganizationalObjectViewTestCase): @classmethod def setUpTestData(cls): - RIR.objects.bulk_create([ + rirs = ( RIR(name='RIR 1', slug='rir-1'), RIR(name='RIR 2', slug='rir-2'), RIR(name='RIR 3', slug='rir-3'), - ]) + ) + RIR.objects.bulk_create(rirs) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -200,10 +202,10 @@ class RIRTestCase(ViewTestCases.OrganizationalObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "RIR 7,Fourth RIR7", - "RIR 8,Fifth RIR8", - "RIR 9,Sixth RIR9", + "id,name,description", + f"{rirs[0].pk},RIR 7,Fourth RIR7", + f"{rirs[1].pk},RIR 8,Fifth RIR8", + f"{rirs[2].pk},RIR 9,Sixth RIR9", ) cls.bulk_edit_data = { @@ -223,11 +225,12 @@ class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) RIR.objects.bulk_create(rirs) - Aggregate.objects.bulk_create([ + aggregates = ( Aggregate(prefix=IPNetwork('10.1.0.0/16'), rir=rirs[0]), Aggregate(prefix=IPNetwork('10.2.0.0/16'), rir=rirs[0]), Aggregate(prefix=IPNetwork('10.3.0.0/16'), rir=rirs[0]), - ]) + ) + Aggregate.objects.bulk_create(aggregates) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -247,10 +250,10 @@ class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "description", - "New description1", - "New description2", - "New description3", + "id,description", + f"{aggregates[0].pk},New description1", + f"{aggregates[1].pk},New description2", + f"{aggregates[2].pk},New description3", ) cls.bulk_edit_data = { @@ -281,11 +284,12 @@ class RoleTestCase(ViewTestCases.OrganizationalObjectViewTestCase): @classmethod def setUpTestData(cls): - Role.objects.bulk_create([ + roles = ( Role(name='Role 1', slug='role-1'), Role(name='Role 2', slug='role-2'), Role(name='Role 3', slug='role-3'), - ]) + ) + Role.objects.bulk_create(roles) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -305,10 +309,10 @@ class RoleTestCase(ViewTestCases.OrganizationalObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "Role 7,New description7", - "Role 8,New description8", - "Role 9,New description9", + "id,name,description", + f"{roles[0].pk},Role 7,New description7", + f"{roles[1].pk},Role 8,New description8", + f"{roles[2].pk},Role 9,New description9", ) cls.bulk_edit_data = { @@ -340,11 +344,12 @@ class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) Role.objects.bulk_create(roles) - Prefix.objects.bulk_create([ + prefixes = ( Prefix(prefix=IPNetwork('10.1.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]), Prefix(prefix=IPNetwork('10.2.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]), Prefix(prefix=IPNetwork('10.3.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]), - ]) + ) + Prefix.objects.bulk_create(prefixes) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -369,10 +374,10 @@ class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "description,status", - f"New description 7,{PrefixStatusChoices.STATUS_RESERVED}", - f"New description 8,{PrefixStatusChoices.STATUS_RESERVED}", - f"New description 9,{PrefixStatusChoices.STATUS_RESERVED}", + "id,description,status", + f"{prefixes[0].pk},New description 7,{PrefixStatusChoices.STATUS_RESERVED}", + f"{prefixes[1].pk},New description 8,{PrefixStatusChoices.STATUS_RESERVED}", + f"{prefixes[2].pk},New description 9,{PrefixStatusChoices.STATUS_RESERVED}", ) cls.bulk_edit_data = { @@ -478,10 +483,10 @@ class IPRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "description,status", - f"New description 7,{IPRangeStatusChoices.STATUS_RESERVED}", - f"New description 8,{IPRangeStatusChoices.STATUS_RESERVED}", - f"New description 9,{IPRangeStatusChoices.STATUS_RESERVED}", + "id,description,status", + f"{ip_ranges[0].pk},New description 7,{IPRangeStatusChoices.STATUS_RESERVED}", + f"{ip_ranges[1].pk},New description 8,{IPRangeStatusChoices.STATUS_RESERVED}", + f"{ip_ranges[2].pk},New description 9,{IPRangeStatusChoices.STATUS_RESERVED}", ) cls.bulk_edit_data = { @@ -523,11 +528,12 @@ class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) VRF.objects.bulk_create(vrfs) - IPAddress.objects.bulk_create([ + ipaddresses = ( IPAddress(address=IPNetwork('192.0.2.1/24'), vrf=vrfs[0]), IPAddress(address=IPNetwork('192.0.2.2/24'), vrf=vrfs[0]), IPAddress(address=IPNetwork('192.0.2.3/24'), vrf=vrfs[0]), - ]) + ) + IPAddress.objects.bulk_create(ipaddresses) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -551,10 +557,10 @@ class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "description,status", - f"New description 7,{IPAddressStatusChoices.STATUS_RESERVED}", - f"New description 8,{IPAddressStatusChoices.STATUS_RESERVED}", - f"New description 9,{IPAddressStatusChoices.STATUS_RESERVED}", + "id,description,status", + f"{ipaddresses[0].pk},New description 7,{IPAddressStatusChoices.STATUS_RESERVED}", + f"{ipaddresses[1].pk},New description 8,{IPAddressStatusChoices.STATUS_RESERVED}", + f"{ipaddresses[2].pk},New description 9,{IPAddressStatusChoices.STATUS_RESERVED}", ) cls.bulk_edit_data = { @@ -573,11 +579,12 @@ class FHRPGroupTestCase(ViewTestCases.PrimaryObjectViewTestCase): @classmethod def setUpTestData(cls): - FHRPGroup.objects.bulk_create(( + fhrp_groups = ( FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP2, group_id=10, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_PLAINTEXT, auth_key='foobar123'), FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP3, group_id=20, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, auth_key='foobar123'), FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_HSRP, group_id=30), - )) + ) + FHRPGroup.objects.bulk_create(fhrp_groups) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -599,10 +606,10 @@ class FHRPGroupTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "FHRP Group 1,New description 1", - "FHRP Group 2,New description 2", - "FHRP Group 3,New description 3", + "id,name,description", + f"{fhrp_groups[0].pk},FHRP Group 1,New description 1", + f"{fhrp_groups[1].pk},FHRP Group 2,New description 2", + f"{fhrp_groups[2].pk},FHRP Group 3,New description 3", ) cls.bulk_edit_data = { @@ -622,11 +629,12 @@ class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase): ) Site.objects.bulk_create(sites) - VLANGroup.objects.bulk_create([ + vlan_groups = ( VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]), VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[0]), VLANGroup(name='VLAN Group 3', slug='vlan-group-3', scope=sites[0]), - ]) + ) + VLANGroup.objects.bulk_create(vlan_groups) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -647,10 +655,10 @@ class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase): ) cls.csv_update_data = ( - f"name,description", - f"VLAN Group 7,Fourth VLAN group7", - f"VLAN Group 8,Fifth VLAN group8", - f"VLAN Group 9,Sixth VLAN group9", + f"id,name,description", + f"{vlan_groups[0].pk},VLAN Group 7,Fourth VLAN group7", + f"{vlan_groups[1].pk},VLAN Group 8,Fifth VLAN group8", + f"{vlan_groups[2].pk},VLAN Group 9,Sixth VLAN group9", ) cls.bulk_edit_data = { @@ -682,11 +690,12 @@ class VLANTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) Role.objects.bulk_create(roles) - VLAN.objects.bulk_create([ + vlans = ( VLAN(group=vlangroups[0], vid=101, name='VLAN101', site=sites[0], role=roles[0]), VLAN(group=vlangroups[0], vid=102, name='VLAN102', site=sites[0], role=roles[0]), VLAN(group=vlangroups[0], vid=103, name='VLAN103', site=sites[0], role=roles[0]), - ]) + ) + VLAN.objects.bulk_create(vlans) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -710,10 +719,10 @@ class VLANTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "VLAN107,New description 7", - "VLAN108,New description 8", - "VLAN109,New description 9", + "id,name,description", + f"{vlans[0].pk},VLAN107,New description 7", + f"{vlans[1].pk},VLAN108,New description 8", + f"{vlans[2].pk},VLAN109,New description 9", ) cls.bulk_edit_data = { @@ -731,11 +740,12 @@ class ServiceTemplateTestCase(ViewTestCases.PrimaryObjectViewTestCase): @classmethod def setUpTestData(cls): - ServiceTemplate.objects.bulk_create([ + service_templates = ( ServiceTemplate(name='Service Template 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[101]), ServiceTemplate(name='Service Template 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[102]), ServiceTemplate(name='Service Template 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[103]), - ]) + ) + ServiceTemplate.objects.bulk_create(service_templates) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -755,10 +765,10 @@ class ServiceTemplateTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "Service Template 7,First service template7", - "Service Template 8,Second service template8", - "Service Template 9,Third service template9", + "id,name,description", + f"{service_templates[0].pk},Service Template 7,First service template7", + f"{service_templates[1].pk},Service Template 8,Second service template8", + f"{service_templates[2].pk},Service Template 9,Third service template9", ) cls.bulk_edit_data = { @@ -780,11 +790,12 @@ class ServiceTestCase(ViewTestCases.PrimaryObjectViewTestCase): devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1') device = Device.objects.create(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) - Service.objects.bulk_create([ + services = ( Service(device=device, name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[101]), Service(device=device, name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[102]), Service(device=device, name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[103]), - ]) + ) + Service.objects.bulk_create(services) tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -807,10 +818,10 @@ class ServiceTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.csv_update_data = ( - "name,description", - "Service 7,First service7", - "Service 8,Second service8", - "Service 9,Third service9", + "id,name,description", + f"{services[0].pk},Service 7,First service7", + f"{services[1].pk},Service 8,Second service8", + f"{services[2].pk},Service 9,Third service9", ) cls.bulk_edit_data = { @@ -849,21 +860,6 @@ class ServiceTestCase(ViewTestCases.PrimaryObjectViewTestCase): class L2VPNTestCase(ViewTestCases.PrimaryObjectViewTestCase): model = L2VPN - csv_data = ( - 'name,slug,type,identifier', - 'L2VPN 5,l2vpn-5,vxlan,456', - 'L2VPN 6,l2vpn-6,vxlan,444', - ) - - csv_update_data = ( - 'name,description', - 'L2VPN 7,New description 7', - 'L2VPN 8,New description 8', - ) - - bulk_edit_data = { - 'description': 'New Description', - } @classmethod def setUpTestData(cls): @@ -878,9 +874,24 @@ class L2VPNTestCase(ViewTestCases.PrimaryObjectViewTestCase): L2VPN(name='L2VPN 2', slug='l2vpn-2', type=L2VPNTypeChoices.TYPE_VXLAN, identifier='650002'), L2VPN(name='L2VPN 3', slug='l2vpn-3', type=L2VPNTypeChoices.TYPE_VXLAN, identifier='650003') ) - L2VPN.objects.bulk_create(l2vpns) + cls.csv_data = ( + 'name,slug,type,identifier', + 'L2VPN 5,l2vpn-5,vxlan,456', + 'L2VPN 6,l2vpn-6,vxlan,444', + ) + + cls.csv_update_data = ( + 'id,name,description', + f'{l2vpns[0].pk},L2VPN 7,New description 7', + f'{l2vpns[1].pk},L2VPN 8,New description 8', + ) + + cls.bulk_edit_data = { + 'description': 'New Description', + } + cls.form_data = { 'name': 'L2VPN 8', 'slug': 'l2vpn-8', @@ -943,10 +954,10 @@ class L2VPNTerminationTestCase( ) cls.csv_update_data = ( - "l2vpn", - "L2VPN 2", - "L2VPN 2", - "L2VPN 2", + "id,l2vpn", + f"{terminations[0].pk},L2VPN 2", + f"{terminations[1].pk},L2VPN 2", + f"{terminations[2].pk},L2VPN 2", ) cls.bulk_edit_data = {}