def test_clean_historic_duns(database, monkeypatch): """ Test to make sure if a new DUNS is loaded and we reload historic DUNS (skipping the major load), we should remove the historic equivalents. """ monkeypatch.setattr('dataactcore.utils.parentDuns.get_duns_props_from_sam', mock_get_duns_props_from_sam) sess = database.session all_duns = ['00000000{}'.format(x) for x in range(1, 5)] existing_duns = all_duns[2:] for duns in existing_duns: sess.add(DUNS(awardee_or_recipient_uniqu=duns)) sess.commit() duns_file = os.path.join(CONFIG_BROKER['path'], 'tests', 'unit', 'data', 'historic_DUNS_export_small.csv') # normal run update_historical_duns.run_duns_batches(duns_file, sess, None, block_size=1) update_historical_duns.import_historic_duns(sess) # update old DUNS as part of load_duns.py updated_duns = sess.query(DUNS).filter(DUNS.awardee_or_recipient_uniqu == '000000002').one() updated_duns.historic = False sess.commit() # rerun with a skip update_historical_duns.clean_historic_duns(sess) update_historical_duns.import_historic_duns(sess) # check to see if historic duns equivalent is removed expected_count = sess.query(HistoricDUNS).filter(HistoricDUNS.awardee_or_recipient_uniqu == '000000002').count() assert expected_count == 0
def update_duns(models, new_data, benchmarks=False): """Modify existing models or create new ones""" logger.info("Updating duns") if benchmarks: update_duns_start = time.time() for _, row in new_data.iterrows(): awardee_or_recipient_uniqu = row['awardee_or_recipient_uniqu'] if awardee_or_recipient_uniqu not in models: models[awardee_or_recipient_uniqu] = DUNS() for field, value in row.items(): setattr(models[awardee_or_recipient_uniqu], field, value) if benchmarks: logger.info("Updating duns took {} seconds".format(time.time() - update_duns_start))
def test_pubished_date_failure(database): """ Test failure for For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = A, AwardeeOrRecipientUniqueIdentifier must be active as of the ActionDate, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P'). This is a warning because CorrectionDeleteIndicator is C and the action date is before January 1, 2017. """ duns_1 = DUNS(awardee_or_recipient_uniqu='111111111', registration_date='06/21/2015', expiration_date='06/21/2016') det_award_1 = DetachedAwardFinancialAssistanceFactory( assistance_type='02', action_date='06/20/2015', awardee_or_recipient_uniqu='111111111', action_type='A', record_type=2, business_types='A', correction_delete_indicatr='C') det_award_2 = DetachedAwardFinancialAssistanceFactory( assistance_type='03', action_date='06/22/2016', awardee_or_recipient_uniqu='111111111', action_type='a', record_type=2, business_types='a', correction_delete_indicatr='c') det_award_3 = DetachedAwardFinancialAssistanceFactory( assistance_type='04', action_date='06/22/2016', awardee_or_recipient_uniqu='111111111', action_type='A', record_type=2, business_types='A', correction_delete_indicatr='C') det_award_4 = DetachedAwardFinancialAssistanceFactory( assistance_type='05', action_date='06/22/2016', awardee_or_recipient_uniqu='111111111', action_type='a', record_type=2, business_types='a', correction_delete_indicatr='c') errors = number_of_errors( _FILE, database, models=[duns_1, det_award_1, det_award_2, det_award_3, det_award_4]) assert errors == 4
def test_remove_existing_duns(database): """ Testing the removing existing duns function""" sess = database.session # of the duns 000000001-000000009, half of them are in the database all_duns = ['00000000{}'.format(x) for x in range(0, 10)] existing_duns = all_duns[: 4] data = pd.DataFrame.from_dict({'awardee_or_recipient_uniqu': all_duns}) for duns in existing_duns: sess.add(DUNS(awardee_or_recipient_uniqu=duns)) sess.commit() # confirm that the dataframe returned only has half the duns expected_duns = list(set(existing_duns) ^ set(all_duns)) new_df = update_historical_duns.remove_existing_duns(data, sess) assert sorted(expected_duns) == sorted(new_df['awardee_or_recipient_uniqu'].tolist())
def test_pubished_date_failure(database): """ Test failure for For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = A, AwardeeOrRecipientUniqueIdentifier must be active as of the ActionDate, unless the record is an aggregate record (RecordType=1) or individual recipient (BusinessTypes includes 'P'). This is a warning because CorrectionLateDeleteIndicator is C and the action date is before January 1, 2017.""" duns_1 = DUNS(awardee_or_recipient_uniqu="111111111", registration_date="06/21/2015", expiration_date="06/21/2016") det_award_1 = DetachedAwardFinancialAssistanceFactory( assistance_type="02", action_date="06/20/2015", awardee_or_recipient_uniqu="111111111", action_type="A", record_type=2, business_types="A", correction_late_delete_ind="C") det_award_2 = DetachedAwardFinancialAssistanceFactory( assistance_type="03", action_date="06/22/2016", awardee_or_recipient_uniqu="111111111", action_type="A", record_type=2, business_types="A", correction_late_delete_ind="C") det_award_3 = DetachedAwardFinancialAssistanceFactory( assistance_type="04", action_date="06/22/2016", awardee_or_recipient_uniqu="111111111", action_type="A", record_type=2, business_types="A", correction_late_delete_ind="C") det_award_4 = DetachedAwardFinancialAssistanceFactory( assistance_type="05", action_date="06/22/2016", awardee_or_recipient_uniqu="111111111", action_type="A", record_type=2, business_types="A", correction_late_delete_ind="C") errors = number_of_errors( _FILE, database, models=[duns_1, det_award_1, det_award_2, det_award_3, det_award_4]) assert errors == 4
def test_pubished_date_failure(database): """ Test failure for AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = B, C, or D, AwardeeOrRecipientUniqueIdentifier should be active on the ActionDate, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P'). """ duns_1 = DUNS(awardee_or_recipient_uniqu='111111111', registration_date='06/21/2017', expiration_date='06/21/2018') det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='b', assistance_type='02', action_date='06/20/2017', record_type=2, business_types='a', correction_delete_indicatr='') det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='C', assistance_type='03', action_date='06/20/2017', record_type=2, business_types='A', correction_delete_indicatr='c') det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='d', assistance_type='04', action_date='06/22/2018', record_type=2, business_types='a', correction_delete_indicatr='C') det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='D', assistance_type='05', action_date='06/22/2018', record_type=2, business_types='A', correction_delete_indicatr=None) errors = number_of_errors( _FILE, database, models=[duns_1, det_award_1, det_award_2, det_award_3, det_award_4]) assert errors == 4
def test_pubished_date_failure(database): """ Test failure for AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = B, C, or D, AwardeeOrRecipientUniqueIdentifier should be active on the ActionDate, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P').""" duns_1 = DUNS(awardee_or_recipient_uniqu="111111111", registration_date="06/21/2017", expiration_date="06/21/2018") det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="B", assistance_type="02", action_date="06/20/2017", record_type=2, business_types="A") det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="C", assistance_type="03", action_date="06/20/2017", record_type=2, business_types="A") det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="D", assistance_type="04", action_date="06/22/2018", record_type=2, business_types="A") det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="D", assistance_type="05", action_date="06/22/2018", record_type=2, business_types="A") errors = number_of_errors( _FILE, database, models=[duns_1, det_award_1, det_award_2, det_award_3, det_award_4]) assert errors == 4
def test_pubished_date_success(database): """ For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010, AwardeeOrRecipientUniqueIdentifier must be found in our records, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P').""" duns_1 = DUNS(awardee_or_recipient_uniqu="111111111") det_award_1 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="111111111", assistance_type="02", action_date="10/02/2010", record_type=2, business_types="A") # Different assistance type det_award_2 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="111111112", assistance_type="01", action_date="10/02/2010", record_type=2, business_types="A") # Before October 1, 2010 det_award_3 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="111111112", assistance_type="02", action_date="09/30/2010", record_type=2, business_types="A") # Handled by d31_1 det_award_4 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="111111112", assistance_type="03", action_date="10/02/2010", record_type=1, business_types="A") det_award_5 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="111111112", assistance_type="03", action_date="10/02/2010", record_type=3, business_types="A") det_award_6 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="111111112", assistance_type="04", action_date="10/02/2010", record_type=2, business_types="P") # Handled by d31_2 det_award_7 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="", assistance_type="05", action_date="10/02/2010", record_type=2, business_types="A") det_award_8 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu=None, assistance_type="02", action_date="10/02/2010", record_type=2, business_types="A") # Handled by d31_3 det_award_9 = DetachedAwardFinancialAssistanceFactory(awardee_or_recipient_uniqu="ABCDEFGHI", assistance_type="03", action_date="10/02/2010", record_type=2, business_types="A") errors = number_of_errors(_FILE, database, models=[duns_1, 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_pubished_date_failure(database): """ Test invalid for For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010, AwardeeOrRecipientUniqueIdentifier must be found in our records, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P'). """ duns_1 = DUNS(awardee_or_recipient_uniqu='111111111') det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='02', action_date='10/02/2010', record_type=2, business_types='A', correction_delete_indicatr='') det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111113', assistance_type='03', action_date='10/03/2010', record_type=2, business_types='a', correction_delete_indicatr='c') det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111114', assistance_type='04', action_date='10/04/2010', record_type=2, business_types='A', correction_delete_indicatr='C') det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111114', assistance_type='05', action_date='10/05/2010', record_type=2, business_types='A', correction_delete_indicatr=None) errors = number_of_errors( _FILE, database, models=[duns_1, det_award_1, det_award_2, det_award_3, det_award_4]) assert errors == 4
def test_pubished_date_failure(database): """ Test invalid for For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010, AwardeeOrRecipientUniqueIdentifier must be found in our records, unless the record is an aggregate record (RecordType=1) or individual recipient (BusinessTypes includes 'P').""" duns_1 = DUNS(awardee_or_recipient_uniqu="111111111") det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111112", assistance_type="02", action_date="10/02/2010", record_type=2, business_types="A") det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111113", assistance_type="03", action_date="10/03/2010", record_type=2, business_types="A") det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111114", assistance_type="04", action_date="10/04/2010", record_type=2, business_types="A") det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111114", assistance_type="05", action_date="10/05/2010", record_type=2, business_types="A") errors = number_of_errors( _FILE, database, models=[duns_1, det_award_1, det_award_2, det_award_3, det_award_4]) assert errors == 4
def test_run_duns_batches(database, monkeypatch): """ Overall test of the update_historical_duns script testing most of the functionality """ monkeypatch.setattr( 'dataactcore.utils.parentDuns.get_location_business_from_sam', mock_get_location_business_from_sam) sess = database.session all_duns = ['00000000{}'.format(x) for x in range(1, 5)] existing_duns = all_duns[2:] for duns in existing_duns: sess.add(DUNS(awardee_or_recipient_uniqu=duns)) sess.commit() duns_file = os.path.join(CONFIG_BROKER['path'], 'tests', 'unit', 'data', 'DUNS_export_small.csv') update_historical_duns.run_duns_batches(duns_file, sess, None, block_size=1) expected_results = { '000000001': { 'awardee_or_recipient_uniqu': '000000001', 'registration_date': '2004-04-01', 'expiration_date': '2013-01-11', 'last_sam_mod_date': '2013-01-11', 'activation_date': '2012-01-11', 'legal_business_name': 'TEST DUNS 1', 'address_line_1': 'Test address 1', 'address_line_2': 'Test address 2', 'city': 'Test city', 'state': 'Test state', 'zip': 'Test zip', 'zip4': 'Test zip4', 'country_code': 'Test country', 'congressional_district': 'Test congressional district', 'business_types_codes': ['A', 'B', 'C'] }, '000000002': { 'awardee_or_recipient_uniqu': '000000002', 'registration_date': '2004-04-02', 'expiration_date': '2013-01-12', 'last_sam_mod_date': '2013-01-12', 'activation_date': '2012-01-12', 'legal_business_name': 'TEST DUNS 2', 'address_line_1': 'Other Test address 1', 'address_line_2': 'Other Test address 2', 'city': 'Other Test city', 'state': 'Other Test state', 'zip': 'Other Test zip', 'zip4': 'Other Test zip4', 'country_code': 'Other Test country', 'congressional_district': 'Other Test congressional district', 'business_types_codes': ['D', 'E', 'F'] }, '000000003': { 'awardee_or_recipient_uniqu': '000000003', 'registration_date': None, 'expiration_date': None, 'last_sam_mod_date': None, 'activation_date': None, 'legal_business_name': None, 'address_line_1': None, 'address_line_2': None, 'city': None, 'state': None, 'zip': None, 'zip4': None, 'country_code': None, 'congressional_district': None, 'business_types_codes': None }, '000000004': { 'awardee_or_recipient_uniqu': '000000004', 'registration_date': None, 'expiration_date': None, 'last_sam_mod_date': None, 'activation_date': None, 'legal_business_name': None, 'address_line_1': None, 'address_line_2': None, 'city': None, 'state': None, 'zip': None, 'zip4': None, 'country_code': None, 'congressional_district': None, 'business_types_codes': None } } results = {} for duns_obj in sess.query(DUNS).all(): results[duns_obj.awardee_or_recipient_uniqu] = { 'awardee_or_recipient_uniqu': duns_obj.awardee_or_recipient_uniqu, 'registration_date': str(duns_obj.registration_date) if duns_obj.registration_date else None, 'expiration_date': str(duns_obj.expiration_date) if duns_obj.expiration_date else None, 'last_sam_mod_date': str(duns_obj.last_sam_mod_date) if duns_obj.last_sam_mod_date else None, 'activation_date': str(duns_obj.activation_date) if duns_obj.activation_date else None, 'legal_business_name': duns_obj.legal_business_name, 'address_line_1': duns_obj.address_line_1, 'address_line_2': duns_obj.address_line_2, 'city': duns_obj.city, 'state': duns_obj.state, 'zip': duns_obj.zip, 'zip4': duns_obj.zip4, 'country_code': duns_obj.country_code, 'congressional_district': duns_obj.congressional_district, 'business_types_codes': duns_obj.business_types_codes } assert results == expected_results
def test_pubished_date_success(database): """ For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = A, AwardeeOrRecipientUniqueIdentifier must be active as of the ActionDate, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P'). This is an error because CorrectionDeleteIndicator is not C or the action date is after January 1, 2017. """ duns_1 = DUNS(awardee_or_recipient_uniqu='111111111', registration_date='06/21/2017', expiration_date='06/21/2018') det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='a', assistance_type='02', action_date='06/22/2017', record_type=2, business_types='a', correction_delete_indicatr='') # Different assistant type det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='A', assistance_type='01', action_date='06/20/2017', record_type=2, business_types='A', correction_delete_indicatr='') # Before October 1, 2010 det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='a', assistance_type='02', action_date='09/30/2010', record_type=2, business_types='A', correction_delete_indicatr=None) # Handled by d31_1 det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='A', assistance_type='03', action_date='06/20/2017', record_type=1, business_types='A', correction_delete_indicatr=None) det_award_5 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='A', assistance_type='03', action_date='06/20/2017', record_type=3, business_types='A', correction_delete_indicatr='') det_award_6 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='a', assistance_type='04', action_date='06/20/2017', record_type=2, business_types='P', correction_delete_indicatr='') # Handled by d31_2 det_award_7 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='', action_type='A', assistance_type='05', action_date='06/20/2017', record_type=2, business_types='A', correction_delete_indicatr='') det_award_8 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu=None, action_type='A', assistance_type='02', action_date='06/20/2017', record_type=2, business_types='a', correction_delete_indicatr='') # Handled by d31_3 det_award_9 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='ABCDEFGHI', action_type='a', assistance_type='03', action_date='06/20/2017', record_type=2, business_types='A', correction_delete_indicatr='') # Handled by d31_4 det_award_10 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', action_type='A', assistance_type='04', action_date='06/20/2017', record_type=2, business_types='A', correction_delete_indicatr='') # Handled by d31_6 det_award_11 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='a', assistance_type='05', action_date='06/20/2010', record_type=2, business_types='A', correction_delete_indicatr='c') # handled in d31_7 det_award_12 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='B', assistance_type='02', action_date='06/20/2017', record_type=2, business_types='A', correction_delete_indicatr=None) # handled in d4 det_award_13 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='A', assistance_type='03', action_date='YYYYMMDD', record_type=2, business_types='a', correction_delete_indicatr='') det_award_14 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', action_type='a', assistance_type='04', action_date='AAAAAAAAAA', record_type=2, business_types='A', correction_delete_indicatr='') # Ignore correction delete indicator of D det_award_15 = DetachedAwardFinancialAssistanceFactory( assistance_type='02', action_date='06/20/2017', awardee_or_recipient_uniqu='111111111', action_type='A', record_type=2, business_types='a', correction_delete_indicatr='d') errors = number_of_errors(_FILE, database, models=[ duns_1, 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, det_award_11, det_award_12, det_award_13, det_award_14, det_award_15 ]) assert errors == 0
def test_workflows(database, monkeypatch): """ Test both scenarios of the script, starting with a full run """ monkeypatch.setattr('dataactcore.utils.parentDuns.get_duns_props_from_sam', mock_get_duns_props_from_sam) sess = database.session all_duns = ['00000000{}'.format(x) for x in range(1, 5)] existing_duns = all_duns[2:] for duns in existing_duns: sess.add(DUNS(awardee_or_recipient_uniqu=duns)) sess.commit() duns_file = os.path.join(CONFIG_BROKER['path'], 'tests', 'unit', 'data', 'historic_DUNS_export_small.csv') update_historical_duns.run_duns_batches(duns_file, sess, None, block_size=1) update_historical_duns.import_historic_duns(sess) expected_results = { '000000001': { 'awardee_or_recipient_uniqu': '000000001', 'registration_date': '2004-04-01', 'expiration_date': '2013-01-11', 'last_sam_mod_date': '2013-01-11', 'activation_date': '2012-01-11', 'legal_business_name': 'TEST DUNS 1', 'address_line_1': 'Test address 1', 'address_line_2': 'Test address 2', 'city': 'Test city', 'state': 'Test state', 'zip': 'Test zip', 'zip4': 'Test zip4', 'country_code': 'Test country', 'congressional_district': 'Test congressional district', 'business_types_codes': ['A', 'B', 'C'], 'business_types': ['Name A', 'Name B', 'Name C'], 'dba_name': 'Name 1', 'ultimate_parent_unique_ide': '999999999', 'ultimate_parent_legal_enti': 'Parent Legal Name 1', 'high_comp_officer1_full_na': 'Test Exec 1', 'high_comp_officer1_amount': '1', 'high_comp_officer2_full_na': 'Test Exec 2', 'high_comp_officer2_amount': '2', 'high_comp_officer3_full_na': 'Test Exec 3', 'high_comp_officer3_amount': '3', 'high_comp_officer4_full_na': 'Test Exec 4', 'high_comp_officer4_amount': '4', 'high_comp_officer5_full_na': 'Test Exec 5', 'high_comp_officer5_amount': '5' }, '000000002': { 'awardee_or_recipient_uniqu': '000000002', 'registration_date': '2004-04-02', 'expiration_date': '2013-01-12', 'last_sam_mod_date': '2013-01-12', 'activation_date': '2012-01-12', 'legal_business_name': 'TEST DUNS 2', 'address_line_1': 'Other Test address 1', 'address_line_2': 'Other Test address 2', 'city': 'Other Test city', 'state': 'Other Test state', 'zip': 'Other Test zip', 'zip4': 'Other Test zip4', 'country_code': 'Other Test country', 'congressional_district': 'Other Test congressional district', 'business_types_codes': ['D', 'E', 'F'], 'business_types': ['Name D', 'Name E', 'Name F'], 'dba_name': 'Name 2', 'ultimate_parent_unique_ide': '999999998', 'ultimate_parent_legal_enti': 'Parent Legal Name 2', 'high_comp_officer1_full_na': 'Test Other Exec 6', 'high_comp_officer1_amount': '6', 'high_comp_officer2_full_na': 'Test Other Exec 7', 'high_comp_officer2_amount': '7', 'high_comp_officer3_full_na': 'Test Other Exec 8', 'high_comp_officer3_amount': '8', 'high_comp_officer4_full_na': 'Test Other Exec 9', 'high_comp_officer4_amount': '9', 'high_comp_officer5_full_na': 'Test Other Exec 10', 'high_comp_officer5_amount': '10' }, '000000003': { 'awardee_or_recipient_uniqu': '000000003', 'registration_date': None, 'expiration_date': None, 'last_sam_mod_date': None, 'activation_date': None, 'legal_business_name': None, 'address_line_1': None, 'address_line_2': None, 'city': None, 'state': None, 'zip': None, 'zip4': None, 'country_code': None, 'congressional_district': None, 'business_types_codes': None, 'business_types': None, 'dba_name': None, 'ultimate_parent_unique_ide': None, 'ultimate_parent_legal_enti': None, 'high_comp_officer1_full_na': None, 'high_comp_officer1_amount': None, 'high_comp_officer2_full_na': None, 'high_comp_officer2_amount': None, 'high_comp_officer3_full_na': None, 'high_comp_officer3_amount': None, 'high_comp_officer4_full_na': None, 'high_comp_officer4_amount': None, 'high_comp_officer5_full_na': None, 'high_comp_officer5_amount': None }, '000000004': { 'awardee_or_recipient_uniqu': '000000004', 'registration_date': None, 'expiration_date': None, 'last_sam_mod_date': None, 'activation_date': None, 'legal_business_name': None, 'address_line_1': None, 'address_line_2': None, 'city': None, 'state': None, 'zip': None, 'zip4': None, 'country_code': None, 'congressional_district': None, 'business_types_codes': None, 'business_types': None, 'dba_name': None, 'ultimate_parent_unique_ide': None, 'ultimate_parent_legal_enti': None, 'high_comp_officer1_full_na': None, 'high_comp_officer1_amount': None, 'high_comp_officer2_full_na': None, 'high_comp_officer2_amount': None, 'high_comp_officer3_full_na': None, 'high_comp_officer3_amount': None, 'high_comp_officer4_full_na': None, 'high_comp_officer4_amount': None, 'high_comp_officer5_full_na': None, 'high_comp_officer5_amount': None } } results = {} for duns_obj in sess.query(DUNS).all(): results[duns_obj.awardee_or_recipient_uniqu] = { 'awardee_or_recipient_uniqu': duns_obj.awardee_or_recipient_uniqu, 'registration_date': str(duns_obj.registration_date) if duns_obj.registration_date else None, 'expiration_date': str(duns_obj.expiration_date) if duns_obj.expiration_date else None, 'last_sam_mod_date': str(duns_obj.last_sam_mod_date) if duns_obj.last_sam_mod_date else None, 'activation_date': str(duns_obj.activation_date) if duns_obj.activation_date else None, 'legal_business_name': duns_obj.legal_business_name, 'address_line_1': duns_obj.address_line_1, 'address_line_2': duns_obj.address_line_2, 'city': duns_obj.city, 'state': duns_obj.state, 'zip': duns_obj.zip, 'zip4': duns_obj.zip4, 'country_code': duns_obj.country_code, 'congressional_district': duns_obj.congressional_district, 'business_types_codes': duns_obj.business_types_codes, 'business_types': duns_obj.business_types, 'dba_name': duns_obj.dba_name, 'ultimate_parent_unique_ide': duns_obj.ultimate_parent_unique_ide, 'ultimate_parent_legal_enti': duns_obj.ultimate_parent_legal_enti, 'high_comp_officer1_full_na': duns_obj.high_comp_officer1_full_na, 'high_comp_officer1_amount': duns_obj.high_comp_officer1_amount, 'high_comp_officer2_full_na': duns_obj.high_comp_officer2_full_na, 'high_comp_officer2_amount': duns_obj.high_comp_officer2_amount, 'high_comp_officer3_full_na': duns_obj.high_comp_officer3_full_na, 'high_comp_officer3_amount': duns_obj.high_comp_officer3_amount, 'high_comp_officer4_full_na': duns_obj.high_comp_officer4_full_na, 'high_comp_officer4_amount': duns_obj.high_comp_officer4_amount, 'high_comp_officer5_full_na': duns_obj.high_comp_officer5_full_na, 'high_comp_officer5_amount': duns_obj.high_comp_officer5_amount } assert results == expected_results # Test to see if truncating the DUNS table while keeping the historic reuploads the historic values sess.query(DUNS).filter(DUNS.historic.is_(True)).delete(synchronize_session=False) # Make sure all the historic DUNS are removed from the DUNS table assert sess.query(DUNS).filter(DUNS.historic.is_(True)).all() == [] # Redo script but don't go through run_duns_batches update_historical_duns.clean_historic_duns(sess) update_historical_duns.import_historic_duns(sess) results = {} for duns_obj in sess.query(DUNS).all(): results[duns_obj.awardee_or_recipient_uniqu] = { 'awardee_or_recipient_uniqu': duns_obj.awardee_or_recipient_uniqu, 'registration_date': str(duns_obj.registration_date) if duns_obj.registration_date else None, 'expiration_date': str(duns_obj.expiration_date) if duns_obj.expiration_date else None, 'last_sam_mod_date': str(duns_obj.last_sam_mod_date) if duns_obj.last_sam_mod_date else None, 'activation_date': str(duns_obj.activation_date) if duns_obj.activation_date else None, 'legal_business_name': duns_obj.legal_business_name, 'address_line_1': duns_obj.address_line_1, 'address_line_2': duns_obj.address_line_2, 'city': duns_obj.city, 'state': duns_obj.state, 'zip': duns_obj.zip, 'zip4': duns_obj.zip4, 'country_code': duns_obj.country_code, 'congressional_district': duns_obj.congressional_district, 'business_types_codes': duns_obj.business_types_codes, 'business_types': duns_obj.business_types, 'dba_name': duns_obj.dba_name, 'ultimate_parent_unique_ide': duns_obj.ultimate_parent_unique_ide, 'ultimate_parent_legal_enti': duns_obj.ultimate_parent_legal_enti, 'high_comp_officer1_full_na': duns_obj.high_comp_officer1_full_na, 'high_comp_officer1_amount': duns_obj.high_comp_officer1_amount, 'high_comp_officer2_full_na': duns_obj.high_comp_officer2_full_na, 'high_comp_officer2_amount': duns_obj.high_comp_officer2_amount, 'high_comp_officer3_full_na': duns_obj.high_comp_officer3_full_na, 'high_comp_officer3_amount': duns_obj.high_comp_officer3_amount, 'high_comp_officer4_full_na': duns_obj.high_comp_officer4_full_na, 'high_comp_officer4_amount': duns_obj.high_comp_officer4_amount, 'high_comp_officer5_full_na': duns_obj.high_comp_officer5_full_na, 'high_comp_officer5_amount': duns_obj.high_comp_officer5_amount } assert results == expected_results
def test_run_duns_batches(database, monkeypatch): """ Test run_duns_batches for the core functionality """ monkeypatch.setattr('dataactcore.utils.parentDuns.get_duns_props_from_sam', mock_get_duns_props_from_sam) sess = database.session all_duns = ['00000000{}'.format(x) for x in range(1, 5)] existing_duns = all_duns[2:] for duns in existing_duns: sess.add(DUNS(awardee_or_recipient_uniqu=duns)) sess.commit() duns_file = os.path.join(CONFIG_BROKER['path'], 'tests', 'unit', 'data', 'historic_DUNS_export_small.csv') update_historical_duns.run_duns_batches(duns_file, sess, None, block_size=1) expected_results = { '000000001': { 'awardee_or_recipient_uniqu': '000000001', 'registration_date': '2004-04-01', 'expiration_date': '2013-01-11', 'last_sam_mod_date': '2013-01-11', 'activation_date': '2012-01-11', 'legal_business_name': 'TEST DUNS 1', 'address_line_1': 'Test address 1', 'address_line_2': 'Test address 2', 'city': 'Test city', 'state': 'Test state', 'zip': 'Test zip', 'zip4': 'Test zip4', 'country_code': 'Test country', 'congressional_district': 'Test congressional district', 'business_types_codes': ['A', 'B', 'C'], 'business_types': ['Name A', 'Name B', 'Name C'], 'dba_name': 'Name 1', 'ultimate_parent_unique_ide': '999999999', 'ultimate_parent_legal_enti': 'Parent Legal Name 1', 'high_comp_officer1_full_na': 'Test Exec 1', 'high_comp_officer1_amount': '1', 'high_comp_officer2_full_na': 'Test Exec 2', 'high_comp_officer2_amount': '2', 'high_comp_officer3_full_na': 'Test Exec 3', 'high_comp_officer3_amount': '3', 'high_comp_officer4_full_na': 'Test Exec 4', 'high_comp_officer4_amount': '4', 'high_comp_officer5_full_na': 'Test Exec 5', 'high_comp_officer5_amount': '5' }, '000000002': { 'awardee_or_recipient_uniqu': '000000002', 'registration_date': '2004-04-02', 'expiration_date': '2013-01-12', 'last_sam_mod_date': '2013-01-12', 'activation_date': '2012-01-12', 'legal_business_name': 'TEST DUNS 2', 'address_line_1': 'Other Test address 1', 'address_line_2': 'Other Test address 2', 'city': 'Other Test city', 'state': 'Other Test state', 'zip': 'Other Test zip', 'zip4': 'Other Test zip4', 'country_code': 'Other Test country', 'congressional_district': 'Other Test congressional district', 'business_types_codes': ['D', 'E', 'F'], 'business_types': ['Name D', 'Name E', 'Name F'], 'dba_name': 'Name 2', 'ultimate_parent_unique_ide': '999999998', 'ultimate_parent_legal_enti': 'Parent Legal Name 2', 'high_comp_officer1_full_na': 'Test Other Exec 6', 'high_comp_officer1_amount': '6', 'high_comp_officer2_full_na': 'Test Other Exec 7', 'high_comp_officer2_amount': '7', 'high_comp_officer3_full_na': 'Test Other Exec 8', 'high_comp_officer3_amount': '8', 'high_comp_officer4_full_na': 'Test Other Exec 9', 'high_comp_officer4_amount': '9', 'high_comp_officer5_full_na': 'Test Other Exec 10', 'high_comp_officer5_amount': '10' } } results = {} for duns_obj in sess.query(HistoricDUNS).all(): results[duns_obj.awardee_or_recipient_uniqu] = { 'awardee_or_recipient_uniqu': duns_obj.awardee_or_recipient_uniqu, 'registration_date': str(duns_obj.registration_date) if duns_obj.registration_date else None, 'expiration_date': str(duns_obj.expiration_date) if duns_obj.expiration_date else None, 'last_sam_mod_date': str(duns_obj.last_sam_mod_date) if duns_obj.last_sam_mod_date else None, 'activation_date': str(duns_obj.activation_date) if duns_obj.activation_date else None, 'legal_business_name': duns_obj.legal_business_name, 'address_line_1': duns_obj.address_line_1, 'address_line_2': duns_obj.address_line_2, 'city': duns_obj.city, 'state': duns_obj.state, 'zip': duns_obj.zip, 'zip4': duns_obj.zip4, 'country_code': duns_obj.country_code, 'congressional_district': duns_obj.congressional_district, 'business_types_codes': duns_obj.business_types_codes, 'business_types': duns_obj.business_types, 'dba_name': duns_obj.dba_name, 'ultimate_parent_unique_ide': duns_obj.ultimate_parent_unique_ide, 'ultimate_parent_legal_enti': duns_obj.ultimate_parent_legal_enti, 'high_comp_officer1_full_na': duns_obj.high_comp_officer1_full_na, 'high_comp_officer1_amount': duns_obj.high_comp_officer1_amount, 'high_comp_officer2_full_na': duns_obj.high_comp_officer2_full_na, 'high_comp_officer2_amount': duns_obj.high_comp_officer2_amount, 'high_comp_officer3_full_na': duns_obj.high_comp_officer3_full_na, 'high_comp_officer3_amount': duns_obj.high_comp_officer3_amount, 'high_comp_officer4_full_na': duns_obj.high_comp_officer4_full_na, 'high_comp_officer4_amount': duns_obj.high_comp_officer4_amount, 'high_comp_officer5_full_na': duns_obj.high_comp_officer5_full_na, 'high_comp_officer5_amount': duns_obj.high_comp_officer5_amount } assert results == expected_results
def test_pubished_date_success(database): """ For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = A, AwardeeOrRecipientUniqueIdentifier must be active as of the ActionDate, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P'). This is a warning because CorrectionDeleteIndicator is C and the action date is before January 1, 2017.""" duns_1 = DUNS(awardee_or_recipient_uniqu="111111111", registration_date="06/21/2015", expiration_date="06/21/2016") det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="02", action_date="06/22/2015", record_type=2, business_types="A", correction_delete_indicatr="C") # Different assistant type det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="01", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="C") # Before October 1, 2010 det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="02", action_date="09/30/2010", record_type=2, business_types="A", correction_delete_indicatr="C") # Handled by d31_1 det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="03", action_date="06/20/2015", record_type=1, business_types="A", correction_delete_indicatr="C") det_award_5 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="03", action_date="06/20/2015", record_type=3, business_types="A", correction_delete_indicatr="C") det_award_6 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="04", action_date="06/20/2015", record_type=2, business_types="P", correction_delete_indicatr="C") # Handled by d31_2 det_award_7 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="", action_type="A", assistance_type="05", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="C") det_award_8 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu=None, action_type="A", assistance_type="02", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="C") # Handled by d31_3 det_award_9 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="ABCDEFGHI", action_type="A", assistance_type="03", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="C") # Handled by d31_4 det_award_10 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111112", action_type="A", assistance_type="04", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="C") # Handled by d31_5 det_award_11 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="05", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="D") det_award_12 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="02", action_date="06/20/2017", record_type=2, business_types="A", correction_delete_indicatr="C") # handled in d31_7 det_award_13 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="B", assistance_type="03", action_date="06/20/2015", record_type=2, business_types="A", correction_delete_indicatr="C") # handled in d4 det_award_14 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="04", action_date="YYYYMMDD", record_type=2, business_types="A", correction_delete_indicatr="C") det_award_15 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="05", action_date="AAAAAAAAAA", record_type=2, business_types="A", correction_delete_indicatr="C") errors = number_of_errors(_FILE, database, models=[ duns_1, 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, det_award_11, det_award_12, det_award_13, det_award_14, det_award_15 ]) assert errors == 0
def test_pubished_date_success(database): """ For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010, AwardeeOrRecipientUniqueIdentifier must be found in our records, unless the record is an aggregate or PII-redacted non-aggregate record (RecordType=1 or 3) or individual recipient (BusinessTypes includes 'P'). """ duns_1 = DUNS(awardee_or_recipient_uniqu='111111111') det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111111', assistance_type='02', action_date='10/02/2010', record_type=2, business_types='a', correction_delete_indicatr='') # Different assistance type det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='01', action_date='10/02/2010', record_type=2, business_types='A', correction_delete_indicatr='c') # Before October 1, 2010 det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='02', action_date='09/30/2010', record_type=2, business_types='A', correction_delete_indicatr='C') # Handled by d31_1 det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='03', action_date='10/02/2010', record_type=1, business_types='a', correction_delete_indicatr=None) det_award_5 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='03', action_date='10/02/2010', record_type=3, business_types='A', correction_delete_indicatr='') det_award_6 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='04', action_date='10/02/2010', record_type=2, business_types='P', correction_delete_indicatr='') # Handled by d31_2 det_award_7 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='', assistance_type='05', action_date='10/02/2010', record_type=2, business_types='A', correction_delete_indicatr='') det_award_8 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu=None, assistance_type='02', action_date='10/02/2010', record_type=2, business_types='A', correction_delete_indicatr='') # Handled by d31_3 det_award_9 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='ABCDEFGHI', assistance_type='03', action_date='10/02/2010', record_type=2, business_types='A', correction_delete_indicatr='') # Ignore correction delete indicator of D det_award_10 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu='111111112', assistance_type='02', action_date='10/02/2010', record_type=2, business_types='A', correction_delete_indicatr='d') errors = number_of_errors(_FILE, database, models=[ duns_1, 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 ]) assert errors == 0
def test_pubished_date_success(database): """ For AssistanceType of 02, 03, 04, or 05 whose ActionDate is after October 1, 2010 and ActionType = B, C, or D, AwardeeOrRecipientUniqueIdentifier should be active on the ActionDate, unless the record is an aggregate record (RecordType=1) or individual recipient (BusinessTypes includes 'P'). """ duns_1 = DUNS(awardee_or_recipient_uniqu="111111111", registration_date="06/21/2017", expiration_date="06/21/2018") det_award_1 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="B", assistance_type="02", action_date="06/22/2017", record_type=2, business_types="A") det_award_2 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="C", assistance_type="03", action_date="06/22/2017", record_type=2, business_types="A") det_award_3 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="D", assistance_type="04", action_date="06/22/2017", record_type=2, business_types="A") det_award_4 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="D", assistance_type="05", action_date="06/22/2017", record_type=2, business_types="A") # Different Assistance Type det_award_5 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="B", assistance_type="01", action_date="06/20/2017", record_type=2, business_types="A") # Before October 1, 2010 det_award_6 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="B", assistance_type="02", action_date="09/30/2010", record_type=2, business_types="A") # Handled by d31_1 det_award_7 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="", action_type="B", assistance_type="03", action_date="06/20/2017", record_type=1, business_types="A") det_award_8 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu=None, action_type="B", assistance_type="04", action_date="06/20/2017", record_type=2, business_types="P") # Handled by d31_2 det_award_9 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="", action_type="B", assistance_type="05", action_date="06/20/2017", record_type=2, business_types="A") det_award_10 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu=None, action_type="B", assistance_type="02", action_date="06/20/2017", record_type=2, business_types="A") # Handled by d31_3 det_award_11 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="ABCDEFGHI", action_type="B", assistance_type="03", action_date="06/20/2017", record_type=2, business_types="A") # Handled by d31_4 det_award_12 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111112", action_type="B", assistance_type="04", action_date="06/20/2017", record_type=2, business_types="A") # Handled by d31_5 det_award_13 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="05", action_date="06/20/2017", record_type=2, business_types="A") # handled in d4 det_award_14 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="02", action_date="YYYYMMDD", record_type=2, business_types="A") det_award_15 = DetachedAwardFinancialAssistanceFactory( awardee_or_recipient_uniqu="111111111", action_type="A", assistance_type="03", action_date="AAAAAAAAAA", record_type=2, business_types="A") errors = number_of_errors(_FILE, database, models=[ duns_1, 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, det_award_11, det_award_12, det_award_13, det_award_14, det_award_15 ]) assert errors == 0