|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mongoengine import * |
| 6 | +from tests.utils import MongoDBTestCase, get_as_pymongo |
| 7 | + |
| 8 | + |
| 9 | +class Status(Enum): |
| 10 | + NEW = "new" |
| 11 | + DONE = "done" |
| 12 | + |
| 13 | + |
| 14 | +class ModelWithEnum(Document): |
| 15 | + status = EnumField(Status) |
| 16 | + |
| 17 | + |
| 18 | +class TestStringEnumField(MongoDBTestCase): |
| 19 | + def test_storage(self): |
| 20 | + model = ModelWithEnum(status=Status.NEW).save() |
| 21 | + assert get_as_pymongo(model) == {"_id": model.id, "status": "new"} |
| 22 | + |
| 23 | + def test_set_enum(self): |
| 24 | + ModelWithEnum.drop_collection() |
| 25 | + ModelWithEnum(status=Status.NEW).save() |
| 26 | + assert ModelWithEnum.objects(status=Status.NEW).count() == 1 |
| 27 | + assert ModelWithEnum.objects.first().status == Status.NEW |
| 28 | + |
| 29 | + def test_set_by_value(self): |
| 30 | + ModelWithEnum.drop_collection() |
| 31 | + ModelWithEnum(status="new").save() |
| 32 | + assert ModelWithEnum.objects.first().status == Status.NEW |
| 33 | + |
| 34 | + def test_filter(self): |
| 35 | + ModelWithEnum.drop_collection() |
| 36 | + ModelWithEnum(status="new").save() |
| 37 | + assert ModelWithEnum.objects(status="new").count() == 1 |
| 38 | + assert ModelWithEnum.objects(status=Status.NEW).count() == 1 |
| 39 | + assert ModelWithEnum.objects(status=Status.DONE).count() == 0 |
| 40 | + |
| 41 | + def test_change_value(self): |
| 42 | + m = ModelWithEnum(status="new") |
| 43 | + m.status = Status.DONE |
| 44 | + m.save() |
| 45 | + assert m.status == Status.DONE |
| 46 | + |
| 47 | + def test_set_default(self): |
| 48 | + class ModelWithDefault(Document): |
| 49 | + status = EnumField(Status, default=Status.DONE) |
| 50 | + |
| 51 | + m = ModelWithDefault().save() |
| 52 | + assert m.status == Status.DONE |
| 53 | + |
| 54 | + def test_enum_field_can_be_empty(self): |
| 55 | + ModelWithEnum.drop_collection() |
| 56 | + m = ModelWithEnum().save() |
| 57 | + assert m.status is None |
| 58 | + assert ModelWithEnum.objects()[0].status is None |
| 59 | + assert ModelWithEnum.objects(status=None).count() == 1 |
| 60 | + |
| 61 | + def test_set_none_explicitly(self): |
| 62 | + ModelWithEnum.drop_collection() |
| 63 | + ModelWithEnum(status=None).save() |
| 64 | + assert ModelWithEnum.objects.first().status is None |
| 65 | + |
| 66 | + def test_cannot_create_model_with_wrong_enum_value(self): |
| 67 | + m = ModelWithEnum(status="wrong_one") |
| 68 | + with pytest.raises(ValidationError): |
| 69 | + m.validate() |
| 70 | + |
| 71 | + def test_user_is_informed_when_tries_to_set_choices(self): |
| 72 | + with pytest.raises(ValueError, match="'choices' can't be set on EnumField"): |
| 73 | + EnumField(Status, choices=["my", "custom", "options"]) |
| 74 | + |
| 75 | + |
| 76 | +class Color(Enum): |
| 77 | + RED = 1 |
| 78 | + BLUE = 2 |
| 79 | + |
| 80 | + |
| 81 | +class ModelWithColor(Document): |
| 82 | + color = EnumField(Color, default=Color.RED) |
| 83 | + |
| 84 | + |
| 85 | +class TestIntEnumField(MongoDBTestCase): |
| 86 | + def test_enum_with_int(self): |
| 87 | + ModelWithColor.drop_collection() |
| 88 | + m = ModelWithColor().save() |
| 89 | + assert m.color == Color.RED |
| 90 | + assert ModelWithColor.objects(color=Color.RED).count() == 1 |
| 91 | + assert ModelWithColor.objects(color=1).count() == 1 |
| 92 | + assert ModelWithColor.objects(color=2).count() == 0 |
| 93 | + |
| 94 | + def test_create_int_enum_by_value(self): |
| 95 | + model = ModelWithColor(color=2).save() |
| 96 | + assert model.color == Color.BLUE |
| 97 | + |
| 98 | + def test_storage_enum_with_int(self): |
| 99 | + model = ModelWithColor(color=Color.BLUE).save() |
| 100 | + assert get_as_pymongo(model) == {"_id": model.id, "color": 2} |
| 101 | + |
| 102 | + def test_validate_model(self): |
| 103 | + with pytest.raises(ValidationError, match="Value must be one of"): |
| 104 | + ModelWithColor(color=3).validate() |
| 105 | + |
| 106 | + with pytest.raises(ValidationError, match="Value must be one of"): |
| 107 | + ModelWithColor(color="wrong_type").validate() |
0 commit comments