9
9
FullEventMetadata ,
10
10
)
11
11
from src .challenges .challenge_event_bus import ChallengeEventBus
12
- from src .challenges .listen_streak_challenge import listen_streak_challenge_manager
12
+ from src .challenges .listen_streak_endless_challenge import (
13
+ listen_streak_endless_challenge_manager ,
14
+ )
13
15
from src .challenges .referral_challenge import (
14
16
referral_challenge_manager ,
15
17
verified_referral_challenge_manager ,
@@ -566,7 +568,11 @@ def setup_listen_streak_challenge(session):
566
568
# Setup
567
569
blocks = [
568
570
Block (blockhash = "0x1" , number = 1 , parenthash = "" , is_current = False ),
569
- Block (blockhash = "0x2" , number = 2 , parenthash = "" , is_current = True ),
571
+ Block (blockhash = "0x2" , number = 2 , parenthash = "" , is_current = False ),
572
+ Block (blockhash = "0x3" , number = 3 , parenthash = "" , is_current = False ),
573
+ Block (blockhash = "0x4" , number = 4 , parenthash = "" , is_current = False ),
574
+ Block (blockhash = "0x5" , number = 5 , parenthash = "" , is_current = False ),
575
+ Block (blockhash = "0x6" , number = 6 , parenthash = "" , is_current = True ),
570
576
]
571
577
users = [
572
578
User (
@@ -589,50 +595,217 @@ def setup_listen_streak_challenge(session):
589
595
updated_at = datetime .now (),
590
596
is_verified = True ,
591
597
),
598
+ User (
599
+ blockhash = "0x3" ,
600
+ blocknumber = 3 ,
601
+ user_id = 3 ,
602
+ is_current = True ,
603
+ wallet = "0xFakeWallet3" ,
604
+ created_at = datetime .now (),
605
+ updated_at = datetime .now (),
606
+ is_verified = False ,
607
+ ),
608
+ User (
609
+ blockhash = "0x4" ,
610
+ blocknumber = 4 ,
611
+ user_id = 4 ,
612
+ is_current = True ,
613
+ wallet = "0xFakeWallet4" ,
614
+ created_at = datetime .now (),
615
+ updated_at = datetime .now (),
616
+ is_verified = False ,
617
+ ),
618
+ User (
619
+ blockhash = "0x5" ,
620
+ blocknumber = 5 ,
621
+ user_id = 5 ,
622
+ is_current = True ,
623
+ wallet = "0xFakeWallet5" ,
624
+ created_at = datetime .now (),
625
+ updated_at = datetime .now (),
626
+ is_verified = False ,
627
+ ),
628
+ User (
629
+ blockhash = "0x6" ,
630
+ blocknumber = 6 ,
631
+ user_id = 6 ,
632
+ is_current = True ,
633
+ wallet = "0xFakeWallet6" ,
634
+ created_at = datetime .now (),
635
+ updated_at = datetime .now (),
636
+ is_verified = False ,
637
+ ),
592
638
]
593
639
594
640
challenges = [
595
641
Challenge (
596
- id = "l " ,
597
- type = ChallengeType .numeric ,
642
+ id = "e " ,
643
+ type = ChallengeType .aggregate ,
598
644
active = True ,
599
645
amount = "1" ,
600
- step_count = 7 ,
646
+ step_count = 2147483647 ,
601
647
)
602
648
]
603
649
604
650
user_challenges = [
605
651
UserChallenge (
606
- challenge_id = "l " ,
652
+ challenge_id = "e " ,
607
653
user_id = 1 ,
608
654
specifier = "1" ,
609
655
is_complete = False ,
610
656
current_step_count = 5 ,
611
657
amount = 1 ,
612
- completed_at = datetime .now (),
658
+ created_at = datetime .now (),
613
659
),
614
660
UserChallenge (
615
- challenge_id = "l " ,
661
+ challenge_id = "e " ,
616
662
user_id = 2 ,
617
663
specifier = "2" ,
618
664
is_complete = False ,
619
665
current_step_count = 5 ,
620
666
amount = 1 ,
667
+ created_at = datetime .now (),
668
+ ),
669
+ # User 3 has just started a new streak for the first time
670
+ UserChallenge (
671
+ challenge_id = "e" ,
672
+ user_id = 3 ,
673
+ specifier = "3x0" ,
674
+ is_complete = False ,
675
+ current_step_count = 3 ,
676
+ amount = 7 ,
677
+ created_at = datetime .now (),
678
+ ),
679
+ # User 4 has an ongoing listen streak of 9
680
+ UserChallenge (
681
+ challenge_id = "e" ,
682
+ user_id = 4 ,
683
+ specifier = "4x0" ,
684
+ is_complete = True ,
685
+ current_step_count = 7 ,
686
+ amount = 7 ,
687
+ completed_at = datetime .now () - timedelta (days = 2 ),
688
+ created_at = datetime .now () - timedelta (days = 9 ),
689
+ ),
690
+ UserChallenge (
691
+ challenge_id = "e" ,
692
+ user_id = 4 ,
693
+ specifier = "4x1" ,
694
+ is_complete = True ,
695
+ current_step_count = 1 ,
696
+ amount = 1 ,
697
+ completed_at = datetime .now () - timedelta (days = 1 ),
698
+ created_at = datetime .now () - timedelta (days = 1 ),
699
+ ),
700
+ UserChallenge (
701
+ challenge_id = "e" ,
702
+ user_id = 4 ,
703
+ specifier = "4x2" ,
704
+ is_complete = True ,
705
+ current_step_count = 1 ,
706
+ amount = 1 ,
621
707
completed_at = datetime .now (),
708
+ created_at = datetime .now (),
709
+ ),
710
+ # User 5 has one complete listen streak of 8, and an in-progress streak of 3
711
+ UserChallenge (
712
+ challenge_id = "e" ,
713
+ user_id = 5 ,
714
+ specifier = "5x0" ,
715
+ is_complete = True ,
716
+ current_step_count = 7 ,
717
+ amount = 7 ,
718
+ completed_at = datetime .now () - timedelta (days = 30 ),
719
+ created_at = datetime .now () - timedelta (days = 37 ),
720
+ ),
721
+ UserChallenge (
722
+ challenge_id = "e" ,
723
+ user_id = 5 ,
724
+ specifier = "5x1" ,
725
+ is_complete = True ,
726
+ current_step_count = 1 ,
727
+ amount = 1 ,
728
+ completed_at = datetime .now () - timedelta (days = 29 ),
729
+ created_at = datetime .now () - timedelta (days = 29 ),
730
+ ),
731
+ UserChallenge (
732
+ challenge_id = "e" ,
733
+ user_id = 5 ,
734
+ specifier = "5x2" ,
735
+ is_complete = False ,
736
+ current_step_count = 3 ,
737
+ amount = 3 ,
738
+ created_at = datetime .now (),
739
+ ),
740
+ # User 6 has one complete streak of 8, and a broken streak of 2
741
+ UserChallenge (
742
+ challenge_id = "e" ,
743
+ user_id = 6 ,
744
+ specifier = "6x0" ,
745
+ is_complete = True ,
746
+ current_step_count = 7 ,
747
+ amount = 7 ,
748
+ completed_at = datetime .now () - timedelta (days = 30 ),
749
+ created_at = datetime .now () - timedelta (days = 37 ),
750
+ ),
751
+ UserChallenge (
752
+ challenge_id = "e" ,
753
+ user_id = 6 ,
754
+ specifier = "6x1" ,
755
+ is_complete = True ,
756
+ current_step_count = 1 ,
757
+ amount = 1 ,
758
+ completed_at = datetime .now () - timedelta (days = 29 ),
759
+ created_at = datetime .now () - timedelta (days = 29 ),
760
+ ),
761
+ UserChallenge (
762
+ challenge_id = "e" ,
763
+ user_id = 6 ,
764
+ specifier = "6x2" ,
765
+ is_complete = False ,
766
+ current_step_count = 2 ,
767
+ amount = 7 ,
768
+ created_at = datetime .now () - timedelta (days = 4 ),
622
769
),
623
770
]
624
771
625
772
listen_streak_challenges = [
773
+ # User 1 has a completed listen streak of 5
626
774
ChallengeListenStreak (
627
775
user_id = 1 ,
628
776
last_listen_date = datetime .now () - timedelta (hours = 12 ),
629
777
listen_streak = 5 ,
630
778
),
779
+ # User 2 has a broken listen streak of 5 - override should take effect
631
780
ChallengeListenStreak (
632
781
user_id = 2 ,
633
782
last_listen_date = datetime .now () - timedelta (hours = 50 ),
634
783
listen_streak = 5 ,
635
784
),
785
+ # User 3 has a new listen streak of 3
786
+ ChallengeListenStreak (
787
+ user_id = 3 ,
788
+ last_listen_date = datetime .now () - timedelta (hours = 12 ),
789
+ listen_streak = 3 ,
790
+ ),
791
+ # User 4 has an ongoing listen streak of 9
792
+ ChallengeListenStreak (
793
+ user_id = 4 ,
794
+ last_listen_date = datetime .now () - timedelta (hours = 12 ),
795
+ listen_streak = 9 ,
796
+ ),
797
+ # User 5 has an ongoing listen streak of 3
798
+ ChallengeListenStreak (
799
+ user_id = 5 ,
800
+ last_listen_date = datetime .now () - timedelta (hours = 12 ),
801
+ listen_streak = 3 ,
802
+ ),
803
+ # User 6 has a broken listen streak of 3 - override should take effect
804
+ ChallengeListenStreak (
805
+ user_id = 6 ,
806
+ last_listen_date = datetime .now () - timedelta (days = 3 ),
807
+ listen_streak = 2 ,
808
+ ),
636
809
]
637
810
638
811
# Wipe any existing challenges in the DB from running migrations, etc
@@ -650,7 +823,7 @@ def setup_listen_streak_challenge(session):
650
823
651
824
redis_conn = get_redis ()
652
825
bus = ChallengeEventBus (redis_conn )
653
- bus .register_listener (DEFAULT_EVENT , listen_streak_challenge_manager )
826
+ bus .register_listener (DEFAULT_EVENT , listen_streak_endless_challenge_manager )
654
827
return bus
655
828
656
829
@@ -662,7 +835,7 @@ def test_get_challenges_with_no_override_step_count(app):
662
835
663
836
challenges = get_challenges (1 , False , session , bus )
664
837
assert len (challenges ) == 1
665
- assert challenges [0 ]["challenge_id" ] == "l "
838
+ assert challenges [0 ]["challenge_id" ] == "e "
666
839
assert challenges [0 ]["current_step_count" ] == 5
667
840
668
841
@@ -674,5 +847,40 @@ def test_get_challenges_with_override_step_count(app):
674
847
675
848
challenges = get_challenges (2 , False , session , bus )
676
849
assert len (challenges ) == 1
677
- assert challenges [0 ]["challenge_id" ] == "l"
850
+ assert challenges [0 ]["challenge_id" ] == "e"
851
+ assert challenges [0 ]["current_step_count" ] == 0
852
+
853
+
854
+ def test_listen_streak_endless_challenge (app ):
855
+ with app .app_context ():
856
+ db = get_db ()
857
+ with db .scoped_session () as session :
858
+ bus = setup_listen_streak_challenge (session )
859
+
860
+ challenges = get_challenges (3 , False , session , bus )
861
+ assert len (challenges ) == 1
862
+ assert challenges [0 ]["challenge_id" ] == "e"
863
+ assert challenges [0 ]["is_complete" ] == False
864
+ assert challenges [0 ]["amount" ] == "0"
865
+ assert challenges [0 ]["current_step_count" ] == 3
866
+
867
+ challenges = get_challenges (4 , False , session , bus )
868
+ assert len (challenges ) == 1
869
+ assert challenges [0 ]["challenge_id" ] == "e"
870
+ assert challenges [0 ]["is_complete" ] == True
871
+ assert challenges [0 ]["amount" ] == "9"
872
+ assert challenges [0 ]["current_step_count" ] == 9
873
+
874
+ challenges = get_challenges (5 , False , session , bus )
875
+ assert len (challenges ) == 1
876
+ assert challenges [0 ]["challenge_id" ] == "e"
877
+ assert challenges [0 ]["is_complete" ] == True
878
+ assert challenges [0 ]["current_step_count" ] == 3
879
+ assert challenges [0 ]["amount" ] == "8"
880
+
881
+ challenges = get_challenges (6 , False , session , bus )
882
+ assert len (challenges ) == 1
883
+ assert challenges [0 ]["challenge_id" ] == "e"
884
+ assert challenges [0 ]["is_complete" ] == True
678
885
assert challenges [0 ]["current_step_count" ] == 0
886
+ assert challenges [0 ]["amount" ] == "8"
0 commit comments