|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +class BehaviorProduct < ActiveRecord::Base |
| 6 | + include ParadeDB::Model |
| 7 | + self.table_name = :products |
| 8 | + self.has_paradedb_index = true |
| 9 | +end |
| 10 | + |
| 11 | +class UserApiBehaviorIntegrationTest < Minitest::Test |
| 12 | + def setup |
| 13 | + skip "Behavior integration tests require PostgreSQL" unless postgresql? |
| 14 | + |
| 15 | + ensure_paradedb_setup! |
| 16 | + seed_products! |
| 17 | + end |
| 18 | + |
| 19 | + def test_matching_all_executes_and_returns_rows |
| 20 | + ids = BehaviorProduct.search(:description) |
| 21 | + .matching_all("running", "shoes") |
| 22 | + .order(:id) |
| 23 | + .pluck(:id) |
| 24 | + |
| 25 | + assert_equal [1, 2], ids |
| 26 | + end |
| 27 | + |
| 28 | + def test_matching_any_executes_and_returns_rows |
| 29 | + ids = BehaviorProduct.search(:description) |
| 30 | + .matching_any("wireless", "hiking") |
| 31 | + .order(:id) |
| 32 | + .pluck(:id) |
| 33 | + |
| 34 | + assert_equal [3, 5], ids |
| 35 | + end |
| 36 | + |
| 37 | + def test_phrase_near_and_phrase_prefix_execute |
| 38 | + phrase_ids = BehaviorProduct.search(:description).phrase("running shoes").order(:id).pluck(:id) |
| 39 | + near_ids = BehaviorProduct.search(:description).near("running", "shoes", distance: 1).order(:id).pluck(:id) |
| 40 | + prefix_ids = BehaviorProduct.search(:category).phrase_prefix("foot").order(:id).pluck(:id) |
| 41 | + |
| 42 | + assert_equal [1, 2], phrase_ids |
| 43 | + assert_equal [1, 2], near_ids |
| 44 | + assert_equal [1, 2, 5], prefix_ids |
| 45 | + end |
| 46 | + |
| 47 | + def test_term_regex_and_fuzzy_execute |
| 48 | + term_ids = BehaviorProduct.search(:category).term("audio").order(:id).pluck(:id) |
| 49 | + regex_ids = BehaviorProduct.search(:description).regex("run.*").order(:id).pluck(:id) |
| 50 | + fuzzy_ids = BehaviorProduct.search(:description).fuzzy("shose", distance: 2).order(:id).pluck(:id) |
| 51 | + |
| 52 | + assert_equal [3, 4], term_ids |
| 53 | + assert_equal [1, 2, 6], regex_ids |
| 54 | + assert_equal [1, 2], fuzzy_ids |
| 55 | + end |
| 56 | + |
| 57 | + def test_more_like_this_executes_and_returns_similar_rows |
| 58 | + ids = BehaviorProduct.more_like_this(3, fields: [:description]).limit(5).pluck(:id) |
| 59 | + |
| 60 | + assert_includes ids, 4 |
| 61 | + end |
| 62 | + |
| 63 | + def test_with_score_and_with_snippet_materialize_columns |
| 64 | + rows = BehaviorProduct.search(:description) |
| 65 | + .matching_all("running shoes") |
| 66 | + .with_score |
| 67 | + .with_snippet(:description, start_tag: "<b>", end_tag: "</b>", max_chars: 60) |
| 68 | + .order(:id) |
| 69 | + .to_a |
| 70 | + |
| 71 | + refute_empty rows |
| 72 | + rows.each do |row| |
| 73 | + refute_nil row.search_score |
| 74 | + refute_nil row.description_snippet |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def test_facets_and_with_facets_execute_and_parse_results |
| 79 | + facet_hash = BehaviorProduct.search(:description).matching_all("earbuds").facets(:rating) |
| 80 | + assert_kind_of Hash, facet_hash |
| 81 | + assert_includes facet_hash, "rating" |
| 82 | + assert_match(/[35]/, facet_hash["rating"].to_json) |
| 83 | + |
| 84 | + rel = BehaviorProduct.search(:description) |
| 85 | + .matching_all("running shoes") |
| 86 | + .with_facets(:rating, size: 10) |
| 87 | + .order(:id) |
| 88 | + .limit(10) |
| 89 | + rows = rel.to_a |
| 90 | + assert_equal [1, 2], rows.map(&:id) |
| 91 | + |
| 92 | + rel_facets = rel.facets |
| 93 | + assert_kind_of Hash, rel_facets |
| 94 | + assert_includes rel_facets, "rating" |
| 95 | + assert_match(/[45]/, rel_facets["rating"].to_json) |
| 96 | + end |
| 97 | + |
| 98 | + def test_with_facets_without_topn_shape_raises_friendly_error |
| 99 | + rel = BehaviorProduct.search(:description).matching_all("running shoes").with_facets(:rating, size: 10) |
| 100 | + error = assert_raises(ParadeDB::FacetQueryError) { rel.to_a } |
| 101 | + assert_includes error.message, "ORDER BY and LIMIT" |
| 102 | + end |
| 103 | + |
| 104 | + private |
| 105 | + |
| 106 | + def postgresql? |
| 107 | + ActiveRecord::Base.connection.adapter_name.downcase.include?("postgres") |
| 108 | + end |
| 109 | + |
| 110 | + def ensure_paradedb_setup! |
| 111 | + return if self.class.instance_variable_get(:@paradedb_setup_done) |
| 112 | + |
| 113 | + conn = ActiveRecord::Base.connection |
| 114 | + conn.execute("CREATE EXTENSION IF NOT EXISTS pg_search;") |
| 115 | + conn.execute("DROP INDEX IF EXISTS products_bm25_idx;") |
| 116 | + conn.execute(<<~SQL) |
| 117 | + CREATE INDEX products_bm25_idx ON products |
| 118 | + USING bm25 (id, description, category, rating, in_stock, price) |
| 119 | + WITH (key_field='id'); |
| 120 | + SQL |
| 121 | + |
| 122 | + self.class.instance_variable_set(:@paradedb_setup_done, true) |
| 123 | + end |
| 124 | + |
| 125 | + def seed_products! |
| 126 | + conn = ActiveRecord::Base.connection |
| 127 | + conn.execute("TRUNCATE TABLE products RESTART IDENTITY;") |
| 128 | + |
| 129 | + BehaviorProduct.create!(description: "running shoes lightweight", category: "footwear", rating: 5, in_stock: true, price: 120) |
| 130 | + BehaviorProduct.create!(description: "trail running shoes grip", category: "footwear", rating: 4, in_stock: true, price: 90) |
| 131 | + BehaviorProduct.create!(description: "wireless bluetooth earbuds", category: "audio", rating: 5, in_stock: true, price: 80) |
| 132 | + BehaviorProduct.create!(description: "budget wired earbuds", category: "audio", rating: 3, in_stock: false, price: 20) |
| 133 | + BehaviorProduct.create!(description: "hiking boots waterproof", category: "footwear", rating: 4, in_stock: true, price: 110) |
| 134 | + BehaviorProduct.create!(description: "running socks breathable", category: "apparel", rating: 2, in_stock: true, price: 15) |
| 135 | + end |
| 136 | +end |
0 commit comments