|
1 | 1 | import pytest |
2 | | -from dcim.models import Device |
| 2 | +from dcim.models import Device, DeviceType |
| 3 | +from django.contrib.contenttypes.models import ContentType |
3 | 4 | from django.db import connection |
4 | 5 | from django.db.models import BigIntegerField |
5 | 6 | from django.db.models.fields.json import KeyTextTransform |
6 | 7 | from django.db.models.functions import Cast |
| 8 | +from extras.models import CustomField |
7 | 9 | from factories import DeviceFactory, NameSetDBFactory, SerializerDBFactory |
8 | 10 |
|
9 | 11 | from validity.models import NameSet |
10 | 12 | from validity.models.serializer import Serializer |
11 | | -from validity.utils.orm import CustomPrefetchMixin, QuerySetMap |
| 13 | +from validity.utils.orm import CustomFieldBuilder, CustomPrefetchMixin, QuerySetMap |
12 | 14 |
|
13 | 15 |
|
14 | 16 | @pytest.mark.parametrize("attrib", ["pk", "name"]) |
@@ -55,3 +57,53 @@ def test_custom_postfetch(monkeypatch): |
55 | 57 | NameSetDBFactory(name=f"ns{i}") |
56 | 58 | for device in custom_qs: |
57 | 59 | assert device.name.replace("dev", "ns") == device.nameset.name |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.django_db |
| 63 | +def test_custom_field_builder_creates_object_field(): |
| 64 | + serializer_ct = ContentType.objects.get_for_model(Serializer) |
| 65 | + device_ct = ContentType.objects.get_for_model(Device) |
| 66 | + cf_builder = CustomFieldBuilder(cf_model=CustomField, content_type_model=ContentType) |
| 67 | + |
| 68 | + custom_field = cf_builder.create( |
| 69 | + name="validity_test_serializer", |
| 70 | + label="Validity Test Serializer", |
| 71 | + type="object", |
| 72 | + required=False, |
| 73 | + object_type=serializer_ct, |
| 74 | + bind_to=[Device], |
| 75 | + ) |
| 76 | + |
| 77 | + custom_field.refresh_from_db() |
| 78 | + assert custom_field.name == "validity_test_serializer" |
| 79 | + assert custom_field.related_object_type == serializer_ct |
| 80 | + assert list(custom_field.object_types.all()) == [device_ct] |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.django_db |
| 84 | +def test_custom_field_builder_reuses_existing_field(): |
| 85 | + cf_builder = CustomFieldBuilder(cf_model=CustomField, content_type_model=ContentType) |
| 86 | + device_type_ct = ContentType.objects.get_for_model(DeviceType) |
| 87 | + |
| 88 | + original = cf_builder.create( |
| 89 | + name="validity_test_reused", |
| 90 | + label="Original Label", |
| 91 | + type="text", |
| 92 | + required=False, |
| 93 | + bind_to=[Device], |
| 94 | + ) |
| 95 | + reused = cf_builder.create( |
| 96 | + name="validity_test_reused", |
| 97 | + label="Changed Label", |
| 98 | + type="boolean", |
| 99 | + required=True, |
| 100 | + bind_to=[DeviceType], |
| 101 | + ) |
| 102 | + |
| 103 | + original.refresh_from_db() |
| 104 | + assert reused == original |
| 105 | + assert CustomField.objects.filter(name="validity_test_reused").count() == 1 |
| 106 | + assert original.label == "Changed Label" |
| 107 | + assert original.type == "boolean" |
| 108 | + assert original.required is True |
| 109 | + assert list(original.object_types.all()) == [device_type_ct] |
0 commit comments