|
7 | 7 |
|
8 | 8 | from circuits.models import Provider |
9 | 9 | from dcim.models import DeviceRole, DeviceType, Manufacturer, Platform, Rack, Region, Site, SiteGroup |
10 | | -from extras.choices import JournalEntryKindChoices, ObjectChangeActionChoices |
| 10 | +from extras.choices import ( |
| 11 | + CustomFieldTypeChoices, CustomFieldFilterLogicChoices, JournalEntryKindChoices, ObjectChangeActionChoices, |
| 12 | +) |
11 | 13 | from extras.filtersets import * |
12 | 14 | from extras.models import * |
13 | 15 | from ipam.models import IPAddress |
|
16 | 18 | from virtualization.models import Cluster, ClusterGroup, ClusterType |
17 | 19 |
|
18 | 20 |
|
| 21 | +class CustomFieldTestCase(TestCase, BaseFilterSetTests): |
| 22 | + queryset = CustomField.objects.all() |
| 23 | + filterset = CustomFieldFilterSet |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def setUpTestData(cls): |
| 27 | + content_types = ContentType.objects.filter(model__in=['site', 'rack', 'device']) |
| 28 | + |
| 29 | + custom_fields = ( |
| 30 | + CustomField( |
| 31 | + name='Custom Field 1', |
| 32 | + type=CustomFieldTypeChoices.TYPE_TEXT, |
| 33 | + required=True, |
| 34 | + weight=100, |
| 35 | + filter_logic=CustomFieldFilterLogicChoices.FILTER_LOOSE |
| 36 | + ), |
| 37 | + CustomField( |
| 38 | + name='Custom Field 2', |
| 39 | + type=CustomFieldTypeChoices.TYPE_INTEGER, |
| 40 | + required=False, |
| 41 | + weight=200, |
| 42 | + filter_logic=CustomFieldFilterLogicChoices.FILTER_EXACT |
| 43 | + ), |
| 44 | + CustomField( |
| 45 | + name='Custom Field 3', |
| 46 | + type=CustomFieldTypeChoices.TYPE_BOOLEAN, |
| 47 | + required=False, |
| 48 | + weight=300, |
| 49 | + filter_logic=CustomFieldFilterLogicChoices.FILTER_DISABLED |
| 50 | + ), |
| 51 | + ) |
| 52 | + CustomField.objects.bulk_create(custom_fields) |
| 53 | + custom_fields[0].content_types.add(content_types[0]) |
| 54 | + custom_fields[1].content_types.add(content_types[1]) |
| 55 | + custom_fields[2].content_types.add(content_types[2]) |
| 56 | + |
| 57 | + def test_name(self): |
| 58 | + params = {'name': ['Custom Field 1', 'Custom Field 2']} |
| 59 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) |
| 60 | + |
| 61 | + def test_content_types(self): |
| 62 | + params = {'content_types': 'dcim.site'} |
| 63 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) |
| 64 | + params = {'content_type_id': [ContentType.objects.get_for_model(Site).pk]} |
| 65 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) |
| 66 | + |
| 67 | + def test_required(self): |
| 68 | + params = {'required': True} |
| 69 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) |
| 70 | + |
| 71 | + def test_weight(self): |
| 72 | + params = {'weight': [100, 200]} |
| 73 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) |
| 74 | + |
| 75 | + def test_filter_logic(self): |
| 76 | + params = {'filter_logic': CustomFieldFilterLogicChoices.FILTER_LOOSE} |
| 77 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) |
| 78 | + |
| 79 | + |
19 | 80 | class WebhookTestCase(TestCase, BaseFilterSetTests): |
20 | 81 | queryset = Webhook.objects.all() |
21 | 82 | filterset = WebhookFilterSet |
@@ -62,6 +123,8 @@ def test_name(self): |
62 | 123 | def test_content_types(self): |
63 | 124 | params = {'content_types': 'dcim.site'} |
64 | 125 | self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) |
| 126 | + params = {'content_type_id': [ContentType.objects.get_for_model(Site).pk]} |
| 127 | + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) |
65 | 128 |
|
66 | 129 | def test_type_create(self): |
67 | 130 | params = {'type_create': True} |
|
0 commit comments