-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
cql2_like_to_es()
understand escaped backslashes (#286)
**Related Issue(s):** - Closes #285 **Description:** This is a break/fix PR. The first commit adds a suite of tests that document correct LIKE-to-wildcard query value conversions, and then fixes the `cql2_like_to_es()` code to correctly process escaped backslashes. **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [x] Tests pass (run `make test`) _(`make test` fails because the Docker container port ranges don't match, but CI, which doesn't use `make test`, passes)_ - [ ] Documentation has been updated to reflect changes, if applicable _n/a, no docs in repo_ - [x] Changes are added to the changelog
- Loading branch information
Showing
3 changed files
with
76 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
|
||
from stac_fastapi.core.extensions.filter import cql2_like_to_es | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"cql2_value, expected_es_value", | ||
( | ||
# no-op | ||
("", ""), | ||
# backslash | ||
("\\\\", "\\"), | ||
# percent | ||
("%", "*"), | ||
(r"\%", "%"), | ||
(r"\\%", r"\*"), | ||
(r"\\\%", r"\%"), | ||
# underscore | ||
("_", "?"), | ||
(r"\_", "_"), | ||
(r"\\_", r"\?"), | ||
(r"\\\_", r"\_"), | ||
), | ||
) | ||
def test_cql2_like_to_es_success(cql2_value: str, expected_es_value: str) -> None: | ||
"""Verify CQL2 LIKE query strings are converted correctly.""" | ||
|
||
assert cql2_like_to_es(cql2_value) == expected_es_value | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"cql2_value", | ||
( | ||
pytest.param("\\", id="trailing backslash escape"), | ||
pytest.param("\\1", id="invalid escape sequence"), | ||
), | ||
) | ||
def test_cql2_like_to_es_invalid(cql2_value: str) -> None: | ||
"""Verify that incomplete or invalid escape sequences are rejected. | ||
CQL2 currently doesn't appear to define how to handle invalid escape sequences. | ||
This test assumes that undefined behavior is caught. | ||
""" | ||
|
||
with pytest.raises(ValueError): | ||
cql2_like_to_es(cql2_value) |