def test_spending_by_award_type_success(client, monkeypatch,
                                        elasticsearch_award_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    # test small request
    resp = client.post(
        "/api/v2/search/spending_by_award/",
        content_type="application/json",
        data=json.dumps({
            "fields": ["Award ID", "Recipient Name"],
            "filters": {
                "award_type_codes": ["A", "B", "C"]
            }
        }),
    )
    assert resp.status_code == status.HTTP_200_OK

    # test IDV award types
    resp = client.post(
        "/api/v2/search/spending_by_award/",
        content_type="application/json",
        data=json.dumps({
            "fields": ["Award ID", "Recipient Name"],
            "filters": {
                "award_type_codes": [
                    "IDV_A", "IDV_B", "IDV_B_A", "IDV_B_B", "IDV_B_C", "IDV_C",
                    "IDV_D", "IDV_E"
                ]
            },
        }),
    )
    assert resp.status_code == status.HTTP_200_OK

    # test all features
    resp = client.post(
        "/api/v2/search/spending_by_award",
        content_type="application/json",
        data=json.dumps({
            "fields": ["Award ID", "Recipient Name"],
            "filters": non_legacy_filters()
        }),
    )
    assert resp.status_code == status.HTTP_200_OK

    # test subawards
    resp = client.post(
        "/api/v2/search/spending_by_award",
        content_type="application/json",
        data=json.dumps({
            "fields": ["Sub-Award ID"],
            "filters": non_legacy_filters(),
            "subawards": True
        }),
    )
    assert resp.status_code == status.HTTP_200_OK
Пример #2
0
def test_success_with_all_filters(client, monkeypatch,
                                  elasticsearch_award_index):
    """
    General test to make sure that all groups respond with a Status Code of 200 regardless of the filters.
    """

    elasticsearch_award_index.update_index()

    logging_statements = []
    monkeypatch.setattr(
        "usaspending_api.search.v2.views.spending_by_award.logger.info",
        lambda message: logging_statements.append(message),
    )

    logging_statements.clear()
    resp = client.post(
        "/api/v2/search/spending_by_award",
        content_type="application/json",
        data=json.dumps({
            "filters": non_legacy_filters(),
            "fields": ["Award ID"],
            "page": 1,
            "limit": 60,
            "sort": "Award ID",
            "order": "desc",
            "subawards": False,
        }),
        **{EXPERIMENTAL_API_HEADER: ELASTICSEARCH_HEADER_VALUE},
    )
    assert resp.status_code == status.HTTP_200_OK, f"Failed to return 200 Response"
    assert len(logging_statements) == 1, "Expected one logging statement"
Пример #3
0
def test_spending_by_geography_congressional_success(client):
    # test for required filters
    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps({
            "scope": "place_of_performance",
            "geo_layer": "district",
            "filters": {
                "recipient_locations": [{
                    "country": "ABC"
                }]
            },
        }),
    )
    assert resp.status_code == status.HTTP_200_OK

    # test all filters
    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps({
            "scope": "recipient_location",
            "geo_layer": "district",
            "geo_layer_filters": ["01"],
            "filters": non_legacy_filters(),
        }),
    )
    assert resp.status_code == status.HTTP_200_OK
