示例#1
0
def test_category_cfda_awards(cfda_test_data, monkeypatch,
                              elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    test_payload = {
        "category": "cfda",
        "subawards": False,
        "page": 1,
        "limit": 50
    }

    spending_by_category_logic = CfdaViewSet().perform_search(test_payload, {})

    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
        }],
        "messages": [get_time_period_message()],
    }

    assert expected_response == spending_by_category_logic
示例#2
0
def test_category_state_territory(geo_test_data, monkeypatch,
                                  elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    test_payload = {
        "category": "state_territory",
        "subawards": False,
        "page": 1,
        "limit": 50
    }

    spending_by_category_logic = StateTerritoryViewSet().perform_search(
        test_payload, {})

    expected_response = {
        "category": "state_territory",
        "limit": 50,
        "page_metadata": {
            "page": 1,
            "next": None,
            "previous": None,
            "hasNext": False,
            "hasPrevious": False
        },
        "results": [{
            "amount": 10,
            "code": "XY",
            "name": "Test State",
            "id": None
        }],
        "messages": [get_time_period_message()],
    }

    assert expected_response == spending_by_category_logic
示例#3
0
def test_spending_by_geography_failure_with_invalid_fields(
    client, monkeypatch, elasticsearch_award_index, awards_and_transactions
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    # Test invalid "geo_layer" string
    resp = post(client, def_codes=["L"], geo_layer="NOT VALID", geo_layer_filters=["SC-01"], spending_type="obligation")
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    assert resp.data["detail"] == "Field 'geo_layer' is outside valid values ['state', 'county', 'district']"

    # Test invalid "spending_type" string
    resp = post(client, def_codes=["L"], geo_layer="state", geo_layer_filters=["SC-01"], spending_type="NOT VALID")
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    assert (
        resp.data["detail"]
        == "Field 'spending_type' is outside valid values ['obligation', 'outlay', 'face_value_of_loan']"
    )

    # Test invalid "award_type_codes" string
    resp = post(
        client,
        award_type_codes=["NOT VALID"],
        def_codes=["L"],
        geo_layer="state",
        geo_layer_filters=["SC-01"],
        spending_type="obligation",
    )
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    assert "Field 'filter|award_type_codes' is outside valid values " in resp.data["detail"]
def test_pagination_page_and_limit(client, monkeypatch, helpers,
                                   elasticsearch_award_index,
                                   awards_and_transactions):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client,
                                              url,
                                              def_codes=["L", "M"],
                                              page=2,
                                              limit=1,
                                              sort="description")
    expected_results = {
        "results": [{
            "code": "456789123",
            "award_count": 1,
            "description": "RECIPIENT 2",
            "face_value_of_loan": 30.0,
            "id": ["3c92491a-f2cd-ec7d-294b-7daf91511866-R"],
            "obligation": 20.0,
            "outlay": 10.0,
        }],
        "page_metadata": {
            "hasNext": True,
            "hasPrevious": True,
            "limit": 1,
            "next": 3,
            "page": 2,
            "previous": 1,
            "total": 3,
        },
    }

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json() == expected_results
def test_correct_response_multiple_defc(client, monkeypatch, helpers,
                                        elasticsearch_award_index,
                                        cfda_awards_and_transactions):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client,
                                              url,
                                              def_codes=["L", "M"])
    expected_results = [
        {
            "code": "20.200",
            "award_count": 2,
            "description": "CFDA 2",
            "face_value_of_loan": 330.0,
            "id": 200,
            "obligation": 220.0,
            "outlay": 100.0,
            "resource_link": "www.example.com/200",
        },
        {
            "code": "10.100",
            "award_count": 1,
            "description": "CFDA 1",
            "face_value_of_loan": 3.0,
            "id": 100,
            "obligation": 2.0,
            "outlay": 0.0,
            "resource_link": None,
        },
    ]

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results
示例#6
0
def test_spending_by_award_tas_sub_account(client, monkeypatch, elasticsearch_award_index, mock_tas_data):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    data = {
        "filters": {
            "tas_codes": [{"aid": "028", "main": "8006", "sub": "000"}],
            "award_type_codes": ["A", "B", "C", "D"],
        },
        "fields": ["Award ID"],
        "subawards": False,
    }
    resp = client.post("/api/v2/search/spending_by_award", content_type="application/json", data=json.dumps(data))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data["results"]) == 1

    data = {
        "filters": {
            "tas_codes": [{"aid": "028", "main": "8006", "sub": "005"}],
            "award_type_codes": ["A", "B", "C", "D"],
        },
        "fields": ["Award ID"],
        "subawards": False,
    }
    resp = client.post("/api/v2/search/spending_by_award", content_type="application/json", data=json.dumps(data))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data["results"]) == 1
