def fiscal_period(self): """ This is the fiscal period we want to limit our queries to when querying CPE values for self.fiscal_year. If it's prior to Q1 submission window close date, we will return quarter 1 anyhow and just show what we have (which will likely be incomplete). """ return get_final_period_of_quarter( calculate_last_completed_fiscal_quarter(self.fiscal_year)) or 3
def test_calculate_last_completed_fiscal_quarter(): now = datetime.now(timezone.utc) yesterday = now + timedelta(days=-1) tomorrow = now + timedelta(days=1) current_fy = fyh.generate_fiscal_year(now) mommy.make( "submissions.DABSSubmissionWindowSchedule", submission_fiscal_year=2000, submission_reveal_date=now, submission_fiscal_quarter=1, is_quarter=True, ) mommy.make( "submissions.DABSSubmissionWindowSchedule", submission_fiscal_year=2000, submission_reveal_date=now, submission_fiscal_quarter=2, is_quarter=False, ) mommy.make( "submissions.DABSSubmissionWindowSchedule", submission_fiscal_year=2010, submission_reveal_date=tomorrow, submission_fiscal_quarter=2, is_quarter=True, ) mommy.make( "submissions.DABSSubmissionWindowSchedule", submission_fiscal_year=current_fy, submission_reveal_date=yesterday, submission_fiscal_quarter=3, is_quarter=True, ) mommy.make( "submissions.DABSSubmissionWindowSchedule", submission_fiscal_year=current_fy, submission_reveal_date=now, submission_fiscal_quarter=4, is_quarter=True, ) assert fyh.calculate_last_completed_fiscal_quarter(2000) == 1 # not 2, since is_quarter=False assert fyh.calculate_last_completed_fiscal_quarter(2001) is None # no row in table for 2001 assert fyh.calculate_last_completed_fiscal_quarter(2010) is None # not revealed yet assert fyh.calculate_last_completed_fiscal_quarter(current_fy) == 4 # not 3, since both are revealed & quarters
def get_prior_year_agency_budgetary_resources(self): """ Even though we're looking at fiscal_year - 1, it's possible that fiscal year hasn't been closed out yet. For example, if today is 5 Oct 1999 then the submission window for FY 1999 is not closed yet even though we're in FY2000 Q1. """ prior_fiscal_year = self.fiscal_year - 1 prior_fiscal_year_last_completed_fiscal_period = convert_fiscal_quarter_to_fiscal_period( calculate_last_completed_fiscal_quarter(prior_fiscal_year)) return AppropriationAccountBalances.objects.filter( submission__reporting_fiscal_year=prior_fiscal_year, submission__reporting_fiscal_period= prior_fiscal_year_last_completed_fiscal_period, treasury_account_identifier__funding_toptier_agency=self. toptier_agency, ).aggregate(agency_budgetary_resources=Sum( "total_budgetary_resources_amount_cpe") )["agency_budgetary_resources"]
def test_calculate_last_completed_fiscal_quarter(): """ FY2000 Q1 == 1999-10-01 - 1999-12-31 available after 2000-02-14 FY2000 Q2 == 2000-01-01 - 2000-03-31 available after 2000-05-15 FY2000 Q3 == 2000-04-01 - 2000-06-30 available after 2000-08-14 FY2000 Q4 == 2000-07-01 - 2000-09-30 available after 2000-11-14 """ assert fyh.calculate_last_completed_fiscal_quarter(2000, date(1999, 10, 1)) is None assert fyh.calculate_last_completed_fiscal_quarter(2000, date(1999, 11, 1)) is None assert fyh.calculate_last_completed_fiscal_quarter(2000, date(1999, 12, 1)) is None assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 1, 1)) is None assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 2, 1)) is None assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 2, 14)) is None assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 2, 15)) == 1 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 3, 1)) == 1 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 4, 1)) == 1 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 5, 1)) == 1 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 5, 15)) == 1 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 5, 16)) == 2 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 6, 1)) == 2 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 7, 1)) == 2 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 8, 1)) == 2 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 9, 1)) == 3 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 10, 1)) == 3 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 11, 1)) == 3 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2000, 12, 1)) == 4 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2001, 1, 1)) == 4 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2001, 2, 1)) == 4 assert fyh.calculate_last_completed_fiscal_quarter(2000, date(2001, 3, 1)) == 4 # Test a fiscal year WAY in the future. We should definitely have flying cars by then. assert fyh.calculate_last_completed_fiscal_quarter(2010, date(2001, 3, 1)) is None # And one test using the default as_of_date just to make sure nothing catches on fire. assert fyh.calculate_last_completed_fiscal_quarter(2000) == 4
def setup_test_data(db): """ Insert data into DB for testing """ dabs = mommy.make("submissions.DABSSubmissionWindowSchedule", submission_reveal_date="2020-10-09") sub = mommy.make( "submissions.SubmissionAttributes", submission_id=1, toptier_code="123", quarter_format_flag=False, reporting_fiscal_year=2019, reporting_fiscal_period=6, published_date="2019-07-03", submission_window_id=dabs.id, ) sub2 = mommy.make( "submissions.SubmissionAttributes", submission_id=2, toptier_code="987", quarter_format_flag=False, reporting_fiscal_year=CURRENT_FISCAL_YEAR, reporting_fiscal_period=CURRENT_LAST_PERIOD, published_date=f"{CURRENT_FISCAL_YEAR}-{CURRENT_LAST_PERIOD+1:02}-07", submission_window_id=dabs.id, ) sub3 = mommy.make( "submissions.SubmissionAttributes", submission_id=3, toptier_code="001", quarter_format_flag=True, reporting_fiscal_year=CURRENT_FISCAL_YEAR, reporting_fiscal_period=CURRENT_LAST_PERIOD, published_date=f"{CURRENT_FISCAL_YEAR}-{CURRENT_LAST_PERIOD+1:02}-07", submission_window_id=dabs.id, ) mommy.make("references.Agency", id=1, toptier_agency_id=1, toptier_flag=True) mommy.make("references.Agency", id=2, toptier_agency_id=2, toptier_flag=True) mommy.make("references.Agency", id=3, toptier_agency_id=3, toptier_flag=True) agencies = [ mommy.make("references.ToptierAgency", toptier_agency_id=1, toptier_code="123", abbreviation="ABC", name="Test Agency"), mommy.make( "references.ToptierAgency", toptier_agency_id=2, toptier_code="987", abbreviation="XYZ", name="Test Agency 2", ), mommy.make( "references.ToptierAgency", toptier_agency_id=3, toptier_code="001", abbreviation="AAA", name="Test Agency 3", ), ] treas_accounts = [ mommy.make( "accounts.TreasuryAppropriationAccount", treasury_account_identifier=1, awarding_toptier_agency_id=agencies[0].toptier_agency_id, tas_rendering_label="tas-1-overview", ), mommy.make( "accounts.TreasuryAppropriationAccount", treasury_account_identifier=2, awarding_toptier_agency_id=agencies[2].toptier_agency_id, tas_rendering_label="tas-2-overview", ), mommy.make( "accounts.TreasuryAppropriationAccount", treasury_account_identifier=3, awarding_toptier_agency_id=agencies[1].toptier_agency_id, tas_rendering_label="tas-3-overview", ), ] approps = [ { "sub_id": sub.submission_id, "treasury_account": treas_accounts[0], "total_resources": 50 }, { "sub_id": sub3.submission_id, "treasury_account": treas_accounts[1], "total_resources": 12 }, { "sub_id": sub3.submission_id, "treasury_account": treas_accounts[1], "total_resources": 29 }, { "sub_id": sub2.submission_id, "treasury_account": treas_accounts[2], "total_resources": 15.5 }, ] for approp in approps: mommy.make( "accounts.AppropriationAccountBalances", submission_id=approp["sub_id"], treasury_account_identifier=approp["treasury_account"], total_budgetary_resources_amount_cpe=approp["total_resources"], ) reporting_tases = [ { "year": sub.reporting_fiscal_year, "period": sub.reporting_fiscal_period, "label": treas_accounts[0].tas_rendering_label, "toptier_code": agencies[0].toptier_code, "diff": 29.5, }, { "year": sub2.reporting_fiscal_year, "period": sub2.reporting_fiscal_period, "label": treas_accounts[1].tas_rendering_label, "toptier_code": agencies[2].toptier_code, "diff": -1.3, }, { "year": sub2.reporting_fiscal_year, "period": sub2.reporting_fiscal_period, "label": treas_accounts[2].tas_rendering_label, "toptier_code": agencies[1].toptier_code, "diff": 20.5, }, ] for reporting_tas in reporting_tases: mommy.make( "reporting.ReportingAgencyTas", fiscal_year=reporting_tas["year"], fiscal_period=reporting_tas["period"], tas_rendering_label=reporting_tas["label"], toptier_code=reporting_tas["toptier_code"], diff_approp_ocpa_obligated_amounts=reporting_tas["diff"], appropriation_obligated_amount=100, ) mommy.make( "reporting.ReportingAgencyOverview", reporting_agency_overview_id=1, toptier_code="123", fiscal_year=2019, fiscal_period=6, total_dollars_obligated_gtas=1788370.03, total_budgetary_resources=22478810.97, total_diff_approp_ocpa_obligated_amounts=84931.95, unlinked_procurement_c_awards=1, unlinked_assistance_c_awards=2, unlinked_procurement_d_awards=3, unlinked_assistance_d_awards=4, ) mommy.make( "reporting.ReportingAgencyOverview", reporting_agency_overview_id=2, toptier_code="987", fiscal_year=CURRENT_FISCAL_YEAR, fiscal_period=CURRENT_LAST_PERIOD, total_dollars_obligated_gtas=18.6, total_budgetary_resources=100, total_diff_approp_ocpa_obligated_amounts=0, unlinked_procurement_c_awards=10, unlinked_assistance_c_awards=20, unlinked_procurement_d_awards=30, unlinked_assistance_d_awards=40, ) mommy.make( "reporting.ReportingAgencyOverview", reporting_agency_overview_id=3, toptier_code="001", fiscal_year=CURRENT_FISCAL_YEAR, fiscal_period=CURRENT_LAST_PERIOD, total_dollars_obligated_gtas=20.0, total_budgetary_resources=10.0, total_diff_approp_ocpa_obligated_amounts=10.0, unlinked_procurement_c_awards=100, unlinked_assistance_c_awards=200, unlinked_procurement_d_awards=300, unlinked_assistance_d_awards=400, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="123", fiscal_year=2019, fiscal_period=6, tas_rendering_label="TAS 1", obligated_amount=10.0, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="123", fiscal_year=2019, fiscal_period=6, tas_rendering_label="TAS 2", obligated_amount=1.0, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="987", fiscal_year=CURRENT_FISCAL_YEAR, fiscal_period=CURRENT_LAST_PERIOD, tas_rendering_label="TAS 2", obligated_amount=12.0, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="987", fiscal_year=current_fiscal_year(), fiscal_period=get_final_period_of_quarter( calculate_last_completed_fiscal_quarter(current_fiscal_year())) or 3, tas_rendering_label="TAS 3", obligated_amount=0, )
import pytest from django.conf import settings from model_mommy import mommy from rest_framework import status from usaspending_api.common.helpers.fiscal_year_helpers import ( current_fiscal_year, calculate_last_completed_fiscal_quarter, get_final_period_of_quarter, ) url = "/api/v2/reporting/agencies/overview/" CURRENT_FISCAL_YEAR = current_fiscal_year() CURRENT_LAST_QUARTER = calculate_last_completed_fiscal_quarter( CURRENT_FISCAL_YEAR) or 1 CURRENT_LAST_PERIOD = get_final_period_of_quarter(CURRENT_LAST_QUARTER) or 3 assurance_statement_1 = ( f"{settings.FILES_SERVER_BASE_URL}/agency_submissions/Raw%20DATA%20Act%20Files/" "2019/P06/123%20-%20Test%20Agency%20(ABC)/2019-P06-123_Test%20Agency%20(ABC)-Assurance_Statement.txt" ) assurance_statement_2 = ( f"{settings.FILES_SERVER_BASE_URL}/agency_submissions/Raw%20DATA%20Act%20Files/" f"{CURRENT_FISCAL_YEAR}/P{CURRENT_LAST_PERIOD:02}/987%20-%20Test%20Agency%202%20(XYZ)/" f"{CURRENT_FISCAL_YEAR}-P{CURRENT_LAST_PERIOD:02}-987_Test%20Agency%202%20(XYZ)-Assurance_Statement.txt" ) assurance_statement_3 = ( f"{settings.FILES_SERVER_BASE_URL}/agency_submissions/Raw%20DATA%20Act%20Files/" f"{CURRENT_FISCAL_YEAR}/Q{CURRENT_LAST_QUARTER}/001%20-%20Test%20Agency%203%20(AAA)/" f"{CURRENT_FISCAL_YEAR}-Q{CURRENT_LAST_QUARTER}-001_Test%20Agency%203%20(AAA)-Assurance_Statement.txt"