mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 08:46:10 -06:00
Clean up CSVImportTestCase
This commit is contained in:
parent
450f68c40e
commit
462fe816d6
@ -1,11 +1,9 @@
|
||||
import urllib.parse
|
||||
|
||||
from dcim.models import *
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from dcim.models import *
|
||||
from users.models import ObjectPermission
|
||||
from utilities.testing import ModelViewTestCase, TestCase, create_tags
|
||||
from utilities.testing import ModelViewTestCase, create_tags
|
||||
|
||||
|
||||
class CSVImportTestCase(ModelViewTestCase):
|
||||
@ -13,16 +11,7 @@ class CSVImportTestCase(ModelViewTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
tags = create_tags('Alpha', 'Bravo', 'Charlie')
|
||||
|
||||
# Create three Regions
|
||||
regions = (
|
||||
Region(name='Region 1', slug='region-1'),
|
||||
Region(name='Region 2', slug='region-2'),
|
||||
Region(name='Region 3', slug='region-3'),
|
||||
)
|
||||
for region in regions:
|
||||
region.save()
|
||||
create_tags('Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo')
|
||||
|
||||
def _get_csv_data(self, csv_data):
|
||||
return '\n'.join(csv_data)
|
||||
@ -30,9 +19,11 @@ class CSVImportTestCase(ModelViewTestCase):
|
||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||
def test_valid_tags(self):
|
||||
csv_data = (
|
||||
'name,slug,description,tags',
|
||||
'Region 4,region-4,Fourth region,"Alpha,Bravo"',
|
||||
'Region 5,region-5,Fourth region,"Alpha,Charlie"',
|
||||
'name,slug,tags',
|
||||
'Region 1,region-1,"alpha,bravo"',
|
||||
'Region 2,region-2,"charlie,delta"',
|
||||
'Region 3,region-3,echo',
|
||||
'Region 4,region-4,',
|
||||
)
|
||||
|
||||
data = {
|
||||
@ -50,18 +41,29 @@ class CSVImportTestCase(ModelViewTestCase):
|
||||
|
||||
# Test POST with permission
|
||||
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 200)
|
||||
regions = Region.objects.all()
|
||||
self.assertEqual(regions.count(), 4)
|
||||
region = Region.objects.get(slug="region-4")
|
||||
self.assertTrue("alpha" in region.tags.values_list("slug", flat=True))
|
||||
self.assertTrue("bravo" in region.tags.values_list("slug", flat=True))
|
||||
region = Region.objects.get(slug="region-5")
|
||||
self.assertTrue("alpha" in region.tags.values_list("slug", flat=True))
|
||||
self.assertTrue("charlie" in region.tags.values_list("slug", flat=True))
|
||||
self.assertEqual(
|
||||
list(regions[0].tags.values_list('name', flat=True)),
|
||||
['Alpha', 'Bravo']
|
||||
)
|
||||
self.assertEqual(
|
||||
list(regions[1].tags.values_list('name', flat=True)),
|
||||
['Charlie', 'Delta']
|
||||
)
|
||||
self.assertEqual(
|
||||
list(regions[2].tags.values_list('name', flat=True)),
|
||||
['Echo']
|
||||
)
|
||||
self.assertEqual(regions[3].tags.count(), 0)
|
||||
|
||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||
def test_non_valid_tags(self):
|
||||
def test_invalid_tags(self):
|
||||
csv_data = (
|
||||
'name,slug,description,tags',
|
||||
'Region 4,region-4,Fourth region,"Alpha,Tango"',
|
||||
'name,slug,tags',
|
||||
'Region 1,region-1,"Alpha,Bravo"', # Valid
|
||||
'Region 2,region-2,"Alpha,Tango"', # Invalid
|
||||
)
|
||||
|
||||
data = {
|
||||
@ -76,8 +78,7 @@ class CSVImportTestCase(ModelViewTestCase):
|
||||
|
||||
# Try GET with model-level permission
|
||||
self.assertHttpStatus(self.client.get(self._get_url('import')), 200)
|
||||
print(self._get_url('import'))
|
||||
|
||||
# Test POST with permission
|
||||
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 200)
|
||||
self.assertFalse(Region.objects.filter(slug="region-4").exists())
|
||||
self.assertEqual(Region.objects.count(), 0)
|
||||
|
Loading…
Reference in New Issue
Block a user