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 34cc301f03 Ensure casting by boolean attribute when querying
`QueryAttribute#value_for_database` calls only `type.serialize`, and
`Boolean#serialize` is a no-op unlike other attribute types.

It caused the issue #32624. Whether or not `serialize` will invoke
`cast` is undefined in our test cases, but it actually does not work
properly unless it does so for now.

Fixes #32624.
2018-05-29 05:22:31 +09:00

43 lines
1.3 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
end