updated tests, switched to rails 3.0.0rc2

This commit is contained in:
albert
2010-08-26 14:36:02 -04:00
parent 694b6f0813
commit ad39553aac
48 changed files with 898 additions and 406 deletions

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class AdvertisementTest < ActiveSupport::TestCase
context "An advertisement" do

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class ArtistTest < ActiveSupport::TestCase
context "An artist" do

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class ArtistUrlTest < ActiveSupport::TestCase
context "An artist url" do

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class BanTest < ActiveSupport::TestCase
context "A ban" do

View File

@@ -1,11 +1,19 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class CommentTest < ActiveSupport::TestCase
context "A comment" do
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "be created" do
comment = Factory.build(:comment)
comment.save
@@ -37,12 +45,12 @@ class CommentTest < ActiveSupport::TestCase
user = Factory.create(:user)
post = Factory.create(:post)
c1 = Factory.create(:comment, :post => post)
assert_nothing_raised {c1.vote!(user, true)}
assert_raise(CommentVote::Error) {c1.vote!(user, true)}
assert_nothing_raised {c1.vote!(true)}
assert_raise(CommentVote::Error) {c1.vote!(true)}
assert_equal(1, CommentVote.count)
c2 = Factory.create(:comment, :post => post)
assert_nothing_raised {c2.vote!(user, true)}
assert_nothing_raised {c2.vote!(true)}
assert_equal(2, CommentVote.count)
end

View File

@@ -11,8 +11,9 @@ class ArtistTest < ActiveSupport::TestCase
CurrentUser.user = user
CurrentUser.ip_addr = "1.2.3.4"
assert_equal(user.id, CurrentUser.current_user)
assert_equal("1.2.3.4", CurrentUser.current_ip_addr)
assert_not_nil(CurrentUser.user)
assert_equal(user.id, CurrentUser.user.id)
assert_equal("1.2.3.4", CurrentUser.ip_addr)
end
end
end

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class DmailTest < ActiveSupport::TestCase
context "A dmail" do

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class DownloadTest < ActiveSupport::TestCase
context "A post download" do

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class FavoriteTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A favorite" do
should "know which table it belongs to" do
user1 = Factory.create(:user)

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class ForumPostTest < ActiveSupport::TestCase
context "A forum post" do

View File

@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class ForumTopicTest < ActiveSupport::TestCase
end

View File

@@ -1,16 +1,27 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class IpBanTest < ActiveSupport::TestCase
def test_count_by_ip_addr
comment = Factory.create(:comment)
counts = IpBan.count_by_ip_addr("comments", [comment.creator_id])
assert_equal([{"ip_addr" => "1.2.3.4", "count" => "1"}], counts)
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
def test_search
post = create_post()
comment = create_comment(post, :ip_addr => "1.2.3.4", :body => "aaa")
counts = IpBan.search([comment.user_id])
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "be able to count the number of comments an IP address is associated with" do
comment = Factory.create(:comment)
counts = IpBan.count_by_ip_addr("comments", [comment.creator_id], "creator_id", "ip_addr")
assert_equal([{"ip_addr" => "127.0.0.1", "count" => "1"}], counts)
end
should "be able to count any updates from a user, groupiny by IP address" do
comment = Factory.create(:comment, :ip_addr => "1.2.3.4", :body => "aaa")
counts = IpBan.search([comment.creator_id])
assert_equal([{"ip_addr" => "1.2.3.4", "count" => "1"}], counts["comments"])
end
end

View File

@@ -1,4 +1,4 @@
require 'test_helper'
require_relative '../test_helper'
class JanitorTrialTest < ActiveSupport::TestCase
# Replace this with your real tests.

View File

@@ -1,4 +1,4 @@
require 'test_helper'
require_relative '../test_helper'
class JobTest < ActiveSupport::TestCase
# Replace this with your real tests.

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class NoteTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A note" do
setup do
MEMCACHE.flush_all

View File

