3535)
3636
3737
38+ def _sql (sql ) -> str :
39+ return "\n " .join (line .rstrip () for line in str (sql ).split ("\n " )).strip ()
40+
41+
3842def test_tokenizer_renderers_cover_public_wrappers ():
3943 assert tokenize .unicode (alias = "description_unicode" , lowercase = True , stemmer = "english" ).render () == (
4044 "pdb.unicode_words('alias=description_unicode,lowercase=true,stemmer=english')"
@@ -82,12 +86,11 @@ def test_bm25_index_compile_with_tokenizers():
8286 postgresql_with = {"key_field" : "id" },
8387 )
8488
85- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
86-
87- assert "USING bm25" in sql
88- assert "((description)::pdb.unicode_words('lowercase=true,stemmer=english'))" in sql
89- assert "((category)::pdb.literal_normalized('alias=category_exact'))" in sql
90- assert "key_field = id" in sql
89+ assert (
90+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
91+ == """\
92+ CREATE INDEX products_bm25_idx ON products USING bm25 (id, ((description)::pdb.unicode_words('lowercase=true,stemmer=english')), ((category)::pdb.literal_normalized('alias=category_exact'))) WITH (key_field = id)"""
93+ )
9194
9295
9396def test_bm25_index_compile_unicode_omits_none_options ():
@@ -99,10 +102,11 @@ def test_bm25_index_compile_unicode_omits_none_options():
99102 postgresql_with = {"key_field" : "id" },
100103 )
101104
102- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
103-
104- assert "((description)::pdb.unicode_words('lowercase=true'))" in sql
105- assert "stemmer=null" not in sql
105+ assert (
106+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
107+ == """\
108+ CREATE INDEX products_bm25_idx ON products USING bm25 (id, ((description)::pdb.unicode_words('lowercase=true'))) WITH (key_field = id)"""
109+ )
106110
107111
108112def test_bm25_index_compile_with_structured_tokenizer_config ():
@@ -123,9 +127,11 @@ def test_bm25_index_compile_with_structured_tokenizer_config():
123127 postgresql_using = "bm25" ,
124128 postgresql_with = {"key_field" : "id" },
125129 )
126- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
127-
128- assert "((description)::pdb.simple('alias=description_simple,lowercase=true,stemmer=english'))" in sql
130+ assert (
131+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
132+ == """\
133+ CREATE INDEX products_bm25_structured_idx ON products USING bm25 (id, ((description)::pdb.simple('alias=description_simple,lowercase=true,stemmer=english'))) WITH (key_field = id)"""
134+ )
129135
130136
131137def test_bm25_index_compile_with_tokenizer_positional_and_named_args ():
@@ -146,9 +152,11 @@ def test_bm25_index_compile_with_tokenizer_positional_and_named_args():
146152 postgresql_using = "bm25" ,
147153 postgresql_with = {"key_field" : "id" },
148154 )
149- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
150-
151- assert "((description)::pdb.ngram(3,8,'alias=description_ngram,prefix_only=true,positions=true'))" in sql
155+ assert (
156+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
157+ == """\
158+ CREATE INDEX products_bm25_ngram_idx ON products USING bm25 (id, ((description)::pdb.ngram(3,8,'alias=description_ngram,prefix_only=true,positions=true'))) WITH (key_field = id)"""
159+ )
152160
153161
154162def test_tokenizer_from_config_rejects_non_identifier_tokenizer_name ():
@@ -169,9 +177,11 @@ def test_bm25_index_compile_lindera_wrapper():
169177 postgresql_using = "bm25" ,
170178 postgresql_with = {"key_field" : "id" },
171179 )
172- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
173-
174- assert "((description)::pdb.lindera('japanese','alias=description_jp'))" in sql
180+ assert (
181+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
182+ == """\
183+ CREATE INDEX products_bm25_lindera_idx ON products USING bm25 (id, ((description)::pdb.lindera('japanese','alias=description_jp'))) WITH (key_field = id)"""
184+ )
175185
176186
177187def test_bm25_index_compile_regex_pattern_wrapper ():
@@ -182,9 +192,11 @@ def test_bm25_index_compile_regex_pattern_wrapper():
182192 postgresql_using = "bm25" ,
183193 postgresql_with = {"key_field" : "id" },
184194 )
185- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
186-
187- assert "((description)::pdb.regex_pattern('(?i)\\ \\ bh\\ \\ w*','alias=description_regex'))" in sql
195+ assert (
196+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
197+ == """\
198+ CREATE INDEX products_bm25_regex_idx ON products USING bm25 (id, ((description)::pdb.regex_pattern('(?i)\\ \\ bh\\ \\ w*','alias=description_regex'))) WITH (key_field = id)"""
199+ )
188200
189201
190202def test_bm25_index_compile_json_key_with_tokenizer ():
@@ -198,9 +210,11 @@ def test_bm25_index_compile_json_key_with_tokenizer():
198210 postgresql_using = "bm25" ,
199211 postgresql_with = {"key_field" : "id" },
200212 )
201- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
202-
203- assert "((metadata ->> 'color')::pdb.literal('alias=metadata_color'))" in sql
213+ assert (
214+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
215+ == """\
216+ CREATE INDEX products_bm25_json_idx ON products USING bm25 (id, ((metadata ->> 'color')::pdb.literal('alias=metadata_color'))) WITH (key_field = id)"""
217+ )
204218
205219
206220def test_bm25_index_compile_multiple_json_keys ():
@@ -218,10 +232,11 @@ def test_bm25_index_compile_multiple_json_keys():
218232 postgresql_using = "bm25" ,
219233 postgresql_with = {"key_field" : "id" },
220234 )
221- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
222-
223- assert "alias=metadata_color" in sql
224- assert "alias=metadata_location" in sql
235+ assert (
236+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
237+ == """\
238+ CREATE INDEX products_bm25_json_multi_idx ON products USING bm25 (id, ((metadata ->> 'color')::pdb.literal('alias=metadata_color')), ((metadata ->> 'location')::pdb.literal('alias=metadata_location'))) WITH (key_field = id)"""
239+ )
225240
226241
227242def test_bm25_index_compile_non_text_expression_with_pdb_alias ():
@@ -234,9 +249,11 @@ def test_bm25_index_compile_non_text_expression_with_pdb_alias():
234249 postgresql_with = {"key_field" : "id" },
235250 )
236251
237- sql = str (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
238-
239- assert "((id + 1)::pdb.alias('next_id'))" in sql
252+ assert (
253+ _sql (CreateIndex (idx ).compile (dialect = postgresql .dialect (), compile_kwargs = {"literal_binds" : True }))
254+ == """\
255+ CREATE INDEX products_bm25_expr_idx ON products USING bm25 (id, description, ((id + 1)::pdb.alias('next_id'))) WITH (key_field = id)"""
256+ )
240257
241258
242259def test_bm25_field_non_postgres_compile_raises ():
0 commit comments