1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reuse existing model for testing duplicated children records

Follow up of #32952.
This commit is contained in:
Ryuta Kamizono 2018-06-07 04:28:23 +09:00
parent 86e42b5366
commit 48c95cf7e5
3 changed files with 12 additions and 18 deletions

View file

@ -27,7 +27,6 @@ require "models/member_detail"
require "models/organization"
require "models/guitar"
require "models/tuning_peg"
require "models/topic"
require "models/reply"
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
@ -560,19 +559,18 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa
end
def test_parent_should_not_get_saved_with_duplicate_children_records
Topic.delete_all
assert_no_difference "Reply.count" do
assert_no_difference "SillyUniqueReply.count" do
reply = Reply.new
reply.silly_unique_replies.build([
{ content: "Best content" },
{ content: "Best content" }
])
content = "Best content"
reply1 = ValidateUniqueContentReply.new(content: content)
reply2 = ValidateUniqueContentReply.new(content: content)
topic = Topic.new(validate_unique_content_replies: [reply1, reply2])
assert_not topic.save
assert topic.errors.any?
assert_equal 0, Topic.count
assert_equal 0, ValidateUniqueContentReply.count
assert_not reply.save
assert_not_empty reply.errors
end
end
end
def test_invalid_build

View file

@ -6,6 +6,7 @@ class Reply < Topic
belongs_to :topic, foreign_key: "parent_id", counter_cache: true
belongs_to :topic_with_primary_key, class_name: "Topic", primary_key: "title", foreign_key: "parent_title", counter_cache: "replies_count", touch: true
has_many :replies, class_name: "SillyReply", dependent: :destroy, foreign_key: "parent_id"
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
end
class UniqueReply < Reply
@ -14,10 +15,6 @@ class UniqueReply < Reply
end
class SillyUniqueReply < UniqueReply
end
class ValidateUniqueContentReply < Reply
belongs_to :topic, foreign_key: "parent_id"
validates :content, uniqueness: true
end

View file

@ -47,7 +47,6 @@ class Topic < ActiveRecord::Base
has_many :unique_replies, dependent: :destroy, foreign_key: "parent_id"
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
has_many :validate_unique_content_replies, dependent: :destroy, foreign_key: "parent_id"
serialize :content