def main(): logger.info(f"Running app: {config.NBA_APP_NAME}") logger.debug("Updating env variables...") env_vars = update_config_with_env_vars() logger.debug("Getting runtime parameters...") args = get_argv() start = False end = False if "NBA_STARTDATE" in args: start = date.fromisoformat(args["NBA_STARTDATE"]) if "NBA_ENDDATE" in args: end = date.fromisoformat(args["NBA_ENDDATE"]) logger.info("Getting data...") if start and end: logger.info(f"Using provided start: {start} " f"and end {end} dates.") data = merge_line_score( fetch_scoreboard_data(start_date=start, end_date=end)) else: logger.info("Defaulting to yesterday as start and end date.") data = merge_line_score(fetch_scoreboard_data()) logger.info("Retrieved data.") logger.info(f"Pushing data to db at: {env_vars['NBA_DB_URL']}") results = batch_upload(data=data, db=start_engine(env_vars["NBA_DB_URL"]), batch_def=config.BATCHES['default']) logger.info( f"Pushing monitor stats to db at: {env_vars['NBA_MONITOR_DB_URL']}") for result in results: post_monitor_data(db=start_engine(env_vars['NBA_MONITOR_DB_URL']), data=result) logger.info("Finished pushing monitor stats.") logger.info("Finished run!")
def test_fetch_scoreboard_defaults_to_config_timeout_vals( define_test_data_for_timeouts): from config import TIMEOUT_SECS t1 = time.time() fetch_scoreboard_data(start_date=define_test_data_for_timeouts["start"], end_date=define_test_data_for_timeouts["end"]) t2 = time.time() assert_that(t2 - t1).is_greater_than(TIMEOUT_SECS)
def test_fetch_scoreboard_waits_expected_seconds_for_intervals_of_requests( define_test_data_for_timeouts): t1 = time.time() fetch_scoreboard_data( start_date=define_test_data_for_timeouts["start"], end_date=define_test_data_for_timeouts["end"], timeout_days=define_test_data_for_timeouts["timeout_days"], timeout_secs=define_test_data_for_timeouts["timeout_secs"]) t2 = time.time() assert_that(t2 - t1).is_greater_than(120)
def get_multi_data_frames(): dfmt = "%Y/%m/%d" start = date(2019, 12, 1) end = date(2019, 12, 3) middle = date(2019, 12, 2).strftime(dfmt) return merge_line_score( fetch_scoreboard_data(start_date=start, end_date=end))
def test_fetch_scoreboard_data_has_expected_keys(define_test_dates): assert_that( fetch_scoreboard_data( define_test_dates[0], define_test_dates[1])[define_test_dates[2]]).\ contains("GameHeader", "LineScore", "SeriesStandings", "LastMeeting", "EastConfStandingsByDay", "WestConfStandingsByDay", "TeamLeaders", "Available", "TicketLinks", "WinProbability")
def test_fetch_scoreboard_data_values_are_pandas_data_frames( define_test_dates): check = [] for value in fetch_scoreboard_data( define_test_dates[0], define_test_dates[1])[define_test_dates[2]].values(): if isinstance(value, pd.DataFrame): check.append(value) assert_that(check).is_not_empty()
def test_fetch_scoreboard_with_defaults_returns_only_one_object(): # every object will return a dict of 10 items today = date.today().strftime("%Y/%m/%d") assert_that(len(fetch_scoreboard_data()[today])).is_equal_to(10)
def test_fetch_scoreboard_with_defaults_returns_expected_object(): today = date.today().strftime("%Y/%m/%d") assert_that(fetch_scoreboard_data()[today]).contains( "GameHeader", "LineScore", "SeriesStandings", "LastMeeting", "EastConfStandingsByDay", "WestConfStandingsByDay", "TeamLeaders", "Available", "TicketLinks", "WinProbability")
def test_fetch_scoreboard_data_returns_dict(define_test_dates): assert_that( fetch_scoreboard_data( define_test_dates[0], define_test_dates[1]))\ .is_type_of(dict)