提交 056acbd0 编写于 作者: R Rafael Mendonça França

Merge pull request #10676 from tkhr/add-test-to-counter_cache_test.rb

Add test to counter cache test.rb
......@@ -1236,7 +1236,7 @@ def test_inspect_class
def test_inspect_instance
topic = topics(:first)
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", important: nil, approved: false, replies_count: 1, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{topic.created_at.to_s(:db)}", updated_at: "#{topic.updated_at.to_s(:db)}">), topic.inspect
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{topic.created_at.to_s(:db)}", updated_at: "#{topic.updated_at.to_s(:db)}">), topic.inspect
end
def test_inspect_new_instance
......
......@@ -51,6 +51,18 @@ class ::SpecialReply < ::Reply
end
end
test 'reset multiple association counters' do
Topic.increment_counter(:replies_count, @topic.id)
assert_difference '@topic.reload.replies_count', -1 do
Topic.reset_counters(@topic.id, :replies, :unique_replies)
end
Topic.increment_counter(:unique_replies_count, @topic.id)
assert_difference '@topic.reload.unique_replies_count', -1 do
Topic.reset_counters(@topic.id, :replies, :unique_replies)
end
end
test "reset counters with string argument" do
Topic.increment_counter('replies_count', @topic.id)
......
......@@ -35,18 +35,18 @@ def test_human_name
def test_read_attribute_names
assert_equal(
%w( id title author_name author_email_address bonus_time written_on last_read content important group approved replies_count parent_id parent_title type created_at updated_at ).sort,
%w( id title author_name author_email_address bonus_time written_on last_read content important group approved replies_count unique_replies_count parent_id parent_title type created_at updated_at ).sort,
@first.attribute_names.sort
)
end
def test_columns
assert_equal 17, Topic.columns.length
assert_equal 18, Topic.columns.length
end
def test_columns_are_returned_in_the_order_they_were_declared
column_names = Topic.columns.map { |column| column.name }
assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content important approved replies_count parent_id parent_title type group created_at updated_at), column_names
assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content important approved replies_count unique_replies_count parent_id parent_title type group created_at updated_at), column_names
end
def test_content_columns
......
......@@ -7,6 +7,7 @@ class Reply < Topic
end
class UniqueReply < Reply
belongs_to :topic, :foreign_key => 'parent_id', :counter_cache => true
validates_uniqueness_of :content, :scope => 'parent_id'
end
......
......@@ -675,6 +675,7 @@ def create_table(*args, &block)
end
t.boolean :approved, :default => true
t.integer :replies_count, :default => 0
t.integer :unique_replies_count, :default => 0
t.integer :parent_id
t.string :parent_title
t.string :type
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册