Implicitly fix admin choices configuration
This commit is contained in:
parent
1ed261d4b2
commit
6182b36daf
2 changed files with 181 additions and 2 deletions
|
|
@ -749,3 +749,158 @@ def test_form_config_handles_empty_string_as_none():
|
|||
normalized_config = form._normalize_form_config_types(config_with_empty_strings)
|
||||
field = normalized_config["fieldsets"][0]["fields"][0]
|
||||
assert field["max_length"] is None
|
||||
|
||||
|
||||
def test_single_element_choices_are_normalized():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
mock_crd = Mock()
|
||||
mock_crd.resource_schema = {
|
||||
"properties": {
|
||||
"spec": {
|
||||
"properties": {
|
||||
"version": {
|
||||
"type": "string",
|
||||
"enum": ["6.2", "7.0", "7.2"],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config_with_single_choices = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "text",
|
||||
"label": "Name",
|
||||
"controlplane_field_mapping": "name",
|
||||
},
|
||||
{
|
||||
"type": "choice",
|
||||
"label": "Version",
|
||||
"controlplane_field_mapping": "spec.version",
|
||||
"choices": [["6.2"]], # Single element - should be transformed
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
spec_schema = mock_crd.resource_schema["properties"]["spec"]
|
||||
errors = []
|
||||
|
||||
for field in config_with_single_choices["fieldsets"][0]["fields"]:
|
||||
if field.get("type") == "choice":
|
||||
form._validate_choice_field(
|
||||
field, field["controlplane_field_mapping"], spec_schema, "spec", errors
|
||||
)
|
||||
|
||||
assert len(errors) == 0, f"Expected no errors but got: {errors}"
|
||||
version_field = config_with_single_choices["fieldsets"][0]["fields"][1]
|
||||
assert version_field["choices"] == [["6.2", "6.2"]]
|
||||
|
||||
|
||||
def test_two_element_choices_work_correctly():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
mock_crd = Mock()
|
||||
mock_crd.resource_schema = {
|
||||
"properties": {
|
||||
"spec": {
|
||||
"properties": {
|
||||
"version": {
|
||||
"type": "string",
|
||||
"enum": ["6.2", "7.0"],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config_with_proper_choices = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "choice",
|
||||
"label": "Version",
|
||||
"controlplane_field_mapping": "spec.version",
|
||||
"choices": [["6.2", "Version 6.2"], ["7.0", "Version 7.0"]],
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
spec_schema = mock_crd.resource_schema["properties"]["spec"]
|
||||
errors = []
|
||||
|
||||
for field in config_with_proper_choices["fieldsets"][0]["fields"]:
|
||||
if field.get("type") == "choice":
|
||||
form._validate_choice_field(
|
||||
field, field["controlplane_field_mapping"], spec_schema, "spec", errors
|
||||
)
|
||||
|
||||
assert len(errors) == 0, f"Expected no errors but got: {errors}"
|
||||
version_field = config_with_proper_choices["fieldsets"][0]["fields"][0]
|
||||
assert version_field["choices"] == [["6.2", "Version 6.2"], ["7.0", "Version 7.0"]]
|
||||
|
||||
|
||||
def test_empty_choices_fail_validation():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
config_with_empty_choice = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "choice",
|
||||
"label": "Version",
|
||||
"controlplane_field_mapping": "spec.version",
|
||||
"choices": [[]], # Empty choice - invalid
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
errors = []
|
||||
|
||||
for field in config_with_empty_choice["fieldsets"][0]["fields"]:
|
||||
if field.get("type") == "choice":
|
||||
form._validate_choice_field(
|
||||
field, field["controlplane_field_mapping"], {}, "spec", errors
|
||||
)
|
||||
|
||||
assert len(errors) > 0
|
||||
assert "must have 1 or 2 elements" in str(errors[0])
|
||||
|
||||
|
||||
def test_three_plus_element_choices_fail_validation():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
config_with_long_choice = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "choice",
|
||||
"label": "Version",
|
||||
"controlplane_field_mapping": "spec.version",
|
||||
"choices": [
|
||||
["6.2", "Version 6.2", "Extra"]
|
||||
], # 3 elements - invalid
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
errors = []
|
||||
|
||||
for field in config_with_long_choice["fieldsets"][0]["fields"]:
|
||||
if field.get("type") == "choice":
|
||||
form._validate_choice_field(
|
||||
field, field["controlplane_field_mapping"], {}, "spec", errors
|
||||
)
|
||||
|
||||
assert len(errors) > 0
|
||||
assert "must have 1 or 2 elements" in str(errors[0])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue