-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support lists as field value choices
- Loading branch information
Showing
5 changed files
with
80 additions
and
48 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
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 was deleted.
Oops, something went wrong.
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,57 @@ | ||
import pytest | ||
|
||
from enum import Enum | ||
|
||
from protean.exceptions import ValidationError | ||
from protean.fields import String | ||
|
||
|
||
def test_choices_as_enum(): | ||
"""Test choices validations for the string field""" | ||
|
||
class StatusChoices(Enum): | ||
"""Set of choices for the status""" | ||
|
||
PENDING = "Pending" | ||
SUCCESS = "Success" | ||
ERROR = "Error" | ||
|
||
status = String(max_length=10, choices=StatusChoices) | ||
assert status is not None | ||
|
||
# Test loading a value | ||
assert status._load("Pending") == "Pending" | ||
# Test loading an Enum | ||
assert status._load(StatusChoices.PENDING) == "Pending" | ||
|
||
# Test invalid value | ||
with pytest.raises(ValidationError) as e_info: | ||
status._load("Failure") | ||
|
||
assert e_info.value.messages == { | ||
"unlinked": [ | ||
"Value `'Failure'` is not a valid choice. " | ||
"Must be among ['Pending', 'Success', 'Error']" | ||
] | ||
} | ||
|
||
|
||
def test_choices_as_list(): | ||
"""Test choices validations for the string field""" | ||
|
||
status = String(max_length=10, choices=["Pending", "Success", "Error"]) | ||
assert status is not None | ||
|
||
# Test loading a value | ||
assert status._load("Pending") == "Pending" | ||
|
||
# Test invalid value | ||
with pytest.raises(ValidationError) as e_info: | ||
status._load("Failure") | ||
|
||
assert e_info.value.messages == { | ||
"unlinked": [ | ||
"Value `'Failure'` is not a valid choice. " | ||
"Must be among ['Pending', 'Success', 'Error']" | ||
] | ||
} |
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