Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sql): allow func calls in rhs of contains #49

Merged
merged 1 commit into from
Nov 15, 2023
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
2 changes: 1 addition & 1 deletion odata_query/sql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _to_pattern(self, arg: ast._Node, prefix: str = "", suffix: str = "") -> str
Transform a node into a pattern usable in `LIKE` clauses.
:meta private:
"""
if isinstance(arg, ast.Identifier):
if isinstance(arg, (ast.Identifier, ast.Call)):
res = self.visit(arg)
if prefix:
res = f"'{prefix}' || " + res
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/django/test_odata_to_django_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ def tz(offset: int) -> dt.tzinfo:
"contains(title, 'TEST') eq false",
Q(contains_title_test__exact=Value(False)),
),
# GITHUB-47
(
"contains(tolower(name), tolower('A'))",
Q(name__lower__contains=fn.Lower(Value("A"))),
),
],
)
def test_odata_filter_to_django_q_pre_v4(
Expand Down Expand Up @@ -565,6 +570,11 @@ def test_odata_filter_to_django_q_pre_v4(
"contains(title, 'TEST') eq false",
Q(lu.Exact(lu.Contains(F("title"), Value("TEST")), Value(False))),
),
# GITHUB-47
(
"contains(tolower(name), tolower('A'))",
Q(lu.Contains(fn.Lower(F("name")), fn.Lower(Value("A")))),
),
],
)
def test_odata_filter_to_django_q_after_v4(
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/sql/test_odata_to_athena_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
"measurement_class eq 'C' and endswith(data_collector, 'rie')",
"\"measurement_class\" = 'C' AND \"data_collector\" LIKE '%rie'",
),
# GITHUB-47
(
"contains(tolower(name), tolower('A'))",
"LOWER(\"name\") LIKE '%' || LOWER('A') || '%'",
),
],
)
def test_odata_filter_to_sql(odata_query: str, expected: str, lexer, parser):
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/sql/test_odata_to_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
"measurement_class eq 'C' and endswith(data_collector, 'rie')",
"\"measurement_class\" = 'C' AND \"data_collector\" LIKE '%rie'",
),
# GITHUB-47
(
"contains(tolower(name), tolower('A'))",
"LOWER(\"name\") LIKE '%' || LOWER('A') || '%'",
),
],
)
def test_odata_filter_to_sql(
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/sql/test_odata_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
"measurement_class eq 'C' and endswith(data_collector, 'rie')",
"\"measurement_class\" = 'C' AND \"data_collector\" LIKE '%rie'",
),
# GITHUB-47
(
"contains(tolower(name), tolower('A'))",
"LOWER(\"name\") LIKE '%' || LOWER('A') || '%'",
),
],
)
def test_odata_filter_to_sql(odata_query: str, expected: str, lexer, parser):
Expand Down
1 change: 1 addition & 0 deletions tests/integration/sql/test_querying.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def sample_data_sess(db_conn, db_schema):
("blogpost", "year(published_at) eq 2019", 1),
# GITHUB-19
("blogpost", "contains(title, 'Query') eq true", 1),
("blogpost", "contains(tolower(title), tolower('Query'))", 1),
],
)
def test_query_with_odata(
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/sqlalchemy/test_odata_to_sqlalchemy_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ def tz(offset: int) -> dt.tzinfo:
"contains(title, 'TEST') eq true",
BlogPost.c.title.contains("TEST") == True, # noqa:E712
),
# GITHUB-47
(
"contains(tolower(title), tolower('A'))",
functions_ext.lower(BlogPost.c.title).contains(functions_ext.lower("A")),
),
],
)
def test_odata_filter_to_sqlalchemy_query(
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/sqlalchemy/test_odata_to_sqlalchemy_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ def tz(offset: int) -> dt.tzinfo:
"contains(title, 'TEST') eq true",
BlogPost.title.contains("TEST") == True, # noqa:E712
),
# GITHUB-47
(
"contains(tolower(title), tolower('A'))",
functions_ext.lower(BlogPost.title).contains(functions_ext.lower("A")),
),
],
)
def test_odata_filter_to_sqlalchemy_query(
Expand Down