Skip to content

Commit

Permalink
✨ Recursively check TextChoicesWithDescriptions subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Coperh committed Dec 11, 2023
1 parent 1bea376 commit aeb2bef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
28 changes: 27 additions & 1 deletion tests/test_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_add_choice_values_help_text_with_descriptions():
assert help_text == expected_text


def test_add_choice_values_help_text_with_asdsadasdescriptions():
def test_text_choice_with_descriptions_validator():
class BadChoicesWithDescriptions(TextChoicesWithDescriptions):
option1 = "option1", "option one name"
option2 = "option2", "option two name"
Expand All @@ -67,3 +67,29 @@ def descriptions(cls):
),
):
ensure_description_exists(BadChoicesWithDescriptions)


def test_text_choice_with_descriptions_validator_recursion():
class TextChoiceSubclass(TextChoicesWithDescriptions):
@classmethod
def descriptions(cls):
return {}

class BadChoicesSubclassWithDescriptions(TextChoiceSubclass):
option1 = "option1", "option one name"
option2 = "option2", "option two name"

@classmethod
def descriptions(cls):
return {
ChoicesWithDescriptions.option1: "Option one description",
# ChoicesWithDescriptions.option2: "Description of option two",
}

with pytest.raises(
ValueError,
match=re.escape(
"Choice (option2, option two name) in BadChoicesSubclassWithDescriptions is missing a description"
),
):
ensure_description_exists(BadChoicesSubclassWithDescriptions)
10 changes: 6 additions & 4 deletions vng_api_common/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def ready(self):
register_serializer_field()
set_custom_hyperlinkedmodelserializer_field()
set_charfield_error_messages()
ensure_text_choice_descriptions()
ensure_text_choice_descriptions(TextChoicesWithDescriptions)


def patch_duration_type():
Expand Down Expand Up @@ -92,6 +92,8 @@ def set_charfield_error_messages():
)


def ensure_text_choice_descriptions():
for cls in TextChoicesWithDescriptions.__subclasses__():
ensure_description_exists(cls)
def ensure_text_choice_descriptions(text_choice_class):
ensure_description_exists(text_choice_class)

for cls in text_choice_class.__subclasses__():
ensure_text_choice_descriptions(cls)

0 comments on commit aeb2bef

Please sign in to comment.