Fix tests for MySQL

This commit is contained in:
Ryan Bigg 2013-11-06 17:43:23 +11:00
parent 5429a1420a
commit b2149a1e13
4 changed files with 30 additions and 18 deletions

View File

@ -27,7 +27,7 @@ module Ransack
when "SQLite"
unescaped
else
# Necessary for PostgreSQL
# Necessary for PostgreSQL and MySQL
unescaped.to_s.gsub(/([\\|\%|.])/, '\\\\\\1')
end
end

View File

@ -18,7 +18,7 @@ module Ransack
result = subject.evaluate(search)
result.should be_an ::ActiveRecord::Relation
result.to_sql.should match /"name" = 'Joe Blow'/
result.to_sql.should match /#{quote_column_name("name")} = 'Joe Blow'/
end
it 'SELECTs DISTINCT when :distinct => true' do

View File

@ -22,12 +22,14 @@ module Ransack
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'/
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
@s.result.to_sql.should match /#{field} = #{ActiveRecord::Base.connection.quoted_true}/
end
it 'generates an equality condition for boolean false' do
@s.awesome_eq = false
@s.result.to_sql.should match /"people"."awesome" = 'f'/
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
@s.result.to_sql.should match /#{field} = #{ActiveRecord::Base.connection.quoted_false}/
end
it 'does not generate a condition for nil' do
@ -41,6 +43,8 @@ module Ransack
it_has_behavior 'wildcard escaping', :name_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" ILIKE '%\\%\\._\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
/`people`.`name` LIKE '%\\\\%\\\\._\\\\\\\\%'/
else
/"people"."name" LIKE '%%._\\%'/
end) do
@ -49,7 +53,8 @@ module Ransack
it 'generates a LIKE query with value surrounded by %' do
@s.name_cont = 'ric'
@s.result.to_sql.should match /"people"."name" I?LIKE '%ric%'/
field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
@s.result.to_sql.should match /#{field} I?LIKE '%ric%'/
end
end
@ -57,6 +62,8 @@ module Ransack
it_has_behavior 'wildcard escaping', :name_not_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" NOT ILIKE '%\\%\\._\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
/`people`.`name` NOT LIKE '%\\\\%\\\\._\\\\\\\\%'/
else
/"people"."name" NOT LIKE '%%._\\%'/
end) do
@ -65,21 +72,24 @@ module Ransack
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 I?LIKE '%ric%'/
field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
@s.result.to_sql.should match /#{field} NOT I?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/
field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
@s.result.to_sql.should match /#{field} 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/
field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
@s.result.to_sql.should match /#{field} IS NOT NULL/
end
end
end

View File

@ -113,25 +113,27 @@ module Ransack
end
describe '#result' do
let(:people_name_field) { "#{quote_table_name("people")}.#{quote_column_name("name")}" }
let(:children_people_name_field) { "#{quote_table_name("children_people")}.#{quote_column_name("name")}" }
it 'evaluates conditions contextually' do
search = Search.new(Person, :children_name_eq => 'Ernie')
search.result.should be_an ActiveRecord::Relation
where = search.result.where_values.first
where.to_sql.should match /"children_people"\."name" = 'Ernie'/
where.to_sql.should match /#{children_people_name_field} = 'Ernie'/
end
it 'evaluates compound conditions contextually' do
search = Search.new(Person, :children_name_or_name_eq => 'Ernie')
search.result.should be_an ActiveRecord::Relation
where = search.result.where_values.first
where.to_sql.should match /"children_people"\."name" = 'Ernie' OR "people"\."name" = 'Ernie'/
where.to_sql.should match /#{children_people_name_field} = 'Ernie' OR #{people_name_field} = 'Ernie'/
end
it 'evaluates polymorphic belongs_to association conditions contextually' do
search = Search.new(Note, :notable_of_Person_type_name_eq => 'Ernie')
search.result.should be_an ActiveRecord::Relation
where = search.result.where_values.first
where.to_sql.should match /"people"."name" = 'Ernie'/
where.to_sql.should match /#{people_name_field} = 'Ernie'/
end
it 'evaluates nested conditions' do
@ -145,9 +147,9 @@ module Ransack
)
search.result.should be_an ActiveRecord::Relation
where = search.result.where_values.first
where.to_sql.should match /"children_people"."name" = 'Ernie'/
where.to_sql.should match /"people"."name" = 'Ernie'/
where.to_sql.should match /"children_people_2"."name" = 'Ernie'/
where.to_sql.should match /#{children_people_name_field} = 'Ernie'/
where.to_sql.should match /#{people_name_field} = 'Ernie'/
where.to_sql.should match /#{quote_table_name("children_people_2")}.#{quote_column_name("name")} = 'Ernie'/
end
it 'evaluates arrays of groupings' do
@ -161,10 +163,10 @@ module Ransack
where = search.result.where_values.first
sql = where.to_sql
first, second = sql.split(/ AND /)
first.should match /"people"."name" = 'Ernie'/
first.should match /"children_people"."name" = 'Ernie'/
second.should match /"people"."name" = 'Bert'/
second.should match /"children_people"."name" = 'Bert'/
first.should match /#{people_name_field} = 'Ernie'/
first.should match /#{children_people_name_field} = 'Ernie'/
second.should match /#{people_name_field} = 'Bert'/
second.should match /#{children_people_name_field} = 'Bert'/
end
it 'returns distinct records when passed :distinct => true' do