mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #11163: Auto-detect data format during bulk import
This commit is contained in:
@@ -2,6 +2,8 @@ from django import forms
|
||||
from django.test import TestCase
|
||||
|
||||
from ipam.forms import IPAddressImportForm
|
||||
from utilities.choices import ImportFormatChoices
|
||||
from utilities.forms import ImportForm
|
||||
from utilities.forms.fields import CSVDataField
|
||||
from utilities.forms.utils import expand_alphanumeric_pattern, expand_ipaddress_pattern
|
||||
|
||||
@@ -365,3 +367,50 @@ class CSVDataFieldTest(TestCase):
|
||||
"""
|
||||
with self.assertRaises(forms.ValidationError):
|
||||
self.field.clean(input)
|
||||
|
||||
|
||||
class ImportFormTest(TestCase):
|
||||
|
||||
def test_format_detection(self):
|
||||
form = ImportForm()
|
||||
|
||||
data = (
|
||||
"a,b,c\n"
|
||||
"1,2,3\n"
|
||||
"4,5,6\n"
|
||||
)
|
||||
self.assertEqual(form._detect_format(data), ImportFormatChoices.CSV)
|
||||
|
||||
data = '{"a": 1, "b": 2, "c": 3"}'
|
||||
self.assertEqual(form._detect_format(data), ImportFormatChoices.JSON)
|
||||
|
||||
data = '[{"a": 1, "b": 2, "c": 3"}, {"a": 4, "b": 5, "c": 6"}]'
|
||||
self.assertEqual(form._detect_format(data), ImportFormatChoices.JSON)
|
||||
|
||||
data = (
|
||||
"- a: 1\n"
|
||||
" b: 2\n"
|
||||
" c: 3\n"
|
||||
"- a: 4\n"
|
||||
" b: 5\n"
|
||||
" c: 6\n"
|
||||
)
|
||||
self.assertEqual(form._detect_format(data), ImportFormatChoices.YAML)
|
||||
|
||||
data = (
|
||||
"---\n"
|
||||
"a: 1\n"
|
||||
"b: 2\n"
|
||||
"c: 3\n"
|
||||
"---\n"
|
||||
"a: 4\n"
|
||||
"b: 5\n"
|
||||
"c: 6\n"
|
||||
)
|
||||
self.assertEqual(form._detect_format(data), ImportFormatChoices.YAML)
|
||||
|
||||
# Invalid data
|
||||
with self.assertRaises(forms.ValidationError):
|
||||
form._detect_format('')
|
||||
with self.assertRaises(forms.ValidationError):
|
||||
form._detect_format('?')
|
||||
|
Reference in New Issue
Block a user