|
| 1 | +from unittest import skipIf |
| 2 | + |
| 3 | +from django.conf import settings |
1 | 4 | from django.test import TestCase |
2 | 5 |
|
3 | 6 | from core.models import AutoSyncRecord, DataSource |
| 7 | +from dcim.models import Site |
4 | 8 | from extras.models import CustomLink |
| 9 | +from ipam.models import Prefix |
5 | 10 | from netbox.models.features import get_model_features, has_feature, model_is_public |
6 | | -from netbox.tests.dummy_plugin.models import DummyModel |
7 | 11 | from taggit.models import Tag |
8 | 12 |
|
9 | 13 |
|
10 | 14 | class ModelFeaturesTestCase(TestCase): |
| 15 | + """ |
| 16 | + A test case class for verifying model features and utility functions. |
| 17 | + """ |
11 | 18 |
|
| 19 | + @skipIf('netbox.tests.dummy_plugin' not in settings.PLUGINS, 'dummy_plugin not in settings.PLUGINS') |
12 | 20 | def test_model_is_public(self): |
13 | 21 | """ |
14 | 22 | Test that the is_public() utility function returns True for public models only. |
15 | 23 | """ |
| 24 | + from netbox.tests.dummy_plugin.models import DummyModel |
| 25 | + |
16 | 26 | # Public model |
17 | 27 | self.assertFalse(hasattr(DataSource, '_netbox_private')) |
18 | 28 | self.assertTrue(model_is_public(DataSource)) |
@@ -51,3 +61,53 @@ def test_get_model_features(self): |
51 | 61 | features = get_model_features(CustomLink) |
52 | 62 | self.assertIn('cloning', features) |
53 | 63 | self.assertNotIn('bookmarks', features) |
| 64 | + |
| 65 | + def test_cloningmixin_injects_gfk_attribute(self): |
| 66 | + """ |
| 67 | + Tests the cloning mixin with GFK attribute injection in the `clone` method. |
| 68 | +
|
| 69 | + This test validates that the `clone` method correctly handles |
| 70 | + and retains the General Foreign Key (GFK) attributes on an |
| 71 | + object when the cloning fields are explicitly defined. |
| 72 | + """ |
| 73 | + site = Site.objects.create(name='Test Site', slug='test-site') |
| 74 | + prefix = Prefix.objects.create(prefix='10.0.0.0/24', scope=site) |
| 75 | + |
| 76 | + original_clone_fields = getattr(Prefix, 'clone_fields', None) |
| 77 | + try: |
| 78 | + Prefix.clone_fields = ('scope_type', 'scope_id') |
| 79 | + attrs = prefix.clone() |
| 80 | + |
| 81 | + self.assertEqual(attrs['scope_type'], prefix.scope_type_id) |
| 82 | + self.assertEqual(attrs['scope_id'], prefix.scope_id) |
| 83 | + self.assertEqual(attrs['scope'], prefix.scope_id) |
| 84 | + finally: |
| 85 | + if original_clone_fields is None: |
| 86 | + delattr(Prefix, 'clone_fields') |
| 87 | + else: |
| 88 | + Prefix.clone_fields = original_clone_fields |
| 89 | + |
| 90 | + def test_cloningmixin_does_not_inject_gfk_attribute_if_incomplete(self): |
| 91 | + """ |
| 92 | + Tests the cloning mixin with incomplete cloning fields does not inject the GFK attribute. |
| 93 | +
|
| 94 | + This test validates that the `clone` method correctly handles |
| 95 | + the case where the cloning fields are incomplete, ensuring that |
| 96 | + the generic foreign key (GFK) attribute is not injected during |
| 97 | + the cloning process. |
| 98 | + """ |
| 99 | + site = Site.objects.create(name='Test Site', slug='test-site') |
| 100 | + prefix = Prefix.objects.create(prefix='10.0.0.0/24', scope=site) |
| 101 | + |
| 102 | + original_clone_fields = getattr(Prefix, 'clone_fields', None) |
| 103 | + try: |
| 104 | + Prefix.clone_fields = ('scope_type',) |
| 105 | + attrs = prefix.clone() |
| 106 | + |
| 107 | + self.assertIn('scope_type', attrs) |
| 108 | + self.assertNotIn('scope', attrs) |
| 109 | + finally: |
| 110 | + if original_clone_fields is None: |
| 111 | + delattr(Prefix, 'clone_fields') |
| 112 | + else: |
| 113 | + Prefix.clone_fields = original_clone_fields |
0 commit comments