Skip to content

Commit 3c53c6c

Browse files
committed
Assert against full generated SQL instead of snippets
1 parent 91855d4 commit 3c53c6c

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

spec/index_migration_integration_spec.rb

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ class IndexMigrationBookByNameIndex < ParadeDB::Index
224224
after_indexdef = indexdef_for("books_bm25_idx")
225225

226226
refute_equal before_indexdef, after_indexdef
227-
assert_includes after_indexdef, "author"
227+
assert_sql_equal <<~SQL, after_indexdef
228+
CREATE INDEX books_bm25_idx ON public.books
229+
USING bm25 (id, ((title)::pdb.simple), ((author)::pdb.literal))
230+
WITH (key_field=id)
231+
SQL
228232
end
229233

230234
it "supports reindex_bm25 and guards concurrent reindex in a transaction" do
@@ -269,10 +273,15 @@ class IndexMigrationBookByNameIndex < ParadeDB::Index
269273
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, stream)
270274
schema = stream.string
271275

272-
assert_includes schema, "add_bm25_index"
273-
assert_includes schema, ":books"
274-
assert_includes schema, "title_simple"
275-
assert_includes schema, "target_segment_count"
276+
add_stmt = schema.each_line.find do |line|
277+
line.include?("add_bm25_index :books") &&
278+
line.include?("title_simple") &&
279+
line.include?("target_segment_count")
280+
end
281+
282+
assert_equal <<~RUBY.strip, add_stmt.to_s.strip
283+
add_bm25_index :books, fields: { id: {}, title: { tokenizer: :simple, alias: "title_simple" } }, key_field: :id, name: "books_bm25_idx", index_options: { :target_segment_count => 17 }
284+
RUBY
276285
expect(schema).not_to match(/add_index.*books_bm25_idx/)
277286
expect(schema).not_to match(/t\.index.*books_bm25_idx/)
278287
end
@@ -296,8 +305,11 @@ class IndexMigrationBookByNameIndex < ParadeDB::Index
296305
conn.create_paradedb_index(expression_index)
297306
indexdef = indexdef_for("books_bm25_idx")
298307

299-
assert_includes indexdef, "metadata"
300-
assert_includes indexdef, "pdb.simple"
308+
assert_sql_equal <<~SQL, indexdef
309+
CREATE INDEX books_bm25_idx ON public.books
310+
USING bm25 (id, (((metadata ->> 'title'::text))::pdb.simple('alias=metadata_title')))
311+
WITH (key_field=id)
312+
SQL
301313
end
302314

303315
it "allows aliased computed numeric expressions without requiring a tokenizer" do
@@ -323,7 +335,11 @@ class IndexMigrationBookByNameIndex < ParadeDB::Index
323335
)
324336
end.not_to raise_error
325337

326-
assert_includes indexdef_for("search_idx"), "::pdb.alias('rating')"
338+
assert_sql_equal <<~SQL, indexdef_for("search_idx")
339+
CREATE INDEX search_idx ON public.mock_items
340+
USING bm25 (id, description, (((rating + 1))::pdb.alias('rating')))
341+
WITH (key_field=id)
342+
SQL
327343
assert index_exists?("search_idx")
328344
ensure
329345
conn.execute("DROP INDEX IF EXISTS search_idx;") rescue nil
@@ -359,7 +375,9 @@ class IndexMigrationBookByNameIndex < ParadeDB::Index
359375
line.include?("target_segment_count")
360376
end
361377

362-
refute_nil add_stmt
378+
assert_equal <<~RUBY.strip, add_stmt.to_s.strip
379+
add_bm25_index :books, fields: { id: {}, title: { tokenizers: [{ tokenizer: :literal }, { tokenizer: :simple, alias: "title_simple" }] } }, key_field: :id, name: "books_bm25_idx", index_options: { :target_segment_count => 17 }
380+
RUBY
363381

364382
conn.remove_bm25_index(:books, if_exists: true)
365383
expect { conn.instance_eval(add_stmt.strip) }.not_to raise_error
@@ -391,10 +409,9 @@ class IndexMigrationBookByNameIndex < ParadeDB::Index
391409
line.include?("add_bm25_index :books") && line.include?("metadata_title_text")
392410
end
393411

394-
refute_nil add_stmt
395-
assert_includes add_stmt, "metadata"
396-
assert_includes add_stmt, "title"
397-
assert_includes add_stmt, "::text"
412+
assert_equal <<~RUBY.strip, add_stmt.to_s.strip
413+
add_bm25_index :books, fields: { id: {}, "metadata ->> 'title'::text" => { tokenizer: :simple, alias: "metadata_title_text", named_args: { :lowercase => true } } }, key_field: :id, name: "books_bm25_idx"
414+
RUBY
398415

399416
conn.remove_bm25_index(:books, if_exists: true)
400417
expect { conn.instance_eval(add_stmt.strip) }.not_to raise_error

0 commit comments

Comments
 (0)