1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00
activerecord-hackery--ransack/spec/ransack/predicate_spec.rb
Washington Luiz 03978b5016 Revert dot scaping in SQL values
Which was added in #191 to fix a issue present in Rails < 4. However
that fix is no longer necessary since Rails 4 deals properly with dots
in SQL. See these for details:

  https://github.com/rails/rails/issues/9055
  https://github.com/rails/rails/commit/a2dab46

This would be a query generated in Rails 3.2.13

  "SELECT \"spree_users\".* FROM \"spree_users\"
    WHERE (\"spree_users\".\"email\" LIKE 'spree_commerce@example.com%')"

While on Rails 4.0.0.rc1 it would be generated like this:

  "SELECT \"spree_users\".* FROM \"spree_users\"
    WHERE (\"spree_users\".\"email\" LIKE 'spree_commerce@example\\.com%')"

and so return unexpected results
2013-06-02 03:51:51 -03:00

74 lines
2 KiB
Ruby

require 'spec_helper'
module Ransack
describe Predicate do
before do
@s = Search.new(Person)
end
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
it "escapes % and \\ in value" do
subject.send(:"#{method}=", '%._\\')
subject.result.to_sql.should match(regexp)
end
end
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
describe 'cont' do
it_has_behavior 'wildcard escaping', :name_cont, /"people"."name" LIKE '%\\%._\\\\%'/ do
subject { @s }
end
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
it_has_behavior 'wildcard escaping', :name_not_cont, /"people"."name" NOT LIKE '%\\%._\\\\%'/ do
subject { @s }
end
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
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
end
end