@@ -1,8 +0,0 @@
require 'test_helper'
class NoteVersionTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class PoolTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A pool" do
setup do
MEMCACHE.flush_all

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class PostModerationDetailTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A post moderation detail" do
should "hide posts" do
posts = []
@@ -30,8 +42,6 @@ class PostModerationDetailTest < ActiveSupport::TestCase
PostModerationDetail.prune!
end
post.is_flagged = false
post.updater_id = user.id
post.updater_ip_addr = "127.0.0.1"
post.save
assert(post.errors.empty?)
assert_difference("PostModerationDetail.count", -1) do

View File

@@ -5,6 +5,7 @@ class PostTest < ActiveSupport::TestCase
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
@@ -14,6 +15,15 @@ class PostTest < ActiveSupport::TestCase
context "Removal:" do
context "Removing a post" do
should "update the fast count" do
post = Factory.create(:post, :tag_string => "aaa")
assert_equal(1, Post.fast_count)
assert_equal(1, Post.fast_count("aaa"))
post.remove!
assert_equal(0, Post.fast_count)
assert_equal(0, Post.fast_count("aaa"))
end
should "duplicate the post in the archive table and remove it from the base table" do
post = Factory.create(:post)
@@ -33,6 +43,17 @@ class PostTest < ActiveSupport::TestCase
post.remove!
assert_equal(0, Tag.find_by_name("aaa").post_count)
end
should "preserve the id" do
post = Factory.create(:post, :tag_string => "aaa")
post_id = post.id
post.remove!
removed_post = RemovedPost.last
assert_equal(post_id, removed_post.id)
removed_post.unremove!
post = Post.last
assert_equal(post_id, post.id)
end
end
end
@@ -137,246 +158,284 @@ class PostTest < ActiveSupport::TestCase
end
end
context "During moderation a post" do
setup do
@post = Factory.create(:post)
@user = Factory.create(:user)
end
context "Moderation:" do
context "An approved post" do
should "be unapproved once and only once" do
post = Factory.create(:post)
post.unapprove!("bad")
assert(post.is_flagged?, "Post should be flagged.")
assert_not_nil(post.unapproval, "Post should have an unapproval record.")
assert_equal("bad", post.unapproval.reason)
assert_raise(Unapproval::Error) {post.unapprove!("bad")}
end
should "be unapproved once and only once" do
@post.unapprove!("bad", @user.id, "127.0.0.1")
assert(@post.is_flagged?, "Post should be flagged.")
assert_not_nil(@post.unapproval, "Post should have an unapproval record.")
assert_equal("bad", @post.unapproval.reason)
assert_raise(Unapproval::Error) {@post.unapprove!("bad", @user.id, "127.0.0.1")}
end
should "not unapprove if no reason is given" do
assert_raise(Unapproval::Error) {@post.unapprove!("", @user.id, "127.0.0.1")}
end
should "be destroyed" do
@post.destroy(1, "127.0.0.1")
assert(@post.is_deleted?, "Post should be deleted.")
end
should "be approved" do
@post.approve!(1, "127.0.0.1")
assert(!@post.is_pending?, "Post should not be pending.")
@deleted_post = Factory.create(:post, :is_deleted => true)
@deleted_post.approve!(1, "127.0.0.1")
assert(!@post.is_deleted?, "Post should not be deleted.")
end
end
context "A post version" do
should "be created on any save" do
@user = Factory.create(:user)
@post = Factory.create(:post)
@reverter = Factory.create(:user)
assert_equal(1, @post.versions.size)
@post.rating = "e"
@post.updater_id = @user.id
@post.updater_ip_addr = "125.0.0.0"
@post.save
assert_equal(2, @post.versions.size)
assert_equal(@user.id, @post.versions.last.updater_id)
assert_equal("125.0.0.0", @post.versions.last.updater_ip_addr)
@post.revert_to!(PostVersion.first, @reverter.id, "127.0.0.1")
assert_equal("tag1 tag2", @post.tag_string)
assert_equal("q", @post.rating)
end
end
context "A post's tags" do
setup do
@post = Factory.create(:post)
end
should "have an array representation" do
@post.set_tag_string("aaa bbb")
assert_equal(%w(aaa bbb), @post.tag_array)
assert_equal(%w(tag1 tag2), @post.tag_array_was)
end
should "reset the tag array cache when updated" do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
user = Factory.create(:user)
assert_equal(%w(aaa bbb ccc), post.tag_array)
post.tag_string = "ddd eee fff"
post.updater_id = user.id
post.updater_ip_addr = "127.0.0.1"
post.tag_string = "ddd eee fff"
post.save
assert_equal("ddd eee fff", post.tag_string)
assert_equal(%w(ddd eee fff), post.tag_array)
end
should "create the actual tag records" do
assert_difference("Tag.count", 3) do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
should "not unapprove if no reason is given" do
post = Factory.create(:post)
assert_raise(Unapproval::Error) {post.unapprove!("")}
end
end
should "update the post counts of relevant tag records" do
post1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
post2 = Factory.create(:post, :tag_string => "bbb ccc ddd")
post3 = Factory.create(:post, :tag_string => "ccc ddd eee")
user = Factory.create(:user)
assert_equal(1, Tag.find_by_name("aaa").post_count)
assert_equal(2, Tag.find_by_name("bbb").post_count)
assert_equal(3, Tag.find_by_name("ccc").post_count)
post3.tag_string = "xxx"
post3.updater_id = user.id
post3.updater_ip_addr = "127.0.0.1"
post3.save
assert_equal(1, Tag.find_by_name("aaa").post_count)
assert_equal(2, Tag.find_by_name("bbb").post_count)
assert_equal(2, Tag.find_by_name("ccc").post_count)
assert_equal(1, Tag.find_by_name("ddd").post_count)
assert_equal(0, Tag.find_by_name("eee").post_count)
assert_equal(1, Tag.find_by_name("xxx").post_count)
end
should "be counted" do
@user = Factory.create(:user)
@artist_tag = Factory.create(:artist_tag)
@copyright_tag = Factory.create(:copyright_tag)
@general_tag = Factory.create(:tag)
@new_post = Factory.create(:post, :tag_string => "#{@artist_tag.name} #{@copyright_tag.name} #{@general_tag.name}")
assert_equal(1, @new_post.tag_count_artist)
assert_equal(1, @new_post.tag_count_copyright)
assert_equal(1, @new_post.tag_count_general)
assert_equal(0, @new_post.tag_count_character)
assert_equal(3, @new_post.tag_count)
@new_post.tag_string = "babs"
@new_post.updater_id = @user.id
@new_post.updater_ip_addr = "127.0.0.1"
@new_post.save
assert_equal(0, @new_post.tag_count_artist)
assert_equal(0, @new_post.tag_count_copyright)
assert_equal(1, @new_post.tag_count_general)
assert_equal(0, @new_post.tag_count_character)
assert_equal(1, @new_post.tag_count)
end
should "be merged with any changes that were made after loading the initial set of tags part 1" do
@user = Factory.create(:user)
@post = Factory.create(:post, :tag_string => "aaa bbb ccc")
# user a adds <ddd>
@post_edited_by_user_a = Post.find(@post.id)
@post_edited_by_user_a.old_tag_string = "aaa bbb ccc"
@post_edited_by_user_a.tag_string = "aaa bbb ccc ddd"
@post_edited_by_user_a.updater_id = @user.id
@post_edited_by_user_a.updater_ip_addr = "127.0.0.1"
@post_edited_by_user_a.save
context "An unapproved post" do
should "preserve the approver's identity when approved" do
user = CurrentUser.user
post = Factory.create(:post, :is_pending => true)
post.approve!
assert_equal("approver:#{user.name}", post.approver_string)
end
# user b removes <ccc> adds <eee>
@post_edited_by_user_b = Post.find(@post.id)
@post_edited_by_user_b.old_tag_string = "aaa bbb ccc"
@post_edited_by_user_b.tag_string = "aaa bbb eee"
@post_edited_by_user_b.updater_id = @user.id
@post_edited_by_user_b.updater_ip_addr = "127.0.0.1"
@post_edited_by_user_b.save
# final should be <aaa>, <bbb>, <ddd>, <eee>
@final_post = Post.find(@post.id)
assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(@final_post.tag_string).sort)
should "preserve the unapproval association even when removed" do
post = Factory.create(:post)
post.unapprove!("bad")
post.remove!
removed_post = RemovedPost.last
assert_not_nil(removed_post.unapproval)
assert_equal("bad", removed_post.unapproval.reason)
end
context "that has been reapproved" do
should "no longer be flagged or pending" do
post = Factory.create(:post)
post.unapprove!("bad")
post.approve!
assert(post.errors.empty?, post.errors.full_messages.join(", "))
post.reload
assert_equal(false, post.is_flagged?)
assert_equal(false, post.is_pending?)
end
should "cannot be unapproved again" do
post = Factory.create(:post)
post.unapprove!("bad")
post.approve!
assert_raise(Unapproval::Error) {post.unapprove!("bad")}
end
end
end
end
should "be merged with any changes that were made after loading the initial set of tags part 2" do
# This is the same as part 1, only the order of operations is reversed.
# The results should be the same.
context "Versioning:" do
context "Saving a post" do
should "create a new version" do
post = Factory.create(:post)
assert_equal(1, post.versions.size)
@user = Factory.create(:user)
@post = Factory.create(:post, :tag_string => "aaa bbb ccc")
# user a removes <ccc> adds <eee>
@post_edited_by_user_a = Post.find(@post.id)
@post_edited_by_user_a.old_tag_string = "aaa bbb ccc"
@post_edited_by_user_a.tag_string = "aaa bbb eee"
@post_edited_by_user_a.updater_id = @user.id
@post_edited_by_user_a.updater_ip_addr = "127.0.0.1"
@post_edited_by_user_a.save
post.rating = "e"
post.save
assert_equal(2, post.versions.size)
assert_equal(CurrentUser.user.id, post.versions.last.updater_id)
assert_equal(CurrentUser.ip_addr, post.versions.last.updater_ip_addr)
post.revert_to!(PostVersion.first)
assert_equal("tag1 tag2", post.tag_string)
assert_equal("q", post.rating)
end
end
# user b adds <ddd>
@post_edited_by_user_b = Post.find(@post.id)
@post_edited_by_user_b.old_tag_string = "aaa bbb ccc"
@post_edited_by_user_b.tag_string = "aaa bbb ccc ddd"
@post_edited_by_user_b.updater_id = @user.id
@post_edited_by_user_b.updater_ip_addr = "127.0.0.1"
@post_edited_by_user_b.save
# final should be <aaa>, <bbb>, <ddd>, <eee>
@final_post = Post.find(@post.id)
assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(@final_post.tag_string).sort)
context "Reverting a post" do
should "identify the person who reverted the post" do
post = Factory.create(:post)
reverter = Factory.create(:user)
post.rating = "e"
post.save
post.rating = "q"
post.save
CurrentUser.user = Factory.create(:user)
post.revert_to!(PostVersion.first)
post.reload
assert_equal(CurrentUser.user.id, post.versions.last.updater_id)
end
end
end
context "Tagging:" do
context "A post" do
should "have an array representation of its tags" do
post = Factory.create(:post)
post.set_tag_string("aaa bbb")
assert_equal(%w(aaa bbb), post.tag_array)
assert_equal(%w(tag1 tag2), post.tag_array_was)
end
context "that has been updated" do
should "reset its tag array cache" do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
user = Factory.create(:user)
assert_equal(%w(aaa bbb ccc), post.tag_array)
post.tag_string = "ddd eee fff"
post.tag_string = "ddd eee fff"
post.save
assert_equal("ddd eee fff", post.tag_string)
assert_equal(%w(ddd eee fff), post.tag_array)
end
should "create the actual tag records" do
assert_difference("Tag.count", 3) do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
end
end
should "update the post counts of relevant tag records" do
post1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
post2 = Factory.create(:post, :tag_string => "bbb ccc ddd")
post3 = Factory.create(:post, :tag_string => "ccc ddd eee")
assert_equal(1, Tag.find_by_name("aaa").post_count)
assert_equal(2, Tag.find_by_name("bbb").post_count)
assert_equal(3, Tag.find_by_name("ccc").post_count)
post3.tag_string = "xxx"
post3.save
assert_equal(1, Tag.find_by_name("aaa").post_count)
assert_equal(2, Tag.find_by_name("bbb").post_count)
assert_equal(2, Tag.find_by_name("ccc").post_count)
assert_equal(1, Tag.find_by_name("ddd").post_count)
assert_equal(0, Tag.find_by_name("eee").post_count)
assert_equal(1, Tag.find_by_name("xxx").post_count)
end
should "update its tag counts" do
artist_tag = Factory.create(:artist_tag)
copyright_tag = Factory.create(:copyright_tag)
general_tag = Factory.create(:tag)
new_post = Factory.create(:post, :tag_string => "#{artist_tag.name} #{copyright_tag.name} #{general_tag.name}")
assert_equal(1, new_post.tag_count_artist)
assert_equal(1, new_post.tag_count_copyright)
assert_equal(1, new_post.tag_count_general)
assert_equal(0, new_post.tag_count_character)
assert_equal(3, new_post.tag_count)
new_post.tag_string = "babs"
new_post.save
assert_equal(0, new_post.tag_count_artist)
assert_equal(0, new_post.tag_count_copyright)
assert_equal(1, new_post.tag_count_general)
assert_equal(0, new_post.tag_count_character)
assert_equal(1, new_post.tag_count)
end
should "merge any changes that were made after loading the initial set of tags part 1" do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
# user a adds <ddd>
post_edited_by_user_a = Post.find(post.id)
post_edited_by_user_a.old_tag_string = "aaa bbb ccc"
post_edited_by_user_a.tag_string = "aaa bbb ccc ddd"
post_edited_by_user_a.save
# user b removes <ccc> adds <eee>
post_edited_by_user_b = Post.find(post.id)
post_edited_by_user_b.old_tag_string = "aaa bbb ccc"
post_edited_by_user_b.tag_string = "aaa bbb eee"
post_edited_by_user_b.save
# final should be <aaa>, <bbb>, <ddd>, <eee>
final_post = Post.find(post.id)
assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort)
end
should "merge any changes that were made after loading the initial set of tags part 2" do
# This is the same as part 1, only the order of operations is reversed.
# The results should be the same.
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
# user a removes <ccc> adds <eee>
post_edited_by_user_a = Post.find(post.id)
post_edited_by_user_a.old_tag_string = "aaa bbb ccc"
post_edited_by_user_a.tag_string = "aaa bbb eee"
post_edited_by_user_a.save
# user b adds <ddd>
post_edited_by_user_b = Post.find(post.id)
post_edited_by_user_b.old_tag_string = "aaa bbb ccc"
post_edited_by_user_b.tag_string = "aaa bbb ccc ddd"
post_edited_by_user_b.save
# final should be <aaa>, <bbb>, <ddd>, <eee>
final_post = Post.find(post.id)
assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort)
end
end
context "that has been tagged with a metatag" do
should "not include the metatag in its tag string" do
post = Factory.create(:post)
post.tag_string = "aaa pool:1234 pool:test rating:s fav:bob"
post.save
assert_equal("aaa", post.tag_string)
end
end
end
end
context "Adding a meta-tag" do
setup do
@post = Factory.create(:post)
end
context "Favorites:" do
context "Adding a post to a user's favorites" do
should "update the fav strings ont he post" do
user = Factory.create(:user)
post = Factory.create(:post)
post.add_favorite(user)
post.reload
assert_equal("fav:#{user.id}", post.fav_string)
assert(Favorite.exists?(:user_id => user.id, :post_id => post.id))
should "be ignored" do
@user = Factory.create(:user)
@post.updater_id = @user.id
@post.updater_ip_addr = "127.0.0.1"
@post.tag_string = "aaa pool:1234 pool:test rating:s fav:bob"
@post.save
assert_equal("aaa", @post.tag_string)
end
end
post.add_favorite(user)
post.reload
assert_equal("fav:#{user.id}", post.fav_string)
assert(Favorite.exists?(:user_id => user.id, :post_id => post.id))
context "Favoriting a post" do
should "update the favorite string" do
@user = Factory.create(:user)
@post = Factory.create(:post)
@post.add_favorite(@user)
assert_equal("fav:#{@user.name}", @post.fav_string)
@post.remove_favorite(@user)
assert_equal("", @post.fav_string)
post.remove_favorite(user)
post.reload
assert_equal("", post.fav_string)
assert(!Favorite.exists?(:user_id => user.id, :post_id => post.id))
post.remove_favorite(user)
post.reload
assert_equal("", post.fav_string)
assert(!Favorite.exists?(:user_id => user.id, :post_id => post.id))
end
end
end
context "Pooling a post" do
should "work" do
post = Factory.create(:post)
pool = Factory.create(:pool)
post.add_pool(pool)
assert_equal("pool:#{pool.name}", post.pool_string)
post.remove_pool(pool)
assert_equal("", post.pool_string)
context "Pools:" do
context "Adding a post to a pool" do
should "update the post's pool string" do
post = Factory.create(:post)
pool = Factory.create(:pool)
post.add_pool(pool)
post.reload
assert_equal("pool:#{pool.id}", post.pool_string)
post.add_pool(pool)
post.reload
assert_equal("pool:#{pool.id}", post.pool_string)
post.remove_pool(pool)
post.reload
assert_equal("", post.pool_string)
post.remove_pool(pool)
post.reload
assert_equal("", post.pool_string)
end
end
end
context "A post's uploader" do
should "be defined" do
post = Factory.create(:post)
user1 = Factory.create(:user)
user2 = Factory.create(:user)
user3 = Factory.create(:user)
post.uploader = user1
assert_equal("uploader:#{user1.name}", post.uploader_string)
post.uploader_id = user2.id
assert_equal("uploader:#{user2.name}", post.uploader_string)
assert_equal(user2.id, post.uploader_id)
assert_equal(user2.name, post.uploader_name)
context "Uploading:" do
context "Uploading a post" do
should "capture who uploaded the post" do
post = Factory.create(:post)
user1 = Factory.create(:user)
user2 = Factory.create(:user)
user3 = Factory.create(:user)
post.uploader = user1
assert_equal("uploader:#{user1.id}", post.uploader_string)
post.uploader_id = user2.id
assert_equal("uploader:#{user2.id}", post.uploader_string)
assert_equal(user2.id, post.uploader_id)
assert_equal(user2.name, post.uploader_name)
end
end
end
context "A tag search" do
context "Searching:" do
should "return posts for 1 tag" do
post1 = Factory.create(:post, :tag_string => "aaa")
post2 = Factory.create(:post, :tag_string => "aaa bbb")
@@ -445,7 +504,6 @@ class PostTest < ActiveSupport::TestCase
post3 = Factory.create(:post)
user = Factory.create(:user)
post1.add_favorite(user)
post1.save
relation = Post.find_by_tags("fav:#{user.name}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
@@ -457,7 +515,6 @@ class PostTest < ActiveSupport::TestCase
post3 = Factory.create(:post)
pool = Factory.create(:pool)
post1.add_pool(pool)
post1.save
relation = Post.find_by_tags("pool:#{pool.name}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
@@ -468,7 +525,6 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :uploader => user)
post2 = Factory.create(:post)
post3 = Factory.create(:post)
assert_equal("uploader:#{user.name}", post1.uploader_string)
relation = Post.find_by_tags("uploader:#{user.name}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
@@ -482,25 +538,7 @@ class PostTest < ActiveSupport::TestCase
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
should "filter out deleted posts by default" do
post1 = Factory.create(:post, :is_deleted => true)
post2 = Factory.create(:post, :is_deleted => true)
post3 = Factory.create(:post, :is_deleted => false)
relation = Post.find_by_tags("")
assert_equal(1, relation.count)
assert_equal(post3.id, relation.first.id)
end
should "return posts for a particular status" do
post1 = Factory.create(:post, :is_deleted => true)
post2 = Factory.create(:post, :is_deleted => false)
post3 = Factory.create(:post, :is_deleted => false)
relation = Post.find_by_tags("status:deleted")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
should "return posts for a source search" do
post1 = Factory.create(:post, :source => "abcd")
post2 = Factory.create(:post, :source => "abcdefg")
@@ -543,7 +581,7 @@ class PostTest < ActiveSupport::TestCase
end
end
context "Voting on a post" do
context "Voting:" do
should "not allow duplicate votes" do
user = Factory.create(:user)
post = Factory.create(:post)
@@ -554,4 +592,25 @@ class PostTest < ActiveSupport::TestCase
assert_equal(1, post.score)
end
end
context "Counting:" do
context "Creating a post" do
should "increment the post count" do
assert_equal(0, Post.fast_count(""))
post = Factory.create(:post, :tag_string => "aaa bbb")
assert_equal(1, Post.fast_count(""))
assert_equal(1, Post.fast_count("aaa"))
assert_equal(1, Post.fast_count("bbb"))
assert_equal(0, Post.fast_count("ccc"))
post.tag_string = "ccc"
post.save
assert_equal(1, Post.fast_count(""))
assert_equal(0, Post.fast_count("aaa"))
assert_equal(0, Post.fast_count("bbb"))
assert_equal(1, Post.fast_count("ccc"))
end
end
end
end

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class RelatedTagCalculatorTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A related tag calculator" do
should "calculate related tags for a tag" do
posts = []
@@ -10,7 +22,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
tag = Tag.find_by_name("aaa")
calculator = RelatedTagCalculator.new
assert_equal({"bbb" => 3, "ccc" => 2, "ddd" => 1}, calculator.calculate_from_sample("aaa", 10))
assert_equal({"aaa" => 3, "bbb" => 3, "ccc" => 2, "ddd" => 1}, calculator.calculate_from_sample("aaa", 10))
end
should "calculate typed related tags for a tag" do
@@ -35,7 +47,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
tag = Tag.find_by_name("aaa")
calculator = RelatedTagCalculator.new
counts = calculator.calculate_from_sample("aaa", 10)
assert_equal("bbb 3 ccc 2 ddd 1", calculator.convert_hash_to_string(counts))
assert_equal("aaa 3 bbb 3 ccc 2 ddd 1", calculator.convert_hash_to_string(counts))
end
end
end

View File

@@ -1,11 +1,19 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class TagAliasTest < ActiveSupport::TestCase
context "A tag alias" do
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "convert a tag to its normalized version" do
tag1 = Factory.create(:tag, :name => "aaa")
tag2 = Factory.create(:tag, :name => "bbb")

View File

@@ -1,10 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class TagImplicationTest < ActiveSupport::TestCase
context "A tag implication" do
setup do
MEMCACHE.flush_all
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
@user = Factory.create(:user)
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "not validate when a circular relation is created" do

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class TagSubscriptionTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A tag subscription" do
should "find the union of all posts for each tag in its tag query" do
posts = []

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class TagTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A tag category fetcher" do
setup do
MEMCACHE.flush_all

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class UploadTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "An upload" do
teardown do
FileUtils.rm_f(Dir.glob("#{Rails.root}/tmp/test.*"))

View File

@@ -1,6 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class UserFeedbackTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A user's feedback" do
should "should not validate if the creator is not privileged" do
user = Factory.create(:user)

View File

@@ -1,11 +1,19 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class UserTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A user" do
setup do
MEMCACHE.flush_all
end
should "not validate if the originating ip address is banned" do
Factory.create(:ip_ban)
user = Factory.build(:user)
@@ -22,7 +30,7 @@ class UserTest < ActiveSupport::TestCase
user.update_attribute(:is_contributor, false)
40.times do
Factory.create(:post, :uploader => user, :is_deleted => true)
Factory.create(:removed_post, :uploader => user)
end
assert(!user.can_upload?)
@@ -118,18 +126,18 @@ class UserTest < ActiveSupport::TestCase
context "name" do
should "be #{Danbooru.config.default_guest_name} given an invalid user id" do
assert_equal(Danbooru.config.default_guest_name, User.find_name(-1))
assert_equal(Danbooru.config.default_guest_name, User.id_to_name(-1))
end
should "be fetched given a user id" do
@user = Factory.create(:user)
assert_equal(@user.name, User.find_name(@user.id))
assert_equal(@user.name, User.id_to_name(@user.id))
end
should "be updated" do
@user = Factory.create(:user)
@user.update_attribute(:name, "danzig")
assert_equal("danzig", User.find_name(@user.id))
assert_equal("danzig", User.id_to_name(@user.id))
end
end

View File

@@ -1,11 +1,19 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative '../test_helper'
class WikiPageTest < ActiveSupport::TestCase
setup do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A wiki page" do
setup do
MEMCACHE.flush_all
end
should "normalize its title" do
wp = Factory.create(:wiki_page, :title => "HOT POTATO")
assert_equal("hot_potato", wp.title)