Skip to content

Commit 2f24f52

Browse files
authored
Merge pull request #80 from Doist/chiara/fix-cohort-and-empty-results
fix: `has_events_marked` to check actual bits, not just key existence
2 parents 33aeeb1 + 7098720 commit 2f24f52

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

bitmapist/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ class MixinEventsMisc:
389389

390390
def has_events_marked(self):
391391
cli = get_redis(self.system)
392-
return bool(cli.exists(self.redis_key))
392+
# Note: bitcount() returns 0 if the key doesn't exist
393+
# https://redis.io/docs/latest/commands/bitcount/
394+
return cli.bitcount(self.redis_key) > 0
393395

394396
def delete(self):
395397
cli = get_redis(self.system)

test/test_cohort.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,28 @@ def events():
3131
@pytest.mark.parametrize(
3232
("select1", "select1b", "select2", "select2b", "expected"),
3333
[
34+
# Basic tests without select1b/select2b
3435
("active", None, "active", None, [2, 100, 50]),
3536
("active", None, "unknown", None, [2, "", ""]),
3637
("unknown", None, "active", None, [0, "", ""]),
38+
39+
# Tests with select1b (AND conditions for select1)
3740
("signup", "active", "active", None, [2, 100, 50]),
38-
("signup", "active", "active", "signup", [2, 100, 0]),
41+
42+
# Tests with both select1b and select2b
3943
("task1", "task2", "task2", "task1", [2, 100, 100]),
40-
("task1", "task2", "task1", "task2", [2, 100, 100]),
44+
45+
# When select1 has no events but select1b has events, result should be 0
46+
("unknown", "active", "active", None, [0, "", ""]),
47+
48+
# When select1 has events but select2 AND select2b results in 0
49+
("active", None, "unknown", "active", [2, "", ""]),
50+
51+
# When select1 has events but select1b has no events (no overlap)
52+
("active", "unknown", "active", None, [0, "", ""]),
53+
54+
# When select2 has events but select2b has no events (no overlap)
55+
("active", None, "active", "unknown", [2, "", ""]),
4156
],
4257
)
4358
def test_cohort(select1, select1b, select2, select2b, expected, events):

0 commit comments

Comments
 (0)