Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions traffic_control/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def traffic_sign_type(self):

def clean(self):
self.validate_target_model_content_schema()
self.validate_change_target_model(self.target_model, raise_exception=True)

def validate_target_model_content_schema(self):
target_models_with_content_schema = (DeviceTypeTargetModel.ADDITIONAL_SIGN,)
Expand Down
48 changes: 48 additions & 0 deletions traffic_control/tests/test_traffic_control_device_type_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest
from django import forms

from traffic_control.enums import DeviceTypeTargetModel
from traffic_control.models.common import TrafficControlDeviceType
from traffic_control.tests.factories import AdditionalSignRealFactory, TrafficControlDeviceTypeFactory


class TrafficControlDeviceTypeForm(forms.ModelForm):
class Meta:
model = TrafficControlDeviceType
fields = "__all__"


@pytest.mark.parametrize("create_related_object, expect_valid", ((False, True), (True, False)))
@pytest.mark.django_db
def test__traffic_control_device_type_admin__change_target_model(create_related_object, expect_valid):
"""
The traffic control device type form prevents an editor from altering the target
model of a traffic control device type that is currently in use. The error message
will be informative.

"""
data = {
"code": "D123",
"icon": "d123.svg",
"description": "A test device type",
"legacy_code": "123D",
"legacy_description": "A legacy description",
"target_model": DeviceTypeTargetModel.ADDITIONAL_SIGN,
}
dt = TrafficControlDeviceTypeFactory(**data)
data["target_model"] = DeviceTypeTargetModel.TRAFFIC_SIGN

if create_related_object is True:
AdditionalSignRealFactory(device_type=dt)
else:
pass

form = TrafficControlDeviceTypeForm(data, instance=dt)
if expect_valid:
assert form.is_valid() is True
form.save()
else:
assert form.is_valid() is False
with pytest.raises(ValueError):
form.save()
assert "devices related to this device type instance will become invalid" in form.errors["__all__"][0]
Loading