Added backend support to perform exact matches

This commit is contained in:
Donkie
2024-05-25 20:53:48 +02:00
parent 65bfba89cf
commit b00075ab87
5 changed files with 66 additions and 16 deletions

View File

@@ -175,6 +175,7 @@ async def find(
description=(
"Partial case-insensitive search term for the filament vendor name. "
"Separate multiple terms with a comma. Specify an empty string to match filaments with no vendor name. "
"Surround a term with quotes to search for the exact term."
),
),
vendor_id: Optional[str] = Query(
@@ -194,6 +195,7 @@ async def find(
description=(
"Partial case-insensitive search term for the filament name. Separate multiple terms with a comma. "
"Specify an empty string to match filaments with no name. "
"Surround a term with quotes to search for the exact term."
),
),
material: Optional[str] = Query(
@@ -202,6 +204,7 @@ async def find(
description=(
"Partial case-insensitive search term for the filament material. Separate multiple terms with a comma. "
"Specify an empty string to match filaments with no material. "
"Surround a term with quotes to search for the exact term."
),
),
article_number: Optional[str] = Query(
@@ -211,6 +214,7 @@ async def find(
"Partial case-insensitive search term for the filament article number. "
"Separate multiple terms with a comma. "
"Specify an empty string to match filaments with no article number. "
"Surround a term with quotes to search for the exact term."
),
),
color_hex: Optional[str] = Query(
@@ -232,6 +236,7 @@ async def find(
"Find filaments imported by the given external ID. "
"Separate multiple IDs with a comma. "
"Specify empty string to match filaments with no external ID. "
"Surround a term with quotes to search for the exact term."
),
example="polymaker_pla_polysonicblack_1000_175",
),

View File

@@ -172,6 +172,7 @@ async def find(
description=(
"Partial case-insensitive search term for the filament name. Separate multiple terms with a comma. "
"Specify an empty string to match spools with no filament name. "
"Surround a term with quotes to search for the exact term."
),
),
filament_id: Optional[str] = Query(
@@ -189,6 +190,7 @@ async def find(
description=(
"Partial case-insensitive search term for the filament material. Separate multiple terms with a comma. "
"Specify an empty string to match spools with no filament material. "
"Surround a term with quotes to search for the exact term."
),
),
filament_vendor_name: Optional[str] = Query(
@@ -198,6 +200,7 @@ async def find(
description=(
"Partial case-insensitive search term for the filament vendor name. Separate multiple terms with a comma. "
"Specify an empty string to match spools with no vendor name. "
"Surround a term with quotes to search for the exact term."
),
),
filament_vendor_id: Optional[str] = Query(
@@ -217,6 +220,7 @@ async def find(
description=(
"Partial case-insensitive search term for the spool location. Separate multiple terms with a comma. "
"Specify an empty string to match spools with no location. "
"Surround a term with quotes to search for the exact term."
),
),
lot_nr: Optional[str] = Query(
@@ -225,6 +229,7 @@ async def find(
description=(
"Partial case-insensitive search term for the spool lot number. Separate multiple terms with a comma. "
"Specify an empty string to match spools with no lot nr. "
"Surround a term with quotes to search for the exact term."
),
),
allow_archived: bool = Query(

View File

@@ -83,7 +83,10 @@ async def find(
name: Optional[str] = Query(
default=None,
title="Vendor Name",
description="Partial case-insensitive search term for the vendor name. Separate multiple terms with a comma.",
description=(
"Partial case-insensitive search term for the vendor name. Separate multiple terms with a comma. "
"Surround a term with quotes to search for the exact term."
),
),
external_id: Optional[str] = Query(
default=None,
@@ -92,6 +95,7 @@ async def find(
"Exact match for the vendor external ID. "
"Separate multiple IDs with a comma. "
"Specify empty string to match filaments with no external ID. "
"Surround a term with quotes to search for the exact term."
),
),
sort: Optional[str] = Query(

View File

@@ -47,9 +47,14 @@ def add_where_clause_str_opt(
if value is not None:
conditions = []
for value_part in value.split(","):
# If part is empty, search for empty fields
if len(value_part) == 0:
conditions.append(field.is_(None))
conditions.append(field == "")
# Do exact match if value_part is surrounded by quotes
elif value_part[0] == '"' and value_part[-1] == '"':
conditions.append(field == value_part[1:-1])
# Do fuzzy match if value_part is not surrounded by quotes
else:
conditions.append(field.ilike(f"%{value_part}%"))
@@ -66,8 +71,13 @@ def add_where_clause_str(
if value is not None:
conditions = []
for value_part in value.split(","):
# If part is empty, search for empty fields
if len(value_part) == 0:
conditions.append(field == "")
# Do exact match if value_part is surrounded by quotes
elif value_part[0] == '"' and value_part[-1] == '"':
conditions.append(field == value_part[1:-1])
# Do fuzzy match if value_part is not surrounded by quotes
else:
conditions.append(field.ilike(f"%{value_part}%"))

View File

@@ -61,7 +61,7 @@ def filaments(random_vendor_mod: dict[str, Any], random_empty_vendor_mod: dict[s
f"{URL}/api/v1/filament",
json={
"name": "Filament Z",
"material": "PETG",
"material": "PLA+",
"price": 200,
"density": 1.25,
"diameter": 1.75,
@@ -256,7 +256,20 @@ def test_find_filaments_by_name(filaments: Fixture):
# Execute
result = httpx.get(
f"{URL}/api/v1/filament",
params={"name": "Filament X"},
params={"name": "Filament"},
)
result.raise_for_status()
# Verify
filaments_result = result.json()
assert_lists_compatible(filaments_result, filaments.filaments[:3])
def test_find_filaments_by_exact_name(filaments: Fixture):
# Execute
result = httpx.get(
f"{URL}/api/v1/filament",
params={"name": '"Filament X"'},
)
result.raise_for_status()
@@ -301,7 +314,20 @@ def test_find_filaments_by_multiple_materials(filaments: Fixture):
# Verify
filaments_result = result.json()
assert_lists_compatible(filaments_result, filaments.filaments[:2])
assert_lists_compatible(filaments_result, filaments.filaments[:3])
def test_find_filaments_by_exact_material(filaments: Fixture):
# Execute
result = httpx.get(
f"{URL}/api/v1/filament",
params={"material": '"PLA"'},
)
result.raise_for_status()
# Verify
filaments_result = result.json()
assert_lists_compatible(filaments_result, filaments.filaments[:1])
def test_find_filaments_by_empty_material(filaments: Fixture):