1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #3937: Suppress warning messages in tests for requests expected to yield a 4XX response

This commit is contained in:
Jeremy Stretch
2020-01-17 14:53:33 -05:00
parent 439fa731ba
commit 302f87e108
4 changed files with 30 additions and 9 deletions

View File

@ -7,7 +7,7 @@ from rest_framework import status
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
from ipam.choices import *
from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
from utilities.testing import APITestCase, choices_to_dict
from utilities.testing import APITestCase, choices_to_dict, disable_warnings
class AppTest(APITestCase):
@ -1007,6 +1007,7 @@ class VLANTest(APITestCase):
self.prefix1.save()
url = reverse('ipam-api:vlan-detail', kwargs={'pk': self.vlan1.pk})
with disable_warnings('django.request'):
response = self.client.delete(url, **self.header)
self.assertHttpStatus(response, status.HTTP_409_CONFLICT)

View File

@ -1,3 +1,6 @@
import logging
from contextlib import contextmanager
from django.contrib.auth.models import Permission, User
from rest_framework.test import APITestCase as _APITestCase
@ -62,3 +65,15 @@ def choices_to_dict(choices_list):
return {
choice['value']: choice['label'] for choice in choices_list
}
@contextmanager
def disable_warnings(logger_name):
"""
Temporarily suppress expected warning messages to keep the test output clean.
"""
logger = logging.getLogger(logger_name)
current_level = logger.level
logger.setLevel(logging.ERROR)
yield
logger.setLevel(current_level)

View File

@ -9,7 +9,7 @@ from dcim.models import Region, Site
from extras.choices import CustomFieldTypeChoices
from extras.models import CustomField
from ipam.models import VLAN
from utilities.testing import APITestCase
from utilities.testing import APITestCase, disable_warnings
class WritableNestedSerializerTest(APITestCase):
@ -50,6 +50,7 @@ class WritableNestedSerializerTest(APITestCase):
}
url = reverse('ipam-api:vlan-list')
with disable_warnings('django.request'):
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
@ -85,6 +86,7 @@ class WritableNestedSerializerTest(APITestCase):
}
url = reverse('ipam-api:vlan-list')
with disable_warnings('django.request'):
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
@ -104,6 +106,7 @@ class WritableNestedSerializerTest(APITestCase):
}
url = reverse('ipam-api:vlan-list')
with disable_warnings('django.request'):
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
@ -119,6 +122,7 @@ class WritableNestedSerializerTest(APITestCase):
}
url = reverse('ipam-api:vlan-list')
with disable_warnings('django.request'):
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)

View File

@ -5,7 +5,7 @@ from rest_framework import status
from dcim.choices import InterfaceModeChoices
from dcim.models import Interface
from ipam.models import IPAddress, VLAN
from utilities.testing import APITestCase, choices_to_dict
from utilities.testing import APITestCase, choices_to_dict, disable_warnings
from virtualization.choices import *
from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine
@ -417,6 +417,7 @@ class VirtualMachineTest(APITestCase):
}
url = reverse('virtualization-api:virtualmachine-list')
with disable_warnings('django.request'):
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)