Skip to content

Commit acb2d97

Browse files
committed
improve test coverage
1 parent 266dea5 commit acb2d97

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

dante/asyncdante.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Iterable
3+
from typing import AsyncGenerator
44

55
import aiosqlite
66
from pydantic import BaseModel
@@ -94,5 +94,7 @@ async def clear(self):
9494
await conn.execute(f"DELETE FROM {self.name}")
9595
await self.db._maybe_commit()
9696

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

tests/test_async.py

+34
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ async def test_find_none(db):
7272
assert result is None
7373

7474

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+
7589
@pytest.mark.asyncio
7690
async def test_update_one(db):
7791
coll = await db["test"]
@@ -92,6 +106,15 @@ async def test_update_many(db):
92106
assert result["b"] == 3
93107

94108

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+
95118
@pytest.mark.asyncio
96119
async def test_delete_one(db):
97120
coll = await db["test"]
@@ -115,6 +138,17 @@ async def test_delete_many(db):
115138
assert result == []
116139

117140

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+
118152
@pytest.mark.asyncio
119153
async def test_clear(db):
120154
coll = await db["test"]

tests/test_models.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from pydantic import BaseModel
23

34
from dante import Dante
@@ -59,3 +60,21 @@ def test_update_model():
5960

6061
assert result.a == 1
6162
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()

tests/test_sync.py

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import datetime
22
from time import time
33

4+
import pytest
5+
46
from dante.sync import Dante
57

68

@@ -9,6 +11,7 @@ def test_create_dante_on_disk(tmp_path):
911
db = Dante(db_path)
1012
assert db is not None
1113
_ = db["test"]
14+
db.close()
1215
assert db_path.exists()
1316

1417

@@ -46,6 +49,17 @@ def test_insert_find_many():
4649
assert len(result) == 2
4750

4851

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+
4963
def test_insert_datetime():
5064
db = Dante()
5165
coll = db["test"]
@@ -84,6 +98,13 @@ def test_update_many():
8498
assert result["b"] == 3
8599

86100

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+
87108
def test_delete_one():
88109
db = Dante()
89110
coll = db["test"]
@@ -105,6 +126,15 @@ def test_delete_many():
105126
assert result == []
106127

107128

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+
108138
def test_clear():
109139
db = Dante()
110140
coll = db["test"]

0 commit comments

Comments
 (0)