74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
|
|
from unittest.mock import Mock
|
||
|
|
|
||
|
|
from django.db import models
|
||
|
|
|
||
|
|
from servala.core.crd import generate_custom_form_class
|
||
|
|
from servala.core.models import ControlPlaneCRD
|
||
|
|
|
||
|
|
|
||
|
|
def test_custom_model_form_class_is_none_when_no_form_config():
|
||
|
|
crd = Mock(spec=ControlPlaneCRD)
|
||
|
|
service_def = Mock()
|
||
|
|
service_def.form_config = None
|
||
|
|
crd.service_definition = service_def
|
||
|
|
crd.django_model = Mock()
|
||
|
|
|
||
|
|
if not (
|
||
|
|
crd.django_model
|
||
|
|
and crd.service_definition
|
||
|
|
and crd.service_definition.form_config
|
||
|
|
and crd.service_definition.form_config.get("fieldsets")
|
||
|
|
):
|
||
|
|
result = None
|
||
|
|
else:
|
||
|
|
result = generate_custom_form_class(
|
||
|
|
crd.service_definition.form_config, crd.django_model
|
||
|
|
)
|
||
|
|
|
||
|
|
assert result is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_custom_model_form_class_returns_class_when_form_config_exists():
|
||
|
|
|
||
|
|
crd = Mock(spec=ControlPlaneCRD)
|
||
|
|
service_def = Mock()
|
||
|
|
service_def.form_config = {
|
||
|
|
"fieldsets": [
|
||
|
|
{
|
||
|
|
"title": "General",
|
||
|
|
"fields": [
|
||
|
|
{
|
||
|
|
"type": "text",
|
||
|
|
"label": "Name",
|
||
|
|
"controlplane_field_mapping": "name",
|
||
|
|
"required": True,
|
||
|
|
}
|
||
|
|
],
|
||
|
|
}
|
||
|
|
]
|
||
|
|
}
|
||
|
|
crd.service_definition = service_def
|
||
|
|
|
||
|
|
class TestModel(models.Model):
|
||
|
|
name = models.CharField(max_length=100)
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
app_label = "test"
|
||
|
|
|
||
|
|
crd.django_model = TestModel
|
||
|
|
|
||
|
|
if not (
|
||
|
|
crd.django_model
|
||
|
|
and crd.service_definition
|
||
|
|
and crd.service_definition.form_config
|
||
|
|
and crd.service_definition.form_config.get("fieldsets")
|
||
|
|
):
|
||
|
|
result = None
|
||
|
|
else:
|
||
|
|
result = generate_custom_form_class(
|
||
|
|
crd.service_definition.form_config, crd.django_model
|
||
|
|
)
|
||
|
|
|
||
|
|
assert result is not None
|
||
|
|
assert hasattr(result, "form_config")
|