-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix mapping for choice values #8968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auvipy
merged 7 commits into
encode:master
from
saadullahaleem:feature/asymmetric-related-field
May 3, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af08f67
fix mapping for choice values
saadullahaleem 5b5cfdc
fix tests for ChoiceField IntegerChoices
saadullahaleem 4b9bdb0
fix imports
saadullahaleem 2d26752
fix imports in tests
saadullahaleem f95dc69
Check for multiple choice enums
saadullahaleem 960bebd
fix formatting
saadullahaleem 301087d
add tests for text choices class
saadullahaleem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -5,11 +5,13 @@ | |
import sys | ||
import uuid | ||
from decimal import ROUND_DOWN, ROUND_UP, Decimal | ||
from enum import auto | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import pytz | ||
from django.core.exceptions import ValidationError as DjangoValidationError | ||
from django.db.models import IntegerChoices, TextChoices | ||
from django.http import QueryDict | ||
from django.test import TestCase, override_settings | ||
from django.utils.timezone import activate, deactivate, override | ||
|
@@ -1824,6 +1826,54 @@ def test_edit_choices(self): | |
field.run_validation(2) | ||
assert exc_info.value.detail == ['"2" is not a valid choice.'] | ||
|
||
def test_integer_choices(self): | ||
class ChoiceCase(IntegerChoices): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add test for TextChoices too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @auvipy added |
||
first = auto() | ||
second = auto() | ||
# Enum validate | ||
choices = [ | ||
(ChoiceCase.first, "1"), | ||
(ChoiceCase.second, "2") | ||
] | ||
|
||
field = serializers.ChoiceField(choices=choices) | ||
assert field.run_validation(1) == 1 | ||
assert field.run_validation(ChoiceCase.first) == 1 | ||
assert field.run_validation("1") == 1 | ||
|
||
choices = [ | ||
(ChoiceCase.first.value, "1"), | ||
(ChoiceCase.second.value, "2") | ||
] | ||
|
||
field = serializers.ChoiceField(choices=choices) | ||
assert field.run_validation(1) == 1 | ||
assert field.run_validation(ChoiceCase.first) == 1 | ||
assert field.run_validation("1") == 1 | ||
|
||
def test_text_choices(self): | ||
class ChoiceCase(TextChoices): | ||
first = auto() | ||
second = auto() | ||
# Enum validate | ||
choices = [ | ||
(ChoiceCase.first, "first"), | ||
(ChoiceCase.second, "second") | ||
] | ||
|
||
field = serializers.ChoiceField(choices=choices) | ||
assert field.run_validation(ChoiceCase.first) == "first" | ||
assert field.run_validation("first") == "first" | ||
|
||
choices = [ | ||
(ChoiceCase.first.value, "first"), | ||
(ChoiceCase.second.value, "second") | ||
] | ||
|
||
field = serializers.ChoiceField(choices=choices) | ||
assert field.run_validation(ChoiceCase.first) == "first" | ||
assert field.run_validation("first") == "first" | ||
|
||
|
||
class TestChoiceFieldWithType(FieldValues): | ||
""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have one concerns/question. should we consider both Int and text value as string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've typecasted both the
__str__
representation ofvalue
andvalue.value
for the check here.