Last active
February 2, 2018 05:00
-
-
Save nachokb/7d9763e8fadbe6286383e813f79fcadc to your computer and use it in GitHub Desktop.
numericality validator blank string bug (https://github.com/rails/rails/issues/31870)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# rubocop:disable Documentation | |
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your\ | |
Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
# Activate the gem you are reporting the issue against. | |
gem 'sqlite3' | |
# gem 'rails', '~> 4.1.0' # works as expected | |
gem 'rails', '~> 4.2.0' # fails | |
# gem 'rails', '~> 5.1.0' # fails and MiniTest throws an error | |
# gem 'rails', '~> 5.2.0.rc1' # fails and MiniTest throws an error | |
end | |
require 'rack/test' | |
require 'action_controller/railtie' | |
require 'active_record' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection adapter: 'sqlite3', | |
database: ':memory:' | |
ActiveRecord::Schema.define do | |
create_table :student_classes do |t| | |
t.integer :girls_count | |
t.integer :boys_count | |
t.integer :total_count | |
t.boolean :discriminates_gender | |
end | |
end | |
class StudentClass < ActiveRecord::Base | |
validates :total_count, | |
presence: { | |
unless: :discriminates_gender? | |
}, | |
numericality: { | |
only_integer: true, | |
allow_blank: true | |
} | |
validates :girls_count, | |
presence: { | |
if: :discriminates_gender? | |
}, | |
numericality: { | |
only_integer: true, | |
allow_blank: true | |
} | |
validates :boys_count, | |
presence: { | |
if: :discriminates_gender? | |
}, | |
numericality: { | |
only_integer: true, | |
allow_blank: true | |
} | |
def total_count | |
sum = [girls_count, boys_count].compact.sum | |
self[:total_count] || sum | |
end | |
end | |
require 'minitest/autorun' | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
class BugTest < Minitest::Test | |
def assert_valid(model, msg = nil) | |
assert model.valid?, [msg, model.errors].compact.to_json | |
end | |
def setup | |
@mixed = StudentClass.new discriminates_gender: false | |
@gendered = StudentClass.new discriminates_gender: true | |
end | |
def test_total_can_be_nil | |
@gendered.total_count = nil | |
@gendered.girls_count = 5 | |
@gendered.boys_count = 5 | |
assert_valid @gendered | |
end | |
def test_total_can_be_blank | |
@gendered.total_count = '' | |
@gendered.girls_count = '5' | |
@gendered.boys_count = '5' | |
assert_valid @gendered | |
end | |
def test_genders_can_be_nil | |
@mixed.total_count = 10 | |
@mixed.boys_count = nil | |
@mixed.girls_count = nil | |
assert_valid @mixed | |
end | |
def test_genders_can_be_blank | |
@mixed.total_count = '10' | |
@mixed.boys_count = '' | |
@mixed.girls_count = '' | |
assert_valid @mixed | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment