|
| 1 | +# Test utilities for netbox_custom_objects plugin |
| 2 | +from django.contrib.contenttypes.models import ContentType |
| 3 | +from django.test import Client |
| 4 | +from extras.models import CustomFieldChoiceSet |
| 5 | +from utilities.testing import create_test_user |
| 6 | + |
| 7 | +from netbox_custom_objects.models import CustomObjectType, CustomObjectTypeField |
| 8 | + |
| 9 | + |
| 10 | +class CustomObjectsTestCase: |
| 11 | + """ |
| 12 | + Base test case for custom objects tests. |
| 13 | + """ |
| 14 | + |
| 15 | + @classmethod |
| 16 | + def setUpTestData(cls): |
| 17 | + """Set up test data that should be created once for the entire test class.""" |
| 18 | + pass |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + """Set up test data.""" |
| 22 | + self.user = create_test_user('testuser') |
| 23 | + self.client = Client() |
| 24 | + self.client.force_login(self.user) |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def create_custom_object_type(cls, **kwargs): |
| 28 | + """Helper method to create a custom object type.""" |
| 29 | + defaults = { |
| 30 | + 'name': 'TestObject', |
| 31 | + 'description': 'A test custom object type', |
| 32 | + 'verbose_name_plural': 'Test Objects' |
| 33 | + } |
| 34 | + defaults.update(kwargs) |
| 35 | + return CustomObjectType.objects.create(**defaults) |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def create_custom_object_type_field(cls, custom_object_type, **kwargs): |
| 39 | + """Helper method to create a custom object type field.""" |
| 40 | + defaults = { |
| 41 | + 'custom_object_type': custom_object_type, |
| 42 | + 'name': 'test_field', |
| 43 | + 'label': 'Test Field', |
| 44 | + 'type': 'text' |
| 45 | + } |
| 46 | + defaults.update(kwargs) |
| 47 | + return CustomObjectTypeField.objects.create(**defaults) |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def create_choice_set(cls, **kwargs): |
| 51 | + """Helper method to create a choice set.""" |
| 52 | + defaults = { |
| 53 | + 'name': 'Test Choice Set', |
| 54 | + 'extra_choices': [ |
| 55 | + ['choice1', 'Choice 1'], |
| 56 | + ['choice2', 'Choice 2'], |
| 57 | + ['choice3', 'Choice 3'], |
| 58 | + ] |
| 59 | + } |
| 60 | + defaults.update(kwargs) |
| 61 | + return CustomFieldChoiceSet.objects.create(**defaults) |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def get_device_content_type(cls): |
| 65 | + """Get the device content type for object field testing.""" |
| 66 | + return ContentType.objects.get(app_label='dcim', model='device') |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def get_site_content_type(cls): |
| 70 | + """Get the site content type for object field testing.""" |
| 71 | + return ContentType.objects.get(app_label='dcim', model='site') |
| 72 | + |
| 73 | + def create_simple_custom_object_type(self, **kwargs): |
| 74 | + """Create a simple custom object type with basic fields.""" |
| 75 | + custom_object_type = CustomObjectsTestCase.create_custom_object_type(**kwargs) |
| 76 | + |
| 77 | + # Add a text field as primary |
| 78 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 79 | + custom_object_type, |
| 80 | + name="name", |
| 81 | + label="Name", |
| 82 | + type="text", |
| 83 | + primary=True, |
| 84 | + required=True |
| 85 | + ) |
| 86 | + |
| 87 | + # Add a description field |
| 88 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 89 | + custom_object_type, |
| 90 | + name="description", |
| 91 | + label="Description", |
| 92 | + type="text", |
| 93 | + required=False |
| 94 | + ) |
| 95 | + |
| 96 | + return custom_object_type |
| 97 | + |
| 98 | + def create_complex_custom_object_type(self, **kwargs): |
| 99 | + """Create a complex custom object type with various field types.""" |
| 100 | + custom_object_type = CustomObjectsTestCase.create_custom_object_type(**kwargs) |
| 101 | + choice_set = CustomObjectsTestCase.create_choice_set() |
| 102 | + device_content_type = CustomObjectsTestCase.get_device_content_type() |
| 103 | + |
| 104 | + # Primary text field |
| 105 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 106 | + custom_object_type, |
| 107 | + name="name", |
| 108 | + label="Name", |
| 109 | + type="text", |
| 110 | + primary=True, |
| 111 | + required=True |
| 112 | + ) |
| 113 | + |
| 114 | + # Integer field |
| 115 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 116 | + custom_object_type, |
| 117 | + name="count", |
| 118 | + label="Count", |
| 119 | + type="integer", |
| 120 | + validation_minimum=0, |
| 121 | + validation_maximum=100 |
| 122 | + ) |
| 123 | + |
| 124 | + # Boolean field |
| 125 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 126 | + custom_object_type, |
| 127 | + name="active", |
| 128 | + label="Active", |
| 129 | + type="boolean", |
| 130 | + default=True |
| 131 | + ) |
| 132 | + |
| 133 | + # Select field |
| 134 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 135 | + custom_object_type, |
| 136 | + name="status", |
| 137 | + label="Status", |
| 138 | + type="select", |
| 139 | + choice_set=choice_set |
| 140 | + ) |
| 141 | + |
| 142 | + # Object field (device) |
| 143 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 144 | + custom_object_type, |
| 145 | + name="device", |
| 146 | + label="Device", |
| 147 | + type="object", |
| 148 | + related_object_type=device_content_type |
| 149 | + ) |
| 150 | + |
| 151 | + return custom_object_type |
| 152 | + |
| 153 | + def create_self_referential_custom_object_type(self, **kwargs): |
| 154 | + """Create a custom object type that can reference itself.""" |
| 155 | + custom_object_type = CustomObjectsTestCase.create_custom_object_type(**kwargs) |
| 156 | + |
| 157 | + # Primary text field |
| 158 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 159 | + custom_object_type, |
| 160 | + name="name", |
| 161 | + label="Name", |
| 162 | + type="text", |
| 163 | + primary=True, |
| 164 | + required=True |
| 165 | + ) |
| 166 | + |
| 167 | + return custom_object_type |
| 168 | + |
| 169 | + def create_multi_object_custom_object_type(self, **kwargs): |
| 170 | + """Create a custom object type with multi-object fields.""" |
| 171 | + 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() |
| 174 | + |
| 175 | + # Primary text field |
| 176 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 177 | + custom_object_type, |
| 178 | + name="name", |
| 179 | + label="Name", |
| 180 | + type="text", |
| 181 | + primary=True, |
| 182 | + required=True |
| 183 | + ) |
| 184 | + |
| 185 | + # Multi-object field (devices) |
| 186 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 187 | + custom_object_type, |
| 188 | + name="devices", |
| 189 | + label="Devices", |
| 190 | + type="multiobject", |
| 191 | + related_object_type=device_content_type |
| 192 | + ) |
| 193 | + |
| 194 | + # Multi-object field (sites) |
| 195 | + CustomObjectsTestCase.create_custom_object_type_field( |
| 196 | + custom_object_type, |
| 197 | + name="sites", |
| 198 | + label="Sites", |
| 199 | + type="multiobject", |
| 200 | + related_object_type=site_content_type |
| 201 | + ) |
| 202 | + |
| 203 | + return custom_object_type |
0 commit comments