def test_failure(database):
    """ Test invalid. For ActionType = A, the CFDA_Number must be active as of the ActionDate.
        Not apply to those with CorrectionLateDeleteIndicator = C.
        If publish_date > action_date > archive_date, reject that (not active).
    """

    cfda = CFDAProgram(program_number=12.340, published_date="20130427", archived_date="")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20120111',
                                                          action_type='A', correction_late_delete_ind="B")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20120111',
                                                          action_type='A', correction_late_delete_ind=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20120111',
                                                          action_type='A', correction_late_delete_ind="B")
    det_award_4 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20120111',
                                                          action_type='A', correction_late_delete_ind=None)

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, cfda])
    assert errors == 4

    # test for cfda_number that doesn't exist in the table
    cfda = CFDAProgram(program_number=12.340, published_date="20130427", archived_date="")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(cfda_number="54.321", action_date='20140111',
                                                          action_type='A', correction_late_delete_ind="B")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(cfda_number="AB.CDE", action_date='20140111',
                                                          action_type='A', correction_late_delete_ind=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(cfda_number="11.111", action_date='20130528',
                                                          action_type='B', correction_late_delete_ind="B")

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, cfda])
    assert errors == 3
def test_failure(database):
    """ BusinessTypes must be one to three letters in length. BusinessTypes values must be non-repeated letters
        from A to X. """

    # Test if it's somehow empty or has 4 letters (length test)
    det_award = DetachedAwardFinancialAssistanceFactory(business_types="")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(business_types="ABCD")

    errors = number_of_errors(_FILE, database, models=[det_award, det_award_2])
    assert errors == 2

    # Test repeats
    det_award = DetachedAwardFinancialAssistanceFactory(business_types="BOb")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(business_types="BOB")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(business_types="BbO")
    det_award_4 = DetachedAwardFinancialAssistanceFactory(business_types="BB")

    errors = number_of_errors(_FILE, database, models=[det_award, det_award_2, det_award_3, det_award_4])
    assert errors == 4

    # Test that only valid letters work
    det_award = DetachedAwardFinancialAssistanceFactory(business_types="ABY")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(business_types="C2")

    errors = number_of_errors(_FILE, database, models=[det_award, det_award_2])
    assert errors == 2
def test_failure(database):
    """ Test for USSGL 48XX & 49XX (except 487X & 497X) if any one is provided and
    by_direct_reimbursable_fun is empty the rule fails """

    op_dict = {'by_direct_reimbursable_fun': None, 'object_class': 123, 'ussgl480100_undelivered_or_fyb': None,
               'ussgl480100_undelivered_or_cpe': None, 'ussgl488100_upward_adjustm_cpe': None,
               'ussgl490100_delivered_orde_fyb': None, 'ussgl490100_delivered_orde_cpe': None,
               'ussgl498100_upward_adjustm_cpe': None, 'ussgl480200_undelivered_or_fyb': None,
               'ussgl480200_undelivered_or_cpe': None, 'ussgl488200_upward_adjustm_cpe': None,
               'ussgl490200_delivered_orde_cpe': None, 'ussgl490800_authority_outl_fyb': None,
               'ussgl490800_authority_outl_cpe': None, 'ussgl498200_upward_adjustm_cpe': None}

    keys = ['ussgl480100_undelivered_or_fyb', 'ussgl480100_undelivered_or_cpe',
            'ussgl488100_upward_adjustm_cpe', 'ussgl490100_delivered_orde_fyb',
            'ussgl490100_delivered_orde_cpe', 'ussgl498100_upward_adjustm_cpe',
            'ussgl480200_undelivered_or_fyb', 'ussgl480200_undelivered_or_cpe',
            'ussgl488200_upward_adjustm_cpe', 'ussgl490200_delivered_orde_cpe',
            'ussgl490800_authority_outl_fyb', 'ussgl490800_authority_outl_cpe',
            'ussgl498200_upward_adjustm_cpe']

    for i in range(len(keys)):
        op_dict_copy = copy.deepcopy(op_dict)
        op_dict_copy.pop(keys[i])
        op = ObjectClassProgramActivityFactory(**op_dict_copy)
        assert number_of_errors(_FILE, database, models=[op]) == 1

    op = ObjectClassProgramActivityFactory(by_direct_reimbursable_fun=None, object_class=123)
    assert number_of_errors(_FILE, database, models=[op]) == 1
def test_failure(database):
    """ Test failure for unique PIID, or combination of PIID/ParentAwardId, from file C exists in file D1 during the
        same reporting period. Do not process if allocation transfer agency is not null and does not match agency ID """

    # Perform when there's a transaction obligated amount value in the field
    af = AwardFinancialFactory(piid='some_piid', parent_award_id='some_parent_award_id',
                               allocation_transfer_agency=None, transaction_obligated_amou='12345')
    ap = AwardProcurementFactory(piid='some_other_piid', parent_award_id='some_parent_award_id')

    assert number_of_errors(_FILE, database, models=[af, ap]) == 1

    # Perform when there's an ata in the field and it matches the aid
    af = AwardFinancialFactory(piid='some_piid', parent_award_id='some_parent_award_id',
                               allocation_transfer_agency='bad', agency_identifier='bad',
                               transaction_obligated_amou='12345')
    ap = AwardProcurementFactory(piid='some_piid', parent_award_id='some_other_parent_award_id')

    assert number_of_errors(_FILE, database, models=[af, ap]) == 1

    af = AwardFinancialFactory(piid='some_piid', parent_award_id=None,
                               allocation_transfer_agency='bad', agency_identifier='bad',
                               transaction_obligated_amou='12345')
    ap = AwardProcurementFactory(piid='some_other_piid', parent_award_id='some_other_parent_award_id')

    assert number_of_errors(_FILE, database, models=[af, ap]) == 1
def test_pubished_date_success(database):
    """ Test valid. For (ActionType = B, C, or D), the CFDA_Number need NOT be active as of the ActionDate.
        Not apply to those with CorrectionDeleteIndicator = C.
        Active date: publish_date <= action_date <= archive_date (Fails validation if active).
    """

    cfda = CFDAProgram(program_number=12.340, published_date="20130427", archived_date="")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20140528',
                                                          action_type='B', correction_delete_indicatr="B")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20140428',
                                                          action_type='C', correction_delete_indicatr="D")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.340", action_date='20140428',
                                                          action_type='D', correction_delete_indicatr=None)
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, cfda])
    assert errors == 0

    cfda = CFDAProgram(program_number=12.350, published_date="20130427", archived_date="20140427")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.350", action_date='20130528',
                                                          action_type='B', correction_delete_indicatr="B")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.350", action_date='20130428',
                                                          action_type='C', correction_delete_indicatr="D")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.350", action_date='20130428',
                                                          action_type='D', correction_delete_indicatr=None)
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, cfda])
    assert errors == 0
def test_failure(database):
    """ Test for USSGL 48XX & 49XX (except 487X & 497X) if any one is provided and
    by_direct_reimbursable_fun is empty the rule fails """

    af_dict = {'by_direct_reimbursable_fun': None, 'ussgl480100_undelivered_or_fyb': None,
               'ussgl480100_undelivered_or_cpe': None, 'ussgl488100_upward_adjustm_cpe': None,
               'ussgl490100_delivered_orde_fyb': None, 'ussgl490100_delivered_orde_cpe': None,
               'ussgl498100_upward_adjustm_cpe': None, 'ussgl480200_undelivered_or_fyb': None,
               'ussgl480200_undelivered_or_cpe': None, 'ussgl488200_upward_adjustm_cpe': None,
               'ussgl490200_delivered_orde_cpe': None, 'ussgl490800_authority_outl_fyb': None,
               'ussgl490800_authority_outl_cpe': None, 'ussgl498200_upward_adjustm_cpe': None}

    keys = ['ussgl480100_undelivered_or_fyb', 'ussgl480100_undelivered_or_cpe',
            'ussgl488100_upward_adjustm_cpe', 'ussgl490100_delivered_orde_fyb',
            'ussgl490100_delivered_orde_cpe', 'ussgl498100_upward_adjustm_cpe',
            'ussgl480200_undelivered_or_fyb', 'ussgl480200_undelivered_or_cpe',
            'ussgl488200_upward_adjustm_cpe', 'ussgl490200_delivered_orde_cpe',
            'ussgl490800_authority_outl_fyb', 'ussgl490800_authority_outl_cpe',
            'ussgl498200_upward_adjustm_cpe']

    # Takes out one required key at a time and check that we get exactly one
    # error
    for i in range(len(keys)):
        af_dict_copy = copy.deepcopy(af_dict)
        af_dict_copy.pop(keys[i])
        af = AwardFinancialFactory(**af_dict_copy)
        assert number_of_errors(_FILE, database, models=[af]) == 1

    af = AwardFinancialFactory(by_direct_reimbursable_fun=None)
    assert number_of_errors(_FILE, database, models=[af]) == 1
def test_pubished_date_failure(database):
    """ Test invalid. For (ActionType = B, C, or D), the CFDA_Number need NOT be active as of the ActionDate.
        Not apply to those with CorrectionLateDeleteIndicator = C.
        Active date: publish_date <= action_date <= archive_date (Fails validation if active).
        If action date is < published_date, should trigger a warning.
    """

    cfda = CFDAProgram(program_number="12.345", published_date="20130427", archived_date="")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.345", action_date='20120528',
                                                          action_type='B', correction_late_delete_ind="B")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.345", action_date='20120427',
                                                          action_type='C', correction_late_delete_ind="D")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.345", action_date='20120428',
                                                          action_type='D', correction_late_delete_ind=None)
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, cfda])
    assert errors == 3

    cfda = CFDAProgram(program_number="12.345", published_date="20130427", archived_date="20140528")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.345", action_date='20120528',
                                                          action_type='B', correction_late_delete_ind="B")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.345", action_date='20150427',
                                                          action_type='C', correction_late_delete_ind="D")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(cfda_number="12.345", action_date='20150428',
                                                          action_type='D', correction_late_delete_ind=None)
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, cfda])
    assert errors == 3
def test_ignored_and_failed_original_loan_subsidy_cost_values(database):
    """ Tests that a single warning is thrown for both a federal action obligation of 0
        and an original loan subsidy cost of 0 """

    tas = _TAS
    afa = AwardFinancialAssistanceFactory(tas=tas, fain='abc', uri=None, federal_action_obligation=1,
                                          original_loan_subsidy_cost='0', assistance_type='09')
    afa_2 = AwardFinancialAssistanceFactory(tas=tas, fain='abc', uri=None, federal_action_obligation=1,
                                            original_loan_subsidy_cost='-2.3', assistance_type='09')
    afa_3 = AwardFinancialAssistanceFactory(tas=tas, fain='abc', uri=None, federal_action_obligation=1,
                                            original_loan_subsidy_cost='2.3', assistance_type='08')
    af = AwardFinancialFactory(tas=tas, submisson_id=afa.submission_id, fain=None, uri=None)

    errors = number_of_errors(_FILE, database, models=[afa, af, afa_2, afa_3])
    assert errors == 3

    # Test that this is ignored if assistance type is 08
    afa = AwardFinancialAssistanceFactory(tas=tas, fain='abc', uri=None, federal_action_obligation=1,
                                          original_loan_subsidy_cost='0', assistance_type='08')
    afa_2 = AwardFinancialAssistanceFactory(tas=tas, fain='abc', uri=None, federal_action_obligation=1,
                                            original_loan_subsidy_cost='-2.3', assistance_type='08')
    af = AwardFinancialFactory(tas=tas, submisson_id=afa.submission_id, fain=None, uri=None)

    errors = number_of_errors(_FILE, database, models=[afa, af, afa_2])
    assert errors == 0
def test_success(database):
    """ Test for USSGL 48XX & 49XX (except 487X & 497X) if any one is provided then
    by_direct_reimbursable_fun is not empty """

    op = ObjectClassProgramActivityFactory()
    assert number_of_errors(_FILE, database, models=[op]) == 0

    op = ObjectClassProgramActivityFactory(object_class=1234, by_direct_reimbursable_fun=None)
    assert number_of_errors(_FILE, database, models=[op]) == 0
def test_failure(database):
    """ ObligationsUndeliveredOrdersUnpaidTotal in File C != USSGL 4801 + 4881 in File C for the same date context
    (FYB) """

    af = AwardFinancialFactory(obligations_undelivered_or_fyb=1, ussgl480100_undelivered_or_fyb=None)

    assert number_of_errors(_FILE, database, models=[af]) == 1

    af = AwardFinancialFactory(obligations_undelivered_or_fyb=1, ussgl480100_undelivered_or_fyb=2)

    assert number_of_errors(_FILE, database, models=[af]) == 1
def test_success(database):
    """ ObligationsDeliveredOrdersUnpaidTotal in File C = USSGL 4901 + 4981 in File C for the same date context
    (FYB) """

    af = AwardFinancialFactory(obligations_delivered_orde_fyb=None, ussgl490100_delivered_orde_fyb=None)

    assert number_of_errors(_FILE, database, models=[af]) == 0

    af = AwardFinancialFactory(obligations_delivered_orde_fyb=1, ussgl490100_delivered_orde_fyb=1)

    assert number_of_errors(_FILE, database, models=[af]) == 0
def test_failure(database):
    """ Unique PIID, ParentAwardId from file D1 doesn't exist in file C during the same reporting period,
        except D1 records with zero FederalActionObligation """

    ap = AwardProcurementFactory(piid='some_piid', parent_award_id='some_parent_award_id', federal_action_obligation=1)
    af = AwardFinancialFactory(piid='some_other_piid', parent_award_id='some_parent_award_id')

    assert number_of_errors(_FILE, database, models=[af, ap]) == 1

    ap = AwardProcurementFactory(piid='some_piid', parent_award_id='some_parent_award_id', federal_action_obligation=1)
    af = AwardFinancialFactory(piid='some_piid', parent_award_id='some_other_parent_award_id')

    assert number_of_errors(_FILE, database, models=[af, ap]) == 1
def test_failure(database):
    """ ObligationsDeliveredOrdersUnpaidTotal in File C != USSGL 4901 + 4981 in File C for the same date context
    (CPE) """

    af = AwardFinancialFactory(obligations_delivered_orde_cpe=1, ussgl490100_delivered_orde_cpe=None,
                               ussgl498100_upward_adjustm_cpe=None)

    assert number_of_errors(_FILE, database, models=[af]) == 1

    af = AwardFinancialFactory(obligations_delivered_orde_cpe=1, ussgl490100_delivered_orde_cpe=1,
                               ussgl498100_upward_adjustm_cpe=1)

    assert number_of_errors(_FILE, database, models=[af]) == 1
def test_failure(database):
    """ DeobligationsRecoveriesRefundsOfPriorYearByProgramObjectClass_CPE in File B != USSGL(4871+ 4872 + 4971 + 4972)
    in File B for the same reporting period """

    op = ObjectClassProgramActivityFactory(deobligations_recov_by_pro_cpe=1, ussgl487100_downward_adjus_cpe=None,
                                           ussgl487200_downward_adjus_cpe=None, ussgl497100_downward_adjus_cpe=None,
                                           ussgl497200_downward_adjus_cpe=None)

    assert number_of_errors(_FILE, database, models=[op]) == 1

    op = ObjectClassProgramActivityFactory(deobligations_recov_by_pro_cpe=1, ussgl487100_downward_adjus_cpe=1,
                                           ussgl487200_downward_adjus_cpe=1, ussgl497100_downward_adjus_cpe=1,
                                           ussgl497200_downward_adjus_cpe=1)

    assert number_of_errors(_FILE, database, models=[op]) == 1
def test_failure(database):
    """ Test invalid object class code (3 digits) """

    # This should return because if it's '0000' '000', '00', '0' a warning should be returned
    op = ObjectClassProgramActivityFactory(object_class='0000')
    assert number_of_errors(_FILE, database, models=[op]) == 1

    op = ObjectClassProgramActivityFactory(object_class='000')
    assert number_of_errors(_FILE, database, models=[op]) == 1

    op = ObjectClassProgramActivityFactory(object_class='00')
    assert number_of_errors(_FILE, database, models=[op]) == 1

    op = ObjectClassProgramActivityFactory(object_class='0')
    assert number_of_errors(_FILE, database, models=[op]) == 1
def test_failure(database):
    """ Test invalid object class code (3 digits) """

    # This should return because if it's '0000' '000', '00', '0' a warning should be returned
    af = AwardFinancialFactory(object_class='0000')
    assert number_of_errors(_FILE, database, models=[af]) == 1

    af = AwardFinancialFactory(object_class='000')
    assert number_of_errors(_FILE, database, models=[af]) == 1

    af = AwardFinancialFactory(object_class='00')
    assert number_of_errors(_FILE, database, models=[af]) == 1

    af = AwardFinancialFactory(object_class='0')
    assert number_of_errors(_FILE, database, models=[af]) == 1
def test_success(database):
    """ The provided PrimaryPlaceofPerformanceZIP+4 must be in the state specified by PrimaryPlaceOfPerformanceCode.
        In this specific submission row, the first five digits are valid and located in the correct state, but the
        last 4 are invalid."""

    zips = Zips(zip5="12345", zip_last4="6789", state_abbreviation="NY")
    # ignored because no zip4
    det_award_1 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY*****",
                                                          place_of_performance_zip4a="")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny**123",
                                                          place_of_performance_zip4a=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny**123",
                                                          place_of_performance_zip4a='city-wide')
    # valid 9 digit zip
    det_award_4 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY98765",
                                                          place_of_performance_zip4a="123456789")
    det_award_5 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny98765",
                                                          place_of_performance_zip4a="123456789")
    det_award_6 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny98765",
                                                          place_of_performance_zip4a="12345-6789")
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, det_award_5,
                                                       det_award_6, zips])
    assert errors == 0

    # random wrong length zips and zips with '-' in the wrong place, formatting is checked in another rule
    det_award_1 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10986",
                                                          place_of_performance_zip4a="12345678")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10986",
                                                          place_of_performance_zip4a="1234567898")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10986",
                                                          place_of_performance_zip4a="12345678-9")
    det_award_4 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10986",
                                                          place_of_performance_zip4a="123-456789")
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, zips])
    assert errors == 0

    # invalid 5 digit zip - this should pass but is handled in d41_3
    det_award_1 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10986",
                                                          place_of_performance_zip4a="12346")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NA*****",
                                                          place_of_performance_zip4a='12345')
    det_award_3 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NA*****",
                                                          place_of_performance_zip4a='12346-6789')
    # valid 5 digit zip
    det_award_4 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny**123",
                                                          place_of_performance_zip4a="12345")
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, zips])
    assert errors == 0
def test_success(database):
    """ If both are submitted, FundingSubTierAgencyCode and FundingOfficeCode must belong to the same
        FundingAgencyCode (per the Federal Hierarchy). Ignored if one or both are missing. """
    cgac = CGAC(cgac_id=1, cgac_code='001', agency_name='test')
    frec = FREC(frec_id=1, cgac_id=1, frec_code='0001', agency_name='test2')
    # sub tier codes are different on these offices to prove that we don't care if the office is under that sub tier
    # as long as the top tier codes match
    office_1 = OfficeFactory(office_code='12345a', sub_tier_code='abcd', agency_code=cgac.cgac_code)
    office_2 = OfficeFactory(office_code='123457', sub_tier_code='efgh', agency_code=frec.frec_code)
    agency_1 = SubTierAgency(sub_tier_agency_code='0000', cgac_id=1, frec_id=1, is_frec=False)
    agency_2 = SubTierAgency(sub_tier_agency_code='0001', cgac_id=1, frec_id=1, is_frec=True)

    # Same agency for cgac
    det_award_1 = DetachedAwardFinancialAssistanceFactory(funding_sub_tier_agency_co=agency_1.sub_tier_agency_code,
                                                          funding_office_code=office_1.office_code)
    # Same agency for cgac (uppercase)
    det_award_1 = DetachedAwardFinancialAssistanceFactory(funding_sub_tier_agency_co=agency_1.sub_tier_agency_code,
                                                          funding_office_code=office_1.office_code.upper())
    # Same agency for frec
    det_award_2 = DetachedAwardFinancialAssistanceFactory(funding_sub_tier_agency_co=agency_2.sub_tier_agency_code,
                                                          funding_office_code=office_2.office_code)
    # Missing sub tier code
    det_award_3 = DetachedAwardFinancialAssistanceFactory(funding_sub_tier_agency_co='',
                                                          funding_office_code=office_2.office_code)
    # Missing office code
    det_award_4 = DetachedAwardFinancialAssistanceFactory(funding_sub_tier_agency_co=agency_1.sub_tier_agency_code,
                                                          funding_office_code=None)

    errors = number_of_errors(_FILE, database, models=[cgac, frec, office_1, office_2, agency_1, agency_2, det_award_1,
                                                       det_award_2, det_award_3, det_award_4])
    assert errors == 0
def test_failure(database):
    """ Test that a three digit object class with no flag is an error"""
    # Create a 12 character random uri
    uri = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_two = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    first_uri_row_one = AwardFinancialFactory(transaction_obligated_amou=1100, uri=uri,
                                              allocation_transfer_agency=None)
    first_uri_row_two = AwardFinancialFactory(transaction_obligated_amou=11, uri=uri,
                                              allocation_transfer_agency=None)
    # And add a row that shouldn't be included
    second_uri_row_one = AwardFinancialFactory(transaction_obligated_amou=9999, uri=uri_two,
                                               allocation_transfer_agency=None)
    first_afa_row = AwardFinancialAssistanceFactory(uri=uri, federal_action_obligation=-1100,
                                                    original_loan_subsidy_cost=None)
    second_afa_row = AwardFinancialAssistanceFactory(uri=uri, federal_action_obligation=-10,
                                                     original_loan_subsidy_cost=None)
    other_uri_afa_row = AwardFinancialAssistanceFactory(uri=uri_two, federal_action_obligation=-9999,
                                                        original_loan_subsidy_cost=None)
    other_uri_loan_afa_row = AwardFinancialAssistanceFactory(uri=uri_two, federal_action_obligation=None,
                                                             original_loan_subsidy_cost=-1000, assistance_type='07')

    errors = number_of_errors(_FILE, database, models=[first_uri_row_one, first_uri_row_two, second_uri_row_one,
                                                       first_afa_row, second_afa_row, other_uri_afa_row,
                                                       other_uri_loan_afa_row])
    assert errors == 2
def test_success(database):
    """ For PrimaryPlaceOfPerformanceCode XX##### city must exist in provided state (zip4 provided, warning).
        Ignore for all other formats of PrimaryPlaceOfPerformanceCode """

    # XX00000 validates here because it passes as long as the zip is valid in that state, this is checked
    # in a different place
    city_code = CityCode(city_code="10987", state_code="NY")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY*****",
                                                          place_of_performance_zip4a="2")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY**123",
                                                          place_of_performance_zip4a="1")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY**123",
                                                          place_of_performance_zip4a=None)
    det_award_4 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10987",
                                                          place_of_performance_zip4a="12345")
    det_award_5 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY10987",
                                                          place_of_performance_zip4a="12345-6789")
    det_award_6 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Na10987",
                                                          place_of_performance_zip4a=None)
    det_award_7 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny10988",
                                                          place_of_performance_zip4a='')
    det_award_8 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="nY000000",
                                                          place_of_performance_zip4a='12345')
    det_award_9 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny10988",
                                                          place_of_performance_zip4a='city-wide')
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, det_award_5,
                                                       det_award_6, det_award_7, det_award_8, det_award_9, city_code])
    assert errors == 0
def test_success(database):
    """ For PrimaryPlaceOfPerformanceCode XX##### or XX####R, where PrimaryPlaceOfPerformanceZIP+4 is blank or
        "city-wide": city code ##### or ####R must be valid and exist in the provided state.
    """

    city_code = CityCode(city_code="10987", state_code="NY")
    city_code_2 = CityCode(city_code="1098R", state_code="NY")
    det_award_1 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY*****",
                                                          place_of_performance_zip4a="2")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY**123",
                                                          place_of_performance_zip4a="1")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NY**123",
                                                          place_of_performance_zip4a=None)
    det_award_4 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="ny10986",
                                                          place_of_performance_zip4a="12345")
    det_award_5 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Na10987",
                                                          place_of_performance_zip4a="12345-6789")
    det_award_6 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny10987",
                                                          place_of_performance_zip4a=None)
    det_award_7 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny10987",
                                                          place_of_performance_zip4a='')
    det_award_8 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny10987",
                                                          place_of_performance_zip4a="city-wide")
    # Testing with R ending
    det_award_9 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny1098R",
                                                          place_of_performance_zip4a="city-wide")
    det_award_10 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="Ny1098R",
                                                           place_of_performance_zip4a=None)
    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, det_award_5,
                                                       det_award_6, det_award_7, det_award_8, det_award_9, det_award_10,
                                                       city_code, city_code_2])
    assert errors == 0
def test_failure(database):
    """ Test failure for each unique PIID in File C, the sum of each TransactionObligatedAmount should match (but with
        opposite signs) the sum of the FederalActionObligation reported in D1. This rule does not apply if the ATA field
        is populated and is different from the Agency ID. Ignore rows that contain a PAID. """
    # Create a 12 character random piid
    piid_1 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    piid_2 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    piid_3 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))

    # No ATA, not matching (off by 1)
    af_1_row_1 = AwardFinancialFactory(transaction_obligated_amou=1100, piid=piid_1, parent_award_id='',
                                       allocation_transfer_agency=None)
    af_1_row_2 = AwardFinancialFactory(transaction_obligated_amou=11, piid=piid_1, parent_award_id=None,
                                       allocation_transfer_agency=None)

    # No ATA, not matching, one record, no paid
    af_2 = AwardFinancialFactory(transaction_obligated_amou=9999, piid=piid_2, parent_award_id=None,
                                 allocation_transfer_agency=None)

    # Matching ATA, should not be ignored
    af_3 = AwardFinancialFactory(transaction_obligated_amou=11, piid=piid_3, parent_award_id=None,
                                 allocation_transfer_agency="123", agency_identifier="123")

    # Award Procurement portion of checks
    # Sum of all these would be sum of piid_1 af if one wasn't ignored
    ap_1_row_1 = AwardProcurementFactory(piid=piid_1, parent_award_id=None, federal_action_obligation=-1100)
    ap_1_row_2 = AwardProcurementFactory(piid=piid_1, parent_award_id=None, federal_action_obligation=-10)
    # second piid that simply doesn't match
    ap_2 = AwardProcurementFactory(piid=piid_2, parent_award_id=None, federal_action_obligation=-1111)
    # third piid that should not be ignored because ATA is present but matches
    ap_3 = AwardProcurementFactory(piid=piid_3, parent_award_id=None, federal_action_obligation=0)

    errors = number_of_errors(_FILE, database, models=[af_1_row_1, af_1_row_2, af_2, af_3, ap_1_row_1, ap_1_row_2, ap_2,
                                                       ap_3])
    assert errors == 3
def test_success(database):
    """ Test If LegalEntityCongressionalDistrict is provided, it must be valid in the 5-digit zip code indicated by
        LegalEntityZIP5. Districts that were created under the 2000 census or later are considered valid. This rule is
        ignored when CorrectionDeleteIndicator is D """
    zip1 = ZipsFactory(zip5="12345", congressional_district_no="01", state_abbreviation="AB")
    zip2 = ZipsFactory(zip5="23456", congressional_district_no="01", state_abbreviation="CD")
    sc1 = StateCongressionalFactory(state_code="AB", congressional_district_no="01", census_year=None)
    sc2 = StateCongressionalFactory(state_code="CD", congressional_district_no="02", census_year=2000)

    det_award_1 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="12345", legal_entity_congressional="",
                                                          correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="", legal_entity_congressional="01",
                                                          correction_delete_indicatr='C')
    det_award_3 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="", legal_entity_congressional="",
                                                          correction_delete_indicatr='c')
    det_award_4 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="", legal_entity_congressional="",
                                                          correction_delete_indicatr='d')
    det_award_5 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="12345", legal_entity_congressional=None,
                                                          correction_delete_indicatr=None)
    det_award_6 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="12345", legal_entity_congressional="01",
                                                          correction_delete_indicatr='c')
    det_award_7 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="23456", legal_entity_congressional="02",
                                                          correction_delete_indicatr='')
    # Test ignore cdi of D
    det_award_8 = DetachedAwardFinancialAssistanceFactory(legal_entity_zip5="12345", legal_entity_congressional="03",
                                                          correction_delete_indicatr='d')

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, det_award_5,
                                                       det_award_6, det_award_7, det_award_8, zip1, zip2, sc1, sc2])
    assert errors == 0
def test_success(database):
    """ Test that calculation passes with equal values and with a null """

    value_one = Decimal('101.23')
    value_two = Decimal('102.34')
    value_three = Decimal('103.45')
    value_four = Decimal('104.56')
    award_fin = AwardFinancialFactory(gross_outlay_amount_by_awa_cpe=value_one - value_three + value_two - value_four,
                                      gross_outlays_undelivered_cpe=value_one,
                                      gross_outlays_delivered_or_cpe=value_two,
                                      gross_outlays_undelivered_fyb=value_three,
                                      gross_outlays_delivered_or_fyb=value_four)
    award_fin_2 = AwardFinancialFactory(gross_outlay_amount_by_awa_cpe=-value_three + value_two - value_four,
                                        gross_outlays_undelivered_cpe=None,
                                        gross_outlays_delivered_or_cpe=value_two,
                                        gross_outlays_undelivered_fyb=value_three,
                                        gross_outlays_delivered_or_fyb=value_four)
    award_fin_3 = AwardFinancialFactory(gross_outlay_amount_by_awa_cpe=value_one - value_three - value_four,
                                        gross_outlays_undelivered_cpe=value_one,
                                        gross_outlays_delivered_or_cpe=None,
                                        gross_outlays_undelivered_fyb=value_three,
                                        gross_outlays_delivered_or_fyb=value_four)
    award_fin_4 = AwardFinancialFactory(gross_outlay_amount_by_awa_cpe=value_one + value_two - value_four,
                                        gross_outlays_undelivered_cpe=value_one,
                                        gross_outlays_delivered_or_cpe=value_two,
                                        gross_outlays_undelivered_fyb=None,
                                        gross_outlays_delivered_or_fyb=value_four)
    award_fin_5 = AwardFinancialFactory(gross_outlay_amount_by_awa_cpe=value_one - value_three + value_two,
                                        gross_outlays_undelivered_cpe=value_one,
                                        gross_outlays_delivered_or_cpe=value_two,
                                        gross_outlays_undelivered_fyb=value_three,
                                        gross_outlays_delivered_or_fyb=None)

    assert number_of_errors(_FILE, database, models=[award_fin, award_fin_2, award_fin_3, award_fin_4,
                                                     award_fin_5]) == 0
def test_failure(database):
    """ Test failure PrimaryPlaceOfPerformanceCongressionalDistrict exists in the state indicated by the
        PrimaryPlaceOfPerformanceCode or is 90 in a state with multiple districts or when PrimaryPlaceOfPerformanceCode
        is 00*****. Districts that were created under the 2000 census or later are considered valid.
    """
    state_congr_1 = StateCongressionalFactory(congressional_district_no="01", state_code="NY", census_year=None)
    state_congr_2 = StateCongressionalFactory(congressional_district_no="02", state_code="NY", census_year=None)
    state_congr_3 = StateCongressionalFactory(congressional_district_no="01", state_code="PA", census_year=None)
    state_congr_4 = StateCongressionalFactory(congressional_district_no="03", state_code="NJ", census_year=1999)

    det_award_1 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="nY12345",
                                                          place_of_performance_congr="03")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="PA12345",
                                                          place_of_performance_congr="02")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="PA**345",
                                                          place_of_performance_congr="90")
    det_award_4 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="00*****",
                                                          place_of_performance_congr="01")
    det_award_5 = DetachedAwardFinancialAssistanceFactory(place_of_performance_code="NJ12345",
                                                          place_of_performance_congr="03")

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, det_award_5,
                                                       state_congr_1, state_congr_2, state_congr_3,
                                                       state_congr_4])
    assert errors == 5
def test_no_previous_submission(database):
    """ No previous submission and null gross_outlays_delivered_or_fyb """
    populate_publish_status(database)
    sub_new = SubmissionFactory()
    ocpa_new = ObjectClassProgramActivityFactory(submission_id=sub_new.submission_id,
                                                 gross_outlays_delivered_or_fyb=None)
    assert number_of_errors(_FILE, database, submission=sub_new, models=[ocpa_new]) == 1
def test_success(database):
    """ AwardeeOrRecipientUniqueIdentifier Field must be blank for aggregate records (RecordType=1)
        and individual recipients (BusinessTypes includes 'P'). """
    det_award_1 = DetachedAwardFinancialAssistanceFactory(record_type=1, business_types="ABP",
                                                          awardee_or_recipient_uniqu='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(record_type=1, business_types="ABC",
                                                          awardee_or_recipient_uniqu=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="pbc",
                                                          awardee_or_recipient_uniqu=None)
    det_award_4 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="PBC",
                                                          awardee_or_recipient_uniqu='')
    det_award_5 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="apc",
                                                          awardee_or_recipient_uniqu='')
    det_award_6 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="APC",
                                                          awardee_or_recipient_uniqu=None)
    det_award_7 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="abp",
                                                          awardee_or_recipient_uniqu='')
    det_award_8 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="ABP",
                                                          awardee_or_recipient_uniqu=None)
    det_award_9 = DetachedAwardFinancialAssistanceFactory(record_type=2, business_types="ABC",
                                                          awardee_or_recipient_uniqu='test')

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4, det_award_5,
                                                       det_award_6, det_award_7, det_award_8, det_award_9])
    assert errors == 0
def test_success(database):
    """ Test that a four digit object class with no flag is a success, and a three digit object class with
        a flag is a success """
    # Create a 12 character random uri
    uri = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_two = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_three = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    first_uri_row_one = AwardFinancialFactory(transaction_obligated_amou=1100, uri=uri,
                                              allocation_transfer_agency=None)
    first_uri_row_two = AwardFinancialFactory(transaction_obligated_amou=11, uri=uri,
                                              allocation_transfer_agency=None)
    # And add a row for a different uri
    second_uri_row_one = AwardFinancialFactory(transaction_obligated_amou=9999, uri=uri_two,
                                               allocation_transfer_agency=None)
    third_uri_row_one = AwardFinancialFactory(transaction_obligated_amou=8888, uri=uri_three,
                                              allocation_transfer_agency=123)

    first_afa_row = AwardFinancialAssistanceFactory(uri=uri, federal_action_obligation=-1100,
                                                    original_loan_subsidy_cost=None)
    second_afa_row = AwardFinancialAssistanceFactory(uri=uri, federal_action_obligation=-10,
                                                     original_loan_subsidy_cost=None)
    third_afa_row = AwardFinancialAssistanceFactory(uri=uri, original_loan_subsidy_cost=-1, assistance_type='08',
                                                    federal_action_obligation=None)
    wrong_type_afa_row = AwardFinancialAssistanceFactory(uri=uri, original_loan_subsidy_cost=-2222,
                                                         assistance_type='09', federal_action_obligation=None)
    other_uri_afa_row = AwardFinancialAssistanceFactory(uri=uri_two, federal_action_obligation=-9999,
                                                        original_loan_subsidy_cost=None)
    third_uri_ap_row = AwardFinancialAssistanceFactory(uri=uri_three, federal_action_obligation=-9999)

    errors = number_of_errors(_FILE, database, models=[first_uri_row_one, first_uri_row_two, second_uri_row_one,
                              first_afa_row, second_afa_row, third_afa_row, wrong_type_afa_row, other_uri_afa_row,
                              third_uri_row_one, third_uri_ap_row])
    assert errors == 0
def test_failure(database):
    """ Test failure when LegalEntityZIPLast4 isn't blank for aggregate records (i.e., when RecordType = 1) """

    det_award = DetachedAwardFinancialAssistanceFactory(record_type=1, legal_entity_zip_last4="Test")

    errors = number_of_errors(_FILE, database, models=[det_award])
    assert errors == 1
def test_failure(database):
    """ Test failure for each unique URI in File C, the sum of each TransactionObligatedAmount should match (but with
        opposite signs) the sum of the FederalActionObligation or OriginalLoanSubsidyCost amounts reported in D2. This
        rule does not apply if the ATA field is populated and is different from the Agency ID. """
    # Create a 12 character random uri
    uri_1 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_2 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_3 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))

    # Simple addition that doesn't add up right
    af_1_row_1 = AwardFinancialFactory(transaction_obligated_amou=1100, uri=uri_1, allocation_transfer_agency=None)
    af_1_row_2 = AwardFinancialFactory(transaction_obligated_amou=11, uri=uri_1, allocation_transfer_agency=None)
    # Incorrect addition based on assistance type in AFA
    af_2 = AwardFinancialFactory(transaction_obligated_amou=9999, uri=uri_2, allocation_transfer_agency=None)
    # Don't ignore when ATA and AID match
    af_3 = AwardFinancialFactory(transaction_obligated_amou=1100, uri=uri_3, allocation_transfer_agency="good",
                                 agency_identifier="good")

    # Sum of this uri doesn't add up to af uri sum
    afa_1_row_1 = AwardFinancialAssistanceFactory(uri=uri_1, federal_action_obligation=-1100,
                                                  original_loan_subsidy_cost=None)
    afa_1_row_2 = AwardFinancialAssistanceFactory(uri=uri_1, federal_action_obligation=-10,
                                                  original_loan_subsidy_cost=None)
    # Both of these rows use the column that isn't filled in for summing so neither results in the correct number
    afa_2_row_1 = AwardFinancialAssistanceFactory(uri=uri_2, federal_action_obligation=-9999,
                                                  original_loan_subsidy_cost=None)
    afa_2_row_2 = AwardFinancialAssistanceFactory(uri=uri_2, federal_action_obligation=None,
                                                  original_loan_subsidy_cost=-1000, assistance_type='07')
    # This shouldn't be ignored
    afa_3 = AwardFinancialAssistanceFactory(uri=uri_3, federal_action_obligation=0, original_loan_subsidy_cost=None)

    errors = number_of_errors(_FILE, database, models=[af_1_row_1, af_1_row_2, af_2, af_3, afa_1_row_1, afa_1_row_2,
                                                       afa_2_row_1, afa_2_row_2, afa_3])
    assert errors == 3
Example #31
0
def test_success(database):
    """ OriginalLoanSubsidyCost is required for loans (i.e., when AssistanceType = 07 or 08). """

    det_award = DetachedAwardFinancialAssistanceFactory(
        assistance_type='07',
        original_loan_subsidy_cost=0,
        correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        assistance_type='08',
        original_loan_subsidy_cost=20,
        correction_delete_indicatr='c')
    # Ignore correction delete indicator of D
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        assistance_type='08',
        original_loan_subsidy_cost=None,
        correction_delete_indicatr='d')

    errors = number_of_errors(_FILE,
                              database,
                              models=[det_award, det_award_2, det_award_3])
    assert errors == 0
Example #32
0
def test_success(database):
    """ Test LegalEntityCongressionalDistrict must be blank for aggregate records (RecordType = 1). """
    fabs_1 = FABSFactory(legal_entity_congressional=None,
                         record_type=1,
                         correction_delete_indicatr='C')
    fabs_2 = FABSFactory(legal_entity_congressional='',
                         record_type=1,
                         correction_delete_indicatr=None)
    fabs_3 = FABSFactory(legal_entity_congressional='01',
                         record_type=2,
                         correction_delete_indicatr=None)

    # Ignoring for correction_delete_indicator of D
    fabs_4 = FABSFactory(legal_entity_congressional='',
                         record_type=1,
                         correction_delete_indicatr='D')

    errors = number_of_errors(_FILE,
                              database,
                              models=[fabs_1, fabs_2, fabs_3, fabs_4])
    assert errors == 0
Example #33
0
def test_success(database):
    """ BusinessTypes must be one to three letters in length. BusinessTypes values must be non-repeated letters
        from A to X.
    """
    det_award = DetachedAwardFinancialAssistanceFactory(
        business_types='A', correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        business_types='XB', correction_delete_indicatr=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        business_types='RCm', correction_delete_indicatr='c')
    det_award_4 = DetachedAwardFinancialAssistanceFactory(
        business_types='rcm', correction_delete_indicatr='C')
    # Ignore correction delete indicator of D
    det_award_5 = DetachedAwardFinancialAssistanceFactory(
        business_types='BOB', correction_delete_indicatr='d')

    errors = number_of_errors(
        _FILE,
        database,
        models=[det_award, det_award_2, det_award_3, det_award_4, det_award_5])
    assert errors == 0
def test_success(database):
    """ Test success LegalEntityForeignPostalCode must be blank for domestic recipients (LegalEntityCountryCode = USA)
        and for aggregate records (RecordType = 1).
    """

    fabs_1 = FABSFactory(legal_entity_country_code='Spain', legal_entity_foreign_posta='12345', record_type=2,
                         correction_delete_indicatr='')
    fabs_2 = FABSFactory(legal_entity_country_code='Peru', legal_entity_foreign_posta='', record_type=1,
                         correction_delete_indicatr=None)
    fabs_3 = FABSFactory(legal_entity_country_code='Peru', legal_entity_foreign_posta=None, record_type=3,
                         correction_delete_indicatr='c')
    fabs_4 = FABSFactory(legal_entity_country_code='UsA', legal_entity_foreign_posta=None, record_type=2,
                         correction_delete_indicatr='C')
    fabs_5 = FABSFactory(legal_entity_country_code='USA', legal_entity_foreign_posta='', record_type=3,
                         correction_delete_indicatr='')
    # Ignore correction delete indicator of D
    fabs_6 = FABSFactory(legal_entity_country_code='UKR', legal_entity_foreign_posta='12345', record_type=1,
                         correction_delete_indicatr='d')

    errors = number_of_errors(_FILE, database, models=[fabs_1, fabs_2, fabs_3, fabs_4, fabs_5, fabs_6])
    assert errors == 0
def test_success(database):
    """ AwardingSubTierAgencyCode must be provided when AwardingOfficeCode is not provided. """

    # Missing office code, has sub tier code
    det_award_1 = DetachedAwardFinancialAssistanceFactory(awarding_sub_tier_agency_c='000', awarding_office_code='',
                                                          correction_delete_indicatr='')

    # Both codes present
    det_award_2 = DetachedAwardFinancialAssistanceFactory(awarding_sub_tier_agency_c='000', awarding_office_code='0000',
                                                          correction_delete_indicatr=None)

    # Missing sub tier code, has office code
    det_award_3 = DetachedAwardFinancialAssistanceFactory(awarding_sub_tier_agency_c=None, awarding_office_code='0000',
                                                          correction_delete_indicatr='c')

    # Ignore correction delete indicator of D
    det_award_4 = DetachedAwardFinancialAssistanceFactory(awarding_sub_tier_agency_c='', awarding_office_code='',
                                                          correction_delete_indicatr='d')

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4])
    assert errors == 0
Example #36
0
def test_failure(database):
    """ Test failure PrimaryPlaceOfPerformanceForeignLocationDescription must be blank for domestic recipients
        (i.e., when PrimaryPlaceOfPerformanceCountryCode = USA) or for aggregate and PII-redacted non-aggregate records
        (RecordType=1 or 3).
    """

    fabs_1 = FABSFactory(place_of_performance_forei='Test',
                         place_of_perform_country_c='USA',
                         record_type=2,
                         correction_delete_indicatr='')
    fabs_2 = FABSFactory(place_of_performance_forei='Content',
                         place_of_perform_country_c='usa',
                         record_type=1,
                         correction_delete_indicatr=None)
    fabs_3 = FABSFactory(place_of_performance_forei='Content',
                         place_of_perform_country_c='CAN',
                         record_type=3,
                         correction_delete_indicatr='c')

    errors = number_of_errors(_FILE, database, models=[fabs_1, fabs_2, fabs_3])
    assert errors == 3
Example #37
0
def test_failure(database):
    """ Tests that SF 133 amount sum for lines 1340, 1440 does not match Appropriation borrowing_authority_amount_cpe
        for the specified fiscal year and period
    """
    tas = "".join([_TAS, "_failure"])

    sf_1 = SF133Factory(line=1340,
                        tas=tas,
                        period=1,
                        fiscal_year=2016,
                        amount=1)
    sf_2 = SF133Factory(line=1440,
                        tas=tas,
                        period=1,
                        fiscal_year=2016,
                        amount=1)
    ap_1 = AppropriationFactory(tas=tas, borrowing_authority_amount_cpe=1)
    ap_2 = AppropriationFactory(tas=tas, borrowing_authority_amount_cpe=None)

    assert number_of_errors(_FILE, database, models=[sf_1, sf_2, ap_1,
                                                     ap_2]) == 2
Example #38
0
def test_success(database):
    """ FaceValueLoanGuarantee is required for loans (i.e., when AssistanceType = 07 or 08). """

    det_award = DetachedAwardFinancialAssistanceFactory(
        assistance_type='07',
        face_value_loan_guarantee=0,
        correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        assistance_type='08',
        face_value_loan_guarantee=20,
        correction_delete_indicatr='c')
    # Ignore correction delete indicator of D
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        assistance_type='07',
        face_value_loan_guarantee=None,
        correction_delete_indicatr='d')

    errors = number_of_errors(_FILE,
                              database,
                              models=[det_award, det_award_2, det_award_3])
    assert errors == 0
def test_failure(database):
    """ Test failure for PrimaryPlaceOfPerformanceCode for aggregate records (i.e., when RecordType = 1)
        must be in countywide (XX**###), statewide (XX*****), or foreign (00FORGN) formats.
    """

    det_award_1 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_code='00**333',
        record_type=1,
        correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_code='AB**33',
        record_type=1,
        correction_delete_indicatr=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_code='00*****',
        record_type=1,
        correction_delete_indicatr='c')
    errors = number_of_errors(_FILE,
                              database,
                              models=[det_award_1, det_award_2, det_award_3])
    assert errors == 3
def test_success(database):
    """ FederalActionObligation must be blank for loans (i.e., when AssistanceType = 07 or 08). """

    fabs = FABSFactory(assistance_type='07',
                       federal_action_obligation=None,
                       correction_delete_indicatr='')
    fabs_2 = FABSFactory(assistance_type='08',
                         federal_action_obligation=None,
                         correction_delete_indicatr='c')
    fabs_3 = FABSFactory(assistance_type='07',
                         federal_action_obligation=0,
                         correction_delete_indicatr=None)
    # Ignore correction delete indicator of D
    fabs_4 = FABSFactory(assistance_type='08',
                         federal_action_obligation=20,
                         correction_delete_indicatr='d')

    errors = number_of_errors(_FILE,
                              database,
                              models=[fabs, fabs_2, fabs_3, fabs_4])
    assert errors == 0
Example #41
0
def test_failure(database):
    """ Tests that if the SF-133 amount for line 1000 for the same fiscal year and period is populated, then
        Appropriation budget_authority_unobligat_fyb is not populated. Only one error if there are 2 entries for the
        same row
    """

    sf_1 = SF133Factory(line=1000,
                        tas=_TAS,
                        period=1,
                        fiscal_year=2016,
                        amount=1,
                        disaster_emergency_fund_code='Q')
    sf_2 = SF133Factory(line=1000,
                        tas=_TAS,
                        period=1,
                        fiscal_year=2016,
                        amount=1,
                        disaster_emergency_fund_code='B')
    ap = AppropriationFactory(tas=_TAS, budget_authority_unobligat_fyb=None)

    assert number_of_errors(_FILE, database, models=[sf_1, sf_2, ap]) == 1
def test_success(database):
    """ Test when provided, AwardingOfficeCode must be a valid value from the Federal Hierarchy, including being
        designated specifically as an Assistance/Grant Office in the hierarchy.
    """

    office = OfficeFactory(office_code='12345a',
                           financial_assistance_awards_office=True)
    det_award_1 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code='12345a')
    # test ignore case
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code='12345A')
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code='')
    det_award_4 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code=None)
    errors = number_of_errors(
        _FILE,
        database,
        models=[office, det_award_1, det_award_2, det_award_3, det_award_4])
    assert errors == 0
Example #43
0
def test_failure(database):
    """ Test invalid. CFDA_Number must be in XX.XXX format """

    det_award_1 = DetachedAwardFinancialAssistanceFactory(
        cfda_number='1234', correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        cfda_number='12.34567', correction_delete_indicatr=None)
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        cfda_number='12.3', correction_delete_indicatr='c')
    det_award_4 = DetachedAwardFinancialAssistanceFactory(
        cfda_number='123.456', correction_delete_indicatr='C')
    det_award_5 = DetachedAwardFinancialAssistanceFactory(
        cfda_number='ab.cdf', correction_delete_indicatr='')

    errors = number_of_errors(_FILE,
                              database,
                              models=[
                                  det_award_1, det_award_2, det_award_3,
                                  det_award_4, det_award_5
                              ])
    assert errors == 5
def test_success(database):
    """ OriginalLoanSubsidyCost must be blank for non-loans (i.e., when AssistanceType is not 07 or 08). """

    fabs = FABSFactory(assistance_type='03',
                       original_loan_subsidy_cost=None,
                       correction_delete_indicatr='')
    fabs_2 = FABSFactory(assistance_type='05',
                         original_loan_subsidy_cost=None,
                         correction_delete_indicatr='c')
    fabs_3 = FABSFactory(assistance_type='03',
                         original_loan_subsidy_cost=0,
                         correction_delete_indicatr=None)
    # Ignore correction delete indicator of D
    fabs_4 = FABSFactory(assistance_type='05',
                         original_loan_subsidy_cost=20,
                         correction_delete_indicatr='d')

    errors = number_of_errors(_FILE,
                              database,
                              models=[fabs, fabs_2, fabs_3, fabs_4])
    assert errors == 0
def test_success(database):
    """ Test cases with different combinations of fain, uri, and piid """

    # Test with only one present
    award_fin_fain = AwardFinancialFactory(uri=None, piid=None)
    award_fin_uri = AwardFinancialFactory(fain=None, piid=None)
    award_fin_piid = AwardFinancialFactory(fain=None, uri=None)
    # Test with all three
    award_fin = AwardFinancialFactory()
    # Test with one missing
    award_fin_no_fain = AwardFinancialFactory(fain=None)
    award_fin_no_uri = AwardFinancialFactory(uri=None)
    award_fin_no_piid = AwardFinancialFactory(piid=None)

    assert number_of_errors(_FILE,
                            database,
                            models=[
                                award_fin_fain, award_fin_uri, award_fin_piid,
                                award_fin, award_fin_no_fain, award_fin_no_uri,
                                award_fin_no_piid
                            ]) == 0
Example #46
0
def test_failure(database):
    """ Test failure for when the provided PrimaryPlaceofPerformanceZIP+4 must be in the state specified by
        PrimaryPlaceOfPerformanceCode. In this specific submission row, the first five digits are valid and located
        in the correct state, but the last 4 are invalid.
    """
    zips = Zips(zip5='12345', zip_last4='6789', state_abbreviation='NY')

    # invalid 9 digit zip - first 5 digits good
    fabs_1 = FABSFactory(place_of_performance_code='ny10986',
                         place_of_performance_zip4a='123456788',
                         correction_delete_indicatr='')
    fabs_2 = FABSFactory(place_of_performance_code='NY*****',
                         place_of_performance_zip4a='123456788',
                         correction_delete_indicatr=None)
    fabs_3 = FABSFactory(place_of_performance_code='Ny**123',
                         place_of_performance_zip4a='12345-6788',
                         correction_delete_indicatr='c')
    errors = number_of_errors(_FILE,
                              database,
                              models=[fabs_1, fabs_2, fabs_3, zips])
    assert errors == 3
def test_success_all_zero(database):
    """ Test not returning a warning on '000' when all monetary values are 0 """

    op = ObjectClassProgramActivityFactory(object_class='000', deobligations_recov_by_pro_cpe=0,
                                           gross_outlay_amount_by_pro_cpe=0, gross_outlay_amount_by_pro_fyb=0,
                                           gross_outlays_delivered_or_cpe=0, gross_outlays_delivered_or_fyb=0,
                                           gross_outlays_undelivered_cpe=0, gross_outlays_undelivered_fyb=0,
                                           obligations_delivered_orde_cpe=0, obligations_delivered_orde_fyb=0,
                                           obligations_incurred_by_pr_cpe=0, obligations_undelivered_or_cpe=0,
                                           obligations_undelivered_or_fyb=0, ussgl480100_undelivered_or_cpe=0,
                                           ussgl480100_undelivered_or_fyb=0, ussgl480200_undelivered_or_cpe=0,
                                           ussgl480200_undelivered_or_fyb=0, ussgl483100_undelivered_or_cpe=0,
                                           ussgl483200_undelivered_or_cpe=0, ussgl487100_downward_adjus_cpe=0,
                                           ussgl487200_downward_adjus_cpe=0, ussgl488100_upward_adjustm_cpe=0,
                                           ussgl488200_upward_adjustm_cpe=0, ussgl490100_delivered_orde_cpe=0,
                                           ussgl490100_delivered_orde_fyb=0, ussgl490200_delivered_orde_cpe=0,
                                           ussgl490800_authority_outl_cpe=0, ussgl490800_authority_outl_fyb=0,
                                           ussgl493100_delivered_orde_cpe=0, ussgl497100_downward_adjus_cpe=0,
                                           ussgl497200_downward_adjus_cpe=0, ussgl498100_upward_adjustm_cpe=0,
                                           ussgl498200_upward_adjustm_cpe=0)
    assert number_of_errors(_FILE, database, models=[op]) == 0
Example #48
0
def test_success(database):
    """ Test that TAS values can be found, and null matches work correctly"""
    tas = TASFactory()
    tas_null = TASFactory(allocation_transfer_agency = None, availability_type_code = None, sub_account_code = None)
    award_fin = AwardFinancialFactory(allocation_transfer_agency = tas.allocation_transfer_agency,
                                  agency_identifier = tas.agency_identifier,
                                  beginning_period_of_availa = tas.beginning_period_of_availability,
                                  ending_period_of_availabil = tas.ending_period_of_availability,
                                  availability_type_code = tas.availability_type_code,
                                  main_account_code = tas.main_account_code,
                                  sub_account_code = tas.sub_account_code)
    award_fin_null = AwardFinancialFactory(allocation_transfer_agency = tas_null.allocation_transfer_agency,
                                       agency_identifier = tas_null.agency_identifier,
                                       beginning_period_of_availa = tas_null.beginning_period_of_availability,
                                       ending_period_of_availabil = tas_null.ending_period_of_availability,
                                       availability_type_code = tas_null.availability_type_code,
                                       main_account_code = tas_null.main_account_code,
                                       sub_account_code = tas_null.sub_account_code)

    errors = number_of_errors(_FILE, database, models=[tas, tas_null, award_fin, award_fin_null])
    assert errors == 0
def test_success(database):
    """ Tests that SF 133 amount for line 1910 matches Appropriation total_budgetary_resources_cpe
        for the specified fiscal year and period
    """

    tas = "".join([_TAS, "_success"])

    sf = SF133(line=1910,
               tas=tas,
               period=1,
               fiscal_year=2016,
               amount=1,
               agency_identifier="sys",
               main_account_code="000",
               sub_account_code="000")
    ap = Appropriation(job_id=1,
                       row_number=1,
                       tas=tas,
                       total_budgetary_resources_cpe=1)

    assert number_of_errors(_FILE, database, models=[sf, ap]) == 0
Example #50
0
def test_failure(database):
    """ The unique combination of FAIN, AwardModificationAmendmentNumber, URI, CFDA_Number, and
        AwardingSubTierAgencyCode must exist as a currently published record when the record is a deletion (i.e., if
        CorrectionDeleteIndicator = D). Ignore all other CorrectionDeleteIndicators in this rule.
    """

    det_award_1 = DetachedAwardFinancialAssistanceFactory(afa_generated_unique='ama1asta1fain2uri1',
                                                          correction_delete_indicatr='D')
    # Test that capitalization differences don't affect the error
    det_award_2 = DetachedAwardFinancialAssistanceFactory(afa_generated_unique='amA1astA1fain2uRi1',
                                                          correction_delete_indicatr='D')

    pub_award_1 = PublishedAwardFinancialAssistanceFactory(afa_generated_unique='ama1asta1fain1uri1',
                                                           correction_delete_indicatr=None,
                                                           is_active=True)
    pub_award_2 = PublishedAwardFinancialAssistanceFactory(afa_generated_unique='ama1asta1fAin2uri1',
                                                           correction_delete_indicatr=None,
                                                           is_active=False)

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, pub_award_1, pub_award_2])
    assert errors == 2
def test_success(database):
    """ FederalActionObligation is required for non-loans (i.e., when AssistanceType is not 07 or 08). """

    det_award = DetachedAwardFinancialAssistanceFactory(
        assistance_type='02',
        federal_action_obligation=0,
        correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        assistance_type='10',
        federal_action_obligation=20,
        correction_delete_indicatr='c')
    # Ignore correction delete indicator of D
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        assistance_type='03',
        federal_action_obligation=None,
        correction_delete_indicatr='d')

    errors = number_of_errors(_FILE,
                              database,
                              models=[det_award, det_award_2, det_award_3])
    assert errors == 0
def test_failure(database):
    """ Test fail when provided, AwardingOfficeCode must be a valid value from the Federal Hierarchy, including being
        designated specifically as an Assistance/Grant Office in the hierarchy.
    """

    office_1 = OfficeFactory(office_code='123456',
                             financial_assistance_awards_office=True)
    office_2 = OfficeFactory(office_code='987654',
                             financial_assistance_awards_office=False)
    det_award_1 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code='12345')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code='1234567')
    # Test fail if grant office is false even if code matches
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        awarding_office_code='987654')
    errors = number_of_errors(
        _FILE,
        database,
        models=[office_1, office_2, det_award_1, det_award_2, det_award_3])
    assert errors == 3
def test_success(database):
    """ Test PrimaryPlaceOfPerformanceForeignLocationDescription is required for foreign places of performance
        (i.e., when PrimaryPlaceOfPerformanceCountryCode does not equal USA). This test shouldn't care about
        content when country_code is USA (that is for another validation). """

    det_award_1 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_forei="description",
        place_of_perform_country_c="UK")
    det_award_2 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_forei="description",
        place_of_perform_country_c="USA")
    det_award_3 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_forei=None, place_of_perform_country_c="USA")
    det_award_4 = DetachedAwardFinancialAssistanceFactory(
        place_of_performance_forei="", place_of_perform_country_c="UsA")

    errors = number_of_errors(
        _FILE,
        database,
        models=[det_award_1, det_award_2, det_award_3, det_award_4])
    assert errors == 0
def test_failure(database):
    """ Test failure for AwardeeOrRecipientUniqueIdentifier is required for AssistanceType of 02, 03, 04, or 05 whose
        ActionDate after October 1, 2010, , unless the record is an aggregate or PII-redacted non-aggregate record
        (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P')
    """

    det_award_1 = DetachedAwardFinancialAssistanceFactory(assistance_type='02', record_type=2, business_types='AbC',
                                                          awardee_or_recipient_uniqu=None, action_date='10/02/2010',
                                                          correction_delete_indicatr='')
    det_award_2 = DetachedAwardFinancialAssistanceFactory(assistance_type='03', record_type=5, business_types='aBc',
                                                          awardee_or_recipient_uniqu='', action_date='10/02/2010',
                                                          correction_delete_indicatr='C')
    det_award_3 = DetachedAwardFinancialAssistanceFactory(assistance_type='04', record_type=4, business_types='AbC',
                                                          awardee_or_recipient_uniqu=None, action_date='10/02/2010',
                                                          correction_delete_indicatr='c')
    det_award_4 = DetachedAwardFinancialAssistanceFactory(assistance_type='05', record_type=5, business_types='aBc',
                                                          awardee_or_recipient_uniqu='', action_date='10/02/2010',
                                                          correction_delete_indicatr=None)

    errors = number_of_errors(_FILE, database, models=[det_award_1, det_award_2, det_award_3, det_award_4])
    assert errors == 4
Example #55
0
def test_success(database):
    """ The combination of FAIN, AwardModificationAmendmentNumber, URI, CFDA_Number, and
        AwardingSubTierAgencyCode must be unique from currently published ones unless the record is a correction or
        deletion (i.e., if CorrectionDeleteIndicator = C or D). Ignores inactive records
    """
    fabs_1 = FABSFactory(afa_generated_unique='ama1asta1fain1uri1',
                         correction_delete_indicatr=None)
    fabs_2 = FABSFactory(afa_generated_unique='ama1asta1fain2uri1',
                         correction_delete_indicatr='C')
    fabs_3 = FABSFactory(afa_generated_unique='ama2asta1fain1uri1',
                         correction_delete_indicatr='D')
    pub_fabs_1 = PublishedFABSFactory(
        afa_generated_unique='ama1asta1fain1uri2',
        correction_delete_indicatr=None,
        is_active=True)
    pub_fabs_2 = PublishedFABSFactory(
        afa_generated_unique='ama1asta2fain1uri1',
        correction_delete_indicatr=None,
        is_active=True)
    pub_fabs_3 = PublishedFABSFactory(
        afa_generated_unique='ama1asta1fain2uri1',
        correction_delete_indicatr=None,
        is_active=True)
    pub_fabs_4 = PublishedFABSFactory(
        afa_generated_unique='ama2asta1fain1uri1',
        correction_delete_indicatr=None,
        is_active=True)
    pub_fabs_5 = PublishedFABSFactory(
        afa_generated_unique='ama1asta1fain1uri1',
        correction_delete_indicatr=None,
        is_active=False)

    errors = number_of_errors(_FILE,
                              database,
                              models=[
                                  fabs_1, fabs_2, fabs_3, pub_fabs_1,
                                  pub_fabs_2, pub_fabs_3, pub_fabs_4,
                                  pub_fabs_5
                              ])
    assert errors == 0
Example #56
0
def test_failure(database):
    """ Test fail that empty funding office codes aren't matching invalid office codes from the base record. """

    office_1 = OfficeFactory(office_code='12345a', contract_funding_office=False,
                             financial_assistance_funding_office=True)
    office_2 = OfficeFactory(office_code='abcd', contract_funding_office=True,
                             financial_assistance_funding_office=False)
    # Invalid code in record
    pub_fabs_1 = PublishedFABSFactory(funding_office_code='abc', unique_award_key='zyxwv_123', action_date='20181018',
                                      award_modification_amendme='0', is_active=True)
    # Earliest record inactive, newer record has invalid entry
    pub_fabs_2 = PublishedFABSFactory(funding_office_code='12345a', unique_award_key='4321_cba', action_date='20181018',
                                      award_modification_amendme='0', is_active=False)
    pub_fabs_3 = PublishedFABSFactory(funding_office_code='abc', unique_award_key='4321_cba', action_date='20181019',
                                      award_modification_amendme='1', is_active=True)
    # Has a valid code but it's not a funding assistance office
    pub_fabs_4 = PublishedFABSFactory(funding_office_code='abcd', unique_award_key='123_abc', action_date='20181018',
                                      award_modification_amendme='0', is_active=True)
    # award_modification_amendme number is null
    pub_fabs_5 = PublishedFABSFactory(funding_office_code='abc', unique_award_key='zyxwv_1234', action_date='20181018',
                                      award_modification_amendme=None, is_active=True)

    # Entry for invalid code in base record
    fabs_1 = FABSFactory(funding_office_code='', unique_award_key='zyxwv_123', action_date='20181020',
                         award_modification_amendme='2', correction_delete_indicatr=None)
    # Entry with award_modification_amendme null
    fabs_2 = FABSFactory(funding_office_code='', unique_award_key='zyxwv_123', action_date='20181020',
                         award_modification_amendme=None, correction_delete_indicatr=None)
    # New entry for earliest inactive
    fabs_3 = FABSFactory(funding_office_code='', unique_award_key='4321_cba', action_date='20181020',
                         award_modification_amendme='2', correction_delete_indicatr=None)
    # New entry for has valid non-funding assistance code
    fabs_4 = FABSFactory(funding_office_code='', unique_award_key='123_abc', action_date='20181020',
                         award_modification_amendme='2', correction_delete_indicatr=None)
    # Entry for award_modification_amendme null in base record
    fabs_5 = FABSFactory(funding_office_code='', unique_award_key='zyxwv_1234', action_date='20181020',
                         award_modification_amendme='2', correction_delete_indicatr=None)
    errors = number_of_errors(_FILE, database, models=[office_1, office_2, pub_fabs_1, pub_fabs_2, pub_fabs_3,
                                                       pub_fabs_4, pub_fabs_5, fabs_1, fabs_2, fabs_3, fabs_4, fabs_5])
    assert errors == 5
