Skip to content

Commit

Permalink
Update challenge tests to set start block for testing (#2260)
Browse files Browse the repository at this point in the history
* Update challenge tests to set block number and is active, overriding prod config
  • Loading branch information
rickyrombo committed Jan 8, 2022
1 parent b8ca35f commit 186f870
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 59 deletions.
Expand Up @@ -5,10 +5,12 @@
from src.challenges.challenge_event_bus import ChallengeEvent, ChallengeEventBus
from src.challenges.connect_verified_challenge import connect_verified_challenge_manager
from src.models import Block, User
from src.models.models import Challenge
from src.utils.config import shared_config
from src.utils.db_session import get_db

REDIS_URL = shared_config["redis"]["url"]
BLOCK_NUMBER = 10
logger = logging.getLogger(__name__)


Expand All @@ -18,10 +20,10 @@ def test_connect_verified_challenge(app):
with app.app_context():
db = get_db()

block = Block(blockhash="0x1", number=1)
block = Block(blockhash="0x1", number=BLOCK_NUMBER)
user = User(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=1,
is_current=True,
Expand All @@ -37,6 +39,9 @@ def test_connect_verified_challenge(app):

with db.scoped_session() as session:
bus = ChallengeEventBus(redis_conn)
session.query(Challenge).filter(Challenge.id == "connect-verified").update(
{"active": True, "starting_block": BLOCK_NUMBER}
)

# Register events with the bus
bus.register_listener(
Expand All @@ -50,7 +55,7 @@ def test_connect_verified_challenge(app):

bus.dispatch(
ChallengeEvent.connect_verified,
1, # block_number
BLOCK_NUMBER,
1, # user_id
{},
)
Expand Down
Expand Up @@ -10,6 +10,7 @@
from src.utils.db_session import get_db

REDIS_URL = shared_config["redis"]["url"]
BLOCK_NUMBER = 10


def create_play(offset: int) -> Play:
Expand All @@ -31,17 +32,17 @@ def dispatch_play(offset: int, session: Session, bus: ChallengeEventBus):
session.flush()
bus.dispatch(
ChallengeEvent.track_listen,
1,
BLOCK_NUMBER,
1,
{"created_at": play.created_at.timestamp()},
)


def setup_challenges(session):
block = Block(blockhash="0x1", number=1)
block = Block(blockhash="0x1", number=BLOCK_NUMBER)
user = User(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=1,
is_current=True,
Expand All @@ -59,7 +60,7 @@ def setup_challenges(session):
session.add(user)
session.flush()
session.query(Challenge).filter(Challenge.id == "listen-streak").update(
{"active": True}
{"active": True, "starting_block": BLOCK_NUMBER}
)


Expand Down Expand Up @@ -185,7 +186,7 @@ def test_anon_listen(app):
with bus.use_scoped_dispatch_queue():
bus.dispatch(
ChallengeEvent.track_listen,
0,
BLOCK_NUMBER,
None,
{"created_at": datetime.now()},
)
Expand Down
Expand Up @@ -9,6 +9,7 @@
from src.utils.db_session import get_db

REDIS_URL = shared_config["redis"]["url"]
BLOCK_NUMBER = 10


def test_mobile_install_challenge(app):
Expand All @@ -17,10 +18,10 @@ def test_mobile_install_challenge(app):
with app.app_context():
db = get_db()

block = Block(blockhash="0x1", number=1)
block = Block(blockhash="0x1", number=BLOCK_NUMBER)
user = User(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=1,
is_current=True,
Expand All @@ -39,7 +40,7 @@ def test_mobile_install_challenge(app):

# set challenge as active for purposes of test
session.query(Challenge).filter(Challenge.id == "mobile-install").update(
{"active": True}
{"active": True, "starting_block": BLOCK_NUMBER}
)
bus.register_listener(
ChallengeEvent.mobile_install, mobile_install_challenge_manager
Expand All @@ -48,8 +49,8 @@ def test_mobile_install_challenge(app):
session.flush()
session.add(user)
session.flush()
bus.dispatch(ChallengeEvent.mobile_install, 1, user.user_id)
bus.dispatch(ChallengeEvent.mobile_install, 1, user.user_id)
bus.dispatch(ChallengeEvent.mobile_install, BLOCK_NUMBER, user.user_id)
bus.dispatch(ChallengeEvent.mobile_install, BLOCK_NUMBER, user.user_id)
bus.flush()
bus.process_events(session)

Expand Down
Expand Up @@ -4,10 +4,12 @@
from src.challenges.challenge_event_bus import ChallengeEvent, ChallengeEventBus
from src.challenges.profile_challenge import profile_challenge_manager
from src.models import Block, Follow, Repost, RepostType, Save, SaveType, User
from src.models.models import Challenge
from src.utils.config import shared_config
from src.utils.db_session import get_db

REDIS_URL = shared_config["redis"]["url"]
BLOCK_NUMBER = 10


def test_profile_completion_challenge(app):
Expand All @@ -18,10 +20,10 @@ def test_profile_completion_challenge(app):
with app.app_context():
db = get_db()

block = Block(blockhash="0x1", number=1)
block = Block(blockhash="0x1", number=BLOCK_NUMBER)
user = User(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=1,
is_current=True,
Expand All @@ -38,6 +40,11 @@ def test_profile_completion_challenge(app):
with db.scoped_session() as session:
bus = ChallengeEventBus(redis_conn)

# set challenge as active for purposes of test
session.query(Challenge).filter(Challenge.id == "profile-completion").update(
{"active": True, "starting_block": BLOCK_NUMBER}
)

# Register events with the bus
bus.register_listener(ChallengeEvent.profile_update, profile_challenge_manager)
bus.register_listener(ChallengeEvent.repost, profile_challenge_manager)
Expand All @@ -49,7 +56,7 @@ def test_profile_completion_challenge(app):
session.add(user)

# Process dummy event just to get this thing initted
bus.dispatch(ChallengeEvent.profile_update, 1, 1)
bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -60,7 +67,7 @@ def test_profile_completion_challenge(app):
# Do a repost
repost = Repost(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
user_id=1,
repost_item_id=1,
repost_type=RepostType.track,
Expand All @@ -70,7 +77,7 @@ def test_profile_completion_challenge(app):
)
session.add(repost)
session.flush()
bus.dispatch(ChallengeEvent.repost, 1, 1)
bus.dispatch(ChallengeEvent.repost, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -79,7 +86,7 @@ def test_profile_completion_challenge(app):
# Do a save
save = Save(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
user_id=1,
save_item_id=1,
save_type=SaveType.track,
Expand All @@ -89,7 +96,7 @@ def test_profile_completion_challenge(app):
)
session.add(save)
session.flush()
bus.dispatch(ChallengeEvent.favorite, 1, 1)
bus.dispatch(ChallengeEvent.favorite, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
session.flush()
Expand All @@ -99,7 +106,7 @@ def test_profile_completion_challenge(app):
# Do 1 follow, then 5 total follows
follow = Follow(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
is_current=True,
is_delete=False,
created_at=datetime.now(),
Expand All @@ -108,7 +115,7 @@ def test_profile_completion_challenge(app):
)
session.add(follow)
session.flush()
bus.dispatch(ChallengeEvent.follow, 1, 1)
bus.dispatch(ChallengeEvent.follow, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
session.flush()
Expand All @@ -118,7 +125,7 @@ def test_profile_completion_challenge(app):
follows = [
Follow(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
is_current=True,
is_delete=False,
created_at=datetime.now(),
Expand All @@ -127,7 +134,7 @@ def test_profile_completion_challenge(app):
),
Follow(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
is_current=True,
is_delete=False,
created_at=datetime.now(),
Expand All @@ -136,7 +143,7 @@ def test_profile_completion_challenge(app):
),
Follow(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
is_current=True,
is_delete=False,
created_at=datetime.now(),
Expand All @@ -145,7 +152,7 @@ def test_profile_completion_challenge(app):
),
Follow(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
is_current=True,
is_delete=False,
created_at=datetime.now(),
Expand All @@ -155,7 +162,7 @@ def test_profile_completion_challenge(app):
]
session.add_all(follows)
session.flush()
bus.dispatch(ChallengeEvent.follow, 1, 1)
bus.dispatch(ChallengeEvent.follow, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -166,7 +173,7 @@ def test_profile_completion_challenge(app):
{"profile_picture": "profilepictureurl"}
)
session.flush()
bus.dispatch(ChallengeEvent.profile_update, 1, 1)
bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -177,7 +184,7 @@ def test_profile_completion_challenge(app):
{"bio": "profiledescription"}
)
session.flush()
bus.dispatch(ChallengeEvent.profile_update, 1, 1)
bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -186,7 +193,7 @@ def test_profile_completion_challenge(app):
# Undo it, ensure that our count goes down
session.query(User).filter(User.user_id == 1).update({"bio": None})
session.flush()
bus.dispatch(ChallengeEvent.profile_update, 1, 1)
bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -197,7 +204,7 @@ def test_profile_completion_challenge(app):
{"bio": "profiledescription", "cover_photo": "test_cover_photo"}
)
session.flush()
bus.dispatch(ChallengeEvent.profile_update, 1, 1)
bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand All @@ -206,7 +213,7 @@ def test_profile_completion_challenge(app):
# ensure that if we lose some data now that the thing is complete, we don't change the status of the challenge
session.query(User).filter(User.user_id == 1).update({"cover_photo": None})
session.flush()
bus.dispatch(ChallengeEvent.profile_update, 1, 1)
bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1)
bus.flush()
bus.process_events(session)
state = profile_challenge_manager.get_user_challenge_state(session, ["1"])[0]
Expand Down
Expand Up @@ -14,12 +14,13 @@
from src.utils.db_session import get_db

REDIS_URL = shared_config["redis"]["url"]
BLOCK_NUMBER = 1


def create_user(offset: int) -> User:
return User(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=offset,
is_current=True,
Expand All @@ -38,7 +39,7 @@ def create_user_referral(referrer: int, referred_user_id: int) -> UserEvents:
return UserEvents(
user_id=referred_user_id,
is_current=True,
blocknumber=1,
blocknumber=BLOCK_NUMBER,
blockhash="0x1",
referrer=referrer,
)
Expand All @@ -52,11 +53,11 @@ def dispatch_new_user_signup(
session.flush()
bus.dispatch(
ChallengeEvent.referral_signup,
1,
BLOCK_NUMBER,
referrer,
{"referred_user_id": referred_user_id},
)
bus.dispatch(ChallengeEvent.referred_signup, 1, referred_user_id)
bus.dispatch(ChallengeEvent.referred_signup, BLOCK_NUMBER, referred_user_id)


def test_referral_challenge(app):
Expand All @@ -65,10 +66,10 @@ def test_referral_challenge(app):
with app.app_context():
db = get_db()

block = Block(blockhash="0x1", number=1)
block = Block(blockhash="0x1", number=BLOCK_NUMBER)
referrer = User(
blockhash="0x1",
blocknumber=1,
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=1,
is_current=True,
Expand Down Expand Up @@ -98,16 +99,16 @@ def test_referral_challenge(app):
# set challenge as active for purposes of test
session.query(Challenge).filter(
or_(Challenge.id == "referred", Challenge.id == "referrals")
).update({"active": True})
).update({"active": True, "starting_block": BLOCK_NUMBER})
dispatch_new_user_signup(referrer.user_id, 2, session, bus)
for _ in range(0, 4):
bus.dispatch(
ChallengeEvent.referral_signup,
1,
BLOCK_NUMBER,
referrer.user_id,
{"referred_user_id": 2},
)
bus.dispatch(ChallengeEvent.referred_signup, 1, 2)
bus.dispatch(ChallengeEvent.referred_signup, BLOCK_NUMBER, 2)
bus.flush()
bus.process_events(session)

Expand Down

0 comments on commit 186f870

Please sign in to comment.