2011-03-30 20:31:39 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
describe Predicate do
|
|
|
|
|
|
|
|
before do
|
|
|
|
@s = Search.new(Person)
|
|
|
|
end
|
|
|
|
|
2013-05-23 09:10:05 -04:00
|
|
|
shared_examples 'wildcard escaping' do |method, regexp|
|
|
|
|
it 'automatically converts integers to strings' do
|
|
|
|
subject.parent_id_cont = 1
|
|
|
|
expect { subject.result }.to_not raise_error
|
|
|
|
end
|
2013-06-02 02:51:51 -04:00
|
|
|
it "escapes % and \\ in value" do
|
2013-05-23 09:10:05 -04:00
|
|
|
subject.send(:"#{method}=", '%._\\')
|
|
|
|
subject.result.to_sql.should match(regexp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-17 10:46:30 -04:00
|
|
|
describe 'eq' do
|
|
|
|
it 'generates an equality condition for boolean true' do
|
|
|
|
@s.awesome_eq = true
|
|
|
|
@s.result.to_sql.should match /"people"."awesome" = 't'/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates an equality condition for boolean false' do
|
|
|
|
@s.awesome_eq = false
|
|
|
|
@s.result.to_sql.should match /"people"."awesome" = 'f'/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not generate a condition for nil' do
|
|
|
|
@s.awesome_eq = nil
|
|
|
|
@s.result.to_sql.should_not match /WHERE/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
describe 'cont' do
|
2013-06-02 02:51:51 -04:00
|
|
|
it_has_behavior 'wildcard escaping', :name_cont, /"people"."name" LIKE '%\\%._\\\\%'/ do
|
2013-05-23 09:10:05 -04:00
|
|
|
subject { @s }
|
|
|
|
end
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
it 'generates a LIKE query with value surrounded by %' do
|
|
|
|
@s.name_cont = 'ric'
|
|
|
|
@s.result.to_sql.should match /"people"."name" LIKE '%ric%'/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'not_cont' do
|
2013-06-02 02:51:51 -04:00
|
|
|
it_has_behavior 'wildcard escaping', :name_not_cont, /"people"."name" NOT LIKE '%\\%._\\\\%'/ do
|
2013-05-23 09:10:05 -04:00
|
|
|
subject { @s }
|
|
|
|
end
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
it 'generates a NOT LIKE query with value surrounded by %' do
|
|
|
|
@s.name_not_cont = 'ric'
|
|
|
|
@s.result.to_sql.should match /"people"."name" NOT LIKE '%ric%'/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-15 04:06:19 -04:00
|
|
|
describe 'null' do
|
|
|
|
it 'generates a value IS NULL query' do
|
|
|
|
@s.name_null = true
|
|
|
|
@s.result.to_sql.should match /"people"."name" IS NULL/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'not_null' do
|
|
|
|
it 'generates a value IS NOT NULL query' do
|
|
|
|
@s.name_not_null = true
|
|
|
|
@s.result.to_sql.should match /"people"."name" IS NOT NULL/
|
|
|
|
end
|
|
|
|
end
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2011-07-15 04:06:19 -04:00
|
|
|
end
|