Skip to content

Commit

Permalink
fix: remove " from search to avoid SQL escaping errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vjousse committed Jan 20, 2025
1 parent 733aff4 commit 74ca332
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bw2data/search/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def escape_search_for_fts5(string: str) -> str:
return " ".join(
[
f'"{term[:-1]}"*' if term.endswith("*") else f'"{term}"'
for term in string.split()
for term in string.replace('"', "").split()
]
)

Expand Down
40 changes: 34 additions & 6 deletions tests/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,31 @@ def test_escape_search():
== '"Comte"* "cheese" "from" "cow\'s"'
)

assert (
IndexManager.escape_search_for_fts5('Comte* "cheese" from cow\'s')
== '"Comte"* "cheese" "from" "cow\'s"'
)

assert '"Comte"*' == IndexManager.escape_search_for_fts5("Comte*")


@bw2test
def test_search_with_substrings():
"""Test that searching with ' works correctly"""
im = IndexManager("foo")
im.add_dataset(
{
"database": "foo",
"code": "bar",
"name": "Comte cheese, from cow's milk, consumption mix: {FR} U & (test)",
}
im.add_datasets(
[
{
"database": "foo",
"code": "bar",
"name": "Comte cheese, from cow's milk, consumption mix: {FR} U & (test)",
},
{
"database": "foo",
"code": "baz",
"name": 'meat without bone chicken for direct consumption "fr-organic"',
},
]
)
with Searcher("foo") as s:
assert s.search("Comte cheese from cow's", proxy=False) == [
Expand Down Expand Up @@ -462,3 +474,19 @@ def test_search_with_substrings():
]

assert s.search("Com cheese cow's", proxy=False) == []

assert s.search(
'meat without bone chicken for direct consumption "fr-organic"',
proxy=False,
) == [
{
"comment": "",
"product": "",
"name": 'meat without bone chicken for direct consumption "fr-organic"',
"database": "foo",
"location": "",
"code": "baz",
"categories": "",
"synonyms": "",
}
]

0 comments on commit 74ca332

Please sign in to comment.