tests for fts query syntax

This commit is contained in:
Kovid Goyal 2021-06-19 11:47:52 +05:30
parent 310a1a7d2e
commit e0dad27caa
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 30 additions and 3 deletions

View File

@ -88,7 +88,7 @@ populate_icu_string(const char *text, int text_sz, icu::UnicodeString &str, std:
} }
// }}} // }}}
static char ui_language[16] = {0}; static char ui_language[16] = {'e', 'n', 0};
static std::mutex global_mutex; static std::mutex global_mutex;
class IteratorDescription { class IteratorDescription {

View File

@ -18,9 +18,9 @@ def print(*args, **kwargs):
class TestConn(Connection): class TestConn(Connection):
def __init__(self, remove_diacritics=True): def __init__(self, remove_diacritics=True, language='en'):
from calibre_extensions.sqlite_extension import set_ui_language from calibre_extensions.sqlite_extension import set_ui_language
set_ui_language('en') set_ui_language(language)
super().__init__(':memory:') super().__init__(':memory:')
plugins.load_apsw_extension(self, 'sqlite_extension') plugins.load_apsw_extension(self, 'sqlite_extension')
options = [] options = []
@ -59,6 +59,14 @@ def tokenize(text, flags=None, remove_diacritics=True):
class FTSTest(BaseTest): class FTSTest(BaseTest):
ae = BaseTest.assertEqual ae = BaseTest.assertEqual
def setUp(self):
from calibre_extensions.sqlite_extension import set_ui_language
set_ui_language('en')
def tearDown(self):
from calibre_extensions.sqlite_extension import set_ui_language
set_ui_language('en')
def test_fts_tokenize(self): # {{{ def test_fts_tokenize(self): # {{{
def t(x, s, e, f=0): def t(x, s, e, f=0):
return {'text': x, 'start': s, 'end': e, 'flags': f} return {'text': x, 'start': s, 'end': e, 'flags': f}
@ -118,3 +126,22 @@ class FTSTest(BaseTest):
self.ae(conn.search('''"don't"'''), [("你>don't<叫mess",)]) self.ae(conn.search('''"don't"'''), [("你>don't<叫mess",)])
self.ae(conn.search(""), [(">你<don't叫mess",)]) self.ae(conn.search(""), [(">你<don't叫mess",)])
# }}} # }}}
def test_fts_query_syntax(self): # {{{
conn = TestConn()
conn.insert_text('one two three')
for q in ('"one two three"', 'one + two + three', '"one two" + three'):
self.ae(conn.search(q), [('>one two three<',)])
self.ae(conn.search('two'), [('one >two< three',)])
for q in ('"one two thr" *', 'one + two + thr*'):
self.ae(conn.search(q), [('>one two three<',)])
self.ae(conn.search('^one'), [('>one< two three',)])
self.ae(conn.search('^"one"'), [('>one< two three',)])
self.ae(conn.search('^two'), [])
conn = TestConn()
conn.insert_text('one two three four five six seven')
self.ae(conn.search('NEAR(one four)'), [('>one< two three >four<…',)])
self.ae(conn.search('NEAR("one two" "three four")'), [('>one two< >three four<…',)])
self.ae(conn.search('NEAR(one six, 2)'), [])
# }}}