Sha256: c4365a52937830193a593557eb2cb7c818d01f0a0fbef9c2b133c3cfdacb22ea

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe PgSearch::Multisearch do
  with_table "pg_search_documents", {}, &DOCUMENTS_SCHEMA

  with_model :MultisearchableModel do
    table do |t|
      t.string :title
      t.text :content
      t.timestamps
    end
    model do
      include PgSearch
    end
  end

  describe ".rebuild" do
    it "should fetch the proper columns from the model" do
    end
  end

  describe ".rebuild_sql" do
    context "with one attribute" do
      it "should generate the proper SQL code" do
        model = MultisearchableModel
        connection = model.connection

        model.multisearchable :against => :title

        expected_sql = <<-SQL
INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content)
  SELECT #{connection.quote(model.name)} AS searchable_type,
         #{model.quoted_table_name}.id AS searchable_id,
         (
           coalesce(#{model.quoted_table_name}.title, '')
         ) AS content
  FROM #{model.quoted_table_name}
SQL

        PgSearch::Multisearch.rebuild_sql(MultisearchableModel).should == expected_sql
      end
    end

    context "with multiple attributes" do
      it "should generate the proper SQL code" do
        model = MultisearchableModel
        connection = model.connection

        model.multisearchable :against => [:title, :content]

        expected_sql = <<-SQL
INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content)
  SELECT #{connection.quote(model.name)} AS searchable_type,
         #{model.quoted_table_name}.id AS searchable_id,
         (
           coalesce(#{model.quoted_table_name}.title, '') || ' ' || coalesce(#{model.quoted_table_name}.content, '')
         ) AS content
  FROM #{model.quoted_table_name}
SQL

        PgSearch::Multisearch.rebuild_sql(MultisearchableModel).should == expected_sql
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pg_search-0.4.1 spec/pg_search/multisearch_spec.rb
pg_search-0.4 spec/pg_search/multisearch_spec.rb
pg_search-0.3.4 spec/pg_search/multisearch_spec.rb
pg_search-0.3.3 spec/pg_search/multisearch_spec.rb
pg_search-0.3.2 spec/pg_search/multisearch_spec.rb
pg_search-0.3.1 spec/pg_search/multisearch_spec.rb
pg_search-0.3 spec/pg_search/multisearch_spec.rb