33from typing import Tuple , List , Optional
44
55import pytest
6+ import pytest_asyncio
67import redislite
78from pytest_lazyfixture import lazy_fixture
89
9- from pydantic_redis import Store , RedisConfig , Model
10+ from pydantic_redis import syncio as syn , asyncio as asy
1011
1112
12- class Author (Model ):
13+ class Author (syn . Model ):
1314 _primary_key_field : str = "name"
1415 name : str
1516 active_years : Tuple [int , int ]
1617
1718
18- class Book (Model ):
19+ class AsyncAuthor (asy .Model ):
20+ _primary_key_field : str = "name"
21+ name : str
22+ active_years : Tuple [int , int ]
23+
24+
25+ class Book (syn .Model ):
1926 _primary_key_field : str = "title"
2027 title : str
2128 author : Author
@@ -25,7 +32,17 @@ class Book(Model):
2532 in_stock : bool = True
2633
2734
28- class Library (Model ):
35+ class AsyncBook (asy .Model ):
36+ _primary_key_field : str = "title"
37+ title : str
38+ author : AsyncAuthor
39+ rating : float
40+ published_on : date
41+ tags : List [str ] = []
42+ in_stock : bool = True
43+
44+
45+ class Library (syn .Model ):
2946 # the _primary_key_field is mandatory
3047 _primary_key_field : str = "name"
3148 name : str
@@ -36,11 +53,27 @@ class Library(Model):
3653 new : Tuple [Book , Author , Book , int ] = None
3754
3855
56+ class AsyncLibrary (asy .Model ):
57+ # the _primary_key_field is mandatory
58+ _primary_key_field : str = "name"
59+ name : str
60+ address : str
61+ books : List [AsyncBook ] = None
62+ lost : Optional [List [AsyncBook ]] = None
63+ popular : Optional [Tuple [AsyncBook , AsyncBook ]] = None
64+ new : Tuple [AsyncBook , AsyncAuthor , AsyncBook , int ] = None
65+
66+
3967authors = {
4068 "charles" : Author (name = "Charles Dickens" , active_years = (1220 , 1280 )),
4169 "jane" : Author (name = "Jane Austen" , active_years = (1580 , 1640 )),
4270}
4371
72+ async_authors = {
73+ "charles" : AsyncAuthor (name = "Charles Dickens" , active_years = (1220 , 1280 )),
74+ "jane" : AsyncAuthor (name = "Jane Austen" , active_years = (1580 , 1640 )),
75+ }
76+
4477books = [
4578 Book (
4679 title = "Oliver Twist" ,
@@ -74,6 +107,40 @@ class Library(Model):
74107 ),
75108]
76109
110+ async_books = [
111+ AsyncBook (
112+ title = "Oliver Twist" ,
113+ author = authors ["charles" ],
114+ published_on = date (year = 1215 , month = 4 , day = 4 ),
115+ in_stock = False ,
116+ rating = 2 ,
117+ tags = ["Classic" ],
118+ ),
119+ AsyncBook (
120+ title = "Great Expectations" ,
121+ author = authors ["charles" ],
122+ published_on = date (year = 1220 , month = 4 , day = 4 ),
123+ rating = 5 ,
124+ tags = ["Classic" ],
125+ ),
126+ AsyncBook (
127+ title = "Jane Eyre" ,
128+ author = authors ["charles" ],
129+ published_on = date (year = 1225 , month = 6 , day = 4 ),
130+ in_stock = False ,
131+ rating = 3.4 ,
132+ tags = ["Classic" , "Romance" ],
133+ ),
134+ AsyncBook (
135+ title = "Wuthering Heights" ,
136+ author = authors ["jane" ],
137+ published_on = date (year = 1600 , month = 4 , day = 4 ),
138+ rating = 4.0 ,
139+ tags = ["Classic" , "Romance" ],
140+ ),
141+ ]
142+
143+ # sync fixtures
77144redis_store_fixture = [(lazy_fixture ("redis_store" ))]
78145books_fixture = [(lazy_fixture ("redis_store" ), book ) for book in books ]
79146update_books_fixture = [
@@ -88,6 +155,23 @@ class Library(Model):
88155 (lazy_fixture ("redis_store" ), book .title ) for book in books [- 1 :]
89156]
90157
158+ # async fixtures
159+ async_redis_store_fixture = [(lazy_fixture ("async_redis_store" ))]
160+ async_books_fixture = [
161+ (lazy_fixture ("async_redis_store" ), book ) for book in async_books
162+ ]
163+ async_update_books_fixture = [
164+ (
165+ lazy_fixture ("async_redis_store" ),
166+ book .title ,
167+ {"author" : authors ["jane" ], "in_stock" : not book .in_stock },
168+ )
169+ for book in async_books [- 1 :]
170+ ]
171+ async_delete_books_fixture = [
172+ (lazy_fixture ("async_redis_store" ), book .title ) for book in async_books [- 1 :]
173+ ]
174+
91175
92176@pytest .fixture ()
93177def unused_tcp_port ():
@@ -110,13 +194,28 @@ def redis_server(unused_tcp_port):
110194@pytest .fixture ()
111195def redis_store (redis_server ):
112196 """Sets up a redis store using the redis_server fixture and adds the book model to it"""
113- store = Store (
197+ store = syn . Store (
114198 name = "sample" ,
115- redis_config = RedisConfig (port = redis_server , db = 1 ),
199+ redis_config = syn . RedisConfig (port = redis_server , db = 1 ),
116200 life_span_in_seconds = 3600 ,
117201 )
118202 store .register_model (Book )
119203 store .register_model (Author )
120204 store .register_model (Library )
121205 yield store
122206 store .redis_store .flushall ()
207+
208+
209+ @pytest_asyncio .fixture
210+ async def async_redis_store (redis_server ):
211+ """Sets up a redis store using the redis_server fixture and adds the book model to it"""
212+ store = asy .Store (
213+ name = "sample" ,
214+ redis_config = syn .RedisConfig (port = redis_server , db = 1 ),
215+ life_span_in_seconds = 3600 ,
216+ )
217+ store .register_model (AsyncBook )
218+ store .register_model (AsyncAuthor )
219+ store .register_model (AsyncLibrary )
220+ yield store
221+ await store .redis_store .flushall ()
0 commit comments