Skip to content

Instantly share code, notes, and snippets.

@hkdnet

hkdnet/repro.rb Secret

Created December 20, 2019 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdnet/9889e283c28f5994bd38e42c12ec3364 to your computer and use it in GitHub Desktop.
Save hkdnet/9889e283c28f5994bd38e42c12ec3364 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
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 "activerecord", ENV.fetch('AR_VERSION', '5.2.4')
gem "sqlite3", "~> 1.3.6"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
end
create_table :settings, force: :true do |t|
t.bigint :user_id
t.integer :count, null: false
end
end
class User < ActiveRecord::Base
has_one :setting, required: true
accepts_nested_attributes_for :setting
end
class Setting < ActiveRecord::Base
belongs_to :user
validate do
if count < 0
errors.add(:count, 'error')
end
end
end
class T < Minitest::Test
def test_case
setting = Setting.create!(count: 0)
user = User.create!(setting: setting)
setting.count = -1
setting.save!(validate: false) # we do have invalid records in our production db
user = User.find(user.id)
# We don't want to allow to use invalid setting. End users must update their setting.
# `user.valid?` should return false when the user's setting is invalid.
assert_equal false, user.valid?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment