Skip to content

Commit

Permalink
overwrite pagination update test
Browse files Browse the repository at this point in the history
  • Loading branch information
babebe committed Jan 30, 2025
1 parent 154e6c6 commit 9d67700
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
1 change: 0 additions & 1 deletion api/src/api/opportunities_v1/opportunity_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def opportunity_search(
"response.pagination.total_records": pagination_info.total_records,
}
)
logger.info("Successfully fetched opportunities")

if search_params.get("format") == opportunity_schemas.SearchResponseFormat.CSV:
# Convert the response into a CSV and return the contents
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/users/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def user_save_search(
raise_flask_error(401, "Unauthorized user")

# Retrieve opportunity IDs
opportunity_ids = search_opportunities_id(search_client, json_data)
opportunity_ids = search_opportunities_id(search_client, json_data["search_query"])

with db_session.begin():
saved_search = create_saved_search(db_session, user_id, json_data, opportunity_ids)
Expand Down
15 changes: 13 additions & 2 deletions api/src/services/opportunities_v1/search_opportunities.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,19 @@ def search_opportunities(
return records, response.aggregations, pagination_info


def search_opportunities_id(search_client: search.SearchClient, search_data: dict) -> list:
search_params = SearchOpportunityParams.model_validate(search_data["search_query"])
def search_opportunities_id(search_client: search.SearchClient, search_query: dict) -> list:
# Override pagination when calling opensearch
updated_search_query = {
**search_query,
"pagination": {
"order_by": "post_date",
"page_offset": 1,
"page_size": 1000,
"sort_direction": "descending",
},
}

search_params = SearchOpportunityParams.model_validate(updated_search_query)

search_request = _get_search_request(search_params, False)

Expand Down
19 changes: 13 additions & 6 deletions api/tests/src/api/users/test_user_save_search_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from src.constants.lookup_constants import FundingInstrument
from src.db.models.user_models import UserSavedSearch
from tests.src.api.opportunities_v1.conftest import get_search_request
from tests.src.api.opportunities_v1.test_opportunity_route_search import LOC_HIGHER_EDUCATION
from tests.src.api.opportunities_v1.test_opportunity_route_search import (
NASA_INNOVATIONS,
NASA_SUPERSONIC,
)
from tests.src.db.models.factories import UserFactory


Expand Down Expand Up @@ -90,12 +93,12 @@ def test_user_save_search_post(
search_name = "Test Search"
search_query = get_search_request(
funding_instrument_one_of=[FundingInstrument.GRANT],
agency_one_of=["LOC"],
agency_one_of=["NASA"],
)

# Load into the search index
schema = OpportunityV1Schema()
json_records = [schema.dump(LOC_HIGHER_EDUCATION)]
json_records = [schema.dump(opp) for opp in [NASA_INNOVATIONS, NASA_SUPERSONIC]]
search_client.bulk_upsert(opportunity_index, json_records, "opportunity_id")

# Swap the search index alias
Expand All @@ -113,19 +116,23 @@ def test_user_save_search_post(

assert response.status_code == 200
assert response.json["message"] == "Success"

# Verify the search was saved in the database
saved_search = db_session.query(UserSavedSearch).one()

assert saved_search.user_id == user.user_id
assert saved_search.name == search_name
assert saved_search.search_query == {
"format": "json",
"filters": {"agency": {"one_of": ["LOC"]}, "funding_instrument": {"one_of": ["grant"]}},
"filters": {"agency": {"one_of": ["NASA"]}, "funding_instrument": {"one_of": ["grant"]}},
"pagination": {
"order_by": "opportunity_id",
"page_size": 25,
"page_offset": 1,
"sort_direction": "ascending",
},
}
assert saved_search.searched_opportunity_ids == [LOC_HIGHER_EDUCATION.opportunity_id]
# Verify pagination for the query was over-written. searched_opportunity_ids should be ordered by "post_date"
assert saved_search.searched_opportunity_ids == [
NASA_SUPERSONIC.opportunity_id,
NASA_INNOVATIONS.opportunity_id,
]

0 comments on commit 9d67700

Please sign in to comment.