Skip to content

Commit 1b39bfc

Browse files
authored
Test listen streak challenge 2 listens in 1 day (#11373)
1 parent f854639 commit 1b39bfc

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

packages/discovery-provider/integration_tests/challenges/test_listen_streak_endless_challenge.py

+55-6
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,28 @@
2121
BLOCK_NUMBER = 10
2222

2323

24-
def create_play(offset: int) -> Play:
24+
def create_play_day_offset(day_offset: int, hour_offset: int = 0) -> Play:
25+
delta = timedelta(hours=hour_offset) + timedelta(days=day_offset)
26+
2527
return Play(
26-
id=offset,
28+
id=day_offset * 24 + hour_offset,
2729
user_id=1,
2830
source=None,
2931
play_item_id=1,
3032
slot=1,
3133
signature=None,
32-
updated_at=datetime.now() + timedelta(days=offset),
33-
created_at=datetime.now() + timedelta(days=offset),
34+
updated_at=datetime.now() + delta,
35+
created_at=datetime.now() + delta,
3436
)
3537

3638

37-
def dispatch_play(offset: int, session: Session, bus: ChallengeEventBus):
38-
play = create_play(offset)
39+
def dispatch_play(
40+
day_offset: int,
41+
session: Session,
42+
bus: ChallengeEventBus,
43+
hour_offset: int = 0,
44+
):
45+
play = create_play_day_offset(day_offset, hour_offset)
3946
session.add(play)
4047
session.flush()
4148
bus.dispatch(
@@ -340,6 +347,48 @@ def dp(offset):
340347
assert state[3].current_step_count == 2 and state[3].is_complete == False
341348

342349

350+
def test_multiple_listens_in_one_day(app):
351+
redis_conn = get_redis()
352+
bus = ChallengeEventBus(redis_conn)
353+
# Register events with the bus
354+
bus.register_listener(
355+
ChallengeEvent.track_listen, listen_streak_endless_challenge_manager
356+
)
357+
358+
with app.app_context():
359+
db = get_db()
360+
361+
with db.scoped_session() as session:
362+
setup_challenges(session)
363+
364+
def dp_day(offset):
365+
return dispatch_play(offset, session, bus)
366+
367+
def dp_hour(offset, hour_offset):
368+
return dispatch_play(offset, session, bus, hour_offset=hour_offset)
369+
370+
scope_and_process = make_scope_and_process(bus, session)
371+
372+
# Get to 6 plays over 6 days
373+
scope_and_process(lambda: dp_day(0))
374+
scope_and_process(lambda: dp_day(1))
375+
scope_and_process(lambda: dp_day(2))
376+
scope_and_process(lambda: dp_day(3))
377+
scope_and_process(lambda: dp_day(4))
378+
scope_and_process(lambda: dp_day(5))
379+
380+
# On the 7th day, dispatch 2 plays in the same day
381+
scope_and_process(lambda: dp_day(6))
382+
scope_and_process(lambda: dp_hour(6, 1))
383+
scope_and_process(lambda: dp_hour(6, 2))
384+
385+
state = listen_streak_endless_challenge_manager.get_user_challenge_state(
386+
session
387+
)
388+
assert len(state) == 1
389+
assert state[0].current_step_count == 7 and state[0].is_complete == True
390+
391+
343392
def test_anon_listen(app):
344393
redis_conn = get_redis()
345394
bus = ChallengeEventBus(redis_conn)

0 commit comments

Comments
 (0)