diff --git a/application/data_access/entity_queries.py b/application/data_access/entity_queries.py index 8c0a640b..29bd6580 100644 --- a/application/data_access/entity_queries.py +++ b/application/data_access/entity_queries.py @@ -14,10 +14,31 @@ ) from application.db.models import EntityOrm, OldEntityOrm from application.search.enum import GeometryRelation, PeriodOption +import redis +import json +redis_client = redis.Redis(host="127.0.0.1", port=6379, db=0) logger = logging.getLogger(__name__) +def get_cached_query_result(key): + result = redis_client.get(key) + + if result: + result_data = json.loads(result) + return result_data + + return None + + +def cache_query_result(key, result): + # Serialize the result as JSON + result_json = json.dumps( + result, default=lambda o: o.dict() if hasattr(o, "dict") else str(o) + ) + redis_client.set(key, result_json, ex=3600) # Cache with 1-hour expiration for now + + def get_entity_query( session: Session, id: int, @@ -61,18 +82,61 @@ def get_entities(session, dataset: str, limit: int) -> List[EntityModel]: def get_entity_search(session: Session, parameters: dict): - params = normalised_params(parameters) - count: int - entities: list[EntityModel] - # get count - subquery = session.query(EntityOrm.entity) - subquery = _apply_base_filters(subquery, params) - subquery = _apply_date_filters(subquery, params) - subquery = _apply_location_filters(session, subquery, params) - subquery = _apply_period_option_filter(subquery, params).subquery() - count_query = session.query(func.count()).select_from(subquery) - count = count_query.scalar() - # get entities + orignal_params = normalised_params(parameters) + query_key_orignal = str(orignal_params) + cached_result = get_cached_query_result(query_key_orignal) + if cached_result: + return cached_result + datasets = orignal_params.get("dataset", []) # Assume this is a list of datasets + params = orignal_params + combined_count = 0 + combined_entities = [] + if not datasets: + # get count + combined_count = count_entity_method(session, params) + + # get entities + combined_entities = fetch_all_entities(session, params) + + for dataset in datasets: + # Update params to work with the current dataset + params["dataset"] = dataset + + query_key = str(params) + cached_result = get_cached_query_result(query_key) + if cached_result: + combined_count += cached_result["count"] + combined_entities.extend(cached_result["entities"]) + continue # Skip query if the result is cached + + count: int + entities: list[EntityModel] + + # get count + count = count_entity_method(session, params) + + # get entities + entities = fetch_all_entities(session, params) + + # Combine results + combined_count += count + combined_entities.extend(entities) + + # Cache individual dataset result + result = {"params": params, "count": count, "entities": entities} + cache_query_result(query_key, result) + + # Return the combined result + final_result = { + "params": orignal_params, + "count": combined_count, + "entities": combined_entities, + } + cache_query_result(query_key_orignal, final_result) + return final_result + + +def fetch_all_entities(session, params): query_args = [EntityOrm] query = session.query(*query_args) query = _apply_base_filters(query, params) @@ -82,7 +146,18 @@ def get_entity_search(session: Session, parameters: dict): query = _apply_limit_and_pagination_filters(query, params) entities = query.all() entities = [entity_factory(entity_orm) for entity_orm in entities] - return {"params": params, "count": count, "entities": entities} + return entities + + +def count_entity_method(session, params): + subquery = session.query(EntityOrm.entity) + subquery = _apply_base_filters(subquery, params) + subquery = _apply_date_filters(subquery, params) + subquery = _apply_location_filters(session, subquery, params) + subquery = _apply_period_option_filter(subquery, params).subquery() + count_query = session.query(func.count()).select_from(subquery) + count = count_query.scalar() + return count def lookup_entity_link( @@ -176,14 +251,14 @@ def _apply_location_filters(session, query, params): for geometry in params.get("geometry", []): simplified_geom = func.ST_GeomFromText(geometry, 4326) - bbox_filter_geometry = func.ST_Envelope(simplified_geom).op("&&")( - EntityOrm.geometry - ) - bbox_filter_point = func.ST_Envelope(simplified_geom).op("&&")( - EntityOrm.geometry - ) if params.get("geometry_relation") == GeometryRelation.intersects.name: + bbox_filter_geometry = func.ST_Envelope(simplified_geom).op("&&")( + EntityOrm.geometry + ) + bbox_filter_point = func.ST_Envelope(simplified_geom).op("&&")( + EntityOrm.point + ) clauses.append( or_( and_( diff --git a/application/routers/entity.py b/application/routers/entity.py index 81f66453..251b390c 100644 --- a/application/routers/entity.py +++ b/application/routers/entity.py @@ -60,21 +60,34 @@ def _get_geojson( def _get_entity_json( - data: List[EntityModel], - include: Optional[Set] = None, + data: List[Union[EntityModel, dict]], # Allow both EntityModel and dict + include: Optional[Set[str]] = None, exclude: Optional[List[str]] = None, ): entities = [] for entity in data: - if include is not None: - # always return at least the entity (id) - include.add("entity") - e = entity.dict(include=include, by_alias=True) + + if isinstance(entity, dict): + # Handle the entity as a dict + if include is not None: + include.add("entity") + e = {key: value for key, value in entity.items() if key in include} + else: + exclude = set(exclude) if exclude else set() + exclude.add("geojson") # Always exclude 'geojson' + e = {key: value for key, value in entity.items() if key not in exclude} else: - exclude = set(exclude) if exclude else set() - exclude.add("geojson") # Always exclude 'geojson' - e = entity.dict(exclude=exclude, by_alias=True) + if include is not None: + # Always return at least the entity (id) + include.add("entity") + e = entity.dict(include=include, by_alias=True) + else: + exclude = set(exclude) if exclude else set() + exclude.add("geojson") # Always exclude 'geojson' + e = entity.dict(exclude=exclude, by_alias=True) + entities.append(e) + return entities diff --git a/tests/acceptance/parameters/test_exclude_field_parameter.py b/tests/acceptance/parameters/test_exclude_field_parameter.py index e0a2cdf2..8edb9698 100644 --- a/tests/acceptance/parameters/test_exclude_field_parameter.py +++ b/tests/acceptance/parameters/test_exclude_field_parameter.py @@ -7,11 +7,12 @@ [["organisation-entity"], ["entry-date", "prefix"]], ) def test_entity_search_exclude_field_for_json_response( - client, - db_session, - test_data: dict, - exclude: list, + client, db_session, test_data: dict, exclude: list, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) # Clear the table to avoid duplicates db_session.query(EntityOrm).delete() db_session.commit() @@ -58,11 +59,12 @@ def test_entity_search_exclude_field_for_json_response( ], ) def test_entity_search_no_exclude_field_for_json_response( - client, - db_session, - test_data: dict, - expected_fields: set, + client, db_session, test_data: dict, expected_fields: set, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) db_session.query(EntityOrm).delete() db_session.commit() # Load entities into the database @@ -90,8 +92,12 @@ def test_entity_search_no_exclude_field_for_json_response( @pytest.mark.parametrize("exclude", [["notes"], ["geometry", "notes"]]) def test_entity_search_exclude_field_for_geojson_response( - client, db_session, test_data: dict, exclude: list + client, db_session, test_data: dict, exclude: list, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) db_session.query(EntityOrm).delete() db_session.commit() # Load entities into the database @@ -138,11 +144,12 @@ def test_entity_search_exclude_field_for_geojson_response( ], ) def test_entity_search_no_exclude_field_for_geojson_response( - client, - db_session, - test_data: dict, - expected_properties: set, + client, db_session, test_data: dict, expected_properties: set, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) db_session.query(EntityOrm).delete() db_session.commit() # Load entities into the database diff --git a/tests/integration/data_access/test_entity_queries.py b/tests/integration/data_access/test_entity_queries.py index eab8f19b..e54cd577 100644 --- a/tests/integration/data_access/test_entity_queries.py +++ b/tests/integration/data_access/test_entity_queries.py @@ -4,6 +4,7 @@ from application.db.models import EntityOrm from application.data_access.entity_queries import get_entity_search + # set up logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) @@ -73,11 +74,15 @@ ], ) def test_get_entity_search_geometry_reference_queries_returns_correct_results( - entities, parameters, expected_count, expected_entities, db_session + entities, parameters, expected_count, expected_entities, db_session, mocker ): """ A test to check if the correct results are returned when using the geometry_reference parameter """ + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) # load data points into entity table # add datasets for entity in entities: @@ -85,7 +90,6 @@ def test_get_entity_search_geometry_reference_queries_returns_correct_results( # run query and get results results = get_entity_search(db_session, parameters) - # assert count assert results["count"] == expected_count, results diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index 42e6f32c..5f1e4433 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -42,7 +42,11 @@ def _transform_dataset_to_response(dataset, is_geojson=False): return dataset -def test_app_returns_valid_geojson_list(client): +def test_app_returns_valid_geojson_list(client, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get("/entity.geojson", headers={"Origin": "localhost"}) data = response.json() assert "type" in data @@ -51,7 +55,11 @@ def test_app_returns_valid_geojson_list(client): assert [] == data["features"] -def test_app_returns_valid_populated_geojson_list(client, test_data): +def test_app_returns_valid_populated_geojson_list(client, test_data, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get("/entity.geojson", headers={"Origin": "localhost"}) data = response.json() assert "type" in data @@ -68,7 +76,11 @@ def test_app_returns_valid_populated_geojson_list(client, test_data): ) == len(data["features"]) -def test_lasso_geo_search_finds_results(client, test_data): +def test_lasso_geo_search_finds_results(client, test_data, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params = { "geometry_relation": "intersects", "geometry": intersects_with_greenspace_entity, @@ -188,7 +200,13 @@ def test_dataset_json_endpoint_returns_as_expected(test_data, client): @pytest.mark.parametrize("point, expected_status_code", wkt_params) -def test_api_handles_invalid_wkt(point, expected_status_code, client, test_data): +def test_api_handles_invalid_wkt( + point, expected_status_code, client, test_data, mocker +): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params = {"geometry_relation": "intersects", "geometry": point} response = client.get("/entity.geojson", params=params) assert response.status_code == expected_status_code diff --git a/tests/integration/test_entity_queries.py b/tests/integration/test_entity_queries.py index f7c976c5..4a9deb97 100644 --- a/tests/integration/test_entity_queries.py +++ b/tests/integration/test_entity_queries.py @@ -2,7 +2,13 @@ from application.db.models import EntityOrm -def test__lookup_entity_link_returns_nothing_when_the_entity_isnt_found(db_session): +def test__lookup_entity_link_returns_nothing_when_the_entity_isnt_found( + db_session, mocker +): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) linked_entity = lookup_entity_link( db_session, "a-reference", "article-4-direction", 123 ) @@ -10,8 +16,12 @@ def test__lookup_entity_link_returns_nothing_when_the_entity_isnt_found(db_sessi def test__lookup_entity_link_returns_the_looked_up_entity_when_the_link_exists( - db_session, + db_session, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) lookup_entity = { "entity": 106, "name": "A space", diff --git a/tests/integration/test_entity_search.py b/tests/integration/test_entity_search.py index f222d9a8..5578d3fc 100644 --- a/tests/integration/test_entity_search.py +++ b/tests/integration/test_entity_search.py @@ -108,6 +108,10 @@ def test_search_entity_by_dataset_names_not_in_system_returns_only_missing( def test_search_entity_by_single_dataset_name(test_data, params, mocker, db_session): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["dataset"] = ["greenspace"] result = get_entity_search(db_session, params) assert 1 == result["count"] @@ -116,7 +120,11 @@ def test_search_entity_by_single_dataset_name(test_data, params, mocker, db_sess assert entity.typology == "geography" -def test_search_entity_by_list_of_dataset_names(test_data, params, db_session): +def test_search_entity_by_list_of_dataset_names(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["dataset"] = ["greenspace", "brownfield-site"] result = get_entity_search(db_session, params) assert 2 == result["count"] @@ -125,7 +133,11 @@ def test_search_entity_by_list_of_dataset_names(test_data, params, db_session): assert e.typology == "geography" -def test_search_entity_by_date_since(test_data, params, db_session): +def test_search_entity_by_date_since(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["entry_date_year"] = 2019 params["entry_date_month"] = 1 params["entry_date_day"] = 1 @@ -191,7 +203,11 @@ def test_search_entity_by_date_since(test_data, params, db_session): assert result["count"] == 0 -def test_search_entity_by_date_before(test_data, params, db_session): +def test_search_entity_by_date_before(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["entry_date_year"] = 2019 params["entry_date_month"] = 1 params["entry_date_day"] = 1 @@ -227,7 +243,11 @@ def test_search_entity_by_date_before(test_data, params, db_session): ] -def test_search_entity_by_date_equal(test_data, params, db_session): +def test_search_entity_by_date_equal(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["entry_date_year"] = 2019 params["entry_date_month"] = 1 params["entry_date_day"] = 1 @@ -243,7 +263,11 @@ def test_search_entity_by_date_equal(test_data, params, db_session): assert entity.dataset == "greenspace" -def test_search_entity_by_point(test_data, params, db_session): +def test_search_entity_by_point(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["longitude"] = -1.64794921875 params["latitude"] = 50.51342652633956 @@ -259,10 +283,16 @@ def test_search_entity_by_point(test_data, params, db_session): assert entity.dataset == "historical-monument" -def test_search_entity_by_single_polygon_intersects(test_data, params, db_session): +def test_search_entity_by_single_polygon_intersects( + test_data, params, db_session, mocker +): from tests.test_data.wkt_data import intersects_with_brownfield_entity as brownfield from tests.test_data.wkt_data import intersects_with_greenspace_entity as greenspace + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [brownfield] params["geometry_relation"] = GeometryRelation.intersects.name @@ -279,11 +309,15 @@ def test_search_entity_by_single_polygon_intersects(test_data, params, db_sessio def test_search_entity_by_list_of_polygons_that_intersect( - test_data, params, db_session + test_data, params, db_session, mocker ): from tests.test_data.wkt_data import intersects_with_brownfield_entity as brownfield from tests.test_data.wkt_data import intersects_with_greenspace_entity as greenspace + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [brownfield, greenspace] params["geometry_relation"] = GeometryRelation.intersects.name @@ -291,9 +325,15 @@ def test_search_entity_by_list_of_polygons_that_intersect( assert result["count"] == 2 -def test_search_entity_by_polygon_with_no_intersection(test_data, params, db_session): +def test_search_entity_by_polygon_with_no_intersection( + test_data, params, db_session, mocker +): from tests.test_data.wkt_data import no_intersection + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [no_intersection] params["geometry_relation"] = GeometryRelation.intersects.name @@ -301,8 +341,12 @@ def test_search_entity_by_polygon_with_no_intersection(test_data, params, db_ses assert 0 == result["count"] -def test_search_all_entities(test_data, params, db_session): +def test_search_all_entities(test_data, params, db_session, mocker): # default is PeriodOption.all - already in params + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) result = get_entity_search(db_session, params) assert result["count"] == len(test_data["entities"]) for e in result["entities"]: @@ -317,8 +361,12 @@ def test_search_all_entities(test_data, params, db_session): ] -def test_search_current_entries(test_data, params, db_session): +def test_search_current_entries(test_data, params, db_session, mocker): # entries without an end date + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["period"] = [PeriodOption.current] result = get_entity_search(db_session, params) @@ -334,7 +382,12 @@ def test_search_current_entries(test_data, params, db_session): ] -def test_search_historical_entries(test_data, params, db_session): +def test_search_historical_entries(test_data, params, db_session, mocker): + + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) # entries with an end date params["period"] = [PeriodOption.historical] @@ -344,7 +397,13 @@ def test_search_historical_entries(test_data, params, db_session): assert e.dataset == "greenspace" -def test_search_includes_only_field_params(test_data, client, exclude_middleware): +def test_search_includes_only_field_params( + test_data, client, exclude_middleware, mocker +): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get("/entity.json?limit=10&field=name") response.raise_for_status() result = response.json() @@ -353,7 +412,13 @@ def test_search_includes_only_field_params(test_data, client, exclude_middleware assert not set(e.keys()).symmetric_difference(set(["name", "entity"])) -def test_search_includes_multiple_field_params(test_data, client, exclude_middleware): +def test_search_includes_multiple_field_params( + test_data, client, exclude_middleware, mocker +): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get("/entity.json?limit=10&field=name&field=dataset") response.raise_for_status() result = response.json() @@ -367,8 +432,12 @@ def test_search_includes_multiple_field_params(test_data, client, exclude_middle "field_name", list(EntityModel.schema()["properties"].keys() - {"geojson"}) ) def test_search_includes_any_field_params( - field_name, test_data, client, exclude_middleware + field_name, test_data, client, exclude_middleware, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get(f"/entity.json?limit=10&field={field_name}") response.raise_for_status() result = response.json() @@ -377,23 +446,39 @@ def test_search_includes_any_field_params( assert not set(e.keys()).symmetric_difference(set([field_name, "entity"])) -def test_search_pagination_does_not_affect_count(test_data, client, exclude_middleware): +def test_search_pagination_does_not_affect_count( + test_data, client, exclude_middleware, mocker +): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get("/entity.json?limit=1") response.raise_for_status() result = response.json() assert result["count"] == len(test_data["entities"]) -def test_search_filtering_does_affect_count(test_data, client, exclude_middleware): +def test_search_filtering_does_affect_count( + test_data, client, exclude_middleware, mocker +): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) response = client.get("/entity.json?limit=1&dataset=greenspace") response.raise_for_status() result = response.json() assert result["count"] == 1 -def test_search_entity_equal_to_a_polygon(test_data, params, db_session): +def test_search_entity_equal_to_a_polygon(test_data, params, db_session, mocker): from tests.test_data.wkt_data import equals_brownfield_site_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [equals_brownfield_site_entity] params["geometry_relation"] = GeometryRelation.equals.name @@ -402,19 +487,28 @@ def test_search_entity_equal_to_a_polygon(test_data, params, db_session): assert result["entities"][0].dataset == "brownfield-site" -def test_search_entity_disjoint_from_a_polygon(test_data, params, db_session): +def test_search_entity_disjoint_from_a_polygon(test_data, params, db_session, mocker): from tests.test_data.wkt_data import no_intersection + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [no_intersection] params["geometry_relation"] = GeometryRelation.disjoint.name result = get_entity_search(db_session, params) + print(result) assert result["count"] == 9 def test_search_entity_that_polygon_touches(test_data, params, mocker, db_session): from tests.test_data.wkt_data import touches_forest_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [touches_forest_entity] params["geometry_relation"] = GeometryRelation.touches.name @@ -423,9 +517,13 @@ def test_search_entity_that_polygon_touches(test_data, params, mocker, db_sessio assert result["entities"][0].dataset == "forest" -def test_search_entity_that_contains_a_polygon(test_data, params, db_session): +def test_search_entity_that_contains_a_polygon(test_data, params, db_session, mocker): from tests.test_data.wkt_data import contained_by_greenspace_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [contained_by_greenspace_entity] params["geometry_relation"] = GeometryRelation.contains.name @@ -435,9 +533,13 @@ def test_search_entity_that_contains_a_polygon(test_data, params, db_session): # when dealing with polygons contains and covers are synonymous -def test_search_entity_that_covers_a_polygon(test_data, params, db_session): +def test_search_entity_that_covers_a_polygon(test_data, params, db_session, mocker): from tests.test_data.wkt_data import contained_by_greenspace_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [contained_by_greenspace_entity] params["geometry_relation"] = GeometryRelation.covers.name @@ -446,9 +548,13 @@ def test_search_entity_that_covers_a_polygon(test_data, params, db_session): assert result["entities"][0].dataset == "greenspace" -def test_search_entity_covered_by_a_polygon(test_data, params, db_session): +def test_search_entity_covered_by_a_polygon(test_data, params, db_session, mocker): from tests.test_data.wkt_data import covers_historical_monument_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [covers_historical_monument_entity] params["geometry_relation"] = GeometryRelation.coveredby.name @@ -457,9 +563,13 @@ def test_search_entity_covered_by_a_polygon(test_data, params, db_session): assert result["entities"][0].dataset == "historical-monument" -def test_search_entity_that_overlaps_a_polygon(test_data, params, db_session): +def test_search_entity_that_overlaps_a_polygon(test_data, params, db_session, mocker): from tests.test_data.wkt_data import intersects_with_brownfield_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [intersects_with_brownfield_entity] params["geometry_relation"] = GeometryRelation.overlaps.name @@ -468,9 +578,13 @@ def test_search_entity_that_overlaps_a_polygon(test_data, params, db_session): assert result["entities"][0].dataset == "brownfield-site" -def test_search_entity_that_is_crossed_by_a_line(test_data, params, db_session): +def test_search_entity_that_is_crossed_by_a_line(test_data, params, db_session, mocker): from tests.test_data.wkt_data import crosses_historical_entity + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) params["geometry"] = [crosses_historical_entity] params["geometry_relation"] = GeometryRelation.crosses.name @@ -480,8 +594,12 @@ def test_search_entity_that_is_crossed_by_a_line(test_data, params, db_session): def test_search_geometry_entity_returns_entities_that_intersect_with_entity( - test_data, params, db_session + test_data, params, db_session, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) test_conservation_area = [ e for e in test_data["entities"] if e["dataset"] == "conservation-area" ][0] @@ -498,7 +616,11 @@ def test_search_geometry_entity_returns_entities_that_intersect_with_entity( assert e.entity in test_trees -def test_search_entity_by_curie(test_data, params, db_session): +def test_search_entity_by_curie(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) expected_entity = [ e for e in test_data["entities"] @@ -518,7 +640,11 @@ def test_search_entity_by_curie(test_data, params, db_session): assert entity.reference == expected_entity["reference"] -def test_search_entity_by_organisation_curie(test_data, params, db_session): +def test_search_entity_by_organisation_curie(test_data, params, db_session, mocker): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) expected_entity = [ e for e in test_data["entities"] diff --git a/tests/integration/test_search_invalid_entities.py b/tests/integration/test_search_invalid_entities.py index 8708b056..398949b2 100644 --- a/tests/integration/test_search_invalid_entities.py +++ b/tests/integration/test_search_invalid_entities.py @@ -69,8 +69,12 @@ def params(raw_params): def test_search_geometry_reference_excludes_invalid_data( - invalid_test_data, params, db_session + invalid_test_data, params, db_session, mocker ): + mocker.patch( + "application.data_access.entity_queries.get_cached_query_result", + return_value=None, + ) invalid = [ int(entity["entity"]) for entity in invalid_test_data["entities"]