Skip to content

Commit

Permalink
ILIKE operator translation (#35)
Browse files Browse the repository at this point in the history
* feat: Translating ILIKE and NOT ILIKE operators to LIKE and NOT LIKE

* test: Testing ILIKE and NOT ILIKE translation
  • Loading branch information
aadel committed Aug 6, 2024
1 parent 96bd392 commit 1ee5d85
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/sqlalchemy_solr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ def visit_clauselist(self, clauselist, **kw):
def visit_array(self, element, **kw):
return f"ARRAY[{self.visit_clauselist(element, **kw)}]"

# Solr LIKE and NOT LIKE operators are case insensetive
def visit_ilike_op_binary(self, binary, operator, **kw):
return super().visit_like_op_binary(binary, operator, **kw)

def visit_not_ilike_op_binary(self, binary, operator, **kw):
return super().visit_not_like_op_binary(binary, operator, **kw)

def unescape_colon(self, s: str) -> str:
"""Unescape colon if present in datetime value"""
return s.replace(r"\:", ":")
Expand Down
39 changes: 39 additions & 0 deletions tests/test_operator_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from sqlalchemy import select
from tests.setup import prepare_orm


class TestOperatorTranslation:

def test_ilike_operator_translation(self, settings):
engine, t = prepare_orm(settings)

select_statement_1 = (
"SELECT sales_test_.`CITY_s` \nFROM sales_test_ "
"\nWHERE sales_test_.`CITY_s` LIKE ?\n LIMIT ?"
)

qry = (select(t.c.CITY_s).where(t.c.CITY_s.ilike("%york%"))).limit(
1
) # pylint: disable=unsubscriptable-object

with engine.connect() as connection:
result = connection.execute(qry)

assert result.context.statement == select_statement_1

def test_not_ilike_operator_translation(self, settings):
engine, t = prepare_orm(settings)

select_statement_1 = (
"SELECT sales_test_.`CITY_s` \nFROM sales_test_ "
"\nWHERE sales_test_.`CITY_s` NOT LIKE ?\n LIMIT ?"
)

qry = (select(t.c.CITY_s).where(t.c.CITY_s.notilike("%york%"))).limit(
1
) # pylint: disable=unsubscriptable-object

with engine.connect() as connection:
result = connection.execute(qry)

assert result.context.statement == select_statement_1

0 comments on commit 1ee5d85

Please sign in to comment.