Skip to content

Instantly share code, notes, and snippets.

@abrom
Created August 14, 2018 01:13
Show Gist options
  • Save abrom/e9ff2cda1676d749677e31af7af604c6 to your computer and use it in GitHub Desktop.
Save abrom/e9ff2cda1676d749677e31af7af604c6 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'securerandom'
$test_db_name = "test_#{SecureRandom.rand(10_000)}.db"
puts "Using #{$test_db_name} database"
# Let's make sure it definitely doesn't exist
File.delete $test_db_name if File.exist? $test_db_name
def setup_test(rails_version:)
puts "\n\n**************************"
puts "Testing rails v#{rails_version}"
puts "**************************\n\n"
require "bundler/inline"
gemfile(true) do
ruby "2.4.4"
source "https://rubygems.org"
gem "rails", rails_version
gem "minitest", "5.10.3"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: $test_db_name)
yield if block_given?
end
[
'5.1.6',
'5.2.0',
'5.2.1',
{ github: 'rails/rails' }
].shuffle.each_with_index do |test_version, index|
fork do
setup_test rails_version: test_version do
if index.zero?
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
# STEP TWO: Define your tables here.
create_table :foos do |t|
t.string :foo_name
end
create_table :bars do |t|
t.string :bar_name
t.references :foo
end
end
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
# Define models
class Foo < ActiveRecord::Base
has_many :bars
accepts_nested_attributes_for :bars
end
class Bar < ActiveRecord::Base
belongs_to :foo
end
# Test the issue
class BugTest < ActiveSupport::TestCase
teardown do
Foo.delete_all
Bar.delete_all
end
def test_1
assert_changes -> { Foo.count }, from: 0, to: 1 do
assert_changes -> { Bar.count }, from: 0, to: 1 do
Foo.create_with(
bars_attributes: [{ bar_name: 'bar_test' }]
).find_or_create_by!(
foo_name: 'foo_test'
)
end
end
end
end
end
Process.wait
end
# Let's clean up after ourselves
File.delete $test_db_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment