Skip to content

Instantly share code, notes, and snippets.

@Envek
Created April 11, 2013 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 Envek/5360783 to your computer and use it in GitHub Desktop.
Save Envek/5360783 to your computer and use it in GitHub Desktop.
Test case for issue rails/rails#8217
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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 "buildings" do |t|
t.string "name"
end
create_table "departments" do |t|
t.string "name"
end
create_table "rooms" do |t|
t.string "name"
t.integer "building_id"
t.integer "department_id"
end
end
class Building < ActiveRecord::Base
has_many :rooms, dependent: :destroy
validates :name, presence: true
end
class Department < ActiveRecord::Base
has_many :rooms, dependent: :nullify
validates :name, presence: true
end
class Room < ActiveRecord::Base
belongs_to :building
belongs_to :department
validates :name, presence: true
# There is no building_id validation, as room can be outside buildings (YEAH!)
default_scope -> { includes(:building).order("rooms.name ASC, buildings.name ASC").references(:building) }
end
class BugTest < MiniTest::Unit::TestCase
def test_association_stuff
building = Building.create!(name: "Main")
department = Department.create!(name: "IT")
room = Room.create!(name: "1", building: building, department: department)
# Start of hack
# Well, I've not found `refute_raises` assertion
# refute_raises(ActiveRecord::StatementInvalid) { department.destroy! }
fail = false
begin
department.destroy!
rescue ActiveRecord::StatementInvalid
fail = true
end
assert_equal false, fail, "Shouldn't raise StatementInvalid error"
# End of hack
# Other checks
assert_equal 1, Building.count, "Shouldn't delete building"
assert_equal 0, Department.count, "Should delete department"
assert_equal 1, Room.count, "Shouldn't delete room"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment