<commit_before>from dataactbroker.handlers.fileHandler import fabs_derivations

from tests.unit.dataactcore.factories.domain import (
    CGACFactory, FRECFactory, SubTierAgencyFactory, StatesFactory, CountyCodeFactory, CFDAProgramFactory,
    ZipCityFactory, ZipsFactory, CityCodeFactory, CountryCodeFactory)

from tests.unit.dataactcore.factories.staging import FPDSContractingOfficeFactory


def initialize_db_values(db, cfda_title=None, cgac_code=None, frec_code=None, use_frec=False):
    """ Initialize the values in the DB that can be used throughout the tests """
    if cgac_code:
        cgac = CGACFactory(cgac_code=cgac_code, agency_name="Test CGAC Agency")
    else:
        cgac = CGACFactory()
    if frec_code:
        frec = FRECFactory(frec_code=frec_code, agency_name="Test FREC Agency", cgac=cgac)
    else:
        frec = FRECFactory(cgac=cgac)
    db.session.add_all([cgac, frec])
    db.session.commit()

    cfda_number = CFDAProgramFactory(program_number=12.345, program_title=cfda_title)
    sub_tier = SubTierAgencyFactory(sub_tier_agency_code="1234", cgac=cgac, frec=frec, is_frec=use_frec,
                                    sub_tier_agency_name="Test Subtier Agency")
    state = StatesFactory(state_code="NY", state_name="New York")
    zip_code_1 = ZipsFactory(zip5="12345", zip_last4="6789", state_abbreviation=state.state_code, county_number="001",
                             congressional_district_no="01")
    zip_code_2 = ZipsFactory(zip5="12345", zip_last4="4321", state_abbreviation=state.state_code, county_number="001",
                             congressional_district_no="02")
    zip_city = ZipCityFactory(zip_code=zip_code_1.zip5, city_name="Test Zip City")
    county_code = CountyCodeFactory(state_code=state.state_code, county_number=zip_code_1.county_number,
                                    county_name="Test County")
    city_code = CityCodeFactory(feature_name="Test City", city_code="00001", state_code=state.state_code,
                                county_name="Test City County")
    contracting_office = FPDSContractingOfficeFactory(contracting_office_code='033103',
                                                      contracting_office_name='Office')
    country_code = CountryCodeFactory(country_code='USA', country_name='United States of America')
    db.session.add_all([sub_tier, state, cfda_number, zip_code_1, zip_code_2, zip_city, county_code, city_code,
                        contracting_office, country_code])
    db.session.commit()


def initialize_test_obj(fao=None, nffa=None, cfda_num="00.000", sub_tier_code="1234", fund_agency_code=None,
                        sub_fund_agency_code=None, ppop_code="NY00000", ppop_zip4a=None, ppop_cd=None, le_zip5=None,
                        le_zip4=None, record_type=2, award_mod_amend=None, fain=None, uri=None, cldi=None,
                        awarding_office='033103', funding_office='033103', legal_city="WASHINGTON", legal_state="DC",
                        primary_place_country='USA', legal_country='USA'):
    """ Initialize the values in the object being run through the fabs_derivations function """
    obj = {
        'federal_action_obligation': fao,
        'non_federal_funding_amount': nffa,
        'cfda_number': cfda_num,
        'awarding_sub_tier_agency_c': sub_tier_code,
        'funding_agency_code': fund_agency_code,
        'funding_sub_tier_agency_co': sub_fund_agency_code,
        'place_of_performance_code': ppop_code,
        'place_of_performance_zip4a': ppop_zip4a,
        'place_of_performance_congr': ppop_cd,
        'legal_entity_zip5': le_zip5,
        'legal_entity_zip_last4': le_zip4,
        'record_type': record_type,
        'award_modification_amendme': award_mod_amend,
        'fain': fain,
        'uri': uri,
        'correction_late_delete_ind': cldi,
        'awarding_office_code': awarding_office,
        'funding_office_code': funding_office,
        'legal_entity_city_name': legal_city,
        'legal_entity_state_code': legal_state,
        'legal_entity_state_code': legal_state,
        'primary_place_of_performance_country_code': primary_place_country,
        'legal_entity_country_code': legal_country
    }
    return obj


def test_total_funding_amount(database):
    initialize_db_values(database)

    # when fao and nffa are empty
    obj = initialize_test_obj()
    obj = fabs_derivations(obj, database.session)
    assert obj['total_funding_amount'] == 0

    # when one of fao and nffa is empty and the other isn't
    obj = initialize_test_obj(fao=5.3)
    obj = fabs_derivations(obj, database.session)
    assert obj['total_funding_amount'] == 5.3

    # when fao and nffa aren't empty
    obj = initialize_test_obj(fao=-10.6, nffa=123)
    obj = fabs_derivations(obj, database.session)
    assert obj['total_funding_amount'] == 112.4


def test_cfda_title(database):
    initialize_db_values(database, cfda_title="Test Title")

    # when cfda_number isn't in the database
    obj = initialize_test_obj()
    obj = fabs_derivations(obj, database.session)<commit_msg>Fix extra line found while comparing<commit_after>from dataactbroker.handlers.fileHandler import fabs_derivations

from tests.unit.dataactcore.factories.domain import (
    CGACFactory, FRECFactory, SubTierAgencyFactory, StatesFactory, CountyCodeFactory, CFDAProgramFactory,
    ZipCityFactory, ZipsFactory, CityCodeFactory, CountryCodeFactory)

from tests.unit.dataactcore.factories.staging import FPDSContractingOfficeFactory


def initialize_db_values(db, cfda_title=None, cgac_code=None, frec_code=None, use_frec=False):
    """ Initialize the values in the DB that can be used throughout the tests """
    if cgac_code:
        cgac = CGACFactory(cgac_code=cgac_code, agency_name="Test CGAC Agency")
    else:
        cgac = CGACFactory()
    if frec_code:
        frec = FRECFactory(frec_code=frec_code, agency_name="Test FREC Agency", cgac=cgac)
    else:
        frec = FRECFactory(cgac=cgac)
    db.session.add_all([cgac, frec])
    db.session.commit()

    cfda_number = CFDAProgramFactory(program_number=12.345, program_title=cfda_title)
    sub_tier = SubTierAgencyFactory(sub_tier_agency_code="1234", cgac=cgac, frec=frec, is_frec=use_frec,
                                    sub_tier_agency_name="Test Subtier Agency")
    state = StatesFactory(state_code="NY", state_name="New York")
    zip_code_1 = ZipsFactory(zip5="12345", zip_last4="6789", state_abbreviation=state.state_code, county_number="001",
                             congressional_district_no="01")
    zip_code_2 = ZipsFactory(zip5="12345", zip_last4="4321", state_abbreviation=state.state_code, county_number="001",
                             congressional_district_no="02")
    zip_city = ZipCityFactory(zip_code=zip_code_1.zip5, city_name="Test Zip City")
    county_code = CountyCodeFactory(state_code=state.state_code, county_number=zip_code_1.county_number,
                                    county_name="Test County")
    city_code = CityCodeFactory(feature_name="Test City", city_code="00001", state_code=state.state_code,
                                county_name="Test City County")
    contracting_office = FPDSContractingOfficeFactory(contracting_office_code='033103',
                                                      contracting_office_name='Office')
    country_code = CountryCodeFactory(country_code='USA', country_name='United States of America')
    db.session.add_all([sub_tier, state, cfda_number, zip_code_1, zip_code_2, zip_city, county_code, city_code,
                        contracting_office, country_code])
    db.session.commit()


def initialize_test_obj(fao=None, nffa=None, cfda_num="00.000", sub_tier_code="1234", fund_agency_code=None,
                        sub_fund_agency_code=None, ppop_code="NY00000", ppop_zip4a=None, ppop_cd=None, le_zip5=None,
                        le_zip4=None, record_type=2, award_mod_amend=None, fain=None, uri=None, cldi=None,
                        awarding_office='033103', funding_office='033103', legal_city="WASHINGTON", legal_state="DC",
                        primary_place_country='USA', legal_country='USA'):
    """ Initialize the values in the object being run through the fabs_derivations function """
    obj = {
        'federal_action_obligation': fao,
        'non_federal_funding_amount': nffa,
        'cfda_number': cfda_num,
        'awarding_sub_tier_agency_c': sub_tier_code,
        'funding_agency_code': fund_agency_code,
        'funding_sub_tier_agency_co': sub_fund_agency_code,
        'place_of_performance_code': ppop_code,
        'place_of_performance_zip4a': ppop_zip4a,
        'place_of_performance_congr': ppop_cd,
        'legal_entity_zip5': le_zip5,
        'legal_entity_zip_last4': le_zip4,
        'record_type': record_type,
        'award_modification_amendme': award_mod_amend,
        'fain': fain,
        'uri': uri,
        'correction_late_delete_ind': cldi,
        'awarding_office_code': awarding_office,
        'funding_office_code': funding_office,
        'legal_entity_city_name': legal_city,
        'legal_entity_state_code': legal_state,
        'primary_place_of_performance_country_code': primary_place_country,
        'legal_entity_country_code': legal_country
    }
    return obj


def test_total_funding_amount(database):
    initialize_db_values(database)

    # when fao and nffa are empty
    obj = initialize_test_obj()
    obj = fabs_derivations(obj, database.session)
    assert obj['total_funding_amount'] == 0

    # when one of fao and nffa is empty and the other isn't
    obj = initialize_test_obj(fao=5.3)
    obj = fabs_derivations(obj, database.session)
    assert obj['total_funding_amount'] == 5.3

    # when fao and nffa aren't empty
    obj = initialize_test_obj(fao=-10.6, nffa=123)
    obj = fabs_derivations(obj, database.session)
    assert obj['total_funding_amount'] == 112.4


def test_cfda_title(database):
    initialize_db_values(database, cfda_title="Test Title")

    # when cfda_number isn't in the database
    obj = initialize_test_obj()
    obj = fabs_derivations(obj, database.session)
{'fraction_non_alphanumeric': 0.09400612224745729, 'fraction_numerical': 0.02172410388071492, 'mean_word_length': 3.6672811059907833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '408897', 'n_tokens_mistral': 3499, 'n_tokens_neox': 3227, 'n_words': 564}