|
21 | 21 | BLOCK_NUMBER = 10
|
22 | 22 |
|
23 | 23 |
|
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 | + |
25 | 27 | return Play(
|
26 |
| - id=offset, |
| 28 | + id=day_offset * 24 + hour_offset, |
27 | 29 | user_id=1,
|
28 | 30 | source=None,
|
29 | 31 | play_item_id=1,
|
30 | 32 | slot=1,
|
31 | 33 | 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, |
34 | 36 | )
|
35 | 37 |
|
36 | 38 |
|
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) |
39 | 46 | session.add(play)
|
40 | 47 | session.flush()
|
41 | 48 | bus.dispatch(
|
@@ -340,6 +347,48 @@ def dp(offset):
|
340 | 347 | assert state[3].current_step_count == 2 and state[3].is_complete == False
|
341 | 348 |
|
342 | 349 |
|
| 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 | + |
343 | 392 | def test_anon_listen(app):
|
344 | 393 | redis_conn = get_redis()
|
345 | 394 | bus = ChallengeEventBus(redis_conn)
|
|
0 commit comments