Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics) Creates ChoicesEnumType
- Loading branch information
1 parent
9ba2969
commit 9ff66f1
Showing
5 changed files
with
42 additions
and
0 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
Empty file.
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,22 @@ | ||
# coding: utf-8 | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from choicesenum import ChoicesEnum | ||
from schematics.types import BaseType, ValidationError | ||
|
||
|
||
class ChoicesEnumType(BaseType): | ||
def __init__(self, type_): | ||
if not issubclass(type_, ChoicesEnum): | ||
raise ValidationError(self.Messages.INVALID_TYPE) | ||
super(ChoicesEnumType, self).__init__(type_) | ||
self.type = type_ | ||
|
||
def to_native(self, value, context=None): | ||
return self.type(value) | ||
|
||
def to_primitive(self, value, context=None): | ||
return self.type(value).value | ||
|
||
class Messages: | ||
INVALID_TYPE = 'Expected a ChoicesEnum sub type' |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ pytest==3.10.1 | |
pytest-cov==2.6.0 | ||
pytest-watch==4.2.0 | ||
pytest-sugar==0.9.2 | ||
schematics==1.1.3 |
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,18 @@ | ||
# coding: utf-8 | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import pytest | ||
from schematics.exceptions import ValidationError | ||
from choicesenum.schematics.types import ChoicesEnumType | ||
|
||
|
||
def test_choices_enum_type(http_statuses): | ||
state = ChoicesEnumType(http_statuses) | ||
assert state.to_native(200) is http_statuses.OK | ||
assert state.to_primitive(200) == http_statuses.OK.value | ||
|
||
|
||
def test_choices_enum_type_should_throw_exception(): | ||
with pytest.raises(ValidationError) as e: | ||
ChoicesEnumType(object) | ||
assert e.value.message[0] == ChoicesEnumType.Messages.INVALID_TYPE |