1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/boolean_test.rb
Ryuta Kamizono 2d12f800f1 Type cast falsy boolean symbols on boolean attribute as false
Before 34cc301, type casting by boolean attribute when querying is a
no-op, so finding by truthy boolean string (i.e.
`where(value: "true") # => value = 'true'`) didn't work as expected
(matches it to FALSE in MySQL #32624). By type casting is ensured, a
value on boolean attribute is always serialized to TRUE or FALSE.

In PostgreSQL, `where(value: :false) # => value = 'false'` was a valid
SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values
are serialized as TRUE.

I'd say using `:false` is mostly a developer's mistake (user's input
basically comes as a string), but `:false` on boolean attribute is
serialized as TRUE is not a desirable behavior for anybody.

This allows falsy boolean symbols as false, i.e.
`klass.create(value: :false).value? # => false` and
`where(value: :false) # => value = FALSE`.

Fixes #35676.
2019-03-30 04:18:25 +09:00

52 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require "cases/helper"
require "models/boolean"
class BooleanTest < ActiveRecord::TestCase
def test_boolean
b_nil = Boolean.create!(value: nil)
b_false = Boolean.create!(value: false)
b_true = Boolean.create!(value: true)
assert_nil Boolean.find(b_nil.id).value
assert_not_predicate Boolean.find(b_false.id), :value?
assert_predicate Boolean.find(b_true.id), :value?
end
def test_boolean_without_questionmark
b_true = Boolean.create!(value: true)
subclass = Class.new(Boolean).find(b_true.id)
superclass = Boolean.find(b_true.id)
assert_equal superclass.read_attribute(:has_fun), subclass.read_attribute(:has_fun)
end
def test_boolean_cast_from_string
b_blank = Boolean.create!(value: "")
b_false = Boolean.create!(value: "0")
b_true = Boolean.create!(value: "1")
assert_nil Boolean.find(b_blank.id).value
assert_not_predicate Boolean.find(b_false.id), :value?
assert_predicate Boolean.find(b_true.id), :value?
end
def test_find_by_boolean_string
b_false = Boolean.create!(value: "false")
b_true = Boolean.create!(value: "true")
assert_equal b_false, Boolean.find_by(value: "false")
assert_equal b_true, Boolean.find_by(value: "true")
end
def test_find_by_falsy_boolean_symbol
ActiveModel::Type::Boolean::FALSE_VALUES.each do |value|
b_false = Boolean.create!(value: value)
assert_not_predicate b_false, :value?
assert_equal b_false, Boolean.find_by(id: b_false.id, value: value.to_s.to_sym)
end
end
end