Skip to content

Commit

Permalink
Merge pull request #2406 from bagerard/improve_enumfield_doc
Browse files Browse the repository at this point in the history
improve EnumField Doc and add quick test
  • Loading branch information
bagerard committed Nov 1, 2020
2 parents 2f4464e + 94a7e81 commit ee664f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,9 @@ def prepare_query_value(self, op, value):


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:
.. code-block:: python
Expand Down
15 changes: 15 additions & 0 deletions tests/fields/test_enum_field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum

from bson import InvalidDocument
import pytest

from mongoengine import *
Expand Down Expand Up @@ -105,3 +106,17 @@ def test_validate_model(self):

with pytest.raises(ValidationError, match="Value must be one of"):
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="[cC]annot encode object"):
m.save()

0 comments on commit ee664f0

Please sign in to comment.