-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_mongokv.py
More file actions
227 lines (159 loc) · 5.37 KB
/
test_mongokv.py
File metadata and controls
227 lines (159 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
import uuid
import pytest
import pytest_asyncio
from mongokv import Mkv
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017")
# ---------- Fixtures ----------
@pytest_asyncio.fixture
async def mkv():
collection_name = f"test_kv_{uuid.uuid4().hex}"
db = Mkv(MONGO_URI, db_name="test_mkvdb", collection_name=collection_name)
try:
await db.db.command("ping")
except Exception:
pytest.skip(f"MongoDB not available at {MONGO_URI}")
await db.purge()
try:
yield db
finally:
# Best-effort cleanup; don't let errors fail the test
try:
await db.purge()
except Exception:
pass
try:
await db.close()
except Exception:
pass
@pytest.fixture
def mkv_sync():
collection_name = f"test_kv_sync_{uuid.uuid4().hex}"
db = Mkv(MONGO_URI, db_name="test_mkvdb", collection_name=collection_name)
try:
db.purge()
except Exception:
pass
try:
yield db
finally:
try:
db.purge()
except Exception:
pass
try:
db.close()
except Exception:
pass
# ---------- Async tests ----------
@pytest.mark.asyncio
async def test_set_and_get_value_async(mkv: Mkv):
await mkv.set("foo", "bar")
value = await mkv.get("foo")
assert value == "bar"
@pytest.mark.asyncio
async def test_get_missing_returns_default_async(mkv: Mkv):
value = await mkv.get("missing", default=123)
assert value == 123
@pytest.mark.asyncio
async def test_get_missing_default_none_async(mkv: Mkv):
with pytest.raises(KeyError):
await mkv.get("missing")
@pytest.mark.asyncio
async def test_overwrite_existing_key_async(mkv: Mkv):
await mkv.set("counter", 1)
await mkv.set("counter", 2)
value = await mkv.get("counter")
assert value == 2
@pytest.mark.asyncio
async def test_remove_existing_key_async(mkv: Mkv):
await mkv.set("temp", "value")
removed = await mkv.remove("temp")
assert removed is True
# Confirm it’s gone
with pytest.raises(KeyError):
await mkv.get("temp")
@pytest.mark.asyncio
async def test_remove_missing_key_returns_false_async(mkv: Mkv):
removed = await mkv.remove("does-not-exist")
assert removed is False
@pytest.mark.asyncio
async def test_all_returns_all_keys_async(mkv: Mkv):
await mkv.set("k1", "v1")
await mkv.set("k2", "v2")
await mkv.set("k3", "v3")
keys = await mkv.all()
# Order is not guaranteed, so assert as a set
assert set(keys) == {"k1", "k2", "k3"}
@pytest.mark.asyncio
async def test_all_on_empty_db_returns_empty_list_async(mkv: Mkv):
keys = await mkv.all()
assert keys == []
@pytest.mark.asyncio
async def test_purge_clears_all_keys_async(mkv: Mkv):
await mkv.set("a", 1)
await mkv.set("b", 2)
keys_before = await mkv.all()
assert set(keys_before) == {"a", "b"}
result = await mkv.purge()
assert result is True
keys_after = await mkv.all()
assert keys_after == []
@pytest.mark.asyncio
async def test_non_string_keys_are_cast_to_str_async(mkv: Mkv):
await mkv.set(123, "number")
await mkv.set(("tuple", 1), "tuple-value")
# We expect them to be stored under str(key)
keys = await mkv.all()
assert set(keys) == {"123", "('tuple', 1)"}
assert await mkv.get(123) == "number"
assert await mkv.get(("tuple", 1)) == "tuple-value"
@pytest.mark.asyncio
async def test_close_does_not_throw_async(mkv: Mkv):
# The fixture already closes in teardown, but we can call it early too.
await mkv.set("k", "v")
await mkv.close() # Should not raise
# Don't assert behavior after close (Motor generally allows it but it's not required)
@pytest.mark.asyncio
async def test_get_missing_default_none_explicit_async(mkv: Mkv):
value = await mkv.get("missing", default=None)
assert value is None
@pytest.mark.asyncio
async def test_get_missing_raises_keyerror_async(mkv: Mkv):
with pytest.raises(KeyError):
await mkv.get("missing")
# ---------- Sync tests (dualmethod behavior) ----------
def test_sync_get_missing_raises_keyerror(mkv_sync: Mkv):
with pytest.raises(KeyError):
mkv_sync.get("missing")
def test_sync_get_missing_default_none_explicit(mkv_sync: Mkv):
value = mkv_sync.get("missing", default=None)
assert value is None
def test_sync_set_and_get(mkv_sync: Mkv):
mkv_sync.set("foo", "bar")
value = mkv_sync.get("foo")
assert value == "bar"
def test_sync_get_missing_returns_default(mkv_sync: Mkv):
value = mkv_sync.get("nope", default="fallback")
assert value == "fallback"
def test_sync_remove_and_purge(mkv_sync: Mkv):
mkv_sync.set("a", 1)
mkv_sync.set("b", 2)
removed = mkv_sync.remove("a")
assert removed is True
with pytest.raises(KeyError):
mkv_sync.get("a")
assert mkv_sync.get("b") == 2
purged = mkv_sync.purge()
assert purged is True
assert mkv_sync.all() == []
def test_sync_non_string_keys_cast_to_str(mkv_sync: Mkv):
mkv_sync.set(10, "ten")
mkv_sync.set(("x", "y"), {"ok": True})
keys = mkv_sync.all()
assert set(keys) == {"10", "('x', 'y')"}
assert mkv_sync.get(10) == "ten"
assert mkv_sync.get(("x", "y")) == {"ok": True}
def test_sync_close_does_not_raise(mkv_sync: Mkv):
mkv_sync.set("k", "v")
mkv_sync.close() # Should not raise