From 7e8c9d349b7e34efbc4e989f5ffe9c028770fee8 Mon Sep 17 00:00:00 2001 From: robbertuittenbroek Date: Thu, 15 Aug 2024 13:18:42 +0200 Subject: [PATCH] Fix case-insensitive search --- amt/repositories/projects.py | 4 ++-- tests/repositories/test_projects.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/amt/repositories/projects.py b/amt/repositories/projects.py index b555be07a..33cf3053d 100644 --- a/amt/repositories/projects.py +++ b/amt/repositories/projects.py @@ -60,9 +60,9 @@ def paginate(self, skip: int, limit: int, search: str) -> list[Project]: if search != "": search_wildcards = f"%{escape_like(search)}%" if get_settings().APP_DATABASE_SCHEME == "postgresql": - statement = statement.filter(Project.name.like(search_wildcards) | Project.name.match(search)) + statement = statement.filter(Project.name.ilike(search_wildcards) | Project.name.match(search)) else: - statement = statement.filter(Project.name.like(search_wildcards)) + statement = statement.filter(Project.name.ilike(search_wildcards)) statement = statement.order_by(func.lower(Project.name)).offset(skip).limit(limit) return list(self.session.execute(statement).scalars()) except Exception as e: diff --git a/tests/repositories/test_projects.py b/tests/repositories/test_projects.py index 878eb6f45..4ccfd39ac 100644 --- a/tests/repositories/test_projects.py +++ b/tests/repositories/test_projects.py @@ -154,6 +154,12 @@ def test_search_multiple(db: DatabaseTestUtils): assert result[1].name == "aba" +def test_search_no_results(db: DatabaseTestUtils): + project_repository = ProjectsRepository(db.get_session()) + result = project_repository.paginate(skip=0, limit=4, search="A") + assert len(result) == 0 + + def test_raises_exception(db: DatabaseTestUtils): db.given([default_project()]) project_repository = ProjectsRepository(db.get_session())