From b8b67a21ae2a177cac03ca6604cbcaa8ab1ec1ef Mon Sep 17 00:00:00 2001 From: Adam Buckingham Date: Tue, 29 Aug 2023 14:14:34 -0400 Subject: [PATCH 1/2] 58: Added grey value for Not Applicable solicitations --- src/fbo_scraper/db/db_utils.py | 62 ++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/fbo_scraper/db/db_utils.py b/src/fbo_scraper/db/db_utils.py index 8d5494d..215f944 100644 --- a/src/fbo_scraper/db/db_utils.py +++ b/src/fbo_scraper/db/db_utils.py @@ -355,6 +355,36 @@ def is_machine_readable(attachments: list) -> bool: return True return False +def apply_predictions_to(solicitation: Solicitation, predicition: int): + new_prediction = deepcopy(solicitation.predictions) # make a copy - if you only chagne the props then SQAlchamy won't know the object changed + + if solicitation.na_flag: + solicitation.reviewRec = "Not Applicable" + new_prediction['value'] = "grey" + new_prediction['508'] = "grey" + else: + if predicition != 0: + new_prediction['value'] = "green" + new_prediction['508'] = "green" + solicitation.reviewRec = "Compliant" + solicitation.compliant = 1 + else: + new_prediction['value'] = "red" + new_prediction['508'] = "red" + solicitation.reviewRec = "Non-compliant (Action Required)" + solicitation.compliant = 0 + + # add a random estar prediction + # TODO: properly compute estar prediction + if solicitation.noticeData.get('epa_psc_match', False): + estar = "red" if random() < .5 else "green" + else: + estar = "Not Applicable" + new_prediction['estar'] = estar + + new_prediction['history'].append( { "date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"), "value": new_prediction['value'], "508": new_prediction['value'], "estar": estar} ) + solicitation.predictions = new_prediction + def insert_data_into_solicitations_table(session, data): ''' Insert opportunities data into the database. @@ -404,35 +434,9 @@ def insert_data_into_solicitations_table(session, data): sol_prediction = handle_attachments(opp, sol, now=now_datetime) - new_prediction = deepcopy(sol.predictions) # make a copy - if you only chagne the props then SQAlchamy won't know the object changed - if sol_prediction != 0: - new_prediction['value'] = "green" - new_prediction['508'] = "green" - else: - new_prediction['value'] = "red" - new_prediction['508'] = "red" - - # add a random estar prediction - # TODO: properly compute estar prediction - if sol.noticeData.get('epa_psc_match', False): - estar = "red" if random() < .5 else "green" - else: - estar = "Not Applicable" - new_prediction['estar'] = estar - - new_prediction['history'].append( { "date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"), "value": new_prediction['value'], "508": new_prediction['value'], "estar": estar} ) - sol.predictions = new_prediction - - - if sol.na_flag: - sol.reviewRec = "Not Applicable" - else: - if new_prediction['value'] == "green": - sol.reviewRec = "Compliant" - sol.compliant = 1 - else: - sol.reviewRec = "Non-compliant (Action Required)" - sol.compliant = 0 + + apply_predictions_to(solicitation=sol, predicition=sol_prediction) + # now set the search text column so that we can easily do a full text search in the API safe_date = sol.date if sol.date else " " From 0043bb3fe2012d57aa2dd5a3594f5e64570eb4b7 Mon Sep 17 00:00:00 2001 From: Adam Buckingham Date: Tue, 29 Aug 2023 14:15:09 -0400 Subject: [PATCH 2/2] 58: Tests for apply_predicitons_to function --- tests/test_db_utils.py | 84 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/tests/test_db_utils.py b/tests/test_db_utils.py index e88c3c1..c21ad2e 100644 --- a/tests/test_db_utils.py +++ b/tests/test_db_utils.py @@ -7,7 +7,7 @@ from tests.mock_opps import mock_schematized_opp_two from fbo_scraper.db.db import Notice, NoticeType, Solicitation, Attachment, Model, now_minus_two from fbo_scraper.db.db_utils import get_db_url, session_scope, insert_data_into_solicitations_table, \ - DataAccessLayer, insert_notice_types, update_solicitation_history, search_for_agency, handle_attachments + DataAccessLayer, insert_notice_types, update_solicitation_history, search_for_agency, handle_attachments, apply_predictions_to from datetime import datetime, timedelta @@ -224,3 +224,85 @@ def __init__(self, notice_type_id, parseStatus, na_flag): assert sol.numDocs == 1 assert sol.na_flag == True assert len(sol.attachments) == 1 + + +@pytest.mark.parametrize("solicitation,prediction, expected", [ + ## Test case 1: Solicitation with grey value and a prediction of 0 + ( + Solicitation( + na_flag=False, + predictions={ + 'value': 'grey', + '508': 'grey', + 'estar': 'Not Applicable', + 'history': [] + } + ), 0, { + 'value': 'red', + '508': 'red', + 'reviewRec': 'Non-compliant (Action Required)', + 'compliant': 0, + }), + ## Test case 2: Solicitation with grey value and a prediction of 1 + ( + Solicitation( + na_flag=False, + predictions={ + 'value': 'grey', + '508': 'grey', + 'estar': 'Not Applicable', + 'history': [] + } + ), 1, + { + 'value':'green', + '508':'green', + 'reviewRec':'Compliant', + 'compliant':1, + }), + ## Test case 3: Solicitation with green value, prediciton 1 and na_flag set to True + ( + Solicitation( + na_flag=True, + predictions={ + 'value': 'green', + '508': 'green', + 'estar': 'Compliant', + 'history': [] + } + ), 1, + { + 'value': 'grey', + '508': 'grey', + 'reviewRec': 'Not Applicable', + 'compliant': None + }), + ## Test case 4: Solicitation with green value, prediciton 0 and na_flag set to True + ( + Solicitation( + na_flag=True, + predictions={ + 'value': 'green', + '508': 'green', + 'estar': 'Compliant', + 'history': [] + } + ), 0, + { + 'value': 'grey', + '508': 'grey', + 'reviewRec': 'Not Applicable', + 'compliant': None + }) +]) +def test_apply_predictions_to(solicitation, prediction, expected): + solicitation.noticeData = {} + + # Call the function with a prediction of 1 + apply_predictions_to(solicitation, prediction) + + # Check that the predictions were updated correctly + assert solicitation.predictions['value'] == expected['value'] + assert solicitation.predictions['508'] == expected['508'] + assert solicitation.reviewRec == expected['reviewRec'] + assert solicitation.compliant == expected['compliant'] \ No newline at end of file