Example #57
0
def test_failure(database):
    """ Test failure for each unique combination of PIID/ParentAwardId in File C, the sum of each
        TransactionObligatedAmount should match (but with opposite signs) the sum of the FederalActionObligation
        reported in D1. This rule does not apply if the ATA field is populated and is different from the Agency ID.
    """
    # Create a 12 character random parent_award_id
    paid_1 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    paid_2 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    paid_3 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))

    piid = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))

    # Basic sum, row 3 is ignored in this sum because it doesn't have a paid
    af_1_row_1 = AwardFinancialFactory(transaction_obligated_amou=1100, piid=piid.lower(), parent_award_id=paid_1,
                                       allocation_transfer_agency=None)
    af_1_row_2 = AwardFinancialFactory(transaction_obligated_amou=11, piid=piid, parent_award_id=paid_1.lower(),
                                       allocation_transfer_agency=None)
    af_1_row_3 = AwardFinancialFactory(transaction_obligated_amou=11, piid=piid.upper(), parent_award_id=None,
                                       allocation_transfer_agency=None)
    # Same ATA/AID or no ATA sum
    af_2_row_1 = AwardFinancialFactory(transaction_obligated_amou=1111, piid=piid, parent_award_id=paid_2.lower(),
                                       allocation_transfer_agency=None)
    af_2_row_2 = AwardFinancialFactory(transaction_obligated_amou=1111, piid=piid.lower(), parent_award_id=paid_2,
                                       allocation_transfer_agency='good', agency_identifier='good')
    # Not ignored with TOA of 0
    af_3 = AwardFinancialFactory(transaction_obligated_amou=0, piid=piid.lower(), parent_award_id=paid_3.lower(),
                                 allocation_transfer_agency='good', agency_identifier='good')

    # Sum of these values doesn't add up (ignoring third one because it has a different paid)
    ap_1_row_1 = AwardProcurementFactory(parent_award_id=paid_1, piid=piid.lower(), federal_action_obligation=-1100)
    ap_1_row_2 = AwardProcurementFactory(parent_award_id=paid_1.lower(), piid=piid, federal_action_obligation=-10)
    ap_1_row_3 = AwardProcurementFactory(parent_award_id='1234', piid=piid.upper(), federal_action_obligation=-1)
    # Sum of the two above should be both of them, not just one
    ap_2 = AwardProcurementFactory(parent_award_id=paid_2, piid=piid, federal_action_obligation=-1111)
    # Should not be ignored because TOA is present
    ap_3 = AwardProcurementFactory(parent_award_id=paid_3, piid=piid, federal_action_obligation=1)

    errors = number_of_errors(_FILE, database, models=[af_1_row_1, af_1_row_2, af_1_row_3, af_2_row_1, af_2_row_2,
                                                       af_3, ap_1_row_1, ap_1_row_2, ap_1_row_3, ap_2, ap_3])
    assert errors == 3
def test_success(database):
    """ Test success LegalEntityForeignCityName is required for foreign recipients (i.e., when
        LegalEntityCountryCode != USA) for non-aggregate and PII-redacted non-aggregate records (RecordType = 2 or 3)
    """

    fabs_1 = FABSFactory(legal_entity_country_code='Japan',
                         legal_entity_foreign_city='Tokyo',
                         record_type=2,
                         correction_delete_indicatr='')
    fabs_2 = FABSFactory(legal_entity_country_code='UK',
                         legal_entity_foreign_city='Manchester',
                         record_type=3,
                         correction_delete_indicatr=None)
    fabs_3 = FABSFactory(legal_entity_country_code='USA',
                         legal_entity_foreign_city=None,
                         record_type=2,
                         correction_delete_indicatr='c')
    fabs_4 = FABSFactory(legal_entity_country_code='UsA',
                         legal_entity_foreign_city='',
                         record_type=3,
                         correction_delete_indicatr='C')
    fabs_5 = FABSFactory(legal_entity_country_code='UK',
                         legal_entity_foreign_city='',
                         record_type=1,
                         correction_delete_indicatr='')
    fabs_6 = FABSFactory(legal_entity_country_code='CAN',
                         legal_entity_foreign_city=None,
                         record_type=1,
                         correction_delete_indicatr='')
    # Ignore correction delete indicator of D
    fabs_7 = FABSFactory(legal_entity_country_code='Canada',
                         legal_entity_foreign_city='',
                         record_type=3,
                         correction_delete_indicatr='d')

    errors = number_of_errors(
        _FILE,
        database,
        models=[fabs_1, fabs_2, fabs_3, fabs_4, fabs_5, fabs_6, fabs_7])
    assert errors == 0
Example #59
0
def test_success(database):
    """ Test for each unique URI in File C, the sum of each TransactionObligatedAmount should match (but with opposite
        signs) the sum of the FederalActionObligation or OriginalLoanSubsidyCost amounts reported in D2. This rule does
        not apply if the ATA field is populated and is different from the Agency ID. """
    # Create a 12 character random uri
    uri_1 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_2 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))
    uri_3 = ''.join(choice(ascii_uppercase + ascii_lowercase + digits) for _ in range(12))

    # Simple sum
    af_1_row_1 = AwardFinancialFactory(transaction_obligated_amou=1100, uri=uri_1, allocation_transfer_agency=None)
    af_1_row_2 = AwardFinancialFactory(transaction_obligated_amou=11, uri=uri_1, allocation_transfer_agency=None)
    # Non-ignored rows with a matching ATA/AID
    af_2_row_1 = AwardFinancialFactory(transaction_obligated_amou=9900, uri=uri_2, allocation_transfer_agency=None)
    af_2_row_2 = AwardFinancialFactory(transaction_obligated_amou=99, uri=uri_2, allocation_transfer_agency="good",
                                       agency_identifier="good")
    # Ignored row with non-matching ATA/AID
    af_3 = AwardFinancialFactory(transaction_obligated_amou=8888, uri=uri_3, allocation_transfer_agency="good",
                                 agency_identifier="bad")

    # Correct sum
    afa_1_row_1 = AwardFinancialAssistanceFactory(uri=uri_1, federal_action_obligation=-1100,
                                                  original_loan_subsidy_cost=None)
    afa_1_row_2 = AwardFinancialAssistanceFactory(uri=uri_1, federal_action_obligation=-10,
                                                  original_loan_subsidy_cost=None)
    # original loan subsidy cost used in this row because assistance type is '08'
    afa_1_row_3 = AwardFinancialAssistanceFactory(uri=uri_1, original_loan_subsidy_cost=-1, assistance_type='08',
                                                  federal_action_obligation=None)
    # federal action obligation used in this row (it's 0), because assistance type is not 07 and 08
    afa_1_row_4 = AwardFinancialAssistanceFactory(uri=uri_1, original_loan_subsidy_cost=-2222, assistance_type='09',
                                                  federal_action_obligation=None)
    # Uri 2 Test for non-ignored ATA
    afa_2 = AwardFinancialAssistanceFactory(uri=uri_2, federal_action_obligation=-9999, original_loan_subsidy_cost=None)
    # Uri 3 test for ignoring a non-matching ATA/AID
    afa_3 = AwardFinancialAssistanceFactory(uri=uri_3, federal_action_obligation=-9999)

    errors = number_of_errors(_FILE, database, models=[af_1_row_1, af_1_row_2, af_2_row_1, af_2_row_2, af_3,
                                                       afa_1_row_1, afa_1_row_2, afa_1_row_3, afa_1_row_4, afa_2,
                                                       afa_3])
    assert errors == 0
Example #60
0
def test_failure(database):
    """ Test that calculation fails for unequal values """
    value_one = Decimal('101.23')
    value_two = Decimal('102.34')
    value_three = Decimal('103.45')
    value_four = Decimal('104.56')
    award_fin = AwardFinancialFactory(
        gross_outlay_amount_by_awa_cpe=value_one - value_three + value_two -
        value_four,
        gross_outlays_undelivered_cpe=value_one,
        gross_outlays_delivered_or_cpe=value_two,
        gross_outlays_undelivered_fyb=value_three,
        gross_outlays_delivered_or_fyb=None)
    award_fin_2 = AwardFinancialFactory(
        gross_outlay_amount_by_awa_cpe=value_one + value_two,
        gross_outlays_undelivered_cpe=value_one,
        gross_outlays_delivered_or_cpe=value_two,
        gross_outlays_undelivered_fyb=value_three,
        gross_outlays_delivered_or_fyb=value_four)

    assert number_of_errors(_FILE, database, models=[award_fin,
                                                     award_fin_2]) == 2