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

Add tests for CSVDataField

This commit is contained in:
Jeremy Stretch
2020-05-01 13:32:28 -04:00
parent 34a17d4571
commit 61ae4be16a
3 changed files with 100 additions and 20 deletions

View File

@@ -1,6 +1,8 @@
from django import forms
from django.test import TestCase
from ipam.forms import IPAddressCSVForm
from ipam.models import VRF
from utilities.forms import *
@@ -281,3 +283,85 @@ class ExpandAlphanumeric(TestCase):
with self.assertRaises(ValueError):
sorted(expand_alphanumeric_pattern('r[a,,b]a'))
class CSVDataFieldTest(TestCase):
def setUp(self):
self.field = CSVDataField(from_form=IPAddressCSVForm)
def test_clean(self):
input = """
address,status,vrf
192.0.2.1/32,Active,Test VRF
"""
output = (
{'address': None, 'status': None, 'vrf': None},
[{'address': '192.0.2.1/32', 'status': 'Active', 'vrf': 'Test VRF'}]
)
self.assertEqual(self.field.clean(input), output)
def test_clean_invalid_header(self):
input = """
address,status,vrf,xxx
192.0.2.1/32,Active,Test VRF,123
"""
with self.assertRaises(forms.ValidationError):
self.field.clean(input)
def test_clean_missing_required_header(self):
input = """
status,vrf
Active,Test VRF
"""
with self.assertRaises(forms.ValidationError):
self.field.clean(input)
def test_clean_default_to_field(self):
input = """
address,status,vrf.name
192.0.2.1/32,Active,Test VRF
"""
output = (
{'address': None, 'status': None, 'vrf': 'name'},
[{'address': '192.0.2.1/32', 'status': 'Active', 'vrf': 'Test VRF'}]
)
self.assertEqual(self.field.clean(input), output)
def test_clean_pk_to_field(self):
input = """
address,status,vrf.pk
192.0.2.1/32,Active,123
"""
output = (
{'address': None, 'status': None, 'vrf': 'pk'},
[{'address': '192.0.2.1/32', 'status': 'Active', 'vrf': '123'}]
)
self.assertEqual(self.field.clean(input), output)
def test_clean_custom_to_field(self):
input = """
address,status,vrf.rd
192.0.2.1/32,Active,123:456
"""
output = (
{'address': None, 'status': None, 'vrf': 'rd'},
[{'address': '192.0.2.1/32', 'status': 'Active', 'vrf': '123:456'}]
)
self.assertEqual(self.field.clean(input), output)
def test_clean_invalid_to_field(self):
input = """
address,status,vrf.xxx
192.0.2.1/32,Active,123:456
"""
with self.assertRaises(forms.ValidationError):
self.field.clean(input)
def test_clean_to_field_on_non_object(self):
input = """
address,status.foo,vrf
192.0.2.1/32,Bar,Test VRF
"""
with self.assertRaises(forms.ValidationError):
self.field.clean(input)