Fixed combining empty and non-empty filter

Previously it would then act as a wildcard filter
This commit is contained in:
Donkie
2023-09-20 21:02:49 +02:00
parent f967aef2cb
commit 597dca7c28
2 changed files with 30 additions and 8 deletions

View File

@@ -45,10 +45,15 @@ def add_where_clause_str_opt(
) -> Select: ) -> Select:
"""Add a where clause to a select statement for an optional string field.""" """Add a where clause to a select statement for an optional string field."""
if value is not None: if value is not None:
if len(value) == 0: conditions = []
stmt = stmt.where(sqlalchemy.or_(field.is_(None), field == "")) for value_part in value.split(","):
else: if len(value_part) == 0:
stmt = stmt.where(sqlalchemy.or_(*(field.ilike(f"%{value}%") for value in value.split(",")))) conditions.append(field.is_(None))
conditions.append(field == "")
else:
conditions.append(field.ilike(f"%{value_part}%"))
stmt = stmt.where(sqlalchemy.or_(*conditions))
return stmt return stmt
@@ -59,10 +64,14 @@ def add_where_clause_str(
) -> Select: ) -> Select:
"""Add a where clause to a select statement for a string field.""" """Add a where clause to a select statement for a string field."""
if value is not None: if value is not None:
if len(value) == 0: conditions = []
stmt = stmt.where(field == "") for value_part in value.split(","):
else: if len(value_part) == 0:
stmt = stmt.where(sqlalchemy.or_(*(field.ilike(f"%{value}%") for value in value.split(",")))) conditions.append(field == "")
else:
conditions.append(field.ilike(f"%{value_part}%"))
stmt = stmt.where(sqlalchemy.or_(*conditions))
return stmt return stmt

View File

@@ -445,6 +445,19 @@ def test_find_spools_by_empty_location(spools: Fixture):
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4])) assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
def test_find_spools_by_empty_and_filled_location(spools: Fixture):
# Execute
result = httpx.get(
f"{URL}/api/v1/spool",
params={"location": "The Pantry,"},
)
result.raise_for_status()
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[3], spools.spools[4]))
def test_find_spools_by_lot_nr(spools: Fixture): def test_find_spools_by_lot_nr(spools: Fixture):
# Execute # Execute
result = httpx.get( result = httpx.get(