1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

Test all the boolean true and false values.

These tests are meant to prevent changing the TRUE_VALUES and
FALSE_VALUES sets in RANSACK::CONSTANTS without a very good reason.

Previously, no test was alerting us against merging changes like #503.
This commit is contained in:
jonatack 2015-02-02 18:23:45 +01:00
parent 9ae1b54476
commit 5009eeadb8

View file

@ -1,6 +1,9 @@
require 'spec_helper'
module Ransack
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
describe Predicate do
before do
@ -20,18 +23,12 @@ module Ransack
end
describe 'eq' do
it 'generates an equality condition for boolean true' do
@s.awesome_eq = true
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} = #{
ActiveRecord::Base.connection.quoted_true}/
it 'generates an equality condition for boolean true values' do
test_boolean_equality_for(true)
end
it 'generates an equality condition for boolean false' do
@s.awesome_eq = false
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} = #{
ActiveRecord::Base.connection.quoted_false}/
it 'generates an equality condition for boolean false values' do
test_boolean_equality_for(false)
end
it 'does not generate a condition for nil' do
@ -361,5 +358,30 @@ module Ransack
expect(@s.result.to_sql).to match /#{field} IS NOT NULL AND #{field} != ''/
end
end
end
private
def test_boolean_equality_for(boolean_value)
test_values_for(boolean_value).each do |value|
s = Search.new(Person, awesome_eq: value)
expect(s.result.to_sql).to match expected_boolean_query(boolean_value)
end
end
def test_values_for(boolean_value)
case boolean_value
when true
TRUE_VALUES
when false
FALSE_VALUES
end
end
def expected_boolean_query(boolean_value)
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
condition = ActiveRecord::Base.connection.send("quoted_#{boolean_value}")
/#{field} = #{condition}/
end
end
end