1- import uuid
1+ from contextlib import contextmanager
2+ from typing import Any , Callable , Coroutine , Iterator
23
34from peewee_async .databases import AioDatabase
4- from tests .conftest import dbs_all , dbs_postgres
5+ from tests .conftest import dbs_all
56from tests .models import TestSignalModel
6- from tests .utils import model_has_fields
7- from peewee_async .signals import aio_pre_save
7+ from peewee_async .signals import AioModel , aio_pre_save , aio_post_save , aio_post_delete , aio_pre_delete , AioSignal , pre_init
88
99
1010
1111
12+
13+ @contextmanager
14+ def _connect (signal : AioSignal , receiver : Callable [..., Coroutine [Any , Any , Any ]], sender : type [AioModel ]) -> Iterator [None ]:
15+ signal .connect (receiver = receiver , sender = sender )
16+ yield
17+ signal .disconnect (receiver = receiver , sender = sender )
18+
19+
1220@dbs_all
1321async def test_aio_pre_save (db : AioDatabase ) -> None :
1422
15- @aio_pre_save (sender = TestSignalModel )
16- async def on_save_handler (model_class , instance , created ):
17- print (model_class , instance , created )
23+
24+ async def on_save_handler (model_class : type [TestSignalModel ], instance : TestSignalModel , created : bool ) -> None :
25+ assert await TestSignalModel .select ().aio_exists () is False
26+ assert model_class is TestSignalModel
27+ assert isinstance (instance , TestSignalModel )
28+ assert created
29+
30+ with _connect (aio_pre_save , receiver = on_save_handler , sender = TestSignalModel ):
31+ await TestSignalModel .aio_create (text = "aio_create" )
32+
33+
34+ @dbs_all
35+ async def test_aio_post_save (db : AioDatabase ) -> None :
36+
37+
38+ async def on_save_handler (model_class : type [TestSignalModel ], instance : TestSignalModel , created : bool ) -> None :
39+ assert await TestSignalModel .select ().aio_exists () is True
40+ assert model_class is TestSignalModel
41+ assert isinstance (instance , TestSignalModel )
42+ assert created
43+
44+ with _connect (aio_post_save , receiver = on_save_handler , sender = TestSignalModel ):
45+ await TestSignalModel .aio_create (text = "aio_create" )
46+
47+
48+ @dbs_all
49+ async def test_aio_pre_delete (db : AioDatabase ) -> None :
50+
51+ t = await TestSignalModel .aio_create (text = "aio_create" )
52+
53+ async def on_delete_handler (model_class : type [TestSignalModel ], instance : TestSignalModel ) -> None :
54+ assert await TestSignalModel .select ().aio_exists () is True
55+ assert model_class is TestSignalModel
56+ assert isinstance (instance , TestSignalModel )
57+
58+ with _connect (aio_pre_delete , receiver = on_delete_handler , sender = TestSignalModel ):
59+ await t .aio_delete_instance ()
60+
61+
62+ @dbs_all
63+ async def test_aio_post_delete (db : AioDatabase ) -> None :
64+
65+ t = await TestSignalModel .aio_create (text = "aio_create" )
66+
67+ async def on_delete_handler (model_class : type [TestSignalModel ], instance : TestSignalModel ) -> None :
68+ assert await TestSignalModel .select ().aio_exists () is False
69+ assert model_class is TestSignalModel
70+ assert isinstance (instance , TestSignalModel )
71+
72+ with _connect (aio_post_delete , receiver = on_delete_handler , sender = TestSignalModel ):
73+ await t .aio_delete_instance ()
74+
75+
76+ @dbs_all
77+ def test_pre_init (db : AioDatabase ) -> None :
78+
79+ def on_init_handler (model_class : type [TestSignalModel ], instance : TestSignalModel ) -> None :
80+ assert model_class is TestSignalModel
81+ assert instance .text == "text"
82+
83+ pre_init .connect (receiver = on_init_handler , sender = TestSignalModel )
84+
85+ TestSignalModel (text = "text" )
1886
19- await TestSignalModel . aio_create ( text = "text" )
87+ pre_init . disconnect ( receiver = on_init_handler , sender = TestSignalModel )
0 commit comments