Coerce string-numbers to numbers in admin form
This commit is contained in:
parent
ece60ad3b1
commit
1ed261d4b2
2 changed files with 155 additions and 0 deletions
|
|
@ -633,3 +633,119 @@ def test_default_value_not_override_existing_instance():
|
|||
|
||||
assert form.initial["name"] == "existing-name"
|
||||
assert form.initial["port"] == 3000
|
||||
|
||||
|
||||
def test_form_config_coerces_string_numbers_to_integers():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
schema = form.form_config_schema
|
||||
|
||||
config_with_string_numbers = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "text",
|
||||
"label": "Service Name",
|
||||
"controlplane_field_mapping": "spec.serviceName",
|
||||
"max_length": "64", # String instead of integer
|
||||
"required": True,
|
||||
},
|
||||
{
|
||||
"type": "textarea",
|
||||
"label": "Description",
|
||||
"controlplane_field_mapping": "spec.description",
|
||||
"rows": "5", # String instead of integer
|
||||
"max_length": "500", # String instead of integer
|
||||
},
|
||||
{
|
||||
"type": "number",
|
||||
"label": "Port",
|
||||
"controlplane_field_mapping": "spec.port",
|
||||
"min_value": "1", # String instead of integer
|
||||
"max_value": "65535", # String instead of integer
|
||||
},
|
||||
{
|
||||
"type": "array",
|
||||
"label": "Tags",
|
||||
"controlplane_field_mapping": "spec.tags",
|
||||
"min_values": "0", # String instead of integer
|
||||
"max_values": "10", # String instead of integer
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
normalized_config = form._normalize_form_config_types(config_with_string_numbers)
|
||||
fields = normalized_config["fieldsets"][0]["fields"]
|
||||
|
||||
assert fields[0]["max_length"] == 64
|
||||
assert isinstance(fields[0]["max_length"], int)
|
||||
|
||||
assert fields[1]["rows"] == 5
|
||||
assert isinstance(fields[1]["rows"], int)
|
||||
assert fields[1]["max_length"] == 500
|
||||
assert isinstance(fields[1]["max_length"], int)
|
||||
|
||||
assert fields[2]["min_value"] == 1
|
||||
assert isinstance(fields[2]["min_value"], int)
|
||||
assert fields[2]["max_value"] == 65535
|
||||
assert isinstance(fields[2]["max_value"], int)
|
||||
|
||||
assert fields[3]["min_values"] == 0
|
||||
assert isinstance(fields[3]["min_values"], int)
|
||||
assert fields[3]["max_values"] == 10
|
||||
assert isinstance(fields[3]["max_values"], int)
|
||||
|
||||
jsonschema.validate(instance=normalized_config, schema=schema)
|
||||
|
||||
|
||||
def test_form_config_handles_float_numbers():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
|
||||
config_with_floats = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "number",
|
||||
"label": "Price",
|
||||
"controlplane_field_mapping": "spec.price",
|
||||
"min_value": "0.01", # String float
|
||||
"max_value": "999.99", # String float
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
normalized_config = form._normalize_form_config_types(config_with_floats)
|
||||
field = normalized_config["fieldsets"][0]["fields"][0]
|
||||
|
||||
assert field["min_value"] == 0.01
|
||||
assert isinstance(field["min_value"], float)
|
||||
assert field["max_value"] == 999.99
|
||||
assert isinstance(field["max_value"], float)
|
||||
|
||||
|
||||
def test_form_config_handles_empty_string_as_none():
|
||||
form = ServiceDefinitionAdminForm()
|
||||
|
||||
config_with_empty_strings = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "text",
|
||||
"label": "Name",
|
||||
"controlplane_field_mapping": "name",
|
||||
"max_length": "", # Empty string
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
normalized_config = form._normalize_form_config_types(config_with_empty_strings)
|
||||
field = normalized_config["fieldsets"][0]["fields"][0]
|
||||
assert field["max_length"] is None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue