Fix pytest-coverage setup
This commit is contained in:
parent
7fc85108cf
commit
450fe0949e
7 changed files with 115 additions and 102 deletions
|
|
@ -5,7 +5,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator
|
|||
from django.db import models
|
||||
|
||||
from servala.core.crd import generate_custom_form_class
|
||||
from servala.core.crd.forms import DEFAULT_FIELD_CONFIGS, MANDATORY_FIELDS
|
||||
from servala.core.crd.forms import DEFAULT_FIELD_CONFIGS
|
||||
from servala.core.forms import ServiceDefinitionAdminForm
|
||||
from servala.core.models import ControlPlaneCRD
|
||||
|
||||
|
|
@ -806,10 +806,10 @@ def test_two_element_choices_work_correctly():
|
|||
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 field["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]
|
||||
|
|
@ -836,10 +836,10 @@ def test_empty_choices_fail_validation():
|
|||
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 field["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])
|
||||
|
|
@ -867,10 +867,10 @@ def test_three_plus_element_choices_fail_validation():
|
|||
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 field["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])
|
||||
|
|
@ -936,88 +936,6 @@ def test_field_with_default_config_can_override_defaults():
|
|||
assert name_field.help_text == DEFAULT_FIELD_CONFIGS["name"]["help_text"]
|
||||
|
||||
|
||||
def test_admin_form_validates_mandatory_fields_present():
|
||||
|
||||
mock_crd = Mock()
|
||||
mock_crd.resource_schema = {
|
||||
"properties": {
|
||||
"spec": {
|
||||
"properties": {
|
||||
"environment": {
|
||||
"type": "string",
|
||||
"enum": ["dev", "prod"],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config_without_name = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "choice",
|
||||
"label": "Environment",
|
||||
"controlplane_field_mapping": "spec.environment",
|
||||
"choices": [["dev", "Development"]],
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
errors = []
|
||||
included_mappings = set()
|
||||
for fieldset in config_without_name.get("fieldsets", []):
|
||||
for field in fieldset.get("fields", []):
|
||||
mapping = field.get("controlplane_field_mapping")
|
||||
included_mappings.add(mapping)
|
||||
|
||||
for mandatory_field in MANDATORY_FIELDS:
|
||||
if mandatory_field not in included_mappings:
|
||||
errors.append(f"Required field '{mandatory_field}' must be included")
|
||||
|
||||
assert len(errors) > 0
|
||||
assert "name" in str(errors[0]).lower()
|
||||
|
||||
|
||||
def test_admin_form_validates_fields_without_defaults_need_label_and_type():
|
||||
config_with_incomplete_field = {
|
||||
"fieldsets": [
|
||||
{
|
||||
"fields": [
|
||||
{"controlplane_field_mapping": "name"}, # Has defaults - OK
|
||||
{
|
||||
"controlplane_field_mapping": "spec.unknown", # No defaults
|
||||
# Missing label and type
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
errors = []
|
||||
|
||||
for fieldset in config_with_incomplete_field.get("fieldsets", []):
|
||||
for field in fieldset.get("fields", []):
|
||||
mapping = field.get("controlplane_field_mapping")
|
||||
|
||||
if mapping not in DEFAULT_FIELD_CONFIGS:
|
||||
if not field.get("label"):
|
||||
errors.append(
|
||||
f"Field with mapping '{mapping}' must have a 'label' property"
|
||||
)
|
||||
if not field.get("type"):
|
||||
errors.append(
|
||||
f"Field with mapping '{mapping}' must have a 'type' property"
|
||||
)
|
||||
|
||||
assert len(errors) == 2
|
||||
assert any("label" in str(e) for e in errors)
|
||||
assert any("type" in str(e) for e in errors)
|
||||
|
||||
|
||||
def test_empty_values_dont_override_default_configs():
|
||||
|
||||
class TestModel(models.Model):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue