Skip to content

Commit 5bd261e

Browse files
authored
Merge pull request #126 from netboxlabs/unit-test-suite
Unit test suite Part 3
2 parents 27fa949 + fd73073 commit 5bd261e

File tree

6 files changed

+784
-55
lines changed

6 files changed

+784
-55
lines changed

.github/workflows/lint-tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jobs:
6969
with:
7070
repository: "netbox-community/netbox"
7171
path: netbox
72+
ref: feature
7273
- name: Install netbox-custom-objects
7374
working-directory: netbox-custom-objects
7475
run: |

netbox_custom_objects/__init__.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import warnings
21
import sys
32

43
from django.core.exceptions import AppRegistryNotReady
@@ -60,37 +59,37 @@ def get_model(self, model_name, require_ready=True):
6059

6160
return obj.get_model()
6261

63-
def get_models(self, include_auto_created=False, include_swapped=False):
64-
"""Return all models for this plugin, including custom object type models."""
65-
# Get the regular Django models first
66-
for model in super().get_models(include_auto_created, include_swapped):
67-
yield model
68-
69-
# Skip custom object type model loading if running during migration
70-
if is_running_migration():
71-
return
72-
73-
# Suppress warnings about database calls during model loading
74-
with warnings.catch_warnings():
75-
warnings.filterwarnings(
76-
"ignore", category=RuntimeWarning, message=".*database.*"
77-
)
78-
warnings.filterwarnings(
79-
"ignore", category=UserWarning, message=".*database.*"
80-
)
81-
82-
# Add custom object type models
83-
from .models import CustomObjectType
84-
85-
# Only load models that are already cached to avoid creating all models at startup
86-
# This prevents the "two TaggableManagers with same through model" error
87-
custom_object_types = CustomObjectType.objects.all()
88-
for custom_type in custom_object_types:
89-
# Only yield already cached models during discovery
90-
if CustomObjectType.is_model_cached(custom_type.id):
91-
model = CustomObjectType.get_cached_model(custom_type.id)
92-
if model:
93-
yield model
62+
# def get_models(self, include_auto_created=False, include_swapped=False):
63+
# """Return all models for this plugin, including custom object type models."""
64+
# # Get the regular Django models first
65+
# for model in super().get_models(include_auto_created, include_swapped):
66+
# yield model
67+
#
68+
# # Skip custom object type model loading if running during migration
69+
# if is_running_migration():
70+
# return
71+
#
72+
# # Suppress warnings about database calls during model loading
73+
# with warnings.catch_warnings():
74+
# warnings.filterwarnings(
75+
# "ignore", category=RuntimeWarning, message=".*database.*"
76+
# )
77+
# warnings.filterwarnings(
78+
# "ignore", category=UserWarning, message=".*database.*"
79+
# )
80+
#
81+
# # Add custom object type models
82+
# from .models import CustomObjectType
83+
#
84+
# # Only load models that are already cached to avoid creating all models at startup
85+
# # This prevents the "two TaggableManagers with same through model" error
86+
# custom_object_types = CustomObjectType.objects.all()
87+
# for custom_type in custom_object_types:
88+
# # Only yield already cached models during discovery
89+
# if CustomObjectType.is_model_cached(custom_type.id):
90+
# model = CustomObjectType.get_cached_model(custom_type.id)
91+
# if model:
92+
# yield model
9493

9594

9695
config = CustomObjectsPluginConfig

netbox_custom_objects/tests/base.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Test utilities for netbox_custom_objects plugin
2-
from django.contrib.contenttypes.models import ContentType
32
from django.test import Client
3+
from core.models import ObjectType
44
from extras.models import CustomFieldChoiceSet
55
from utilities.testing import create_test_user
66

@@ -61,14 +61,14 @@ def create_choice_set(cls, **kwargs):
6161
return CustomFieldChoiceSet.objects.create(**defaults)
6262

6363
@classmethod
64-
def get_device_content_type(cls):
64+
def get_device_object_type(cls):
6565
"""Get the device content type for object field testing."""
66-
return ContentType.objects.get(app_label='dcim', model='device')
66+
return ObjectType.objects.get(app_label='dcim', model='device')
6767

6868
@classmethod
69-
def get_site_content_type(cls):
69+
def get_site_object_type(cls):
7070
"""Get the site content type for object field testing."""
71-
return ContentType.objects.get(app_label='dcim', model='site')
71+
return ObjectType.objects.get(app_label='dcim', model='site')
7272

7373
def create_simple_custom_object_type(self, **kwargs):
7474
"""Create a simple custom object type with basic fields."""
@@ -99,7 +99,7 @@ def create_complex_custom_object_type(self, **kwargs):
9999
"""Create a complex custom object type with various field types."""
100100
custom_object_type = CustomObjectsTestCase.create_custom_object_type(**kwargs)
101101
choice_set = CustomObjectsTestCase.create_choice_set()
102-
device_content_type = CustomObjectsTestCase.get_device_content_type()
102+
device_object_type = CustomObjectsTestCase.get_device_object_type()
103103

104104
# Primary text field
105105
CustomObjectsTestCase.create_custom_object_type_field(
@@ -145,7 +145,7 @@ def create_complex_custom_object_type(self, **kwargs):
145145
name="device",
146146
label="Device",
147147
type="object",
148-
related_object_type=device_content_type
148+
related_object_type=device_object_type
149149
)
150150

151151
return custom_object_type
@@ -169,8 +169,8 @@ def create_self_referential_custom_object_type(self, **kwargs):
169169
def create_multi_object_custom_object_type(self, **kwargs):
170170
"""Create a custom object type with multi-object fields."""
171171
custom_object_type = CustomObjectsTestCase.create_custom_object_type(**kwargs)
172-
device_content_type = CustomObjectsTestCase.get_device_content_type()
173-
site_content_type = CustomObjectsTestCase.get_site_content_type()
172+
device_object_type = CustomObjectsTestCase.get_device_object_type()
173+
site_object_type = CustomObjectsTestCase.get_site_object_type()
174174

175175
# Primary text field
176176
CustomObjectsTestCase.create_custom_object_type_field(
@@ -188,7 +188,7 @@ def create_multi_object_custom_object_type(self, **kwargs):
188188
name="devices",
189189
label="Devices",
190190
type="multiobject",
191-
related_object_type=device_content_type
191+
related_object_type=device_object_type
192192
)
193193

194194
# Multi-object field (sites)
@@ -197,7 +197,7 @@ def create_multi_object_custom_object_type(self, **kwargs):
197197
name="sites",
198198
label="Sites",
199199
type="multiobject",
200-
related_object_type=site_content_type
200+
related_object_type=site_object_type
201201
)
202202

203203
return custom_object_type

netbox_custom_objects/tests/test_field_types.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ class ObjectFieldTypeTestCase(FieldTypeTestCase):
602602
def setUp(self):
603603
"""Set up test data."""
604604
super().setUp()
605-
self.device_content_type = self.get_device_content_type()
605+
self.device_object_type = self.get_device_object_type()
606606

607607
def test_object_field_creation(self):
608608
"""Test creating an object field."""
@@ -611,11 +611,11 @@ def test_object_field_creation(self):
611611
name="device",
612612
label="Device",
613613
type="object",
614-
related_object_type=self.device_content_type
614+
related_object_type=self.device_object_type
615615
)
616616

617617
self.assertEqual(field.type, "object")
618-
self.assertEqual(field.related_object_type, self.device_content_type)
618+
self.assertEqual(field.related_object_type, self.device_object_type)
619619

620620
def test_object_field_model_generation(self):
621621
"""Test object field model generation."""
@@ -624,7 +624,7 @@ def test_object_field_model_generation(self):
624624
name="device",
625625
label="Device",
626626
type="object",
627-
related_object_type=self.device_content_type
627+
related_object_type=self.device_object_type
628628
)
629629
field # To silence ruff error
630630

@@ -661,7 +661,7 @@ class MultiObjectFieldTypeTestCase(FieldTypeTestCase):
661661
def setUp(self):
662662
"""Set up test data."""
663663
super().setUp()
664-
self.device_content_type = self.get_device_content_type()
664+
self.device_object_type = self.get_device_object_type()
665665

666666
def test_multiobject_field_creation(self):
667667
"""Test creating a multiobject field."""
@@ -670,11 +670,11 @@ def test_multiobject_field_creation(self):
670670
name="devices",
671671
label="Devices",
672672
type="multiobject",
673-
related_object_type=self.device_content_type
673+
related_object_type=self.device_object_type
674674
)
675675

676676
self.assertEqual(field.type, "multiobject")
677-
self.assertEqual(field.related_object_type, self.device_content_type)
677+
self.assertEqual(field.related_object_type, self.device_object_type)
678678

679679
def test_multiobject_field_model_generation(self):
680680
"""Test multiobject field model generation."""
@@ -683,7 +683,7 @@ def test_multiobject_field_model_generation(self):
683683
name="devices",
684684
label="Devices",
685685
type="multiobject",
686-
related_object_type=self.device_content_type
686+
related_object_type=self.device_object_type
687687
)
688688
field # To silence ruff error
689689

netbox_custom_objects/tests/test_models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def test_custom_object_type_field_object_type_validation(self):
303303
field.full_clean()
304304

305305
# Should not allow related_object_type for non-object field
306-
device_ct = self.get_device_content_type()
306+
device_ct = self.get_device_object_type()
307307
with self.assertRaises(ValidationError):
308308
field = CustomObjectTypeField(
309309
custom_object_type=self.custom_object_type,
@@ -463,7 +463,7 @@ def setUpTestData(cls):
463463
choice_set=choice_set,
464464
)
465465

466-
site_ct = cls.get_site_content_type()
466+
site_ct = cls.get_site_object_type()
467467
cls.create_custom_object_type_field(
468468
cls.custom_object_type,
469469
name="site",
@@ -472,7 +472,7 @@ def setUpTestData(cls):
472472
related_object_type=site_ct,
473473
)
474474

475-
site_ct = cls.get_site_content_type()
475+
site_ct = cls.get_site_object_type()
476476
cls.create_custom_object_type_field(
477477
cls.custom_object_type,
478478
name="sites",
@@ -492,7 +492,7 @@ def setUp(self):
492492
def test_custom_object_creation(self):
493493
"""Test creating a custom object instance."""
494494
now = timezone.now()
495-
site_ct = self.get_site_content_type()
495+
site_ct = self.get_site_object_type()
496496
site = site_ct.model_class().objects.create()
497497

498498
instance = self.model.objects.create(

0 commit comments

Comments
 (0)