Sha256: 8b645c236e7f4ede45eade31fce06546ea1de98b9d5925b49600b14b6adf3806

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'spec_helper'
require 'ostruct'

describe PgSearch::Features::Trigram do
  subject(:feature) { described_class.new(query, options, columns, Model, normalizer) }
  let(:query) { 'lolwut' }
  let(:options) { {} }
  let(:columns) {[
    PgSearch::Configuration::Column.new(:name, nil, Model),
    PgSearch::Configuration::Column.new(:content, nil, Model)
  ]}
  let(:normalizer) { PgSearch::Normalizer.new(config) }
  let(:config) { OpenStruct.new(:ignore => [], :postgresql_version => 90000) }

  let(:coalesced_columns) do
    <<-SQL.strip_heredoc.chomp
      coalesce(#{Model.quoted_table_name}."name"::text, '') || ' ' || coalesce(#{Model.quoted_table_name}."content"::text, '')
    SQL
  end

  with_model :Model do
    table do |t|
      t.string :name
      t.string :content
    end
  end

  describe 'conditions' do
    it 'escapes the search document and query' do
      config.ignore = []
      expect(feature.conditions.to_sql).to eq("((#{coalesced_columns}) % '#{query}')")
    end

    context 'ignoring accents' do
      it 'escapes the search document and query, but not the accent function' do
        config.ignore = [:accents]
        expect(feature.conditions.to_sql).to eq("((unaccent(#{coalesced_columns})) % unaccent('#{query}'))")
      end
    end

    context 'when a threshold is specified' do
      let(:options) do
        { threshold: 0.5 }
      end

      it 'uses a minimum similarity expression instead of the "%" operator' do
        expect(feature.conditions.to_sql).to eq(
          "(similarity((#{coalesced_columns}), '#{query}') >= 0.5)"
        )
      end
    end

  end

  describe '#rank' do
    it 'returns an expression using the similarity() function' do
      expect(feature.rank.to_sql).to eq("(similarity((#{coalesced_columns}), '#{query}'))")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pg_search-0.7.5 spec/lib/pg_search/features/trigram_spec.rb
pg_search-0.7.4 spec/lib/pg_search/features/trigram_spec.rb
pg_search-0.7.3 spec/lib/pg_search/features/trigram_spec.rb
pg_search-0.7.2 spec/lib/pg_search/features/trigram_spec.rb