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

Update tests for tag changes

This commit is contained in:
Jeremy Stretch
2020-06-12 11:33:23 -04:00
parent 7dc4f8d5cc
commit e905a36fb2
7 changed files with 84 additions and 43 deletions

View File

@@ -14,7 +14,14 @@ def post_data(data):
if value is None:
ret[key] = ''
elif type(value) in (list, tuple):
ret[key] = value
if value and hasattr(value[0], 'pk'):
# Value is a list of instances
ret[key] = [v.pk for v in value]
else:
ret[key] = value
elif hasattr(value, 'pk'):
# Value is an instance
ret[key] = value.pk
else:
ret[key] = str(value)

View File

@@ -5,8 +5,10 @@ from django.db.models import ForeignKey, ManyToManyField
from django.forms.models import model_to_dict
from django.test import Client, TestCase as _TestCase, override_settings
from django.urls import reverse, NoReverseMatch
from django.utils.text import slugify
from netaddr import IPNetwork
from extras.models import Tag
from users.models import ObjectPermission
from utilities.permissions import resolve_permission_ct
from .utils import disable_warnings, post_data
@@ -48,7 +50,7 @@ class TestCase(_TestCase):
obj_perm.object_types.add(ct)
#
# Convenience methods
# Custom assertions
#
def assertHttpStatus(self, response, expected_status):
@@ -75,7 +77,7 @@ class TestCase(_TestCase):
# TODO: Differentiate between tags assigned to the instance and a M2M field for tags (ex: ConfigContext)
if key == 'tags':
model_dict[key] = ','.join(sorted([tag.name for tag in value]))
model_dict[key] = sorted(value)
# Convert ManyToManyField to list of instance PKs
elif model_dict[key] and type(value) in (list, tuple) and hasattr(value[0], 'pk'):
@@ -102,6 +104,19 @@ class TestCase(_TestCase):
self.assertDictEqual(model_dict, relevant_data)
#
# Convenience methods
#
@classmethod
def create_tags(cls, *names):
"""
Create and return a Tag instance for each name given.
"""
tags = [Tag(name=name, slug=slugify(name)) for name in names]
Tag.objects.bulk_create(tags)
return tags
#
# UI Tests