Commit acb2d97 1 parent 266dea5 commit acb2d97 Copy full SHA for acb2d97
File tree 4 files changed +88
-3
lines changed
4 files changed +88
-3
lines changed Original file line number Diff line number Diff line change 1
1
from __future__ import annotations
2
2
3
- from typing import Iterable
3
+ from typing import AsyncGenerator
4
4
5
5
import aiosqlite
6
6
from pydantic import BaseModel
@@ -94,5 +94,7 @@ async def clear(self):
94
94
await conn .execute (f"DELETE FROM { self .name } " )
95
95
await self .db ._maybe_commit ()
96
96
97
- def __aiter__ (self ) -> Iterable [dict | BaseModel ]:
98
- return iter (self .find_many ())
97
+ async def __aiter__ (self ) -> AsyncGenerator [dict | BaseModel ]:
98
+ results = await self .find_many ()
99
+ for r in results :
100
+ yield r
Original file line number Diff line number Diff line change @@ -72,6 +72,20 @@ async def test_find_none(db):
72
72
assert result is None
73
73
74
74
75
+ @pytest .mark .asyncio
76
+ async def test_iteration (db ):
77
+ coll = await db ["test" ]
78
+
79
+ await coll .insert ({"a" : 1 })
80
+
81
+ count = 0
82
+ async for data in coll :
83
+ count += 1
84
+ assert data ["a" ] == 1
85
+
86
+ assert count == 1
87
+
88
+
75
89
@pytest .mark .asyncio
76
90
async def test_update_one (db ):
77
91
coll = await db ["test" ]
@@ -92,6 +106,15 @@ async def test_update_many(db):
92
106
assert result ["b" ] == 3
93
107
94
108
109
+ @pytest .mark .asyncio
110
+ async def test_update_without_filter_fails (db ):
111
+ coll = await db ["test" ]
112
+
113
+ await coll .insert ({"a" : 1 , "b" : 2 })
114
+ with pytest .raises (ValueError ):
115
+ await coll .update_many ({})
116
+
117
+
95
118
@pytest .mark .asyncio
96
119
async def test_delete_one (db ):
97
120
coll = await db ["test" ]
@@ -115,6 +138,17 @@ async def test_delete_many(db):
115
138
assert result == []
116
139
117
140
141
+ @pytest .mark .asyncio
142
+ async def test_delete_without_filter_fails (db ):
143
+ coll = await db ["test" ]
144
+
145
+ with pytest .raises (ValueError ):
146
+ await coll .delete_one ()
147
+
148
+ with pytest .raises (ValueError ):
149
+ await coll .delete_many ()
150
+
151
+
118
152
@pytest .mark .asyncio
119
153
async def test_clear (db ):
120
154
coll = await db ["test" ]
Original file line number Diff line number Diff line change
1
+ import pytest
1
2
from pydantic import BaseModel
2
3
3
4
from dante import Dante
@@ -59,3 +60,21 @@ def test_update_model():
59
60
60
61
assert result .a == 1
61
62
assert result .b == "bar"
63
+
64
+
65
+ def test_update_without_filter_fails ():
66
+ db = Dante ()
67
+ coll = db [MyModel ]
68
+
69
+ obj = MyModel (a = 1 )
70
+
71
+ with pytest .raises (ValueError ):
72
+ coll .update_many (obj )
73
+
74
+
75
+ def test_delete_without_filter_fails ():
76
+ db = Dante ()
77
+ coll = db [MyModel ]
78
+
79
+ with pytest .raises (ValueError ):
80
+ coll .delete_many ()
Original file line number Diff line number Diff line change 1
1
from datetime import datetime
2
2
from time import time
3
3
4
+ import pytest
5
+
4
6
from dante .sync import Dante
5
7
6
8
@@ -9,6 +11,7 @@ def test_create_dante_on_disk(tmp_path):
9
11
db = Dante (db_path )
10
12
assert db is not None
11
13
_ = db ["test" ]
14
+ db .close ()
12
15
assert db_path .exists ()
13
16
14
17
@@ -46,6 +49,17 @@ def test_insert_find_many():
46
49
assert len (result ) == 2
47
50
48
51
52
+ def test_iteration ():
53
+ db = Dante ()
54
+ coll = db ["test" ]
55
+
56
+ coll .insert ({"a" : 1 , "b" : 2 , "c" : 3 })
57
+
58
+ result = [d for d in coll ]
59
+ assert len (result ) == 1
60
+ assert result [0 ]["a" ] == 1
61
+
62
+
49
63
def test_insert_datetime ():
50
64
db = Dante ()
51
65
coll = db ["test" ]
@@ -84,6 +98,13 @@ def test_update_many():
84
98
assert result ["b" ] == 3
85
99
86
100
101
+ def test_update_without_filter_fails ():
102
+ db = Dante ()
103
+ coll = db ["test" ]
104
+ with pytest .raises (ValueError ):
105
+ coll .update_many ({})
106
+
107
+
87
108
def test_delete_one ():
88
109
db = Dante ()
89
110
coll = db ["test" ]
@@ -105,6 +126,15 @@ def test_delete_many():
105
126
assert result == []
106
127
107
128
129
+ def test_delete_without_filter_fails ():
130
+ db = Dante ()
131
+ coll = db ["test" ]
132
+ with pytest .raises (ValueError ):
133
+ coll .delete_many ()
134
+ with pytest .raises (ValueError ):
135
+ coll .delete_one ()
136
+
137
+
108
138
def test_clear ():
109
139
db = Dante ()
110
140
coll = db ["test" ]
You can’t perform that action at this time.
0 commit comments