Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
20 changes: 18 additions & 2 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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():
Expand Down
Loading