Skip to content

Commit 2b94e1e

Browse files
feat: support enums in Model by dumping to json (#168)
* feat: support enums in Model by dumping to json This adds support for Enums to the Model class. They'll be dumpeds to JSON to store in redis. #153 * fix: pre-commit
1 parent 184c526 commit 2b94e1e

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ build: setup ## build python packages
1111
pip install twine build
1212
python -m build --sdist --wheel --outdir dist/
1313
twine check dist/*
14+
15+
test: setup ## Run unit tests
16+
pytest

pydantic_aioredis/abstract.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
from datetime import date
44
from datetime import datetime
5+
from enum import Enum
56
from ipaddress import IPv4Address
67
from ipaddress import IPv4Network
78
from ipaddress import IPv6Address
@@ -35,6 +36,7 @@
3536
SHAPE_FROZENSET,
3637
SHAPE_DICT,
3738
SHAPE_DEFAULTDICT,
39+
Enum,
3840
]
3941

4042

test/test_pydantic_aioredis.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for the redis orm"""
22
from datetime import date
3+
from enum import Enum
34
from ipaddress import ip_network
45
from ipaddress import IPv4Network
56
from random import randint
@@ -408,3 +409,25 @@ class TestModel(Model):
408409
this_model = TestModel(name="test", object=MyClass())
409410
with pytest.raises(TypeError):
410411
await TestModel.insert(this_model)
412+
413+
414+
@pytest.mark.asyncio
415+
async def test_enum_support(redis_store):
416+
"""Test case for aioredis support"""
417+
418+
class TestEnum(str, Enum):
419+
test = "test"
420+
foo = "foo"
421+
bar = "bar"
422+
baz = "baz"
423+
424+
class EnumModel(Model):
425+
_primary_key_field = "id"
426+
id: int
427+
enum: TestEnum
428+
429+
redis_store.register_model(EnumModel)
430+
this_model = EnumModel(id=0, enum="foo")
431+
await EnumModel.insert(this_model)
432+
from_redis = await EnumModel.select()
433+
assert from_redis[0] == this_model

0 commit comments

Comments
 (0)