Пример #4
0
def test_spending_by_award_count_filters(client):
    resp = client.post(
        "/api/v2/search/spending_by_award_count",
        content_type="application/json",
        data=json.dumps({"filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK
Пример #5
0
def test_spending_over_time_subawards_success(client):

    resp = client.post(
        "/api/v2/search/spending_over_time",
        content_type="application/json",
        data=json.dumps({"group": "quarter", "filters": non_legacy_filters(), "subawards": True}),
    )
    assert resp.status_code == status.HTTP_200_OK
def test_spending_by_award_count_filters(client, monkeypatch, elasticsearch_award_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = client.post(
        "/api/v2/search/spending_by_award_count",
        content_type="application/json",
        data=json.dumps({"filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK
Пример #7
0
def test_spending_over_time_subawards_failure(client):
    """Verify error on bad autocomplete request for budget function."""

    resp = client.post(
        "/api/v2/search/spending_over_time",
        content_type="application/json",
        data=json.dumps({"group": "quarter", "filters": non_legacy_filters(), "subawards": "string"}),
    )
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
Пример #8
0
def _test_success_with_all_filters_recipient_location_state(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps({
            "scope": "recipient_location",
            "geo_layer": "state",
            "filters": non_legacy_filters()
        }),
        **{EXPERIMENTAL_API_HEADER: ELASTICSEARCH_HEADER_VALUE},
    )
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
Пример #9
0
def test_naics_autocomplete_failure(client, monkeypatch, elasticsearch_transaction_index):
    """Verify error on bad autocomplete request for budget function."""
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    resp = client.post("/api/v2/search/spending_by_category/", content_type="application/json", data=json.dumps({}))
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

    resp = client.post(
        "/api/v2/search/spending_by_category",
        content_type="application/json",
        data=json.dumps({"group": "quarter", "filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
Пример #10
0
def test_spending_by_geography_incorrect_state(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography/",
        content_type="application/json",
        data=json.dumps({
            "scope": "place_of_performance",
            "geo_layer": "state",
            "geo_layer_filters": ["01"],
            "filters": non_legacy_filters(),
        }),
    )

    assert resp.data["results"][0]["display_name"] in ["Alabama", "None"]
Пример #11
0
def test_spending_by_award_success(client):

    resp = client.post(
        "/api/v2/search/spending_by_award",
        content_type="application/json",
        data=json.dumps({
            "subawards": False,
            "fields": ["Award ID"],
            "sort": "Award ID",
            "filters": non_legacy_filters()
        }),
    )
    assert resp.status_code == status.HTTP_200_OK
Пример #12
0
def test_spending_by_geography_incorrect_district(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography/",
        content_type="application/json",
        data=json.dumps({
            "scope": "place_of_performance",
            "geo_layer": "district",
            "geo_layer_filters": ["01"],
            "filters": non_legacy_filters(),
        }),
    )

    assert len(resp.data["results"]) == 0
Пример #13
0
def test_spending_by_geography_incorrect_county(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography/",
        content_type="application/json",
        data=json.dumps({
            "scope": "place_of_performance",
            "geo_layer": "county",
            "geo_layer_filters": ["01"],
            "filters": non_legacy_filters(),
        }),
    )
    # raise Exception(resp.content)
    assert resp.data["results"][0]["display_name"] == "County"
def test_success_with_all_filters(client, monkeypatch, elasticsearch_transaction_index, basic_award):
    """
    General test to make sure that all groups respond with a Status Code of 200 regardless of the filters.
    """

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    resp = client.post(
        "/api/v2/search/spending_by_category/awarding_subagency",
        content_type="application/json",
        data=json.dumps({"filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
Пример #15
0
def test_spending_by_geography_subawards_failure(client):

    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps({
            "scope": "recipient_location",
            "geo_layer": "county",
            "geo_layer_filters": ["01"],
            "filters": non_legacy_filters(),
            "subawards": "string",
        }),
    )
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
Пример #16
0
def test_success_with_all_filters(client, monkeypatch, elasticsearch_transaction_index):
    """
    General test to make sure that all groups respond with a Status Code of 200 regardless of the filters.
    """

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    for group in GROUPING_LOOKUP.keys():
        resp = client.post(
            "/api/v2/search/spending_over_time",
            content_type="application/json",
            data=json.dumps({"group": group, "filters": non_legacy_filters()}),
        )
        assert resp.status_code == status.HTTP_200_OK, f"Failed to return 200 Response for group: {group}"
def test_success_with_all_filters(client, monkeypatch, elasticsearch_transaction_index, basic_award):
    """
    General test to make sure that all groups respond with a Status Code of 200 regardless of the filters.
    """

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

    resp = client.post(
        "/api/v2/search/spending_by_category/awarding_subagency",
        content_type="application/json",
        data=json.dumps({"filters": non_legacy_filters()}),
        **{EXPERIMENTAL_API_HEADER: ELASTICSEARCH_HEADER_VALUE},
    )
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
    assert len(logging_statements) == 1, "Expected one logging statement"
Пример #18
0
def test_spending_by_category_success(client, monkeypatch, elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    # test for required functions
    resp = client.post(
        "/api/v2/search/spending_by_category",
        content_type="application/json",
        data=json.dumps({"category": "funding_agency", "filters": {"keywords": ["test", "testing"]}}),
    )
    assert resp.status_code == status.HTTP_200_OK

    resp = client.post(
        "/api/v2/search/spending_by_category",
        content_type="application/json",
        data=json.dumps({"category": "cfda", "filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK
Пример #19
0
def test_spending_over_time_success(client, monkeypatch, elasticsearch_transaction_index):

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    # test for needed filters
    resp = client.post(
        "/api/v2/search/spending_over_time",
        content_type="application/json",
        data=json.dumps({"group": "fiscal_year", "filters": {"keywords": ["test", "testing"]}}),
    )
    assert resp.status_code == status.HTTP_200_OK

    # test all filters
    resp = client.post(
        "/api/v2/search/spending_over_time",
        content_type="application/json",
        data=json.dumps({"group": "quarter", "filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK
def _test_success_with_all_filters_recipient_location_county(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps({"scope": "recipient_location", "geo_layer": "county", "filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
def _test_success_with_all_filters_place_of_performance_district(client):
    resp = client.post(
        "/api/v2/search/spending_by_geography",
        content_type="application/json",
        data=json.dumps({"scope": "place_of_performance", "geo_layer": "district", "filters": non_legacy_filters()}),
    )
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
Пример #22
0
def test_spending_by_award_subaward_success(client,
                                            spending_by_award_test_data):

    # Testing all filters
    resp = client.post(
        "/api/v2/search/spending_by_award",
        content_type="application/json",
        data=json.dumps({
            "subawards": True,
            "fields": ["Sub-Award ID"],
            "sort": "Sub-Award ID",
            "filters": non_legacy_filters()
        }),
    )
    assert resp.status_code == status.HTTP_200_OK

    # Testing contents of what is returned
    resp = client.post(
        "/api/v2/search/spending_by_award",
        content_type="application/json",
        data=json.dumps({
            "subawards":
            True,
            "fields": [
                "Sub-Award ID",
                "Sub-Awardee Name",
                "Sub-Award Date",
                "Sub-Award Amount",
                "Awarding Agency",
                "Awarding Sub Agency",
                "Prime Award ID",
                "Prime Recipient Name",
                "recipient_id",
                "prime_award_recipient_id",
            ],
            "sort":
            "Sub-Award ID",
            "filters": {
                "award_type_codes": ["A"]
            },
            "limit":
            2,
            "page":
            1,
        }),
    )
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["page_metadata"]["page"] == 1
    assert resp.json()["page_metadata"]["hasNext"]
    assert resp.json()["limit"] == 2
    assert len(resp.json()["results"]) == 2
    assert resp.json()["results"][0] == {
        "Awarding Agency": "awarding toptier 8006",
        "Awarding Sub Agency": "awarding subtier 8006",
        "Prime Award ID": "PIID6003",
        "Prime Recipient Name": "recipient_name_for_award_1003",
        "Sub-Award Amount": 60000.0,
        "Sub-Award Date": "2019-01-01",
        "Sub-Award ID": "66666",
        "Sub-Awardee Name": "RECIPIENT_NAME_FOR_AWARD_1003",
        "prime_award_internal_id": 3,
        "internal_id": "66666",
        "prime_award_recipient_id": "28aae030-b4b4-4494-8a75-3356208469cf-R",
        "recipient_id": None,
        "prime_award_generated_internal_id": "CONT_AWD_TESTING_3",
    }
    assert resp.json()["results"][1] == {
        "Awarding Agency": "awarding toptier 8003",
        "Awarding Sub Agency": "awarding subtier 8003",
        "Prime Award ID": "PIID3002",
        "Prime Recipient Name": "recipient_name_for_award_1002",
        "Sub-Award Amount": 30000.0,
        "Sub-Award Date": "2016-01-01",
        "Sub-Award ID": "33333",
        "Sub-Awardee Name": "RECIPIENT_NAME_FOR_AWARD_1002",
        "prime_award_internal_id": 2,
        "internal_id": "33333",
        "prime_award_recipient_id": "180bddfc-67f0-42d6-8279-a014d1062d65-R",
        "recipient_id": None,
        "prime_award_generated_internal_id": "CONT_AWD_TESTING_2",
    }