improved test

This commit is contained in:
albert
2012-05-11 18:04:50 -04:00
parent 157789d4d5
commit 91ef2a6df2

View File

@@ -7,8 +7,6 @@ class CommentTest < ActiveSupport::TestCase
CurrentUser.user = user CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all MEMCACHE.flush_all
Danbooru.config.stubs(:member_comment_limit).returns(100)
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
end end
teardown do teardown do
@@ -16,58 +14,78 @@ class CommentTest < ActiveSupport::TestCase
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
end end
should "be created" do context "created by a limited user" do
comment = Factory.build(:comment) setup do
comment.save Danbooru.config.stubs(:member_comment_limit).returns(5)
assert(comment.errors.empty?, comment.errors.full_messages.join(", ")) Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.ago)
end
should "fail creation" do
comment = Factory.build(:comment)
comment.save
assert_equal(["Creator can not post comments within 1 week of sign up, and can only post 5 comments per hour after that"], comment.errors.full_messages)
end
end end
should "not bump the parent post" do context "created by an unlimited user" do
post = Factory.create(:post) setup do
comment = Factory.create(:comment, :do_not_bump_post => "1", :post => post) Danbooru.config.stubs(:member_comment_limit).returns(100)
post.reload Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
assert_nil(post.last_commented_at) end
comment = Factory.create(:comment, :post => post) should "be created" do
post.reload comment = Factory.build(:comment)
assert_not_nil(post.last_commented_at) comment.save
end assert(comment.errors.empty?, comment.errors.full_messages.join(", "))
end
should "not update the post after exceeding the threshold" do
Danbooru.config.stubs(:comment_threshold).returns(1) should "not bump the parent post" do
p = Factory.create(:post) post = Factory.create(:post)
c1 = Factory.create(:comment, :post => p) comment = Factory.create(:comment, :do_not_bump_post => "1", :post => post)
sleep 1 post.reload
c2 = Factory.create(:comment, :post => p) assert_nil(post.last_commented_at)
p.reload
assert_equal(c1.created_at.to_s, p.last_commented_at.to_s) comment = Factory.create(:comment, :post => post)
end post.reload
assert_not_nil(post.last_commented_at)
should "not allow duplicate votes" do end
user = Factory.create(:user)
post = Factory.create(:post) should "not update the post after exceeding the threshold" do
c1 = Factory.create(:comment, :post => post) Danbooru.config.stubs(:comment_threshold).returns(1)
comment_vote = c1.vote!(true) p = Factory.create(:post)
assert_equal([], comment_vote.errors.full_messages) c1 = Factory.create(:comment, :post => p)
comment_vote = c1.vote!(true) sleep 1
assert_equal(["User has already voted for this comment"], comment_vote.errors.full_messages) c2 = Factory.create(:comment, :post => p)
assert_equal(1, CommentVote.count) p.reload
assert_equal(c1.created_at.to_s, p.last_commented_at.to_s)
c2 = Factory.create(:comment, :post => post) end
comment_vote = c2.vote!(true)
assert_equal([], comment_vote.errors.full_messages) should "not allow duplicate votes" do
assert_equal(2, CommentVote.count) user = Factory.create(:user)
end post = Factory.create(:post)
c1 = Factory.create(:comment, :post => post)
should "be searchable" do comment_vote = c1.vote!(true)
c1 = Factory.create(:comment, :body => "aaa bbb ccc") assert_equal([], comment_vote.errors.full_messages)
c2 = Factory.create(:comment, :body => "aaa ddd") comment_vote = c1.vote!(true)
c3 = Factory.create(:comment, :body => "eee") assert_equal(["User has already voted for this comment"], comment_vote.errors.full_messages)
assert_equal(1, CommentVote.count)
matches = Comment.body_matches("aaa")
assert_equal(2, matches.count) c2 = Factory.create(:comment, :post => post)
assert_equal(c2.id, matches.all[0].id) comment_vote = c2.vote!(true)
assert_equal(c1.id, matches.all[1].id) assert_equal([], comment_vote.errors.full_messages)
assert_equal(2, CommentVote.count)
end
should "be searchable" do
c1 = Factory.create(:comment, :body => "aaa bbb ccc")
c2 = Factory.create(:comment, :body => "aaa ddd")
c3 = Factory.create(:comment, :body => "eee")
matches = Comment.body_matches("aaa")
assert_equal(2, matches.count)
assert_equal(c2.id, matches.all[0].id)
assert_equal(c1.id, matches.all[1].id)
end
end end
end end
end end