diff --git a/api/search.py b/api/search.py index ab4c28c0f..9c587a016 100644 --- a/api/search.py +++ b/api/search.py @@ -164,7 +164,10 @@ def make_spring_response(thing: Thing) -> dict: def _get_asset_results(session: Session, q: str, limit: int) -> list[dict]: vector = Asset.search_vector query = search( - select(Asset).join(AssetThingAssociation).join(Thing), + select(Asset) + .join(AssetThingAssociation) + .join(Thing) + .options(selectinload(Asset.things)), q, vector=vector, limit=limit, @@ -176,7 +179,11 @@ def _get_asset_results(session: Session, q: str, limit: int) -> list[dict]: "label": a.name, "group": "Assets", "properties": { - "things": [t.name for t in a.things], + "id": a.id, + "things": [ + {"label": t.name, "id": t.id, "thing_type": t.thing_type} + for t in a.things + ], "storage_service": a.storage_service, "storage_path": a.storage_path, "mime_type": a.mime_type, diff --git a/tests/test_search.py b/tests/test_search.py index 137f82263..42d473cae 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -39,14 +39,16 @@ def override_dependencies_fixture(): app.dependency_overrides = {} -def test_search_api(water_well_thing, spring_thing, contact): +def test_search_api( + water_well_thing, spring_thing, contact, asset_with_associated_thing +): response = client.get("/search", params={"q": "Test"}) assert response.status_code == 200 data = response.json() assert isinstance(data, dict) items = data.get("items") assert isinstance(items, list) - assert len(items) == 3 + assert len(items) == 4 # Check the contacts returned contact_items = [item for item in items if item["group"] == "Contacts"] @@ -62,6 +64,20 @@ def test_search_api(water_well_thing, spring_thing, contact): }, ] + # Check the assets returned + asset_items = [item for item in items if item["group"] == "Assets"] + assert len(asset_items) == 1 + asset_item = asset_items[0] + assert asset_item["label"] == asset_with_associated_thing.name + assert asset_item["properties"]["id"] == asset_with_associated_thing.id + assert asset_item["properties"]["things"] == [ + { + "label": water_well_thing.name, + "id": water_well_thing.id, + "thing_type": water_well_thing.thing_type, + }, + ] + @pytest.mark.skip(reason="This test is not working .") def test_search_api2():