aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorEdward Faulkner <ef@alum.mit.edu>2011-02-04 15:34:44 -0500
committerSantiago Pastorino <santiago@wyeworks.com>2011-02-04 19:39:59 -0200
commit909588d964bf27f20142a0b4d57890114a8d4a7a (patch)
tree13547fb5224456d7345ceb9a80ab1ed4dac2e8ff /activerecord/test
parentdf077604865b12b119be0259575675f45b958524 (diff)
downloadrails-909588d964bf27f20142a0b4d57890114a8d4a7a.tar.gz
rails-909588d964bf27f20142a0b4d57890114a8d4a7a.tar.bz2
rails-909588d964bf27f20142a0b4d57890114a8d4a7a.zip
Fixing ordering of HABTM association deletion [#6191 state:resolved]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb2
-rw-r--r--activerecord/test/cases/habtm_destroy_order_test.rb17
-rw-r--r--activerecord/test/models/lesson.rb11
-rw-r--r--activerecord/test/models/student.rb3
-rw-r--r--activerecord/test/schema/schema.rb13
5 files changed, 45 insertions, 1 deletions
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
index 30730c7094..55b611ca92 100644
--- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -365,7 +365,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
def test_removing_associations_on_destroy
david = DeveloperWithBeforeDestroyRaise.find(1)
assert !david.projects.empty?
- assert_raise(RuntimeError) { david.destroy }
+ david.destroy
assert david.projects.empty?
assert DeveloperWithBeforeDestroyRaise.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = 1").empty?
end
diff --git a/activerecord/test/cases/habtm_destroy_order_test.rb b/activerecord/test/cases/habtm_destroy_order_test.rb
new file mode 100644
index 0000000000..15598392e2
--- /dev/null
+++ b/activerecord/test/cases/habtm_destroy_order_test.rb
@@ -0,0 +1,17 @@
+require "cases/helper"
+require "models/lesson"
+require "models/student"
+
+class HabtmDestroyOrderTest < ActiveRecord::TestCase
+ test "may not delete a lesson with students" do
+ sicp = Lesson.new(:name => "SICP")
+ ben = Student.new(:name => "Ben Bitdiddle")
+ sicp.students << ben
+ sicp.save!
+ assert_raises LessonError do
+ assert_no_difference('Lesson.count') do
+ sicp.destroy
+ end
+ end
+ end
+end
diff --git a/activerecord/test/models/lesson.rb b/activerecord/test/models/lesson.rb
new file mode 100644
index 0000000000..4c88153068
--- /dev/null
+++ b/activerecord/test/models/lesson.rb
@@ -0,0 +1,11 @@
+class LessonError < Exception
+end
+
+class Lesson < ActiveRecord::Base
+ has_and_belongs_to_many :students
+ before_destroy :ensure_no_students
+
+ def ensure_no_students
+ raise LessonError unless students.empty?
+ end
+end
diff --git a/activerecord/test/models/student.rb b/activerecord/test/models/student.rb
new file mode 100644
index 0000000000..f459f2a9a3
--- /dev/null
+++ b/activerecord/test/models/student.rb
@@ -0,0 +1,3 @@
+class Student < ActiveRecord::Base
+ has_and_belongs_to_many :lessons
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 5f9bb7ee41..326c336317 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -279,6 +279,15 @@ ActiveRecord::Schema.define do
t.integer :version, :null => false, :default => 0
end
+ create_table :lessons, :force => true do |t|
+ t.string :name
+ end
+
+ create_table :lessons_students, :id => false, :force => true do |t|
+ t.references :lesson
+ t.references :student
+ end
+
create_table :line_items, :force => true do |t|
t.integer :invoice_id
t.integer :amount
@@ -509,6 +518,10 @@ ActiveRecord::Schema.define do
t.string :sponsorable_type
end
+ create_table :students, :force => true do |t|
+ t.string :name
+ end
+
create_table :subscribers, :force => true, :id => false do |t|
t.string :nick, :null => false
t.string :name