From 46d7bf02ac0cef30574fdf918a86c4c243c68571 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 11 Apr 2023 15:25:48 -0400 Subject: [PATCH] Add tests for ConfigTemplate --- netbox/extras/graphql/types.py | 2 +- netbox/extras/tests/test_api.py | 40 ++++++++++++++++++++++++++ netbox/extras/tests/test_filtersets.py | 22 ++++++++++++++ netbox/extras/tests/test_views.py | 39 +++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) diff --git a/netbox/extras/graphql/types.py b/netbox/extras/graphql/types.py index ba16ccd3e..ae7d5cef6 100644 --- a/netbox/extras/graphql/types.py +++ b/netbox/extras/graphql/types.py @@ -25,7 +25,7 @@ class ConfigContextType(ObjectType): filterset_class = filtersets.ConfigContextFilterSet -class ConfigTemplateType(ObjectType): +class ConfigTemplateType(TagsMixin, ObjectType): class Meta: model = models.ConfigTemplate diff --git a/netbox/extras/tests/test_api.py b/netbox/extras/tests/test_api.py index ef6fa605b..6e5d22531 100644 --- a/netbox/extras/tests/test_api.py +++ b/netbox/extras/tests/test_api.py @@ -513,6 +513,46 @@ class ConfigContextTest(APIViewTestCases.APIViewTestCase): self.assertEqual(rendered_context['bar'], 456) +class ConfigTemplateTest(APIViewTestCases.APIViewTestCase): + model = ConfigTemplate + brief_fields = ['display', 'id', 'name', 'url'] + create_data = [ + { + 'name': 'Config Template 4', + 'template_code': 'Foo: {{ foo }}', + }, + { + 'name': 'Config Template 5', + 'template_code': 'Bar: {{ bar }}', + }, + { + 'name': 'Config Template 6', + 'template_code': 'Baz: {{ baz }}', + }, + ] + bulk_update_data = { + 'description': 'New description', + } + + @classmethod + def setUpTestData(cls): + config_templates = ( + ConfigTemplate( + name='Config Template 1', + template_code='Foo: {{ foo }}' + ), + ConfigTemplate( + name='Config Template 2', + template_code='Bar: {{ bar }}' + ), + ConfigTemplate( + name='Config Template 3', + template_code='Baz: {{ baz }}' + ), + ) + ConfigTemplate.objects.bulk_create(config_templates) + + class ReportTest(APITestCase): class TestReport(Report): diff --git a/netbox/extras/tests/test_filtersets.py b/netbox/extras/tests/test_filtersets.py index 24535152b..e77afd20e 100644 --- a/netbox/extras/tests/test_filtersets.py +++ b/netbox/extras/tests/test_filtersets.py @@ -790,6 +790,28 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests): self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) +class ConfigTemplateTestCase(TestCase, BaseFilterSetTests): + queryset = ConfigTemplate.objects.all() + filterset = ConfigTemplateFilterSet + + @classmethod + def setUpTestData(cls): + config_templates = ( + ConfigTemplate(name='Config Template 1', template_code='TESTING', description='foobar1'), + ConfigTemplate(name='Config Template 2', template_code='TESTING', description='foobar2'), + ConfigTemplate(name='Config Template 3', template_code='TESTING'), + ) + ConfigTemplate.objects.bulk_create(config_templates) + + def test_name(self): + params = {'name': ['Config Template 1', 'Config Template 2']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_description(self): + params = {'description': ['foobar1', 'foobar2']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + class TagTestCase(TestCase, ChangeLoggedFilterSetTests): queryset = Tag.objects.all() filterset = TagFilterSet diff --git a/netbox/extras/tests/test_views.py b/netbox/extras/tests/test_views.py index 98de95e8f..e1cbacd97 100644 --- a/netbox/extras/tests/test_views.py +++ b/netbox/extras/tests/test_views.py @@ -360,6 +360,45 @@ class ConfigContextTestCase( } +class ConfigTemplateTestCase( + ViewTestCases.GetObjectViewTestCase, + ViewTestCases.GetObjectChangelogViewTestCase, + ViewTestCases.DeleteObjectViewTestCase, + ViewTestCases.ListObjectsViewTestCase, + ViewTestCases.BulkEditObjectsViewTestCase, + ViewTestCases.BulkDeleteObjectsViewTestCase +): + model = ConfigTemplate + + @classmethod + def setUpTestData(cls): + TEMPLATE_CODE = """Foo: {{ foo }}""" + + config_templates = ( + ConfigTemplate(name='Config Template 1', template_code=TEMPLATE_CODE), + ConfigTemplate(name='Config Template 2', template_code=TEMPLATE_CODE), + ConfigTemplate(name='Config Template 3', template_code=TEMPLATE_CODE), + ) + ConfigTemplate.objects.bulk_create(config_templates) + + cls.form_data = { + 'name': 'Config Template X', + 'description': 'Config template', + 'template_code': TEMPLATE_CODE, + } + + cls.csv_update_data = ( + "id,name", + f"{config_templates[0].pk},Config Template 7", + f"{config_templates[1].pk},Config Template 8", + f"{config_templates[2].pk},Config Template 9", + ) + + cls.bulk_edit_data = { + 'description': 'New description', + } + + # TODO: Convert to StandardTestCases.Views class ObjectChangeTestCase(TestCase): user_permissions = (