forked from VNG-Realisatie/vng-api-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.db.models import TextChoices | ||
|
||
from vng_api_common.choices import ( | ||
TextChoicesWithDescriptions, | ||
ensure_description_exists, | ||
) | ||
from vng_api_common.serializers import add_choice_values_help_text | ||
|
||
|
||
class NormalChoices(TextChoices): | ||
option1 = "option1", "option one name" | ||
option2 = "option2", "option two name" | ||
|
||
|
||
class ChoicesWithDescriptions(TextChoicesWithDescriptions): | ||
option1 = "option1", "option one name" | ||
option2 = "option2", "option two name" | ||
|
||
@staticmethod | ||
def get_descriptions(): | ||
return { | ||
ChoicesWithDescriptions.option1: "Option one description", | ||
ChoicesWithDescriptions.option2: "Description of option two", | ||
} | ||
|
||
|
||
def test_add_choice_values_help_text(): | ||
help_text = add_choice_values_help_text(NormalChoices) | ||
expected_text = ( | ||
"Uitleg bij mogelijke waarden:\n\n" | ||
"* `option1` - option one name\n" | ||
"* `option2` - option two name" | ||
) | ||
assert help_text == expected_text | ||
|
||
|
||
def test_add_choice_values_help_text_with_descriptions(): | ||
help_text = add_choice_values_help_text(ChoicesWithDescriptions) | ||
expected_text = ( | ||
"Uitleg bij mogelijke waarden:\n\n" | ||
"* `option1` - (option one name) Option one description\n" | ||
"* `option2` - (option two name) Description of option two" | ||
) | ||
assert help_text == expected_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.db.models import TextChoices | ||
|
||
|
||
def ensure_description_exists(cls): | ||
descriptions = cls.get_descriptions() | ||
|
||
for choice in cls.choices: | ||
assert choice in descriptions | ||
|
||
return cls | ||
|
||
|
||
class TextChoicesWithDescriptions(TextChoices): | ||
@classmethod | ||
def get_descriptions(cls): | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters