Skip to content

Commit

Permalink
test: add test to ensure dict with enum keys are encoded properly (#1001
Browse files Browse the repository at this point in the history
)
  • Loading branch information
adeelsohailahmed authored Oct 1, 2024
1 parent b0c9037 commit 8cb5aa8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/odm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
DocumentWithCustomInit,
DocumentWithDecimalField,
DocumentWithDeprecatedHiddenField,
DocumentWithEnumKeysDict,
DocumentWithExtras,
DocumentWithHttpUrlField,
DocumentWithIndexedObjectId,
Expand Down Expand Up @@ -292,6 +293,7 @@ async def init(db):
DocumentToTestSync,
DocumentWithLinkForNesting,
DocumentWithBackLinkForNesting,
DocumentWithEnumKeysDict,
LongSelfLink,
BsonRegexDoc,
NativeRegexDoc,
Expand Down
9 changes: 9 additions & 0 deletions tests/odm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,15 @@ class Settings:
max_nesting_depth = 50


class DictEnum(str, Enum):
RED = "Red"
BLUE = "Blue"


class DocumentWithEnumKeysDict(Document):
color: Dict[DictEnum, str]


class BsonRegexDoc(Document):
regex: Optional[Regex] = None

Expand Down
14 changes: 14 additions & 0 deletions tests/odm/test_encoder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from datetime import date, datetime
from enum import Enum
from uuid import uuid4

import pytest
Expand All @@ -11,10 +12,12 @@
from tests.odm.models import (
BsonRegexDoc,
Child,
DictEnum,
DocumentForEncodingTest,
DocumentForEncodingTestDate,
DocumentWithComplexDictKey,
DocumentWithDecimalField,
DocumentWithEnumKeysDict,
DocumentWithHttpUrlField,
DocumentWithKeepNullsFalse,
DocumentWithStringField,
Expand Down Expand Up @@ -173,6 +176,17 @@ async def test_dict_with_complex_key():
assert new_doc.dict_field.get(uuid) == dt


async def test_dict_with_enum_keys():
doc = DocumentWithEnumKeysDict(color={DictEnum.RED: "favorite"})
await doc.save()

assert isinstance(doc.color, dict)

for key in doc.color:
assert isinstance(key, Enum)
assert key == DictEnum.RED


async def test_native_regex():
regex = re.compile(r"^1?$|^(11+?)\1+$", (re.I | re.M | re.S) ^ re.UNICODE)
doc = await NativeRegexDoc(regex=regex).insert()
Expand Down

0 comments on commit 8cb5aa8

Please sign in to comment.