mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-14 20:44:11 +02:00
where= and where_args= parameters to search() and search_sql()
Closes #441
This commit is contained in:
parent
0b6aba696d
commit
1b09538bc6
3 changed files with 104 additions and 4 deletions
|
|
@ -94,6 +94,38 @@ def test_search_limit_offset(fresh_db):
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fts_version", ("FTS4", "FTS5"))
|
||||
def test_search_where(fresh_db, fts_version):
|
||||
table = fresh_db["t"]
|
||||
table.insert_all(search_records)
|
||||
table.enable_fts(["text", "country"], fts_version=fts_version)
|
||||
results = list(
|
||||
table.search("are", where="country = :country", where_args={"country": "Japan"})
|
||||
)
|
||||
assert results == [
|
||||
{
|
||||
"rowid": 1,
|
||||
"text": "tanuki are running tricksters",
|
||||
"country": "Japan",
|
||||
"not_searchable": "foo",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_search_where_args_disallows_query(fresh_db):
|
||||
table = fresh_db["t"]
|
||||
with pytest.raises(ValueError) as ex:
|
||||
list(
|
||||
table.search(
|
||||
"x", where="author = :query", where_args={"query": "not allowed"}
|
||||
)
|
||||
)
|
||||
assert (
|
||||
ex.value.args[0]
|
||||
== "'query' is a reserved key and cannot be passed to where_args for .search()"
|
||||
)
|
||||
|
||||
|
||||
def test_enable_fts_table_names_containing_spaces(fresh_db):
|
||||
table = fresh_db["test"]
|
||||
table.insert({"column with spaces": "in its name"})
|
||||
|
|
@ -415,6 +447,28 @@ def test_enable_fts_error_message_on_views():
|
|||
"limit 10"
|
||||
),
|
||||
),
|
||||
(
|
||||
{"where": "author = :author"},
|
||||
"FTS5",
|
||||
(
|
||||
"with original as (\n"
|
||||
" select\n"
|
||||
" rowid,\n"
|
||||
" *\n"
|
||||
" from [books]\n"
|
||||
" where author = :author\n"
|
||||
")\n"
|
||||
"select\n"
|
||||
" [original].*\n"
|
||||
"from\n"
|
||||
" [original]\n"
|
||||
" join [books_fts] on [original].rowid = [books_fts].rowid\n"
|
||||
"where\n"
|
||||
" [books_fts] match :query\n"
|
||||
"order by\n"
|
||||
" [books_fts].rank"
|
||||
),
|
||||
),
|
||||
(
|
||||
{"columns": ["title"]},
|
||||
"FTS4",
|
||||
|
|
@ -480,6 +534,28 @@ def test_enable_fts_error_message_on_views():
|
|||
"limit 2"
|
||||
),
|
||||
),
|
||||
(
|
||||
{"where": "author = :author"},
|
||||
"FTS4",
|
||||
(
|
||||
"with original as (\n"
|
||||
" select\n"
|
||||
" rowid,\n"
|
||||
" *\n"
|
||||
" from [books]\n"
|
||||
" where author = :author\n"
|
||||
")\n"
|
||||
"select\n"
|
||||
" [original].*\n"
|
||||
"from\n"
|
||||
" [original]\n"
|
||||
" join [books_fts] on [original].rowid = [books_fts].rowid\n"
|
||||
"where\n"
|
||||
" [books_fts] match :query\n"
|
||||
"order by\n"
|
||||
" rank_bm25(matchinfo([books_fts], 'pcnalx'))"
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_search_sql(kwargs, fts, expected):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue