def test_calc_league_rates():
    """Ensures league averages are soundly calculated
    """

    schema = Player()
    player_trout = schema.dump({
        'player_name': 'Mike Trout',
        'player_type': 'b',
        'player_id': 1,
        'components': {
            'B_H': 20,
            'B_AB': 50,
            'B_HR': 30,
        }
    })
    player_stanton = schema.dump({
        'player_name': 'Giancarlo Stanton',
        'player_type': 'b',
        'player_id': 2,
        'components': {
            'B_H': 10,
            'B_AB': 40,
            'B_HR': 29,
        }
    })

    avgs, devs, rates = league_rates.calculate_league_rates(
        [player_trout, player_stanton], const.BATTING_COMPONENTS)

    assert avgs['b_hr'] == 29.5
    assert avgs['b_h'] == 15.
    assert avgs['b_ab'] == 45.
    assert rates['b_avg'], 3 == 0.333  # should be rounded
def test_pitchers_coerced_to_rp_if_empty(app_config):
    """Ensures pitchers are marked as RP if no games started are projected
    """

    schema = Player()

    player = schema.dump({
        'player_type': 'p',
        'positions': {},
        'player_id': 1,
        'components': {},  # implicitly no games started
    })

    player_pool = mark_positional_eligibility([player], app_config['lg_pos'],
                                              app_config['eligibility'])
    player = player_pool[0]
    assert player['pos_mv'] == 'rp'
    assert 'dh' not in player['pos_eligible']  # dh for batters only
def test_pitchers_set_to_sp(app_config):
    """Pitchers with GS projected should be marked as SP
    """

    schema = Player()

    player = schema.dump({
        'player_type': 'p',
        'positions': {},
        'player_id': 1,
        'components': {
            'P_GS': 1,
        },
    })

    player_pool = mark_positional_eligibility([player], app_config['lg_pos'],
                                              app_config['eligibility'])
    player = player_pool[0]
    assert player['pos_mv'] == 'sp'
def test_position_prioritization(app_config):
    """Ensures correct position insertion order
    """

    schema = Player()

    player = schema.dump({
        'player_type': 'b',
        'positions': {
            '3B': 50,
            'SS': 12,
            'SP': 14,  # ohtani time
        },
        'player_id': 1,
        'components': {},
    })

    player_pool = mark_positional_eligibility([player], app_config['lg_pos'],
                                              app_config['eligibility'])
    player = player_pool[0]

    assert player['pos_mv'] == 'ss'
    assert player['pos_eligible'] == ['ss', 'b3', 'sp']
def test_all_batters_get_dh(app_config):
    """Ensures all batters receive DH (U) eligibility
    """

    schema = Player()

    player_pool = [
        schema.dump({
            'player_type': 'b',
            'positions': {
                '1B': 50,
            },
            'player_id': i,
            'components': {},
        }) for i in range(20)
    ]

    player_pool = mark_positional_eligibility(player_pool,
                                              app_config['lg_pos'],
                                              app_config['eligibility'])

    for player in player_pool:
        assert player['pos_mv'] == 'b1'
        assert 'dh' in player['pos_flex']
"""
Creates subset of Steamer projection data
"""

import json

from pydash import pick

from models import Player

player_pool = json.load(open('steamer-2019.json'))
schema = Player()
player_pool = [schema.dump(player) for player in player_pool]
player_pool = filter(
    lambda p: p['components']['b_pa'] >= 20 or p['components']['p_ip'] >= 5,
    player_pool)

json.dump(list(player_pool), open('player-pool.json', 'w'))