def test_category_psc_subawards(mock_matviews_qs, mock_psc): mock_psc_1 = MockModel(code="PSC 1234", description="PSC DESCRIPTION UP") mock_psc_2 = MockModel(code="PSC 9876", description="PSC DESCRIPTION DOWN") mock_model_1 = MockModel(product_or_service_code="PSC 1234", amount=1) mock_model_2 = MockModel(product_or_service_code="PSC 1234", amount=1) mock_model_3 = MockModel(product_or_service_code="PSC 9876", amount=2) mock_model_4 = MockModel(product_or_service_code="PSC 9876", amount=2) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) add_to_mock_objects(mock_psc, [mock_psc_1, mock_psc_2]) test_payload = {"category": "psc", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "psc", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [ {"amount": 4, "code": "PSC 9876", "id": None, "name": "PSC DESCRIPTION DOWN"}, {"amount": 2, "code": "PSC 1234", "id": None, "name": "PSC DESCRIPTION UP"}, ], } assert expected_response == spending_by_category_logic
def test_spending_by_award_subawards_no_intersection(client, mock_matviews_qs): mock_model_1 = MockModel( award_ts_vector="", subaward_id=9999, award_type="grant", prime_award_type="02", award_id=90, awarding_toptier_agency_name="Department of Pizza", awarding_toptier_agency_abbreviation="DOP", generated_pragmatic_obligation=10 ) add_to_mock_objects(mock_matviews_qs, [mock_model_1]) request = { "subawards": True, "fields": ["Sub-Award ID"], "sort": "Sub-Award ID", "filters": {"award_type_codes": ["02", "03", "04", "05"]}, } resp = client.post( "/api/v2/search/spending_by_award_count", content_type="application/json", data=json.dumps(request) ) assert resp.status_code == status.HTTP_200_OK assert resp.data["results"]["subgrants"] == 1 request["filters"]["award_type_codes"].append("no intersection") resp = client.post( "/api/v2/search/spending_by_award_count", content_type="application/json", data=json.dumps(request) ) assert resp.status_code == status.HTTP_200_OK assert resp.data["results"] == {"subcontracts": 0, "subgrants": 0}, "Results returned, there should all be 0"
def test_all_subawards(mock_matviews_qs): mock_model_1 = MockModel(**subaward_1) mock_model_2 = MockModel(**subaward_2) mock_model_3 = MockModel(**subaward_3) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) test_payload = {"page": 1, "limit": 10, "order": "asc"} svs = SubawardsViewSet() test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) expected_response = [strip_award_id(subaward_1), strip_award_id(subaward_2), strip_award_id(subaward_3)] assert expected_response == subawards_logic test_payload["page"] = 2 test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) assert [] == subawards_logic sub_1 = strip_award_id(subaward_1) sub_2 = strip_award_id(subaward_2) sub_3 = strip_award_id(subaward_3) assert request_with_sort("id") == [sub_3, sub_1, sub_2] assert request_with_sort("amount") == [sub_3, sub_2, sub_1] assert request_with_sort("action_date") == [sub_2, sub_1, sub_3] assert request_with_sort("recipient_name") == [sub_2, sub_3, sub_1]
def test_category_recipient_parent_duns_subawards(mock_matviews_qs, mock_recipients): mock_recipient_1 = MockModel(recipient_unique_id="00UOP00", legal_entity_id=1) mock_recipient_2 = MockModel(recipient_unique_id="1234JD4321", legal_entity_id=2) mock_model_1 = MockModel(recipient_name="University of Pawnee", parent_recipient_unique_id="00UOP00", amount=1) mock_model_2 = MockModel(recipient_name="University of Pawnee", parent_recipient_unique_id="00UOP00", amount=1) mock_model_3 = MockModel(recipient_name="John Doe", parent_recipient_unique_id="1234JD4321", amount=1) mock_model_4 = MockModel(recipient_name="John Doe", parent_recipient_unique_id="1234JD4321", amount=10) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) add_to_mock_objects(mock_recipients, [mock_recipient_1, mock_recipient_2]) test_payload = {"category": "recipient_parent_duns", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "recipient_parent_duns", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [ {"amount": 11, "name": "John Doe", "code": "1234JD4321", "id": 2}, {"amount": 2, "name": "University of Pawnee", "code": "00UOP00", "id": 1}, ], } assert expected_response == spending_by_category_logic
def test_all_subawards(mock_matviews_qs): mock_model_1 = MockModel(**subaward_1) mock_model_2 = MockModel(**subaward_2) mock_model_3 = MockModel(**subaward_3) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) test_payload = { "page": 1, "limit": 10, "order": "asc", } svs = SubawardsViewSet() test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) expected_response = [strip_award_id(subaward_1), strip_award_id(subaward_2), strip_award_id(subaward_3)] assert expected_response == subawards_logic test_payload['page'] = 2 test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) assert [] == subawards_logic test_payload = { "order": "desc", } test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) assert [strip_award_id(subaward_3), strip_award_id(subaward_2), strip_award_id(subaward_1)] == subawards_logic
def test_category_naics_awards(mock_matviews_qs, mock_naics): mock_model_1 = MockModel( naics_code="NAICS 1234", naics_description="NAICS DESC 1234", generated_pragmatic_obligation=1 ) mock_model_2 = MockModel( naics_code="NAICS 1234", naics_description="NAICS DESC 1234", generated_pragmatic_obligation=1 ) mock_model_3 = MockModel( naics_code="NAICS 9876", naics_description="NAICS DESC 9876", generated_pragmatic_obligation=2 ) mock_model_4 = MockModel( naics_code="NAICS 9876", naics_description="NAICS DESC 9876", generated_pragmatic_obligation=2 ) mock_naics_1 = MockModel(code="NAICS 1234", description="SOURCE NAICS DESC 1234", year=1955) mock_naics_2 = MockModel(code="NAICS 9876", description="SOURCE NAICS DESC 9876", year=1985) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) add_to_mock_objects(mock_naics, [mock_naics_1, mock_naics_2]) test_payload = {"category": "naics", "subawards": False, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "naics", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [ {"amount": 4, "code": "NAICS 9876", "name": "SOURCE NAICS DESC 9876", "id": None}, {"amount": 2, "code": "NAICS 1234", "name": "SOURCE NAICS DESC 1234", "id": None}, ], } assert expected_response == spending_by_category_logic
def test_category_state_territory_subawards(mock_matviews_qs): mock_model_1 = MockModel(pop_state_code="XY", amount=1) mock_model_2 = MockModel(pop_state_code="XY", amount=1) mommy.make("recipient.StateData", name="Test State", code="XY") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = { "category": "state_territory", "subawards": True, "page": 1, "limit": 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "state_territory", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [{ "amount": 2, "code": "XY", "name": "Test State", "id": None }], } assert expected_response == spending_by_category_logic
def test_category_funding_agency_subawards(mock_matviews_qs, mock_agencies): mock_toptier = MockModel(toptier_agency_id=1, name="Department of Calzone", abbreviation="DOC") mock_agency = MockModel(id=2, toptier_agency=mock_toptier, toptier_flag=True) mock_agency_1 = MockModel(id=3, toptier_agency=mock_toptier, toptier_flag=False) mock_model_1 = MockModel( funding_agency_id=2, funding_toptier_agency_name="Department of Calzone", funding_toptier_agency_abbreviation="DOC", amount=50, ) mock_model_2 = MockModel( funding_agency_id=3, funding_toptier_agency_name="Department of Calzone", funding_toptier_agency_abbreviation="DOC", amount=50, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_agencies["agency"], [mock_agency, mock_agency_1]) test_payload = {"category": "funding_agency", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "funding_agency", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 100, "name": "Department of Calzone", "code": "DOC", "id": 2}], } assert expected_response == spending_by_category_logic
def test_no_intersection(client, mock_matviews_qs): mock_model_1 = MockModel( award_ts_vector="", type="A", type_of_contract_pricing="", naics_code="98374", cfda_number="987.0", pulled_from=None, uri=None, piid="djsd", fain=None, award_id=90, awarding_toptier_agency_name="Department of Pizza", awarding_toptier_agency_abbreviation="DOP", generated_pragmatic_obligation=10, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1]) request = { "subawards": False, "fields": ["Award ID"], "sort": "Award ID", "filters": {"award_type_codes": ["A", "B", "C", "D"]}, } resp = client.post("/api/v2/search/spending_by_award", content_type="application/json", data=json.dumps(request)) assert resp.status_code == status.HTTP_200_OK assert len(resp.data["results"]) == 1 request["filters"]["award_type_codes"].append("no intersection") resp = client.post("/api/v2/search/spending_by_award", content_type="application/json", data=json.dumps(request)) assert resp.status_code == status.HTTP_200_OK assert len(resp.data["results"]) == 0, "Results returned, there should be 0"
def test_category_awarding_subagency_awards(mock_matviews_qs, mock_agencies): mock_subtier = MockModel(subtier_agency_id=1, name="Department of Sub-pizza", abbreviation="DOSP") mock_agency = MockModel(id=2, subtier_agency=mock_subtier, toptier_flag=False) mock_agency_1 = MockModel(id=3, subtier_agency=mock_subtier, toptier_flag=True) mock_model_1 = MockModel( awarding_agency_id=2, awarding_subtier_agency_name="Department of Sub-pizza", awarding_subtier_agency_abbreviation="DOSP", generated_pragmatic_obligation=10, ) mock_model_2 = MockModel( awarding_agency_id=3, awarding_subtier_agency_name="Department of Sub-pizza", awarding_subtier_agency_abbreviation="DOSP", generated_pragmatic_obligation=10, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_agencies["agency"], [mock_agency, mock_agency_1]) test_payload = {"category": "awarding_subagency", "subawards": False, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "awarding_subagency", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 20, "name": "Department of Sub-pizza", "code": "DOSP", "id": 2}], } assert expected_response == spending_by_category_logic
def test_category_recipient_parent_duns_subawards(mock_matviews_qs, mock_recipients): mock_recipient_1 = MockModel(recipient_unique_id='00UOP00', legal_entity_id=1) mock_recipient_2 = MockModel(recipient_unique_id='1234JD4321', legal_entity_id=2) mock_model_1 = MockModel(recipient_name='University of Pawnee', parent_recipient_unique_id='00UOP00', amount=1) mock_model_2 = MockModel(recipient_name='University of Pawnee', parent_recipient_unique_id='00UOP00', amount=1) mock_model_3 = MockModel(recipient_name='John Doe', parent_recipient_unique_id='1234JD4321', amount=1) mock_model_4 = MockModel(recipient_name='John Doe', parent_recipient_unique_id='1234JD4321', amount=10) add_to_mock_objects( mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) add_to_mock_objects(mock_recipients, [mock_recipient_1, mock_recipient_2]) test_payload = { 'category': 'recipient_parent_duns', 'subawards': True, 'page': 1, 'limit': 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { 'category': 'recipient_parent_duns', 'limit': 50, 'page_metadata': { 'page': 1, 'next': None, 'previous': None, 'hasNext': False, 'hasPrevious': False }, 'results': [{ 'amount': 11, 'name': 'John Doe', 'code': '1234JD4321', 'id': 2, }, { 'amount': 2, 'name': 'University of Pawnee', 'code': '00UOP00', 'id': 1, }] } assert expected_response == spending_by_category_logic
def test_spending_by_award_both_zip_filter(client, mock_matviews_qs): """ Test that filtering by both kinds of zips works""" mock_model_1 = MockModel(recipient_location_zip5="00501", recipient_location_country_code='USA', pop_zip5='00001', pop_country_code='USA', award_id=1, piid=None, fain='abc', uri=None, type='B', pulled_from="AWARD") mock_model_2 = MockModel(recipient_location_zip5="00502", recipient_location_country_code='USA', pop_zip5='00002', pop_country_code='USA', award_id=2, piid=None, fain='abd', uri=None, type='B', pulled_from="AWARD") mock_model_3 = MockModel(recipient_location_zip5="00503", recipient_location_country_code='USA', pop_zip5='00003', pop_country_code='USA', award_id=3, piid=None, fain='abe', uri=None, type='B', pulled_from="AWARD") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) # test simple, single pair of zips that both match resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_locations": [{"country": "USA", "zip": "00501"}], "place_of_performance_locations": [{"country": "USA", "zip": "00001"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00001'} # test simple, single pair of zips that don't match resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_locations": [{"country": "USA", "zip": "00501"}], "place_of_performance_locations": [{"country": "USA", "zip": "00002"}] } })) assert len(resp.data['results']) == 0 # test 2 pairs (only one pair can be made from this) resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_locations": [{"country": "USA", "zip": "00501"}, {"country": "USA", "zip": "00502"}], "place_of_performance_locations": [{"country": "USA", "zip": "00001"}, {"country": "USA", "zip": "00003"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00001'}
def test_spending_by_award_pop_zip_filter(client, mock_matviews_qs): """ Test that filtering by pop zips works""" mock_model_1 = MockModel(pop_zip5="00501", pop_country_code='USA', award_id=1, piid=None, fain='abc', uri=None, type='B', pulled_from="AWARD") mock_model_2 = MockModel(pop_zip5="00502", pop_country_code='USA', award_id=2, piid=None, fain='abd', uri=None, type='B', pulled_from="AWARD") mock_model_3 = MockModel(pop_zip5="00503", pop_country_code='USA', award_id=3, piid=None, fain='abe', uri=None, type='B', pulled_from="AWARD") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) # test simple, single zip resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "place_of_performance_locations": [{"country": "USA", "zip": "00501"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00501'} # test that adding a zip that has no results doesn't remove the results from the first zip resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "place_of_performance_locations": [{"country": "USA", "zip": "00501"}, {"country": "USA", "zip": "10000"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00501'} # test that we get 2 results with 2 valid zips resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "place_of_performance_locations": [{"country": "USA", "zip": "00501"}, {"country": "USA", "zip": "00502"}] } })) possible_results = ({'internal_id': 1, 'Place of Performance Zip5': '00501'}, {'internal_id': 2, 'Place of Performance Zip5': '00502'}) assert len(resp.data['results']) == 2 assert resp.data['results'][0] in possible_results assert resp.data['results'][1] in possible_results # Just to make sure it isn't returning the same thing twice somehow assert resp.data['results'][0] != resp.data['results'][1]
def test_spending_by_award_both_zip_filter(client, mock_matviews_qs): """ Test that filtering by both kinds of zips works""" mock_model_1 = MockModel(recipient_location_zip5="00501", recipient_location_country_code='USA', pop_zip5='00001', pop_country_code='USA', award_id=1, piid=None, fain='abc', uri=None, type='B', pulled_from="AWARD") mock_model_2 = MockModel(recipient_location_zip5="00502", recipient_location_country_code='USA', pop_zip5='00002', pop_country_code='USA', award_id=2, piid=None, fain='abd', uri=None, type='B', pulled_from="AWARD") mock_model_3 = MockModel(recipient_location_zip5="00503", recipient_location_country_code='USA', pop_zip5='00003', pop_country_code='USA', award_id=3, piid=None, fain='abe', uri=None, type='B', pulled_from="AWARD") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) # test simple, single pair of zips that both match resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_locations": [{"country": "USA", "zip": "00501"}], "place_of_performance_locations": [{"country": "USA", "zip": "00001"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00001'} # test simple, single pair of zips that don't match resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_locations": [{"country": "USA", "zip": "00501"}], "place_of_performance_locations": [{"country": "USA", "zip": "00002"}] } })) assert len(resp.data['results']) == 0 # test 2 pairs (only one pair can be made from this) resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_locations": [{"country": "USA", "zip": "00501"}, {"country": "USA", "zip": "00502"}], "place_of_performance_locations": [{"country": "USA", "zip": "00001"}, {"country": "USA", "zip": "00003"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00001'}
def test_spending_by_award_pop_zip_filter(client, mock_matviews_qs): """ Test that filtering by pop zips works""" mock_model_1 = MockModel(pop_zip5="00501", pop_country_code='USA', award_id=1, piid=None, fain='abc', uri=None, type='B', pulled_from="AWARD") mock_model_2 = MockModel(pop_zip5="00502", pop_country_code='USA', award_id=2, piid=None, fain='abd', uri=None, type='B', pulled_from="AWARD") mock_model_3 = MockModel(pop_zip5="00503", pop_country_code='USA', award_id=3, piid=None, fain='abe', uri=None, type='B', pulled_from="AWARD") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) # test simple, single zip resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "place_of_performance_locations": [{"country": "USA", "zip": "00501"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00501'} # test that adding a zip that has no results doesn't remove the results from the first zip resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "place_of_performance_locations": [{"country": "USA", "zip": "00501"}, {"country": "USA", "zip": "10000"}] } })) assert len(resp.data['results']) == 1 assert resp.data['results'][0] == {'internal_id': 1, 'Place of Performance Zip5': '00501'} # test that we get 2 results with 2 valid zips resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "fields": ["Place of Performance Zip5"], "filters": { "award_type_codes": ["A", "B", "C", "D"], "place_of_performance_locations": [{"country": "USA", "zip": "00501"}, {"country": "USA", "zip": "00502"}] } })) possible_results = ({'internal_id': 1, 'Place of Performance Zip5': '00501'}, {'internal_id': 2, 'Place of Performance Zip5': '00502'}) assert len(resp.data['results']) == 2 assert resp.data['results'][0] in possible_results assert resp.data['results'][1] in possible_results # Just to make sure it isn't returning the same thing twice somehow assert resp.data['results'][0] != resp.data['results'][1]
def add_award_recipients(monkeypatch): mock_awards_recipients = MockSet() monkeypatch.setattr( "usaspending_api.recipient.models.SummaryAwardRecipient.objects", mock_awards_recipients) mock_model_list = [] current_id = 1 new_award_count = 12 for i in range(current_id, current_id + new_award_count): mock_model_list.append( MockModel( award_id=i, recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, action_date=datetime(2009, 5, 30), )) current_id += new_award_count new_award_count = 3 for i in range(current_id, current_id + new_award_count): mock_model_list.append( MockModel( award_id=i, recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, action_date=datetime(2009, 5, 1), )) current_id += new_award_count new_award_count = 1 for i in range(current_id, current_id + new_award_count): mock_model_list.append( MockModel( award_id=i, recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, action_date=datetime(2009, 7, 2), )) current_id += new_award_count new_award_count = 2 for i in range(current_id, current_id + new_award_count): mock_model_list.append( MockModel( award_id=i, recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, action_date=datetime(2008, 1, 10), )) current_id += new_award_count new_award_count = 6 for i in range(current_id, current_id + new_award_count): mock_model_list.append( MockModel( award_id=i, recipient_hash="4e418651-4b83-8722-ab4e-e68d80bfb3b3", parent_recipient_unique_id=None, action_date=datetime(2009, 7, 30), )) add_to_mock_objects(mock_awards_recipients, mock_model_list)
def test_category_awarding_subagency_subawards(mock_matviews_qs, mock_agencies): mock_subtier = MockModel(subtier_agency_id=1, name="Department of Sub-pizza", abbreviation="DOSP") mock_agency = MockModel(id=2, subtier_agency=mock_subtier, toptier_flag=False) mock_agency_1 = MockModel(id=3, subtier_agency=mock_subtier, toptier_flag=True) mock_model_1 = MockModel( awarding_agency_id=2, awarding_subtier_agency_name="Department of Sub-pizza", awarding_subtier_agency_abbreviation="DOSP", amount=10, ) mock_model_2 = MockModel( awarding_agency_id=3, awarding_subtier_agency_name="Department of Sub-pizza", awarding_subtier_agency_abbreviation="DOSP", amount=10, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_agencies["agency"], [mock_agency, mock_agency_1]) test_payload = { "category": "awarding_subagency", "subawards": True, "page": 1, "limit": 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "awarding_subagency", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [{ "amount": 20, "name": "Department of Sub-pizza", "code": "DOSP", "id": 2 }], } assert expected_response == spending_by_category_logic
def test_category_naics_awards(mock_matviews_qs): mock_model_1 = MockModel(naics_code="NAICS 1234", naics_description="NAICS DESC 1234", generated_pragmatic_obligation=1) mock_model_2 = MockModel(naics_code="NAICS 1234", naics_description="NAICS DESC 1234", generated_pragmatic_obligation=1) mock_model_3 = MockModel(naics_code="NAICS 9876", naics_description="NAICS DESC 9876", generated_pragmatic_obligation=2) mock_model_4 = MockModel(naics_code="NAICS 9876", naics_description="NAICS DESC 9876", generated_pragmatic_obligation=2) add_to_mock_objects( mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) test_payload = { "category": "naics", "subawards": False, "page": 1, "limit": 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "naics", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [ { "amount": 4, "code": "NAICS 9876", "name": "NAICS DESC 9876", "id": None }, { "amount": 2, "code": "NAICS 1234", "name": "NAICS DESC 1234", "id": None }, ], } assert expected_response == spending_by_category_logic
def test_category_funding_agency_awards(mock_matviews_qs, mock_agencies): mock_toptier = MockModel(toptier_agency_id=1, name="Department of Calzone", abbreviation="DOC") mock_agency = MockModel(id=2, toptier_agency=mock_toptier, toptier_flag=True) mock_agency_1 = MockModel(id=3, toptier_agency=mock_toptier, toptier_flag=False) mock_model_1 = MockModel( funding_agency_id=2, funding_toptier_agency_name="Department of Calzone", funding_toptier_agency_abbreviation="DOC", generated_pragmatic_obligation=50, ) mock_model_2 = MockModel( funding_agency_id=3, funding_toptier_agency_name="Department of Calzone", funding_toptier_agency_abbreviation="DOC", generated_pragmatic_obligation=50, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_agencies["agency"], [mock_agency, mock_agency_1]) test_payload = { "category": "funding_agency", "subawards": False, "page": 1, "limit": 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "funding_agency", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [{ "amount": 100, "name": "Department of Calzone", "code": "DOC", "id": 2 }], } assert expected_response == spending_by_category_logic
def test_category_awarding_subagency_subawards(mock_matviews_qs, mock_agencies): mock_subtier = MockModel(subtier_agency_id=1, name='Department of Sub-pizza', abbreviation='DOSP') mock_agency = MockModel(id=2, subtier_agency=mock_subtier, toptier_flag=False) mock_agency_1 = MockModel(id=3, subtier_agency=mock_subtier, toptier_flag=True) mock_model_1 = MockModel( awarding_agency_id=2, awarding_subtier_agency_name='Department of Sub-pizza', awarding_subtier_agency_abbreviation='DOSP', amount=10) mock_model_2 = MockModel( awarding_agency_id=3, awarding_subtier_agency_name='Department of Sub-pizza', awarding_subtier_agency_abbreviation='DOSP', amount=10) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_agencies['agency'], [mock_agency, mock_agency_1]) test_payload = { 'category': 'awarding_subagency', 'subawards': True, 'page': 1, 'limit': 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { 'category': 'awarding_subagency', 'limit': 50, 'page_metadata': { 'page': 1, 'next': None, 'previous': None, 'hasNext': False, 'hasPrevious': False }, 'results': [{ 'amount': 20, 'name': 'Department of Sub-pizza', 'code': 'DOSP', 'id': 2 }] } assert expected_response == spending_by_category_logic
def test_category_psc_awards(mock_matviews_qs, mock_psc): mock_psc_1 = MockModel(code="PSC 1234", description="PSC DESCRIPTION UP") mock_psc_2 = MockModel(code="PSC 9876", description="PSC DESCRIPTION DOWN") mock_model_1 = MockModel(product_or_service_code="PSC 1234", generated_pragmatic_obligation=1) mock_model_2 = MockModel(product_or_service_code="PSC 1234", generated_pragmatic_obligation=1) mock_model_3 = MockModel(product_or_service_code="PSC 9876", generated_pragmatic_obligation=2) mock_model_4 = MockModel(product_or_service_code="PSC 9876", generated_pragmatic_obligation=2) add_to_mock_objects( mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) add_to_mock_objects(mock_psc, [mock_psc_1, mock_psc_2]) test_payload = { "category": "psc", "subawards": False, "page": 1, "limit": 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "psc", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [ { "amount": 4, "code": "PSC 9876", "id": None, "name": "PSC DESCRIPTION DOWN" }, { "amount": 2, "code": "PSC 1234", "id": None, "name": "PSC DESCRIPTION UP" }, ], } assert expected_response == spending_by_category_logic
def test_child_recipient_failures(client, mock_matviews_qs): """ Testing failed child recipient calls """ child1_id = "00077a9a-5a70-8919-fd19-330762af6b84-C" child1_hash = child1_id[:-2] parent_child1_name = "PARENT RECIPIENT" parent_child1_duns = "000000001" child2_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C" child2_hash = child2_id[:-2] child2_name = "CHILD RECIPIENT" child2_duns = "000000002" other_id = "00002940-fdbe-3fc5-9252-d46c0ae8758c-R" transaction_hash_map = { "latest": { "hash": child1_hash, "duns": parent_child1_duns, "name": parent_child1_name }, "FY2008": { "hash": child2_hash, "duns": child2_duns, "name": parent_child1_name }, "FY2016": { "hash": other_id, "duns": None, "name": child2_name }, } # Mock Recipient Profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # Mock Recipient Lookups for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items(): mommy.make(RecipientLookup, **recipient_lookup) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction["recipient_hash"] = transaction_hash_map[category]["hash"] transaction["recipient_unique_id"] = transaction_hash_map[category][ "duns"] transaction["recipient_name"] = transaction_hash_map[category]["name"] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # Testing for non-existent DUNS non_existent_duns = "000000003" resp = client.get(recipient_children_endpoint(non_existent_duns, "all")) assert resp.status_code == status.HTTP_400_BAD_REQUEST assert resp.data["detail"] == "DUNS not found: '{}'.".format( non_existent_duns)
def test_category_funding_agency_awards(mock_matviews_qs, mock_agencies): mock_toptier = MockModel(toptier_agency_id=1, name='Department of Calzone', abbreviation='DOC') mock_agency = MockModel(id=2, toptier_agency=mock_toptier, toptier_flag=True) mock_agency_1 = MockModel(id=3, toptier_agency=mock_toptier, toptier_flag=False) mock_model_1 = MockModel( funding_agency_id=2, funding_toptier_agency_name='Department of Calzone', funding_toptier_agency_abbreviation='DOC', generated_pragmatic_obligation=50) mock_model_2 = MockModel( funding_agency_id=3, funding_toptier_agency_name='Department of Calzone', funding_toptier_agency_abbreviation='DOC', generated_pragmatic_obligation=50) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_agencies['agency'], [mock_agency, mock_agency_1]) test_payload = { 'category': 'funding_agency', 'subawards': False, 'page': 1, 'limit': 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { 'category': 'funding_agency', 'limit': 50, 'page_metadata': { 'page': 1, 'next': None, 'previous': None, 'hasNext': False, 'hasPrevious': False }, 'results': [{ 'amount': 100, 'name': 'Department of Calzone', 'code': 'DOC', 'id': 2 }] } assert expected_response == spending_by_category_logic
def test_child_recipient_failures(client, mock_matviews_qs): """ Testing failed child recipient calls """ child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C' child1_hash = child1_id[:-2] parent_child1_name = 'PARENT RECIPIENT' parent_child1_duns = '000000001' child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C' child2_hash = child2_id[:-2] child2_name = 'CHILD RECIPIENT' child2_duns = '000000002' other_id = '00002940-fdbe-3fc5-9252-d46c0ae8758c-R' transaction_hash_map = { 'latest': { 'hash': child1_hash, 'duns': parent_child1_duns, 'name': parent_child1_name }, 'FY2008': { 'hash': child2_hash, 'duns': child2_duns, 'name': parent_child1_name }, 'FY2016': { 'hash': other_id, 'duns': None, 'name': child2_name } } # Mock Recipient Profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # Mock Recipient Lookups for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items(): mommy.make(RecipientLookup, **recipient_lookup) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = transaction_hash_map[category]['hash'] transaction['recipient_unique_id'] = transaction_hash_map[category][ 'duns'] transaction['recipient_name'] = transaction_hash_map[category]['name'] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # Testing for non-existent DUNS non_existent_duns = '000000003' resp = client.get(recipient_children_endpoint(non_existent_duns, 'all')) assert resp.status_code == status.HTTP_400_BAD_REQUEST assert resp.data['detail'] == 'DUNS not found: \'{}\'.'.format( non_existent_duns)
def populate_models(mock_matviews_qs): mock_0 = MockModel(action_date=datetime(2010, 3, 1), generated_pragmatic_obligation=100.0) mock_1 = MockModel(action_date=datetime(2011, 3, 1), generated_pragmatic_obligation=110.0) mock_2 = MockModel(action_date=datetime(2012, 3, 1), generated_pragmatic_obligation=120.0) mock_3 = MockModel(action_date=datetime(2013, 3, 1), generated_pragmatic_obligation=130.0) mock_4 = MockModel(action_date=datetime(2014, 3, 1), generated_pragmatic_obligation=140.0) mock_5 = MockModel(action_date=datetime(2015, 3, 1), generated_pragmatic_obligation=150.0) mock_6 = MockModel(action_date=datetime(2016, 3, 1), generated_pragmatic_obligation=160.0) mock_7 = MockModel(action_date=datetime(2017, 3, 1), generated_pragmatic_obligation=170.0) add_to_mock_objects(mock_matviews_qs, [mock_0, mock_1, mock_2, mock_3, mock_4, mock_5, mock_6, mock_7])
def test_category_naics_awards(mock_matviews_qs): mock_model_1 = MockModel(naics_code='NAICS 1234', naics_description='NAICS DESC 1234', generated_pragmatic_obligation=1) mock_model_2 = MockModel(naics_code='NAICS 1234', naics_description='NAICS DESC 1234', generated_pragmatic_obligation=1) mock_model_3 = MockModel(naics_code='NAICS 9876', naics_description='NAICS DESC 9876', generated_pragmatic_obligation=2) mock_model_4 = MockModel(naics_code='NAICS 9876', naics_description='NAICS DESC 9876', generated_pragmatic_obligation=2) add_to_mock_objects( mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) test_payload = { 'category': 'naics', 'subawards': False, 'page': 1, 'limit': 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { 'category': 'naics', 'limit': 50, 'page_metadata': { 'page': 1, 'next': None, 'previous': None, 'hasNext': False, 'hasPrevious': False }, 'results': [{ 'amount': 4, 'code': 'NAICS 9876', 'name': 'NAICS DESC 9876', 'id': None }, { 'amount': 2, 'code': 'NAICS 1234', 'name': 'NAICS DESC 1234', 'id': None }] } assert expected_response == spending_by_category_logic
def test_subaward_query_2(client, refresh_matviews, mock_matviews_qs): mock_model_4 = MockModel(**subaward_12) add_to_mock_objects(mock_matviews_qs, [mock_model_4]) resp = client.post('/api/v2/subawards/', content_type='application/json', data=json.dumps({ "order": "desc", "limit": 100, "award_id": 88, })) assert json.loads(resp.content.decode("utf-8"))['results'][0]['id'] == 12
def test_category_federal_accounts(mock_matviews_qs): mock_model_1 = MockModel( federal_account_id=10, federal_account_display="020-0001", account_title="Test Federal Account", recipient_hash="00000-00000-00000-00000-00000", parent_recipient_unique_id="000000", generated_pragmatic_obligation=1, ) mock_model_2 = MockModel( federal_account_id=10, federal_account_display="020-0001", account_title="Test Federal Account", recipient_hash="00000-00000-00000-00000-00000", parent_recipient_unique_id="000000", generated_pragmatic_obligation=2, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = { "category": "federal_account", "filters": { "recipient_id": "00000-00000-00000-00000-00000-C" }, "subawards": False, "page": 1, "limit": 50, } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "federal_account", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [{ "amount": 3, "code": "020-0001", "name": "Test Federal Account", "id": 10 }], } assert expected_response == spending_by_category_logic
def test_category_psc_awards(mock_matviews_qs, mock_psc): mock_psc_1 = MockModel(code='PSC 1234', description='PSC DESCRIPTION UP') mock_psc_2 = MockModel(code='PSC 9876', description='PSC DESCRIPTION DOWN') mock_model_1 = MockModel(product_or_service_code='PSC 1234', generated_pragmatic_obligation=1) mock_model_2 = MockModel(product_or_service_code='PSC 1234', generated_pragmatic_obligation=1) mock_model_3 = MockModel(product_or_service_code='PSC 9876', generated_pragmatic_obligation=2) mock_model_4 = MockModel(product_or_service_code='PSC 9876', generated_pragmatic_obligation=2) add_to_mock_objects( mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4]) add_to_mock_objects(mock_psc, [mock_psc_1, mock_psc_2]) test_payload = { 'category': 'psc', 'subawards': False, 'page': 1, 'limit': 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { 'category': 'psc', 'limit': 50, 'page_metadata': { 'page': 1, 'next': None, 'previous': None, 'hasNext': False, 'hasPrevious': False }, 'results': [{ 'amount': 4, 'code': 'PSC 9876', 'id': None, 'name': 'PSC DESCRIPTION DOWN', }, { 'amount': 2, 'code': 'PSC 1234', 'id': None, 'name': 'PSC DESCRIPTION UP' }] } assert expected_response == spending_by_category_logic
def test_child_recipient_failures(client, mock_matviews_qs): """ Testing failed child recipient calls """ child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C' child1_hash = child1_id[:-2] parent_child1_name = 'PARENT RECIPIENT' parent_child1_duns = '000000001' child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C' child2_hash = child2_id[:-2] child2_name = 'CHILD RECIPIENT' child2_duns = '000000002' other_id = '00002940-fdbe-3fc5-9252-d46c0ae8758c-R' transaction_hash_map = { 'latest': { 'hash': child1_hash, 'duns': parent_child1_duns, 'name': parent_child1_name }, 'FY2008': { 'hash': child2_hash, 'duns': child2_duns, 'name': parent_child1_name }, 'FY2016': { 'hash': other_id, 'duns': None, 'name': child2_name } } # Mock Recipient Profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # Mock Recipient Lookups for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items(): mommy.make(RecipientLookup, **recipient_lookup) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = transaction_hash_map[category]['hash'] transaction['recipient_unique_id'] = transaction_hash_map[category]['duns'] transaction['recipient_name'] = transaction_hash_map[category]['name'] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # Testing for non-existent DUNS non_existent_duns = '000000003' resp = client.get(recipient_children_endpoint(non_existent_duns, 'all')) assert resp.status_code == status.HTTP_400_BAD_REQUEST assert resp.data['detail'] == 'DUNS not found: \'{}\'.'.format(non_existent_duns)
def test_subaward_query_3(client, refresh_matviews, mock_matviews_qs): mock_model_4 = MockModel(**subaward_12) add_to_mock_objects(mock_matviews_qs, [mock_model_4]) resp = client.post( "/api/v2/subawards/", content_type="application/json", data=json.dumps({ "order": "desc", "limit": 100, "award_id": "generated_unique_award_id_for_88" }), ) assert json.loads(resp.content.decode("utf-8"))["results"][0]["id"] == 12
def add_award_recipients(monkeypatch): mock_awards_recipients = MockSet() monkeypatch.setattr('usaspending_api.recipient.models.SummaryAwardRecipient.objects', mock_awards_recipients) mock_model_list = [] current_id = 1 new_award_count = 12 for i in range(current_id, current_id + new_award_count): mock_model_list.append(MockModel( award_id=i, recipient_hash='21a1b0df-e7cd-349b-b948-60ed0ac1e6a0', parent_recipient_unique_id=None, action_date=datetime(2009, 5, 30), )) current_id += new_award_count new_award_count = 3 for i in range(current_id, current_id + new_award_count): mock_model_list.append(MockModel( award_id=i, recipient_hash='21a1b0df-e7cd-349b-b948-60ed0ac1e6a0', parent_recipient_unique_id=None, action_date=datetime(2009, 5, 1), )) current_id += new_award_count new_award_count = 1 for i in range(current_id, current_id + new_award_count): mock_model_list.append(MockModel( award_id=i, recipient_hash='21a1b0df-e7cd-349b-b948-60ed0ac1e6a0', parent_recipient_unique_id=None, action_date=datetime(2009, 7, 2), )) current_id += new_award_count new_award_count = 2 for i in range(current_id, current_id + new_award_count): mock_model_list.append(MockModel( award_id=i, recipient_hash='21a1b0df-e7cd-349b-b948-60ed0ac1e6a0', parent_recipient_unique_id=None, action_date=datetime(2008, 1, 10), )) current_id += new_award_count new_award_count = 6 for i in range(current_id, current_id + new_award_count): mock_model_list.append(MockModel( award_id=i, recipient_hash='4e418651-4b83-8722-ab4e-e68d80bfb3b3', parent_recipient_unique_id=None, action_date=datetime(2009, 7, 30), )) add_to_mock_objects(mock_awards_recipients, mock_model_list)
def test_spending_by_award_no_intersection(client, mock_matviews_qs): mock_model_1 = MockModel( award_ts_vector="", type="02", type_of_contract_pricing="", naics_code="98374", cfda_number="987.0", pulled_from=None, uri=None, piid="djsd", fain=None, category="grant", award_id=90, awarding_toptier_agency_name="Department of Pizza", awarding_toptier_agency_abbreviation="DOP", generated_pragmatic_obligation=10, counts=3, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1]) request = { "subawards": False, "fields": ["Award ID"], "sort": "Award ID", "filters": { "award_type_codes": ["02", "03", "04", "05"] }, } resp = client.post("/api/v2/search/spending_by_award_count", content_type="application/json", data=json.dumps(request)) assert resp.status_code == status.HTTP_200_OK assert resp.data["results"]["grants"] == 3 request["filters"]["award_type_codes"].append("no intersection") resp = client.post("/api/v2/search/spending_by_award_count", content_type="application/json", data=json.dumps(request)) assert resp.status_code == status.HTTP_200_OK assert resp.data["results"] == { "contracts": 0, "idvs": 0, "grants": 0, "direct_payments": 0, "loans": 0, "other": 0, }, "Results returned, there should all be 0"
def test_subaward_query_1(client, refresh_matviews, mock_matviews_qs): mock_model_1 = MockModel(**subaward_1) mock_model_2 = MockModel(**subaward_2) mock_model_3 = MockModel(**subaward_3) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) resp = client.post('/api/v2/subawards/', content_type='application/json', data=json.dumps({ "order": "desc", "limit": 100, "award_id": 99, })) assert len(json.loads(resp.content.decode("utf-8"))['results']) == 3
def test_subaward_query_2(client, refresh_matviews, mock_matviews_qs): mock_model_4 = MockModel(**subaward_12) add_to_mock_objects(mock_matviews_qs, [mock_model_4]) resp = client.post( '/api/v2/subawards/', content_type='application/json', data=json.dumps( { "order": "desc", "limit": 100, "award_id": 88, }) ) assert json.loads(resp.content.decode("utf-8"))['results'][0]['id'] == 12
def test_specific_award(mock_matviews_qs): mock_model_1 = MockModel(**subaward_10) mock_model_2 = MockModel(**subaward_11) mock_model_3 = MockModel(**subaward_12) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) test_payload = {"award_id": 99} svs = SubawardsViewSet() test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) expected_response = [strip_award_id(subaward_11), strip_award_id(subaward_10)] assert expected_response == subawards_logic
def add_award_recipients(mock_matviews_qs, refresh_matviews): mock_awards_1_0 = MockModel( recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, counts=12, date_signed=datetime(2009, 5, 30), action_date="2009-05-30", ) mock_awards_1_1 = MockModel( recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, counts=3, date_signed=datetime(2009, 5, 1), action_date="2009-05-01", ) mock_awards_1_2 = MockModel( recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, counts=1, date_signed=datetime(2009, 7, 2), action_date="2009-07-02", ) mock_awards_1_3 = MockModel( recipient_hash="21a1b0df-e7cd-349b-b948-60ed0ac1e6a0", parent_recipient_unique_id=None, counts=2, date_signed=datetime(2008, 1, 10), action_date="2008-01-10", ) mock_awards_2_0 = MockModel( recipient_hash="4e418651-4b83-8722-ab4e-e68d80bfb3b3", parent_recipient_unique_id=None, counts=6, date_signed=datetime(2009, 7, 30), action_date="2009-05-30", ) add_to_mock_objects( mock_matviews_qs, [ mock_awards_1_0, mock_awards_1_1, mock_awards_1_2, mock_awards_1_3, mock_awards_2_0, ], )
def test_subaward_query_1(client, refresh_matviews, mock_matviews_qs): mock_model_1 = MockModel(**subaward_1) mock_model_2 = MockModel(**subaward_2) mock_model_3 = MockModel(**subaward_3) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) resp = client.post( '/api/v2/subawards/', content_type='application/json', data=json.dumps( { "order": "desc", "limit": 100, "award_id": 99, }) ) assert len(json.loads(resp.content.decode("utf-8"))['results']) == 3
def test_obtain_recipient_totals_parent(mock_matviews_qs, ): """ Testing recipient totals with parent child relationships """ # Testing with specific parent/child ids parent_id = '00077a9a-5a70-8919-fd19-330762af6b84-P' child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C' child1_hash = child1_id[:-2] parent_child1_duns = '000000001' child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C' child2_hash = child2_id[:-2] other_id = '00002940-fdbe-3fc5-9252-d46c0ae8758c-R' transaction_hash_map = { 'latest': { 'hash': child1_hash, 'parent_duns': parent_child1_duns }, 'FY2016': { 'hash': child2_hash, 'parent_duns': parent_child1_duns, }, 'FY2008': { 'hash': other_id, 'parent_duns': None } } # load recipient profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = transaction_hash_map[category]['hash'] transaction['parent_recipient_unique_id'] = transaction_hash_map[ category]['parent_duns'] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) expected_total = 150 expected_count = 2 results = recipients.obtain_recipient_totals(parent_id, year='all', subawards=False) assert results[0]['total'] == expected_total assert results[0]['count'] == expected_count
def test_obtain_recipient_totals_year(mock_matviews_qs): """ Testing recipient totals with different year values """ # Testing with specific child recipient_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C" recipient_hash = recipient_id[:-2] # load all of the transactions mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction["recipient_hash"] = recipient_hash transaction["parent_recipient_unique_id"] = "000000009" mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # For latest transaction's we're pulling from recipient_profile associated_recipient_profile = TEST_RECIPIENT_PROFILES[recipient_id].copy() associated_recipient_profile["last_12_months"] = 100 associated_recipient_profile["last_12_months_count"] = 1 mommy.make(RecipientProfile, **associated_recipient_profile) # Latest expected_total = 100 expected_count = 1 results = recipients.obtain_recipient_totals(recipient_id, year="latest", subawards=False) assert results[0]["total"] == expected_total assert results[0]["count"] == expected_count # All expected_total = 350 expected_count = 3 results = recipients.obtain_recipient_totals(recipient_id, year="all", subawards=False) assert results[0]["total"] == expected_total assert results[0]["count"] == expected_count # FY2016 expected_total = 50 expected_count = 1 results = recipients.obtain_recipient_totals(recipient_id, year="2016", subawards=False) assert results[0]["total"] == expected_total assert results[0]["count"] == expected_count
def test_obtain_recipient_totals_parent(mock_matviews_qs, ): """ Testing recipient totals with parent child relationships """ # Testing with specific parent/child ids parent_id = "00077a9a-5a70-8919-fd19-330762af6b84-P" child1_id = "00077a9a-5a70-8919-fd19-330762af6b84-C" child1_hash = child1_id[:-2] parent_child1_duns = "000000001" child2_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C" child2_hash = child2_id[:-2] other_id = "00002940-fdbe-3fc5-9252-d46c0ae8758c-R" transaction_hash_map = { "latest": { "hash": child1_hash, "parent_duns": parent_child1_duns }, "FY2016": { "hash": child2_hash, "parent_duns": parent_child1_duns }, "FY2008": { "hash": other_id, "parent_duns": None }, } # load recipient profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction["recipient_hash"] = transaction_hash_map[category]["hash"] transaction["parent_recipient_unique_id"] = transaction_hash_map[ category]["parent_duns"] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) expected_total = 150 expected_count = 2 results = recipients.obtain_recipient_totals(parent_id, year="all", subawards=False) assert results[0]["total"] == expected_total assert results[0]["count"] == expected_count
def test_category_county_awards(mock_matviews_qs): mock_model_1 = MockModel(pop_county_code="04", pop_county_name="COUNTYSVILLE", generated_pragmatic_obligation=1) mock_model_2 = MockModel(pop_county_code="04", pop_county_name="COUNTYSVILLE", generated_pragmatic_obligation=1) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = {"category": "county", "subawards": False, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "county", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 2, "code": "04", "name": "COUNTYSVILLE", "id": None}], } assert expected_response == spending_by_category_logic
def test_category_district_subawards(mock_matviews_qs): mock_model_1 = MockModel(pop_congressional_code="06", pop_state_code="XY", amount=1) mock_model_2 = MockModel(pop_congressional_code="06", pop_state_code="XY", amount=1) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = {"category": "district", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "district", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 2, "code": "06", "name": "XY-06", "id": None}], } assert expected_response == spending_by_category_logic
def test_category_country_subawards(mock_matviews_qs): mock_model_1 = MockModel(pop_country_code="US", amount=1) mock_model_2 = MockModel(pop_country_code="US", amount=1) mommy.make("references.RefCountryCode", country_name="UNITED STATES", country_code="US") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = {"category": "country", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "country", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 2, "code": "US", "name": "UNITED STATES", "id": None}], } assert expected_response == spending_by_category_logic
def test_specific_award(mock_matviews_qs): mock_model_1 = MockModel(**subaward_10) mock_model_2 = MockModel(**subaward_11) mock_model_3 = MockModel(**subaward_12) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3]) test_payload = { "award_id": 99 } svs = SubawardsViewSet() test_params = svs._parse_and_validate_request(test_payload) subawards_logic = svs._business_logic(test_params) expected_response = [strip_award_id(subaward_11), strip_award_id(subaward_10)] assert expected_response == subawards_logic
def test_category_recipient_duns_awards(mock_matviews_qs): # recipient_hash = SELECT MD5(UPPER(CONCAT('<duns>','<recipient_name>')))::uuid; mock_model_1 = MockModel(recipient_hash="59f9a646-cd1c-cbdc-63dd-1020fac59336", generated_pragmatic_obligation=1) mock_model_2 = MockModel(recipient_hash="59f9a646-cd1c-cbdc-63dd-1020fac59336", generated_pragmatic_obligation=1) mock_model_3 = MockModel(recipient_hash="3725ba78-a607-7ab4-1cf6-2a08207bac3c", generated_pragmatic_obligation=1) mock_model_4 = MockModel(recipient_hash="3725ba78-a607-7ab4-1cf6-2a08207bac3c", generated_pragmatic_obligation=10) mock_model_5 = MockModel(recipient_hash="18569a71-3b0a-1586-50a9-cbb8bb070136", generated_pragmatic_obligation=15) mommy.make( "recipient.RecipientLookup", recipient_hash="3725ba78-a607-7ab4-1cf6-2a08207bac3c", legal_business_name="John Doe", duns="1234JD4321", ) mommy.make( "recipient.RecipientLookup", recipient_hash="59f9a646-cd1c-cbdc-63dd-1020fac59336", legal_business_name="University of Pawnee", duns="00UOP00", ) mommy.make( "recipient.RecipientLookup", recipient_hash="18569a71-3b0a-1586-50a9-cbb8bb070136", legal_business_name="MULTIPLE RECIPIENTS", duns=None, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2, mock_model_3, mock_model_4, mock_model_5]) test_payload = {"category": "recipient_duns", "subawards": False, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "recipient_duns", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [ {"amount": 15, "name": "MULTIPLE RECIPIENTS", "code": None, "id": None}, {"amount": 11, "name": "John Doe", "code": "1234JD4321", "id": None}, {"amount": 2, "name": "University of Pawnee", "code": "00UOP00", "id": None}, ], } assert expected_response == spending_by_category_logic
def test_category_state_territory_subawards(mock_matviews_qs): mock_model_1 = MockModel(pop_state_code="XY", amount=1) mock_model_2 = MockModel(pop_state_code="XY", amount=1) mommy.make("recipient.StateData", name="Test State", code="XY") add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = {"category": "state_territory", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "state_territory", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 2, "code": "XY", "name": "Test State", "id": None}], } assert expected_response == spending_by_category_logic
def test_category_cfda_awards(mock_matviews_qs, mock_cfda): mock_model_cfda = MockModel(program_title="CFDA TITLE 1234", program_number="CFDA1234", id=1) mock_model_1 = MockModel(cfda_number="CFDA1234", generated_pragmatic_obligation=1) mock_model_2 = MockModel(cfda_number="CFDA1234", generated_pragmatic_obligation=1) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_cfda, [mock_model_cfda]) test_payload = { "category": "cfda", "subawards": False, "page": 1, "limit": 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "cfda", "limit": 50, "page_metadata": { "page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False }, "results": [{ "amount": 2, "code": "CFDA1234", "name": "CFDA TITLE 1234", "id": 1 }], } assert expected_response == spending_by_category_logic
def test_category_country(mock_matviews_qs): mock_model_1 = MockModel(pop_country_code='US', generated_pragmatic_obligation=1) mock_model_2 = MockModel(pop_country_code='US', generated_pragmatic_obligation=1) mommy.make( 'references.RefCountryCode', country_name='UNITED STATES', country_code='US', ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = { 'category': 'country', 'subawards': False, 'page': 1, 'limit': 50 } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { 'category': 'country', 'limit': 50, 'page_metadata': { 'page': 1, 'next': None, 'previous': None, 'hasNext': False, 'hasPrevious': False }, 'results': [{ 'amount': 2, 'code': 'US', 'name': 'UNITED STATES', 'id': None }] } assert expected_response == spending_by_category_logic
def test_obtain_recipient_totals_parent(mock_matviews_qs, ): """ Testing recipient totals with parent child relationships """ # Testing with specific parent/child ids parent_id = '00077a9a-5a70-8919-fd19-330762af6b84-P' child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C' child1_hash = child1_id[:-2] parent_child1_duns = '000000001' child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C' child2_hash = child2_id[:-2] other_id = '00002940-fdbe-3fc5-9252-d46c0ae8758c-R' transaction_hash_map = { 'latest': { 'hash': child1_hash, 'parent_duns': parent_child1_duns }, 'FY2016': { 'hash': child2_hash, 'parent_duns': parent_child1_duns, }, 'FY2008': { 'hash': other_id, 'parent_duns': None } } # load recipient profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = transaction_hash_map[category]['hash'] transaction['parent_recipient_unique_id'] = transaction_hash_map[category]['parent_duns'] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) expected_total = 150 expected_count = 2 results = recipients.obtain_recipient_totals(parent_id, year='all', subawards=False) assert results[0]['total'] == expected_total assert results[0]['count'] == expected_count
def test_category_cfda_subawards(mock_matviews_qs, mock_cfda): mock_model_cfda = MockModel(program_title="CFDA TITLE 1234", program_number="CFDA1234", id=1) mock_model_1 = MockModel(cfda_number="CFDA1234", amount=1) mock_model_2 = MockModel(cfda_number="CFDA1234", amount=1) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) add_to_mock_objects(mock_cfda, [mock_model_cfda]) test_payload = {"category": "cfda", "subawards": True, "page": 1, "limit": 50} spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "cfda", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 2, "code": "CFDA1234", "name": "CFDA TITLE 1234", "id": 1}], } assert expected_response == spending_by_category_logic
def test_spending_by_award_foreign_filter(client, mock_matviews_qs): """ Verify that foreign country filter is returning the correct results """ mock_model_0 = MockModel(award_id=0, piid=None, fain='aaa', uri=None, type='B', pulled_from="AWARD", recipient_location_country_name="UNITED STATES", recipient_location_country_code="USA") mock_model_1 = MockModel(award_id=1, piid=None, fain='abc', uri=None, type='B', pulled_from="AWARD", recipient_location_country_name="", recipient_location_country_code="USA") mock_model_2 = MockModel(award_id=2, piid=None, fain='abd', uri=None, type='B', pulled_from="AWARD", recipient_location_country_name="UNITED STATES", recipient_location_country_code="") mock_model_3 = MockModel(award_id=3, piid=None, fain='abe', uri=None, type='B', pulled_from="AWARD", recipient_location_country_name="Gibraltar", recipient_location_country_code="GIB") add_to_mock_objects(mock_matviews_qs, [mock_model_0, mock_model_1, mock_model_2, mock_model_3]) # add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_3]) resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "filters": { "award_type_codes": ["A", "B", "C", "D"], # "recipient_locations": [{"country": "USA"}] "recipient_scope": "domestic" }, "fields": ["Award ID"] })) # Three results are returned when searching for "USA"-based recipients # e.g. "USA"; "UNITED STATES"; "USA" and "UNITED STATES"; assert len(resp.data['results']) == 3 resp = client.post( '/api/v2/search/spending_by_award/', content_type='application/json', data=json.dumps({ "filters": { "award_type_codes": ["A", "B", "C", "D"], "recipient_scope": "foreign" }, "fields": ["Award ID"], })) # One result is returned when searching for "Foreign" recipients assert len(resp.data['results']) == 1
def test_obtain_recipient_totals_year(mock_matviews_qs): """ Testing recipient totals with different year values """ # Testing with specific child recipient_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C' recipient_hash = recipient_id[:-2] # load all of the transactions mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = recipient_hash transaction['parent_recipient_unique_id'] = '000000009' mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # For latest transaction's we're pulling from recipient_profile associated_recipient_profile = TEST_RECIPIENT_PROFILES[recipient_id].copy() associated_recipient_profile['last_12_months'] = 100 associated_recipient_profile['last_12_months_count'] = 1 mommy.make(RecipientProfile, **associated_recipient_profile) # Latest expected_total = 100 expected_count = 1 results = recipients.obtain_recipient_totals(recipient_id, year='latest', subawards=False) assert results[0]['total'] == expected_total assert results[0]['count'] == expected_count # All expected_total = 350 expected_count = 3 results = recipients.obtain_recipient_totals(recipient_id, year='all', subawards=False) assert results[0]['total'] == expected_total assert results[0]['count'] == expected_count # FY2016 expected_total = 50 expected_count = 1 results = recipients.obtain_recipient_totals(recipient_id, year='2016', subawards=False) assert results[0]['total'] == expected_total assert results[0]['count'] == expected_count
def test_category_federal_accounts(mock_matviews_qs): mock_model_1 = MockModel( federal_account_id=10, federal_account_display="020-0001", account_title="Test Federal Account", recipient_hash="00000-00000-00000-00000-00000", parent_recipient_unique_id="000000", generated_pragmatic_obligation=1, ) mock_model_2 = MockModel( federal_account_id=10, federal_account_display="020-0001", account_title="Test Federal Account", recipient_hash="00000-00000-00000-00000-00000", parent_recipient_unique_id="000000", generated_pragmatic_obligation=2, ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = { "category": "federal_account", "filters": {"recipient_id": "00000-00000-00000-00000-00000-C"}, "subawards": False, "page": 1, "limit": 50, } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "federal_account", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 3, "code": "020-0001", "name": "Test Federal Account", "id": 10}], } assert expected_response == spending_by_category_logic
def test_category_federal_accounts_subawards( mock_matviews_qs, mock_federal_account, mock_tas, mock_award, mock_financial_account, mock_transaction ): fa = MockModel(id=10, agency_identifier="020", main_account_code="0001", account_title="Test Federal Account") add_to_mock_objects(mock_federal_account, [fa]) tas = MockModel(treasury_account_identifier=2, federal_account_id=10) add_to_mock_objects(mock_tas, [tas]) award = MockModel(id=3) add_to_mock_objects(mock_award, [award]) fs = MockModel(financial_accounts_by_awards_id=4, submission_id=3, treasury_account=tas, award=award) add_to_mock_objects(mock_financial_account, [fs]) award.financial_set = MockSet(fs) t1 = MockModel(award=award, id=5) t2 = MockModel(award=award, id=6) add_to_mock_objects(mock_transaction, [t1, t2]) mock_model_1 = MockModel( transaction=t1, recipient_hash="00000-00000-00000-00000-00000", parent_recipient_unique_id="000000", amount=1 ) mock_model_2 = MockModel( transaction=t2, recipient_hash="00000-00000-00000-00000-00000", parent_recipient_unique_id="000000", amount=1 ) add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2]) test_payload = { "category": "federal_account", "filters": {"recipient_id": "00000-00000-00000-00000-00000-C"}, "subawards": True, "page": 1, "limit": 50, } spending_by_category_logic = BusinessLogic(test_payload).results() expected_response = { "category": "federal_account", "limit": 50, "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False}, "results": [{"amount": 2, "code": "020-0001", "name": "Test Federal Account", "id": 10}], } assert expected_response == spending_by_category_logic
def test_child_recipient_success(client, mock_matviews_qs): """ Testing successfull child recipient calls """ child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C' child1_hash = child1_id[:-2] parent_child1_name = 'PARENT RECIPIENT' parent_child1_duns = '000000001' child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C' child2_hash = child2_id[:-2] child2_name = 'CHILD RECIPIENT' child2_duns = '000000002' transaction_hash_map = { 'latest': { 'hash': child1_hash, 'duns': parent_child1_duns, 'name': parent_child1_name, 'parent_duns': parent_child1_duns }, 'FY2016': { 'hash': child2_hash, 'duns': child2_duns, 'name': child2_name, 'parent_duns': parent_child1_duns }, # Making sure the children total only applies to transactions where it listed the parent # Not all transactions of that child in general 'FY2008': { 'hash': child2_hash, 'duns': child2_duns, 'name': child2_name, 'parent_duns': '000000009' } } # Mock Recipient Profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): mommy.make(RecipientProfile, **recipient_profile) # Mock Recipient Lookups for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items(): mommy.make(RecipientLookup, **recipient_lookup) # load transactions for each child and parent (making sure it's excluded) mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = transaction_hash_map[category]['hash'] transaction['recipient_unique_id'] = transaction_hash_map[category]['duns'] transaction['recipient_name'] = transaction_hash_map[category]['name'] transaction['parent_recipient_unique_id'] = transaction_hash_map[category]['parent_duns'] mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # Ignoring nonexistent child duns - 000000005 child1_object = { 'recipient_id': child1_id, 'name': 'PARENT RECIPIENT', 'duns': parent_child1_duns, 'amount': 100, 'state_province': 'PARENT STATE' } child2_object = { 'recipient_id': child2_id, 'name': 'CHILD RECIPIENT', 'duns': child2_duns, 'amount': 50, 'state_province': 'CHILD STATE' } expected = [child1_object, child2_object] resp = client.get(recipient_children_endpoint(parent_child1_duns, 'all')) assert resp.status_code == status.HTTP_200_OK # testing for equality-only, order unnecessary assert sorted(resp.data, key=lambda key: key['recipient_id']) == expected
def test_recipient_overview(client, mock_matviews_qs): """ Testing a simple example of the endpoint as a whole """ r_id = '00077a9a-5a70-8919-fd19-330762af6b84-C' recipient_hash = r_id[:-2] # Mock Transactions mock_transactions = [] for category, transaction in TEST_SUMMARY_TRANSACTIONS.items(): transaction['recipient_hash'] = recipient_hash mock_transactions.append(MockModel(**transaction)) add_to_mock_objects(mock_matviews_qs, mock_transactions) # Mock Recipient Profiles for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items(): recipient_profile_copy = recipient_profile.copy() if recipient_id == r_id: recipient_profile_copy['last_12_months'] = 100 recipient_profile_copy['last_12_months_count'] = 1 mommy.make(RecipientProfile, **recipient_profile_copy) # Mock Recipient Lookups for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items(): mommy.make(RecipientLookup, **recipient_lookup) # Mock DUNS - should add `category-business` for duns, duns_dict in TEST_DUNS.items(): test_duns_model = duns_dict.copy() country_code = test_duns_model['country_code'] mommy.make(DUNS, **test_duns_model) mommy.make(RefCountryCode, **TEST_REF_COUNTRY_CODE[country_code]) # Mock Legal Entity expected_business_cat = ['expected', 'business', 'cat'] mommy.make(LegalEntity, business_categories=expected_business_cat, recipient_name='PARENT RECIPIENT', recipient_unique_id='000000001') resp = client.get(recipient_overview_endpoint(r_id)) assert resp.status_code == status.HTTP_200_OK expected = { 'name': 'PARENT RECIPIENT', 'duns': '000000001', 'recipient_id': '00077a9a-5a70-8919-fd19-330762af6b84-C', 'recipient_level': 'C', 'parent_name': 'PARENT RECIPIENT', 'parent_duns': '000000001', 'parent_id': '00077a9a-5a70-8919-fd19-330762af6b84-P', 'parents': [{ 'parent_duns': '000000001', 'parent_id': '00077a9a-5a70-8919-fd19-330762af6b84-P', 'parent_name': 'PARENT RECIPIENT', }], 'business_types': sorted(['expected', 'business', 'cat'] + ['category_business']), 'location': { 'address_line1': 'PARENT ADDRESS LINE 1', 'address_line2': 'PARENT ADDRESS LINE 2', 'address_line3': None, 'county_name': None, 'city_name': 'PARENT CITY', 'congressional_code': 'PARENT CONGRESSIONAL DISTRICT', 'country_code': 'PARENT COUNTRY CODE', 'country_name': 'PARENT COUNTRY NAME', 'state_code': 'PARENT STATE', 'zip': 'PARENT ZIP', 'zip4': 'PARENT ZIP4', 'foreign_province': None, 'foreign_postal_code': None }, 'total_transaction_amount': 100, 'total_transactions': 1 } # testing for equality-only, order unnecessary resp.data['business_types'] = sorted(resp.data['business_types']) assert resp.data == expected