Skip to content

Instantly share code, notes, and snippets.

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 Mangara/1d58641bf538e608c5fbe308f48a8b6d to your computer and use it in GitHub Desktop.
Save Mangara/1d58641bf538e608c5fbe308f48a8b6d to your computer and use it in GitHub Desktop.
Additional executable test for Rails issue #32940
# 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", "5.2.0"
gem "sqlite3"
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 :subsets, force: true do |t|
end
create_table :items, force: true do |t|
end
create_table :subset_items, force: true do |t|
t.references :subset, foreign_key: true
t.references :item, foreign_key: true
end
end
class Subset < ApplicationRecord::Base
has_many :subset_items
end
class Item < ApplicationRecord::Base
has_many :subset_items
end
class SubsetItem < ApplicationRecord::Base
belongs_to :item
belongs_to :subset
validates :item_id, uniqueness: { scope: [:subset_id] }
end
class BugTest < Minitest::Test
def test_save_with_scoped_duplicates_fails
item = Item.create
subset = Subset.new(subset_items: [SubsetItem.new(item_id: item.id), SubsetItem.new(item_id: item.id)])
assert_not subset.save
assert_predicate subset.errors, :any?
assert_equal 0, Subset.count
assert_equal 0, SubsetItem.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment