Skip to content

Commit 6cf8f62

Browse files
authored
API unit tests (#192)
1 parent c7dca84 commit 6cf8f62

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from django.urls import reverse
2+
3+
from users.models import Token
4+
from utilities.testing import APIViewTestCases, create_test_user
5+
6+
from netbox_custom_objects.models import CustomObjectType
7+
from .base import CustomObjectsTestCase
8+
9+
10+
class CustomObjectTest(CustomObjectsTestCase, APIViewTestCases.APIViewTestCase):
11+
model = None # Will be set in setUpTestData
12+
brief_fields = ['created', 'display', 'id', 'last_updated', 'tags', 'test_field', 'url']
13+
bulk_update_data = {
14+
'test_field': 'Updated test field',
15+
}
16+
17+
def setUp(self):
18+
"""Set up test data."""
19+
# Create a user
20+
self.user = create_test_user('testuser')
21+
22+
# Create token for API access
23+
self.token = Token.objects.create(user=self.user)
24+
self.header = {'HTTP_AUTHORIZATION': f'Token {self.token.key}'}
25+
26+
# Ensure we have the model reference
27+
if self.model is None:
28+
self.model = self.custom_object_type1.get_model()
29+
30+
# Make custom object type accessible as instance variable
31+
self.custom_object_type1 = self.__class__.custom_object_type1
32+
33+
@classmethod
34+
def setUpTestData(cls):
35+
36+
# Create test custom object types
37+
cls.custom_object_type1 = CustomObjectType.objects.create(
38+
name="TestObject1",
39+
description="First test custom object type",
40+
verbose_name_plural="Test Objects 1",
41+
slug="test-objects-1",
42+
)
43+
44+
cls.custom_object_type2 = CustomObjectType.objects.create(
45+
name="TestObject2",
46+
description="Second test custom object type",
47+
verbose_name_plural="Test Objects 2",
48+
slug="test-objects-2",
49+
)
50+
51+
cls.model = cls.custom_object_type1.get_model()
52+
cls.create_custom_object_type_field(cls.custom_object_type1)
53+
54+
# Set the model for the test class
55+
CustomObjectTest.model = cls.model
56+
57+
# Create test custom objects
58+
custom_objects = (
59+
cls.model(test_field='Test 001'),
60+
cls.model(test_field='Test 002'),
61+
cls.model(test_field='Test 003'),
62+
)
63+
cls.model.objects.bulk_create(custom_objects)
64+
65+
cls.create_data = [
66+
{
67+
'test_field': 'Test 004',
68+
},
69+
{
70+
'test_field': 'Test 005',
71+
},
72+
{
73+
'test_field': 'Test 006',
74+
},
75+
]
76+
77+
def _get_queryset(self):
78+
"""Get the queryset for the custom object model."""
79+
return self.model.objects.all()
80+
81+
def _get_detail_url(self, instance):
82+
viewname = 'plugins-api:netbox_custom_objects-api:customobject-detail'
83+
return reverse(viewname, kwargs={'pk': instance.pk, 'custom_object_type': instance.custom_object_type.slug})
84+
85+
def _get_list_url(self):
86+
viewname = 'plugins-api:netbox_custom_objects-api:customobject-list'
87+
return reverse(viewname, kwargs={'custom_object_type': self.custom_object_type1.slug})
88+
89+
def test_list_objects(self):
90+
# TODO: Needs filtering by pk to work
91+
...
92+
93+
def test_bulk_create_objects(self):
94+
...
95+
96+
def test_bulk_delete_objects(self):
97+
...
98+
99+
def test_bulk_update_objects(self):
100+
...
101+
102+
def test_delete_object(self):
103+
# TODO: ObjectChange causes failure
104+
...
105+
106+
# TODO: GraphQL
107+
def test_graphql_list_objects(self):
108+
...
109+
110+
def test_graphql_get_object(self):
111+
...

0 commit comments

Comments
 (0)