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 sanitize_params to correctly resolve nested columns #222

Merged
merged 4 commits into from
Feb 25, 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 postgrest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def aclose(self) -> None:

def sanitize_param(param: Any) -> str:
param_str = str(param)
reserved_chars = ".,:()"
reserved_chars = ",:()"
if any(char in param_str for char in reserved_chars):
return f'"{param_str}"'
return param_str
Expand Down
21 changes: 21 additions & 0 deletions tests/_async/test_filter_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ def test_filter(filter_request_builder):
assert builder.params['":col.name"'] == "eq.val"


@pytest.mark.parametrize(
"col_name, expected_query_prefix",
[
("col:name", "%22col%3Aname%22"),
("col.name", "col.name"),
],
)
def test_filter_special_characters(
filter_request_builder, col_name, expected_query_prefix
):
builder = filter_request_builder.filter(col_name, "eq", "val")

assert str(builder.params) == expected_query_prefix + "=eq.val"


def test_multivalued_param(filter_request_builder):
builder = filter_request_builder.lte("x", "a").gte("x", "b")

Expand All @@ -47,6 +62,12 @@ def test_match(filter_request_builder):
assert str(builder.params) == "id=eq.1&done=eq.false"


def test_equals(filter_request_builder):
builder = filter_request_builder.eq("x", "a")

assert str(builder.params) == "x=eq.a"


def test_contains(filter_request_builder):
builder = filter_request_builder.contains("x", "a")

Expand Down
21 changes: 21 additions & 0 deletions tests/_sync/test_filter_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ def test_filter(filter_request_builder):
assert builder.params['":col.name"'] == "eq.val"


@pytest.mark.parametrize(
"col_name, expected_query_prefix",
[
("col:name", "%22col%3Aname%22"),
("col.name", "col.name"),
],
)
def test_filter_special_characters(
filter_request_builder, col_name, expected_query_prefix
):
builder = filter_request_builder.filter(col_name, "eq", "val")

assert str(builder.params) == expected_query_prefix + "=eq.val"


def test_multivalued_param(filter_request_builder):
builder = filter_request_builder.lte("x", "a").gte("x", "b")

Expand All @@ -47,6 +62,12 @@ def test_match(filter_request_builder):
assert str(builder.params) == "id=eq.1&done=eq.false"


def test_equals(filter_request_builder):
builder = filter_request_builder.eq("x", "a")

assert str(builder.params) == "x=eq.a"


def test_contains(filter_request_builder):
builder = filter_request_builder.contains("x", "a")

Expand Down
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from postgrest.utils import sanitize_param


@pytest.mark.parametrize(
"value, expected",
[
("param,name", '"param,name"'),
("param:name", '"param:name"'),
("param(name", '"param(name"'),
("param)name", '"param)name"'),
("param,name", '"param,name"'),
("table.column", "table.column"),
("table_column", "table_column"),
],
)
def test_sanitize_params(value, expected):
assert sanitize_param(value) == expected