def create_and_play_league(num_players=20):
    fake_users = sim.register_fake_users(num_users=num_players)
    num_fake_users = db.execute(
        "SELECT COUNT (DISTINCT user_id) FROM users;").fetchone()["count"]
    assert num_fake_users == num_players

    admin_user = fake_users[0]
    fake_users = fake_users[1:]

    fake_league = sim.create_league(admin_user)

    sim.enroll_members(fake_league, fake_users)
    fake_league["num_active_players"] = len(fake_users) + 1
    sim.generate_divisions_and_fixtures(fake_league)

    cursor = db.execute("SELECT match_id FROM match_info;")
    for row in cursor:
        admin_model.approve_match(
            {
                "score_user_1": randint(0, 5),
                "score_user_2": randint(0, 5),
                "match_id": row["match_id"]
            }, admin_user["user_id"])

    return fake_league, fake_users, admin_user
def test_user_cannot_be_added_without_consent(cleanup):
    fake_users = sim.register_fake_users(num_users=2)
    league_info = sim.create_league(fake_users[0])

    join_results = admin_model.update_join_league_requests(
        league_info["league_id"],
        {fake_users[1]["user_id"]: league_model.STATUS_MEMBER
         })["message"][fake_users[1]["user_id"]]
    assert join_results is None
def test_input_validation_update_join_league_requests(cleanup):
    fake_user = sim.register_fake_users(num_users=1)[0]
    _ = sim.create_league(fake_user)

    # Test non-existent statuses
    with pytest.raises(TigerLeaguesException):
        admin_model.update_join_league_requests(1, {1: "hahahahaha"})

    # Test invalid user IDs
    with pytest.raises(TigerLeaguesException):
        admin_model.update_join_league_requests(1, {"fake_user_id": "member"})
def test_league_must_have_at_least_one_admin(cleanup):
    fake_user = sim.register_fake_users(num_users=1)[0]
    league_info = sim.create_league(fake_user)

    with pytest.raises(TigerLeaguesException):
        admin_model.update_join_league_requests(
            league_info["league_id"],
            {fake_user["user_id"]: league_model.STATUS_MEMBER})

    assert league_model.STATUS_ADMIN == admin_model.update_join_league_requests(
        league_info["league_id"],
        {fake_user["user_id"]: league_model.STATUS_ADMIN
         })["message"][fake_user["user_id"]]
def test_simulation(cleanup):
    # Check that many users can be added
    fake_users = sim.register_fake_users(num_users=10)
    num_fake_users = db.execute(
        "SELECT COUNT (DISTINCT user_id) FROM users;").fetchone()["count"]
    assert num_fake_users == 10

    admin_user = fake_users[0]
    fake_users = fake_users[1:]

    league_info = sim.create_league(admin_user)
    num_leagues = db.execute(
        "SELECT COUNT (league_id) FROM league_info;").fetchone()["count"]
    assert num_leagues == 1

    league_status = db.execute(
        "SELECT league_status FROM league_info WHERE league_id = %s",
        values=[league_info["league_id"]]).fetchone()["league_status"]
    assert league_status == league_model.LEAGUE_STAGE_ACCEPTING_USERS

    sim.enroll_members(league_info, fake_users)
    league_info["num_active_players"] = len(fake_users) + 1
    sim.generate_divisions_and_fixtures(league_info)