Skip to content

Commit

Permalink
Merge pull request #285 from GSA/feature/58_grey_na
Browse files Browse the repository at this point in the history
Scraper-58: Added 'grey' value for Not Applicable Solicitations
  • Loading branch information
gsa-bri authored Aug 30, 2023
2 parents 05c9eda + 0043bb3 commit f44d10d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 30 deletions.
62 changes: 33 additions & 29 deletions src/fbo_scraper/db/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 " "
Expand Down
84 changes: 83 additions & 1 deletion tests/test_db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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']

0 comments on commit f44d10d

Please sign in to comment.