improve EnumField Doc and add quick test
This commit is contained in:
parent
2f4464ead5
commit
8ef7213426
@ -1623,7 +1623,9 @@ class BinaryField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class EnumField(BaseField):
|
class EnumField(BaseField):
|
||||||
"""Enumeration Field. Values are stored underneath as strings.
|
"""Enumeration Field. Values are stored underneath as is,
|
||||||
|
so it will only work with simple types (str, int, etc) that
|
||||||
|
are bson encodable
|
||||||
Example usage:
|
Example usage:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
from bson import InvalidDocument
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
@ -105,3 +106,17 @@ class TestIntEnumField(MongoDBTestCase):
|
|||||||
|
|
||||||
with pytest.raises(ValidationError, match="Value must be one of"):
|
with pytest.raises(ValidationError, match="Value must be one of"):
|
||||||
ModelWithColor(color="wrong_type").validate()
|
ModelWithColor(color="wrong_type").validate()
|
||||||
|
|
||||||
|
|
||||||
|
class TestFunkyEnumField(MongoDBTestCase):
|
||||||
|
def test_enum_incompatible_bson_type_fails_during_save(self):
|
||||||
|
class FunkyColor(Enum):
|
||||||
|
YELLOW = object()
|
||||||
|
|
||||||
|
class ModelWithFunkyColor(Document):
|
||||||
|
color = EnumField(FunkyColor)
|
||||||
|
|
||||||
|
m = ModelWithFunkyColor(color=FunkyColor.YELLOW)
|
||||||
|
|
||||||
|
with pytest.raises(InvalidDocument, match="cannot encode object"):
|
||||||
|
m.save()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user