def test_query_search(client, disaster_account_data,
                      elasticsearch_account_index, monkeypatch, helpers):
    helpers.patch_datetime_now(monkeypatch, 2022, 12, 31)

    setup_elasticsearch_test(monkeypatch, elasticsearch_account_index)
    resp = helpers.post_for_spending_endpoint(
        client,
        url,
        query="Agency 008",
        def_codes=["L", "M", "N", "O", "P"],
        spending_type="award",
    )
    expected_results = [{
        "id": 2,
        "code": "008",
        "description": "Agency 008",
        "children": [],
        "award_count": 1,
        "obligation": 2000.0,
        "outlay": 20000.0,
        "total_budgetary_resources": None,
    }]

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results
def test_spending_by_award_foreign_filter(client, monkeypatch,
                                          elasticsearch_award_index,
                                          test_data):
    """ Verify that foreign country filter is returning the correct results """
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    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
示例#9
0
def test_spending_by_award_count_idvs(client, monkeypatch,
                                      elasticsearch_award_index,
                                      award_data_fixture):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    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!"
示例#10
0
def test_spending_by_transaction_count(monkeypatch, transaction_type_data, elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    request_data = {"filters": {"keywords": ["pop tart"]}}
    results = spending_by_transaction_count(request_data)
    expected_results = {"contracts": 1, "grants": 1, "idvs": 1, "loans": 1, "direct_payments": 1, "other": 1}
    assert results == expected_results
示例#11
0
def test_correct_response_with_query(client, monkeypatch, helpers,
                                     elasticsearch_award_index,
                                     awards_and_transactions):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client,
                                              url,
                                              def_codes=["L", "M"],
                                              query="GIBBERISH")
    expected_results = []
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results

    resp = helpers.post_for_spending_endpoint(client,
                                              url,
                                              def_codes=["L", "M"],
                                              query="3")
    expected_results = [{
        "code":
        "987654321",
        "award_count":
        3,
        "description":
        "RECIPIENT 3",
        "id": [
            "d2894d22-67fc-f9cb-4005-33fa6a29ef86-C",
            "d2894d22-67fc-f9cb-4005-33fa6a29ef86-R"
        ],
        "obligation":
        202200.0,
        "outlay":
        101100.0,
    }]
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results
示例#12
0
def test_federal_account_loans_missing_defc(
    client, generic_account_data, helpers, elasticsearch_account_index, monkeypatch
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_account_index)
    resp = helpers.post_for_spending_endpoint(client, url)
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    assert resp.data["detail"] == "Missing value: 'filter|def_codes' is a required field"
示例#13
0
def test_federal_account_loans_invalid_defc_type(
    client, generic_account_data, helpers, elasticsearch_account_index, monkeypatch
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_account_index)
    resp = helpers.post_for_spending_endpoint(client, url, def_codes="100")
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    assert resp.data["detail"] == "Invalid value in 'filter|def_codes'. '100' is not a valid type (array)"
示例#14
0
def test_federal_account_loans_invalid_defc(
    client, generic_account_data, helpers, elasticsearch_account_index, monkeypatch
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_account_index)
    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["ZZ"])
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    assert resp.data["detail"] == "Field 'filter|def_codes' is outside valid values ['9', 'A', 'L', 'M', 'N', 'O', 'P']"
def test_basic_object_class_award_success(
    client, basic_object_class_faba_with_loan_value, elasticsearch_account_index, monkeypatch, helpers
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_account_index)
    helpers.patch_datetime_now(monkeypatch, 2022, 12, 31)

    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["M"])
    expected_results = [
        {
            "id": "001",
            "code": "001",
            "description": "001 name",
            "award_count": 1,
            "obligation": 1.0,
            "outlay": 0.0,
            "children": [
                {
                    "id": 1,
                    "code": "0001",
                    "description": "0001 name",
                    "award_count": 1,
                    "obligation": 1.0,
                    "outlay": 0.0,
                    "face_value_of_loan": 5.0,
                }
            ],
            "face_value_of_loan": 5.0,
        }
    ]
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results

    expected_totals = {"award_count": 1, "face_value_of_loan": 5.0, "obligation": 1.0, "outlay": 0}
    assert resp.json()["totals"] == expected_totals
def test_filtering_subtier_with_bogus_toptier(
    client, monkeypatch, elasticsearch_transaction_index, basic_award, subagency_award
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    resp = client.post(
        "/api/v2/search/spending_by_category/awarding_subagency",
        content_type="application/json",
        data={
            "filters": {
                "time_period": [{"start_date": "2018-10-01", "end_date": "2020-09-30"}],
                "agencies": [
                    {
                        "type": "awarding",
                        "tier": "subtier",
                        "name": "Awarding Subtier Agency 5",
                        "toptier_name": "bogus toptier name",
                    }
                ],
            }
        },
    )
    assert resp.status_code == status.HTTP_200_OK
    assert resp.data == {
        "category": "awarding_subagency",
        "limit": 10,
        "page_metadata": {"page": 1, "next": None, "previous": None, "hasNext": False, "hasPrevious": False},
        "results": [],
        "messages": [get_time_period_message()],
    }
示例#17
0
def test_pagination_page_and_limit(
    client, monkeypatch, helpers, elasticsearch_award_index, cfda_awards_and_transactions
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["L", "M"], page=2, limit=1, sort="description")
    expected_results = {
        "results": [
            {
                "code": "20.200",
                "award_count": 2,
                "description": "CFDA 2",
                "id": 200,
                "obligation": 220.0,
                "outlay": 100.0,
                "resource_link": "www.example.com/200",
            }
        ],
        "page_metadata": {
            "hasNext": True,
            "hasPrevious": True,
            "limit": 1,
            "next": 3,
            "page": 2,
            "previous": 1,
            "total": 3,
        },
    }

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json() == expected_results
def test_correct_response_of_empty_list(client, monkeypatch,
                                        elasticsearch_transaction_index,
                                        awards_and_transactions):

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    resp = client.post(
        "/api/v2/search/spending_by_category/federal_account",
        content_type="application/json",
        data=json.dumps({
            "filters": {
                "time_period": [{
                    "start_date": "2008-10-01",
                    "end_date": "2009-09-30"
                }]
            }
        }),
    )
    expected_response = {
        "category": "federal_account",
        "limit": 10,
        "page_metadata": {
            "page": 1,
            "next": None,
            "previous": None,
            "hasNext": False,
            "hasPrevious": False
        },
        "results": [],
        "messages": [get_time_period_message()],
    }
    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
    assert resp.json() == expected_response
def test_object_class_query(client, elasticsearch_account_index, basic_faba_with_object_class, monkeypatch, helpers):
    setup_elasticsearch_test(monkeypatch, elasticsearch_account_index)
    helpers.patch_datetime_now(monkeypatch, 2022, 12, 31)
    helpers.reset_dabs_cache()

    resp = helpers.post_for_spending_endpoint(
        client, url, query="001 name", def_codes=["A", "M", "N"], spending_type="award"
    )
    expected_results = [
        {
            "id": "001",
            "code": "001",
            "description": "001 name",
            "award_count": 1,
            "obligation": 1.0,
            "outlay": 0.0,
            "children": [
                {
                    "id": "1",
                    "code": "0001",
                    "description": "0001 name",
                    "award_count": 1,
                    "obligation": 1.0,
                    "outlay": 0.0,
                }
            ],
        }
    ]
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results

    expected_totals = {"award_count": 1, "obligation": 1.0, "outlay": 0}
    assert resp.json()["totals"] == expected_totals
示例#20
0
def test_basic_success(client, disaster_account_data, elasticsearch_award_index, monkeypatch, helpers):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)
    helpers.patch_datetime_now(monkeypatch, 2022, 12, 31)
    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["L", "M", "N", "O", "P"])
    expected_results = [
        {
            "id": 2,
            "code": "008",
            "description": "Agency 008",
            "children": [],
            "award_count": 1,
            "obligation": 2000.0,
            "outlay": 20000.0,
            "face_value_of_loan": 333.0,
        },
    ]

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results

    expected_totals = {"award_count": 1, "face_value_of_loan": 333.0, "obligation": 2000.0, "outlay": 20000.0}

    assert resp.json()["totals"] == expected_totals

    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["M"])
    expected_results = []

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results
示例#21
0
def test_correct_response_for_each_filter(
    client, monkeypatch, spending_over_time_test_data, elasticsearch_transaction_index
):
    """
    Verify the content of the response when using different filters. This function creates the ES Index
    and then calls each of the tests instead of recreating the ES Index multiple times with the same data.
    """
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    test_cases = [
        _test_correct_response_for_keywords,
        _test_correct_response_for_time_period,
        _test_correct_response_for_award_type_codes,
        _test_correct_response_for_agencies,
        _test_correct_response_for_tas_codes,
        _test_correct_response_for_pop_location,
        _test_correct_response_for_recipient_location,
        _test_correct_response_for_recipient_search_text,
        _test_correct_response_for_recipient_type_names,
        _test_correct_response_for_award_amounts,
        _test_correct_response_for_cfda_program,
        _test_correct_response_for_naics_codes,
        _test_correct_response_for_psc_codes,
        _test_correct_response_for_contract_pricing_type_codes,
        _test_correct_response_for_set_aside_type_codes,
        _test_correct_response_for_set_extent_competed_type_codes,
        _test_correct_response_for_recipient_id,
    ]

    for test in test_cases:
        test(client)
示例#22
0
def test_correct_response_with_query(
    client, monkeypatch, helpers, elasticsearch_award_index, cfda_awards_and_transactions
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["L", "M"], query="GIBBERISH")
    expected_results = []
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results

    resp = helpers.post_for_spending_endpoint(client, url, def_codes=["L", "M"], query="2")
    expected_results = [
        {
            "code": "20.200",
            "award_count": 2,
            "description": "CFDA 2",
            "face_value_of_loan": 330.0,
            "id": 200,
            "obligation": 220.0,
            "outlay": 100.0,
            "resource_link": "www.example.com/200",
            "applicant_eligibility": "AE2",
            "beneficiary_eligibility": "BE2",
            "cfda_federal_agency": "Agency 2",
            "cfda_objectives": "objectives 2",
            "cfda_website": "www.example.com/cfda_website/200",
        }
    ]
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results
def test_spending_by_geography_failure_with_missing_fields(
        client, monkeypatch, elasticsearch_award_index,
        awards_and_transactions):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    # Test required "def_codes" in filter object
    resp = post(client,
                geo_layer="state",
                geo_layer_filters=["SC-01"],
                spending_type="obligation")
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    assert resp.data[
        "detail"] == "Missing value: 'filter|def_codes' is a required field"

    # Test required "geo_layer" string
    resp = post(client,
                def_codes=["L"],
                geo_layer_filters=["SC-01"],
                spending_type="obligation")
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    assert resp.data[
        "detail"] == "Missing value: 'geo_layer' is a required field"

    # Test required "spending_type" string
    resp = post(client,
                def_codes=["L"],
                geo_layer="state",
                geo_layer_filters=["SC-01"])
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    assert resp.data[
        "detail"] == "Missing value: 'spending_type' is a required field"
def test_correct_response_multiple_defc(client, monkeypatch, helpers,
                                        elasticsearch_award_index,
                                        awards_and_transactions):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client,
                                              url,
                                              def_codes=["L", "M"])
    expected_results = [
        {
            "code":
            "987654321",
            "award_count":
            3,
            "description":
            "RECIPIENT, 3",
            "id": [
                "d2894d22-67fc-f9cb-4005-33fa6a29ef86-C",
                "d2894d22-67fc-f9cb-4005-33fa6a29ef86-R"
            ],
            "obligation":
            202200.0,
            "outlay":
            101100.0,
        },
        {
            "code": "456789123",
            "award_count": 1,
            "description": "RECIPIENT 2",
            "id": ["3c92491a-f2cd-ec7d-294b-7daf91511866-R"],
            "obligation": 20.0,
            "outlay": 10.0,
        },
        {
            "code": "DUNS Number not provided",
            "award_count": 1,
            "description": "RECIPIENT 1",
            "id": ["5f572ec9-8b49-e5eb-22c7-f6ef316f7689-R"],
            "obligation": 2.0,
            "outlay": 1.0,
        },
        {
            "code": "096354360",
            "award_count": 1,
            "description": "MULTIPLE RECIPIENTS",
            "id": None,
            "obligation": 20000.0,
            "outlay": 10000.0,
        },
        {
            "code": "DUNS Number not provided",
            "award_count": 1,
            "description": "MULTIPLE RECIPIENTS",
            "id": None,
            "obligation": 2000000.0,
            "outlay": 1000000.0,
        },
    ]
    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["results"] == expected_results
def test_download_transactions_limit(client, download_test_data, monkeypatch,
                                     elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)
    download_generation.retrieve_db_string = Mock(
        return_value=generate_test_db_connection_string())

    dl_resp = client.post(
        "/api/v2/download/transactions/",
        content_type="application/json",
        data=json.dumps({
            "limit": 1,
            "filters": {
                "award_type_codes": []
            },
            "columns": []
        }),
    )
    resp = client.get("/api/v2/download/status/?file_name={}".format(
        dl_resp.json()["file_name"]))

    expected_number_of_columns = get_number_of_columns_for_query_paths(
        ("transaction", "d1"), ("transaction", "d2"), ("subaward", "d1"),
        ("subaward", "d2"))

    assert resp.status_code == status.HTTP_200_OK
    assert resp.json()["total_rows"] == 2
    assert resp.json()["total_columns"] == expected_number_of_columns
示例#26
0
def test_columns_can_be_sorted(client, monkeypatch, transaction_data, elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    fields = [
        "Action Date",
        "Award ID",
        "Awarding Agency",
        "Awarding Sub Agency",
        "Award Type",
        "Mod",
        "Recipient Name",
        "Action Date",
    ]

    request = {
        "filters": {"keyword": "test", "award_type_codes": ["A", "B", "C", "D"]},
        "fields": fields,
        "page": 1,
        "limit": 5,
        "order": "desc",
    }

    for field in fields:
        request["sort"] = field
        resp = client.post(ENDPOINT, content_type="application/json", data=json.dumps(request))
        assert resp.status_code == status.HTTP_200_OK, f"Failed to sort column: {field}"
示例#27
0
def test_subset_of_fields_returned(client, monkeypatch, transaction_data, elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    fields = ["Award ID", "Recipient Name", "Mod"]

    request = {
        "filters": {"keyword": "test", "award_type_codes": ["A", "B", "C", "D"]},
        "fields": fields,
        "page": 1,
        "limit": 5,
        "sort": "Award ID",
        "order": "desc",
    }

    resp = client.post(ENDPOINT, content_type="application/json", data=json.dumps(request))

    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data["results"]) > 0
    for result in resp.data["results"]:
        for field in fields:
            assert field in result, f"Response item is missing field {field}"

        assert "internal_id" in result
        assert "generated_internal_id" in result
        assert "Last Date to Order" not in result
示例#28
0
def test_download_count(client, download_test_data, monkeypatch,
                        elasticsearch_transaction_index):
    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    download_generation.retrieve_db_string = Mock(
        return_value=generate_test_db_connection_string())
    resp = client.post(
        "/api/v2/download/count/",
        content_type="application/json",
        data=json.dumps({
            "filters": {
                "agencies": [{
                    "type": "awarding",
                    "tier": "toptier",
                    "name": "Bureau of Things"
                }]
            }
        }),
    )
    resp_json = resp.json()

    assert resp.status_code == status.HTTP_200_OK, "Failed to return 200 Response"
    assert resp_json["calculated_transaction_count"] == 1
    assert resp_json[
        "maximum_transaction_limit"] == settings.MAX_DOWNLOAD_LIMIT
    assert resp_json["transaction_rows_gt_limit"] is False
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/district",
        content_type="application/json",
        data=json.dumps({
            "filters": {
                "time_period": [{
                    "start_date": "2018-10-01",
                    "end_date": "2020-09-30"
                }]
            }
        }),
    )
    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 resp.json() == expected_response
示例#30
0
def test_invalid_award_type_codes(
    client, monkeypatch, helpers, elasticsearch_award_index, cfda_awards_and_transactions
):
    setup_elasticsearch_test(monkeypatch, elasticsearch_award_index)

    resp = helpers.post_for_spending_endpoint(client, url, award_type_codes=["ZZ", "08"], def_codes=["L", "M"])
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    assert resp.data["detail"] == "Field 'filter|award_type_codes' is outside valid values ['07', '08']"