Skip to content

Commit 68993e9

Browse files
committed
fix:cf.count
1 parent 7e9594d commit 68993e9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

fakeredis/stack/_cf_mixin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _cf_add(key: CommandItem, item: bytes) -> int:
4747

4848
@staticmethod
4949
def _cf_exist(key: CommandItem, item: bytes) -> int:
50-
return 1 if (item in key.value) else 0
50+
return 1 if (key.value is not None and item in key.value) else 0
5151

5252
@command(name="CF.ADD", fixed=(Key(ScalableCuckooFilter), bytes), repeat=(), flags=msgs.FLAG_DO_NOT_CREATE)
5353
def cf_add(self, key: CommandItem, value: bytes) -> int:
@@ -61,7 +61,11 @@ def cf_addnx(self, key: CommandItem, value: bytes) -> int:
6161

6262
@command(name="CF.COUNT", fixed=(Key(ScalableCuckooFilter), bytes), repeat=(), flags=msgs.FLAG_DO_NOT_CREATE)
6363
def cf_count(self, key: CommandItem, item: bytes) -> int:
64-
return 1 if self._cf_exist(key, item) else 0 # todo
64+
if key.value is None:
65+
return 0
66+
if type(key.value) is not ScalableCuckooFilter:
67+
raise SimpleError(msgs.WRONGTYPE_MSG)
68+
return key.value.count(item)
6569

6670
@command(name="CF.DEL", fixed=(Key(ScalableCuckooFilter), bytes), repeat=(), flags=msgs.FLAG_DO_NOT_CREATE)
6771
def cf_del(self, key: CommandItem, value: bytes) -> int:

test/test_stack/test_cuckoofilter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@pytest.mark.min_server("7")
10-
@pytest.mark.unsupported_server_types("dragonfly")
10+
@pytest.mark.unsupported_server_types("dragonfly", "valkey")
1111
def test_cf_add_and_insert(r: redis.Redis):
1212
assert r.cf().create("cuckoo", 1000)
1313
assert r.cf().add("cuckoo", "filter")
@@ -43,3 +43,12 @@ def test_cf_exists_and_del(r: redis.Redis):
4343
assert 0 == r.cf().count("cuckoo", "notexist")
4444
assert r.cf().delete("cuckoo", "filter")
4545
assert 0 == r.cf().count("cuckoo", "filter")
46+
47+
48+
@pytest.mark.unsupported_server_types("dragonfly")
49+
def test_cf_count_non_existing(r: redis.Redis):
50+
assert r.cf().count("cuckoo", "non_existing") == 0
51+
assert r.cf().create("cuckoo", 1000)
52+
assert r.cf().add("cuckoo", "item1")
53+
assert r.cf().insert("cuckoo", ["item1", "item1", "item2"]) == [1, 1, 1]
54+
assert r.cf().count("cuckoo", "item1") == 3

0 commit comments

Comments
 (0)