def test_safe_get_count(): with pytest.raises(ValueError): _safe_get_count(date.today(), CountHistory([])) _safe_get_count( date.today(), CountHistory([DayCount(date.today() + timedelta(1), 1)])) assert _safe_get_count(date.today(), CountHistory([DayCount(date.today(), 1)]))
def _build_count_history(pairs: List[Tuple[str, str]]) -> CountHistory: daily_counts: List[DayCount] = [] for date_str, count_str in pairs: date_ = parse_mdy(date_str) count = int(float(count_str)) daily_counts.append(DayCount(date_, count)) return CountHistory(daily_counts)
def test_day_rank(): assert _day_rank(date.today(), CountHistory([DayCount(date.today(), 10)])) == 0 assert _day_rank(date(2020, 8, 31), _get_small_count_history()) == 4 assert _day_rank(date(2020, 9, 1), _get_small_count_history()) == 2 assert _day_rank(date(2020, 9, 2), _get_small_count_history()) == 0 assert _day_rank(date(2020, 9, 3), _get_small_count_history()) == 1 assert _day_rank(date(2020, 9, 4), _get_small_count_history()) == 5 assert _day_rank(date(2020, 9, 5), _get_small_count_history()) == 3 assert _day_rank(date(2020, 9, 3), _get_rivoli_test_count_history()) == 8
def test_day_is_monthly_record(): assert _day_is_monthly_record(date.today(), CountHistory([DayCount(date.today(), 10)])) assert _day_is_monthly_record(date(2020, 8, 31), _get_small_count_history()) assert not _day_is_monthly_record(date(2020, 9, 1), _get_small_count_history()) assert _day_is_monthly_record(date(2020, 9, 2), _get_small_count_history()) assert not _day_is_monthly_record(date(2020, 9, 3), _get_small_count_history()) assert not _day_is_monthly_record(date(2020, 9, 4), _get_small_count_history()) assert not _day_is_monthly_record(date(2020, 9, 5), _get_small_count_history()) assert not _day_is_monthly_record(date(2020, 9, 3), _get_rivoli_test_count_history()) assert not _day_is_monthly_record(date(2020, 9, 4), _get_rivoli_test_count_history())
def test_day_is_year_maximum(): assert _day_is_year_maximum(date.today(), CountHistory([DayCount(date.today(), 10)])) assert not _day_is_year_maximum(date(2020, 8, 31), _get_small_count_history()) assert not _day_is_year_maximum(date(2020, 9, 1), _get_small_count_history()) assert _day_is_year_maximum(date(2020, 9, 2), _get_small_count_history()) assert not _day_is_year_maximum(date(2020, 9, 3), _get_small_count_history()) assert not _day_is_year_maximum(date(2020, 9, 4), _get_small_count_history()) assert not _day_is_year_maximum(date(2020, 9, 5), _get_small_count_history()) assert not _day_is_year_maximum(date(2020, 9, 3), _get_rivoli_test_count_history()) assert not _day_is_year_maximum(date(2020, 9, 4), _get_rivoli_test_count_history())
def _load_count_history(input_filename: str) -> CountHistory: return CountHistory.from_csv(load_file(input_filename))
def _remove_posterior_days(day: date, count_history: CountHistory) -> CountHistory: return CountHistory( [cnt for cnt in count_history.daily_counts if cnt.date <= day])
def _get_rivoli_test_count_history() -> CountHistory: return CountHistory.from_csv( open(os.path.join(_get_folder(), 'test_data', 'rivoli_test_data.csv')).read())
def _get_small_count_history() -> CountHistory: return CountHistory.from_csv(_COUNT_HISTORY_CSV)
def test_month_to_cumulate_sums(): assert _month_to_cumulate_sums(CountHistory([])) == {} cumsums = _month_to_cumulate_sums(_get_small_count_history()) assert len(cumsums) == 2 assert cumsums[Month(8, 2020)] == [100] assert cumsums[Month(9, 2020)] == [200, 550, 800, 850, 970]
def test_group_by_year(): assert _group_by_year(CountHistory([])) == {} assert _group_by_year(_get_small_count_history()) == { 2020: [100, 200, 350, 250, 50, 120] }
def test_group_by_month(): assert _group_by_month(CountHistory([])) == {} grouped = _group_by_month(_get_small_count_history()) assert grouped[Month(8, 2020)] == [100] assert grouped[Month(9, 2020)] == [200, 350, 250, 50, 120]
def test_extract_total_count(): assert _extract_total_count(CountHistory([])) == 0 assert _extract_total_count(_get_small_count_history()) == 1070 assert _extract_total_count(_get_rivoli_test_count_history()) == 2841547