Skip to content

Commit 983eae4

Browse files
committed
Improve tests
!test
1 parent 65ccbac commit 983eae4

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

python/cachebox/_cachebox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ def popitem(self) -> typing.Tuple[KT, VT]:
19391939
"""
19401940
try:
19411941
val = self._raw.popitem()
1942-
except _core.CoreKeyError:
1942+
except _core.CoreKeyError: # pragma: no cover
19431943
raise KeyError() from None
19441944
else:
19451945
return val.pack2()

python/cachebox/_core.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ class BaseCacheImpl(typing.Generic[KT, VT]):
6868
def keys(self) -> typing.Iterable[KT]: ...
6969
def values(self) -> typing.Iterable[VT]: ...
7070
def items(self) -> typing.Iterable[typing.Tuple[KT, VT]]: ...
71+
def __copy__(self) -> "BaseCacheImpl[KT, VT]": ...
72+
def __deepcopy__(self, memo) -> "BaseCacheImpl[KT, VT]": ...
73+
def copy(self) -> "BaseCacheImpl[KT, VT]": ...

python/tests/mixin.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,32 @@ def _test_pickle(self, check_order: typing.Callable):
431431
assert c1 == c2
432432
assert c1.capacity() == c2.capacity()
433433
check_order(c1, c2)
434+
435+
def test_copy(self):
436+
import copy
437+
438+
# shallow copy
439+
c1 = self.CACHE(maxsize=0, **self.KWARGS)
440+
c1.insert('dict', {})
441+
c2 = c1.copy()
442+
443+
assert c2 == c1
444+
c2['dict'][1] = 1
445+
446+
assert c1['dict'][1] == 1
447+
448+
c2.insert(1, 1)
449+
assert 1 not in c1
450+
451+
# deepcopy
452+
c1 = self.CACHE(maxsize=0, **self.KWARGS)
453+
c1.insert('dict', {})
454+
c2 = copy.deepcopy(c1)
455+
456+
assert c2 == c1
457+
c2['dict'][1] = 1
458+
459+
assert 1 not in c1['dict']
460+
461+
c2.insert(1, 1)
462+
assert 1 not in c1

python/tests/test_caches.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ def test_popitem_with_expire(self):
430430
with pytest.raises(KeyError):
431431
obj.popitem_with_expire()
432432

433+
def test_items_with_expire(self):
434+
# no need to test completely items_with_expire
435+
# because it's tested in test_iterators
436+
obj = TTLCache(10, 3, {1: 2, 3: 4})
437+
for key, val, ttl in obj.items_with_expire():
438+
assert key in obj
439+
assert val == obj[key]
440+
assert isinstance(ttl, float)
441+
433442

434443
class TestVTTLCache(_TestMixin):
435444
CACHE = VTTLCache
@@ -571,5 +580,14 @@ def inner(c1, c2):
571580
c2 = pickle.loads(pickle.dumps(c1))
572581

573582
assert len(c2) == len(c1)
574-
assert c1.capacity() == c2.capacity()
583+
assert abs(c2.capacity() - c1.capacity()) < 2
575584
inner(c1, c2)
585+
586+
def test_items_with_expire(self):
587+
# no need to test completely items_with_expire
588+
# because it's tested in test_iterators
589+
obj = VTTLCache(10, {1: 2, 3: 4}, ttl=10)
590+
for key, val, ttl in obj.items_with_expire():
591+
assert key in obj
592+
assert val == obj[key]
593+
assert isinstance(ttl, float)

0 commit comments

Comments
 (0)