Example #1
0
def test_correct_response(client, monkeypatch, elasticsearch_transaction_index, awards_and_transactions):

    logging_statements = []
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index, logging_statements)

    resp = client.post(
        "/api/v2/search/spending_by_category/district",
        content_type="application/json",
        data=json.dumps({"filters": {"time_period": [{"start_date": "2018-10-01", "end_date": "2020-09-30"}]}}),
        **{EXPERIMENTAL_API_HEADER: ELASTICSEARCH_HEADER_VALUE},
    )
    expected_response = {
        "category": "district",
        "limit": 10,
        "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False},
        "results": [
            {"amount": 500000.0, "code": "90", "id": None, "name": "SC-MULTIPLE DISTRICTS"},
            {"amount": 50005.0, "code": "10", "id": None, "name": "SC-10"},
            {"amount": 5500.0, "code": "50", "id": None, "name": "WA-50"},
            {"amount": 50.0, "code": "50", "id": None, "name": "SC-50"},
        ],
        "messages": [get_time_period_message()],
    }
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
    assert len(logging_statements) == 1, "Expected one logging statement"
    assert resp.json() == expected_response
def _test_correct_response_for_recipient_location_state_with_geo_filters(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps(
            {
                "scope": "recipient_location",
                "geo_layer": "state",
                "geo_layer_filters": ["WA"],
                "filters": {"time_period": [{"start_date": "2018-10-01", "end_date": "2020-09-30"}]},
            }
        ),
    )
    expected_response = {
        "scope": "recipient_location",
        "geo_layer": "state",
        "results": [
            {
                "aggregated_amount": 55000.0,
                "display_name": "Washington",
                "per_capita": 5.5,
                "population": 10000,
                "shape_code": "WA",
            },
        ],
        "messages": [get_time_period_message()],
    }
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"

    resp_json = resp.json()
    resp_json["results"].sort(key=_get_shape_code_for_sort)
    assert resp_json == expected_response
def test_spending_by_award_count_idvs(client, award_data_fixture):
    test_payload = {
        "subawards": False,
        "filters": {
            "award_type_codes": ["IDV_B", "IDV_B_A", "IDV_B_B", "IDV_B_C"],
            "time_period": [{
                "start_date": "2009-10-01",
                "end_date": "2018-09-30"
            }],
        },
    }

    expected_response = {
        "results": {
            "contracts": 0,
            "idvs": 3,
            "loans": 0,
            "direct_payments": 0,
            "grants": 0,
            "other": 0
        },
        "messages": [get_time_period_message()],
    }

    resp = client.post(get_spending_by_award_count_url(),
                       content_type="application/json",
                       data=json.dumps(test_payload))

    assert resp.status_code == status.HTTP_200_OK
    assert expected_response == resp.data, "Unexpected or missing content!"
Example #4
0
    def construct_es_reponse(self, response) -> dict:
        results = []
        for res in response:
            hit = res.to_dict()
            row = {
                k: hit[v]
                for k, v in self.constants["internal_id_fields"].items()
            }

            for field in self.fields:
                row[field] = hit.get(
                    self.constants["elasticsearch_type_code_to_field_map"][hit[
                        self.constants["award_semaphore"]]].get(field))

            row["internal_id"] = int(row["internal_id"])
            if row.get("Loan Value"):
                row["Loan Value"] = float(row["Loan Value"])
            if row.get("Subsidy Cost"):
                row["Subsidy Cost"] = float(row["Subsidy Cost"])
            if row.get("Award Amount"):
                row["Award Amount"] = float(row["Award Amount"])
            row["generated_internal_id"] = hit["generated_unique_award_id"]
            row["recipient_id"] = hit.get("recipient_unique_id")
            row["parent_recipient_unique_id"] = hit.get(
                "parent_recipient_unique_id")

            if "Award ID" in self.fields:
                row["Award ID"] = hit["display_award_id"]
            row = self.append_recipient_hash_level(row)
            row.pop("parent_recipient_unique_id")
            results.append(row)
        last_id = None
        last_value = None
        if len(response) > 0:
            last_id = response[len(response) -
                               1].to_dict().get("generated_unique_award_id")
            last_value = (
                response[len(response) -
                         1].to_dict().get("total_loan_value") if
                set(self.filters["award_type_codes"]) <= set(loan_type_mapping)
                else response[len(response) -
                              1].to_dict().get("total_obligation"))
        return {
            "limit": self.pagination["limit"],
            "results": results,
            "page_metadata": {
                "page":
                self.pagination["page"],
                "hasNext":
                response.hits.total -
                (self.pagination["page"] - 1) * self.pagination["limit"] >
                self.pagination["limit"],
                "last_id":
                last_id,
                "last_value":
                last_value,
            },
            "messages": [get_time_period_message()],
        }
Example #5
0
 def populate_response(self, results: list, has_next: bool) -> dict:
     return {
         "limit": self.pagination["limit"],
         "results": results,
         "page_metadata": {
             "page": self.pagination["page"],
             "hasNext": has_next
         },
         "messages": [get_time_period_message()],
     }
def test_correct_response(client, monkeypatch, elasticsearch_transaction_index,
                          awards_and_transactions):

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    resp = client.post(
        "/api/v2/search/spending_by_category/county",
        content_type="application/json",
        data=json.dumps({
            "filters": {
                "time_period": [{
                    "start_date": "2018-10-01",
                    "end_date": "2020-09-30"
                }]
            }
        }),
    )
    expected_response = {
        "category":
        "county",
        "limit":
        10,
        "page_metadata": {
            "page": 1,
            "next": None,
            "previous": None,
            "hasNext": False,
            "hasPrevious": False
        },
        "results": [
            {
                "amount": 550005.0,
                "code": "001",
                "id": None,
                "name": "CHARLESTON"
            },
            {
                "amount": 5500.0,
                "code": "005",
                "id": None,
                "name": "TEST NAME"
            },
            {
                "amount": 50.0,
                "code": "005",
                "id": None,
                "name": "TEST NAME"
            },
        ],
        "messages": [get_time_period_message()],
    }
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
    assert resp.json() == expected_response
Example #7
0
def test_new_awards_fiscal_year(client, add_award_recipients):
    test_payload = {
        "group": "fiscal_year",
        "filters": {
            "time_period": [{"start_date": "2008-10-01", "end_date": "2010-09-30"}],
            "recipient_id": "21a1b0df-e7cd-349b-b948-60ed0ac1e6a0-R",
        },
    }
    expected_response = {
        "group": "fiscal_year",
        "results": [
            {"time_period": {"fiscal_year": "2009"}, "new_award_count_in_period": 16},
            {"time_period": {"fiscal_year": "2010"}, "new_award_count_in_period": 0},
        ],
        "messages": [get_time_period_message()],
    }

    resp = client.post(get_new_awards_over_time_url(), content_type="application/json", data=json.dumps(test_payload))
    assert resp.status_code == 200
    assert resp.data["group"] == "fiscal_year"
    assert expected_response == resp.data

    test_payload["filters"]["time_period"] = [{"start_date": "2007-10-01", "end_date": "2010-09-30"}]

    expected_response = {
        "group": "fiscal_year",
        "results": [
            {"time_period": {"fiscal_year": "2008"}, "new_award_count_in_period": 2},
            {"time_period": {"fiscal_year": "2009"}, "new_award_count_in_period": 16},
            {"time_period": {"fiscal_year": "2010"}, "new_award_count_in_period": 0},
        ],
        "messages": [get_time_period_message()],
    }
    resp = client.post(get_new_awards_over_time_url(), content_type="application/json", data=json.dumps(test_payload))
    assert resp.status_code == 200
    assert resp.data["group"] == "fiscal_year"
    assert expected_response == resp.data
def test_spending_by_award_elasticsearch(client, award_data_fixture,
                                         monkeypatch,
                                         elasticsearch_award_index):
    logging_statements = []
    monkeypatch.setattr(
        "usaspending_api.search.v2.views.spending_by_award.logger.info",
        lambda message: logging_statements.append(message),
    )
    monkeypatch.setattr(
        "usaspending_api.common.elasticsearch.search_wrappers.AwardSearch._index_name",
        settings.ES_AWARDS_QUERY_ALIAS_PREFIX,
    )
    elasticsearch_award_index.update_index()
    resp = client.post(
        "/api/v2/search/spending_by_award_count/",
        content_type="application/json",
        data=json.dumps({
            "subawards": False,
            "filters": {
                "time_period": [
                    {
                        "start_date": "2009-10-01",
                        "end_date": "2017-09-30"
                    },
                    {
                        "start_date": "2017-10-01",
                        "end_date": "2018-09-30"
                    },
                ]
            },
        }),
        **{EXPERIMENTAL_API_HEADER: ELASTICSEARCH_HEADER_VALUE},
    )
    expected_response = {
        "results": {
            "contracts": 2,
            "idvs": 4,
            "loans": 1,
            "direct_payments": 0,
            "grants": 0,
            "other": 1
        },
        "messages": [get_time_period_message()],
    }
    assert resp.status_code == status.HTTP_200_OK
    assert len(logging_statements) == 1
    assert resp.status_code == status.HTTP_200_OK
    assert expected_response == resp.data, "Unexpected or missing content!"