def test_current(self, monkeypatch: MonkeyPatch) -> None: def today() -> FiscalDate: return FiscalDate(2016, 10, 1) monkeypatch.setattr(FiscalDate, "today", today) current = FiscalQuarter.current() assert current == FiscalQuarter(2017, 1)
def test_out_of_range(self) -> None: with pytest.raises(ValueError): FiscalQuarter(2017, 0) with pytest.raises(ValueError): FiscalQuarter(2017, 5) with pytest.raises(ValueError): FiscalQuarter(0, 2)
def test_contains(self, a: FiscalDay, b: FiscalDay, d: FiscalDay) -> None: assert b in b assert a not in d assert FiscalDateTime(2015, 10, 1, 0, 0, 0) in a assert datetime.datetime(2015, 10, 1, 0, 0, 0) in a assert FiscalDate(2015, 10, 1) in a assert datetime.date(2015, 10, 1) in a assert b in FiscalMonth(2016, 1) assert b in FiscalQuarter(2016, 1) assert b in FiscalYear(2016)
def generate_last_completed_fiscal_quarter(fiscal_year, fiscal_quarter=None): """ Generate the most recently completed fiscal quarter """ # Get the current fiscal year so that it can be compared against the FY in the request day_difference = current_fiscal_date() - datetime.timedelta( days=SUBMISSION_WINDOW_DAYS) current_fiscal_date_adjusted = FiscalDateTime(day_difference.year, day_difference.month, day_difference.day) # Attempting to get data for current fiscal year (minus SUBMISSION_WINDOW_DAYS days) if fiscal_year == current_fiscal_date_adjusted.fiscal_year: current_fiscal_quarter = current_fiscal_date_adjusted.quarter # If a fiscal quarter has been requested if fiscal_quarter: # If the fiscal quarter requested is not yet completed (or within SUBMISSION_WINDOW_DAYS # days of being completed), error out if current_fiscal_quarter <= fiscal_quarter: raise InvalidParameterException( f"Requested fiscal year and quarter must have been completed over {SUBMISSION_WINDOW_DAYS} " f"days prior to the current date.") # If no fiscal quarter has been requested else: # If it's currently the first quarter (or within SUBMISSION_WINDOW_DAYS days of the # first quarter), throw an error if current_fiscal_quarter == 1: raise InvalidParameterException( f"Cannot obtain data for current fiscal year. At least one quarter must " f"be completed for over {SUBMISSION_WINDOW_DAYS} days.") # roll back to the last completed fiscal quarter if it's any other quarter else: fiscal_quarter = current_fiscal_quarter - 1 # Attempting to get data for any fiscal year before the current one (minus SUBMISSION_WINDOW_DAYS days) elif fiscal_year < current_fiscal_date_adjusted.fiscal_year: # If no fiscal quarter has been requested, give the fourth quarter of the year requested if not fiscal_quarter: fiscal_quarter = 4 else: raise InvalidParameterException( f"Cannot obtain data for future fiscal years or fiscal years that have " f"not been active for over {SUBMISSION_WINDOW_DAYS} days.") # get the fiscal date fiscal_date = FiscalQuarter(fiscal_year, fiscal_quarter).end fiscal_date = datetime.datetime.strftime(fiscal_date, "%Y-%m-%d") return fiscal_date, fiscal_quarter
def test_next_fiscal_quarter(self, a: FiscalDateTime, b: FiscalDateTime) -> None: assert a.next_fiscal_quarter == FiscalQuarter(2017, 3) assert b.next_fiscal_quarter == FiscalQuarter(2018, 2)
def test_prev_fiscal_quarter(self, a: FiscalDateTime, b: FiscalDateTime) -> None: assert a.prev_fiscal_quarter == FiscalQuarter(2017, 1) assert b.prev_fiscal_quarter == FiscalQuarter(2017, 4)
def d(self) -> FiscalQuarter: return FiscalQuarter(2017, 1)
def e(self) -> FiscalQuarter: return FiscalQuarter(2017, 4)
def f(self) -> FiscalQuarter: return FiscalQuarter(2018, 1)
def a(self) -> FiscalQuarter: return FiscalQuarter(2016, 4)
def c(self) -> FiscalQuarter: return FiscalQuarter(2017, 2)
def test_q4(self, a: FiscalYear) -> None: assert a.q4 == FiscalQuarter(2016, 4)
def test_q3(self, a: FiscalYear) -> None: assert a.q3 == FiscalQuarter(2016, 3)
def test_q2(self, a: FiscalYear) -> None: assert a.q2 == FiscalQuarter(2016, 2)
def test_q1(self, a: FiscalYear) -> None: assert a.q1 == FiscalQuarter(2016, 1)
def test_corner_cases(self) -> None: # start_day does not exist in all months with fiscalyear.fiscal_calendar(start_month=5, start_day=31): # Non-leap year assert FiscalQuarter(2019, 1).start.day == 31 assert FiscalQuarter(2019, 1).end.day == 30 assert FiscalQuarter(2019, 2).start.day == 31 assert FiscalQuarter(2019, 2).end.day == 29 assert FiscalQuarter(2019, 3).start.day == 30 assert FiscalQuarter(2019, 3).end.day == 27 assert FiscalQuarter(2019, 4).start.day == 28 assert FiscalQuarter(2019, 4).end.day == 30 # Leap year assert FiscalQuarter(2020, 1).start.day == 31 assert FiscalQuarter(2020, 1).end.day == 30 assert FiscalQuarter(2020, 2).start.day == 31 assert FiscalQuarter(2020, 2).end.day == 29 assert FiscalQuarter(2020, 3).start.day == 30 assert FiscalQuarter(2020, 3).end.day == 28 assert FiscalQuarter(2020, 4).start.day == 29 assert FiscalQuarter(2020, 4).end.day == 30