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

3
.gitignore vendored
View File

@ -6,4 +6,5 @@ lib/danbooru_image_resizer/*.log
db/*.sqlite3
log/*.log
tmp/**/*
public/data
public/data
vendor/cache/*.gem

View File

@ -8,7 +8,7 @@ group :test do
gem "faker"
end
gem "rails", "3.0.0.rc"
gem "rails", "3.0.0.rc2"
gem "pg"
gem "memcache-client", :require => "memcache"
gem "imagesize", :require => "image_size"

View File

@ -2,33 +2,33 @@ GEM
remote: http://gemcutter.org/
specs:
abstract (1.0.0)
actionmailer (3.0.0.rc)
actionpack (= 3.0.0.rc)
actionmailer (3.0.0.rc2)
actionpack (= 3.0.0.rc2)
mail (~> 2.2.5)
actionpack (3.0.0.rc)
activemodel (= 3.0.0.rc)
activesupport (= 3.0.0.rc)
actionpack (3.0.0.rc2)
activemodel (= 3.0.0.rc2)
activesupport (= 3.0.0.rc2)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.9)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.22)
activemodel (3.0.0.rc)
activesupport (= 3.0.0.rc)
tzinfo (~> 0.3.23)
activemodel (3.0.0.rc2)
activesupport (= 3.0.0.rc2)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0.rc)
activemodel (= 3.0.0.rc)
activesupport (= 3.0.0.rc)
arel (~> 0.4.0)
tzinfo (~> 0.3.22)
activeresource (3.0.0.rc)
activemodel (= 3.0.0.rc)
activesupport (= 3.0.0.rc)
activesupport (3.0.0.rc)
arel (0.4.0)
activerecord (3.0.0.rc2)
activemodel (= 3.0.0.rc2)
activesupport (= 3.0.0.rc2)
arel (~> 1.0.0.rc1)
tzinfo (~> 0.3.23)
activeresource (3.0.0.rc2)
activemodel (= 3.0.0.rc2)
activesupport (= 3.0.0.rc2)
activesupport (3.0.0.rc2)
arel (1.0.0.rc1)
activesupport (>= 3.0.0.beta)
builder (2.1.2)
erubis (2.6.6)
@ -48,21 +48,21 @@ GEM
pg (0.9.0)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.9)
rack-mount (0.6.12)
rack (>= 1.0.0)
rack-test (0.5.4)
rack (>= 1.0)
rails (3.0.0.rc)
actionmailer (= 3.0.0.rc)
actionpack (= 3.0.0.rc)
activerecord (= 3.0.0.rc)
activeresource (= 3.0.0.rc)
activesupport (= 3.0.0.rc)
bundler (>= 1.0.0.rc.1)
railties (= 3.0.0.rc)
railties (3.0.0.rc)
actionpack (= 3.0.0.rc)
activesupport (= 3.0.0.rc)
rails (3.0.0.rc2)
actionmailer (= 3.0.0.rc2)
actionpack (= 3.0.0.rc2)
activerecord (= 3.0.0.rc2)
activeresource (= 3.0.0.rc2)
activesupport (= 3.0.0.rc2)
bundler (>= 1.0.0.rc.6)
railties (= 3.0.0.rc2)
railties (3.0.0.rc2)
actionpack (= 3.0.0.rc2)
activesupport (= 3.0.0.rc2)
rake (>= 0.8.3)
thor (~> 0.14.0)
rake (0.8.7)
@ -70,7 +70,7 @@ GEM
thor (0.14.0)
treetop (1.4.8)
polyglot (>= 0.3.1)
tzinfo (0.3.22)
tzinfo (0.3.23)
PLATFORMS
ruby
@ -82,5 +82,5 @@ DEPENDENCIES
memcache-client
mocha
pg
rails (= 3.0.0.rc)
rails (= 3.0.0.rc2)
shoulda

View File

@ -13,7 +13,7 @@ class Cache
start_time = Time.now
sanitized_key_to_value_hash = MEMCACHE.get_multi(key_to_sanitized_key_hash.values)
elapsed = Time.now - start_time
returning({}) do |result_hash|
{}.tap do |result_hash|
key_to_sanitized_key_hash.each do |key, sanitized_key|
if sanitized_key_to_value_hash.has_key?(sanitized_key)
result_hash[key] = sanitized_key_to_value_hash[sanitized_key]

View File

@ -52,6 +52,7 @@ class Download
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
}
source = handle_pixiv(source, headers)
url = URI.parse(source)
http.request_get(url.request_uri, headers) do |res|
case res
when Net::HTTPSuccess then

View File

@ -152,7 +152,7 @@ class Artist < ActiveRecord::Base
module FactoryMethods
def new_with_defaults(params)
returning(Artist.new) do |artist|
Artist.new.tap do |artist|
if params[:name]
artist.name = params[:name]
post = Post.find_by_tags("source:http* #{artist.name}").first

View File

@ -5,8 +5,8 @@ class Comment < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
has_many :votes, :class_name => "CommentVote", :dependent => :destroy
after_save :update_last_commented_at
after_destroy :update_last_commented_at
attr_accessible :body
attr_accessor :do_not_bump_post
scope :recent, :order => "comments.id desc", :limit => 6
scope :search_body, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
@ -21,10 +21,30 @@ class Comment < ActiveRecord::Base
end
def update_last_commented_at
if Comment.where(["post_id = ?", post_id]).count <= Danbooru.config.comment_threshold
if Comment.where(["post_id = ?", post_id]).count <= Danbooru.config.comment_threshold && !do_not_bump_post
execute_sql("UPDATE posts SET last_commented_at = ? WHERE id = ?", created_at, post_id)
end
end
def vote!(is_positive)
if !CurrentUser.user.can_comment_vote?
raise CommentVote::Error.new("You can only vote ten times an hour on comments")
elsif !is_positive && creator.is_janitor_or_higher?
raise CommentVote::Error.new("You cannot downvote janitor/moderator/admin comments")
elsif votes.find_by_user_id(CurrentUser.user.id).nil?
if is_positive
update_attribute(:score, score + 1)
else
update_attribute(:score, score - 1)
end
votes.create(:user_id => CurrentUser.user.id)
else
raise CommentVote::Error.new("You have already voted for this comment")
end
end
end
Comment.connection.extend(PostgresExtensions)

View File

@ -2,20 +2,10 @@ class CommentVote < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :is_positive
validates_uniqueness_of :ip_addr, :scope => :comment_id
belongs_to :comment
belongs_to :user
after_save :update_comment_score
def self.prune!
destroy_all(["created_at < ?", 14.days.ago])
end
def update_comment_score
if is_positive
comment.increment!(:score)
else
comment.decrement!(:score)
end
end
end

View File

@ -8,11 +8,11 @@ class IpBan < ActiveRecord::Base
end
def self.search(user_ids)
comments = count_by_ip_addr("comments", user_ids, "creator_id")
posts = count_by_ip_addr("post_versions", user_ids, "updater_id")
notes = count_by_ip_addr("note_versions", user_ids, "updater_id")
pools = count_by_ip_addr("pool_updates", user_ids, "updater_id")
wiki_pages = count_by_ip_addr("wiki_page_versions", user_ids, "updater_id")
comments = count_by_ip_addr("comments", user_ids, "creator_id", "ip_addr")
posts = count_by_ip_addr("post_versions", user_ids, "updater_id", "updater_ip_addr")
notes = count_by_ip_addr("note_versions", user_ids, "updater_id", "updater_ip_addr")
pools = count_by_ip_addr("pool_versions", user_ids, "updater_id", "updater_ip_addr")
wiki_pages = count_by_ip_addr("wiki_page_versions", user_ids, "updater_id", "updater_ip_addr")
return {
"comments" => comments,
@ -23,7 +23,7 @@ class IpBan < ActiveRecord::Base
}
end
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id")
select_all_sql("SELECT ip_addr, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY ip_addr ORDER BY count(*) DESC", user_ids)
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id", ip_addr_field = "ip_addr")
select_all_sql("SELECT #{ip_addr_field}, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY #{ip_addr_field} ORDER BY count(*) DESC", user_ids)
end
end

View File

@ -91,7 +91,7 @@ class Note < ActiveRecord::Base
end
def self.build_relation(params)
relation = where()
relation = where("TRUE")
if !params[:query].blank?
query = params[:query].scan(/\S+/).join(" & ")

View File

@ -36,6 +36,8 @@ class Pool < ActiveRecord::Base
end
def add_post!(post)
return if post_ids =~ /(?:\A| )#{post.id}(?:\Z| )/
self.post_ids += " #{post.id}"
self.post_ids.strip!
save

View File

@ -183,7 +183,8 @@ class Post < ActiveRecord::Base
module ApprovalMethods
def unapprove!(reason)
raise Unapproval::Error.new("You can't unapprove a post more than once") if is_flagged?
raise Unapproval::Error.new("This post has already been flagged") if is_flagged?
raise Unapproval::Error.new("This post has already been unapproved once") unless unapproval.nil?
unapproval = create_unapproval(
:unapprover_id => CurrentUser.user.id,
@ -194,14 +195,15 @@ class Post < ActiveRecord::Base
if unapproval.errors.any?
raise Unapproval::Error.new(unapproval.errors.full_messages.join("; "))
end
toggle!(:is_flagged)
update_attribute(:is_flagged, true)
end
def approve!
update_attributes(
:is_pending => false
)
self.is_flagged = false
self.is_pending = false
self.approver_string = "approver:#{CurrentUser.user.name}"
save!
end
end
@ -349,7 +351,7 @@ class Post < ActiveRecord::Base
user_id = user
end
return false if fav_string =~ /fav:#{user_id}/
return false if fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/
self.fav_string += " fav:#{user_id}"
self.fav_string.strip!
@ -467,9 +469,9 @@ class Post < ActiveRecord::Base
end
if q[:status] == "deleted"
relation = RemovedPost.where()
relation = RemovedPost.where("TRUE")
else
relation = where()
relation = where("TRUE")
end
relation = add_range_relation(q[:post_id], "posts.id", relation)
@ -584,7 +586,7 @@ class Post < ActiveRecord::Base
end
def uploader_id
uploader_string[9..-1]
uploader_string[9..-1].to_i
end
def uploader_name
@ -602,14 +604,17 @@ class Post < ActiveRecord::Base
module PoolMethods
def add_pool(pool)
return if pool_string =~ /(?:\A| )pool:#{pool.id}(?:\Z| )/
self.pool_string += " pool:#{pool.id}"
self.pool_string.strip!
execute_sql("UPDATE posts SET pool_string = ? WHERE id = ?", pool_string, id)
pool.add_post!(self)
end
def remove_pool(pool)
self.pool_string.gsub!(/(?:\A| )pool:#{pool.id}(?:\Z| )/, " ")
self.pool_string.strip!
execute_sql("UPDATE posts SET pool_string = ? WHERE id = ?", pool_string, id)
pool.remove_post!(self)
end
end
@ -635,7 +640,8 @@ class Post < ActiveRecord::Base
end
module CountMethods
def fast_count(tags)
def fast_count(tags = "")
tags = tags.to_s
count = Cache.get("pfc:#{Cache.sanitize(tags)}")
if count.nil?
count = Post.find_by_tags("#{tags}").count
@ -645,20 +651,14 @@ class Post < ActiveRecord::Base
end
count
end
def fast_delete_count(tags)
fast_count("#{tags} status:deleted")
end
end
module CacheMethods
def expire_cache(tag_name)
if Post.fast_count("") < 1000
Cache.delete("pfc:")
Cache.delete("pfdc:")
end
Cache.delete("pfc:#{Cache.sanitize(tag_name)}")
Cache.delete("pfdc:#{Cache.sanitize(tag_name)}")
end
end
@ -755,6 +755,7 @@ class Post < ActiveRecord::Base
decrement_tag_post_counts
execute_sql("DELETE FROM posts WHERE id = #{id}")
update_parent_on_destroy
tag_array.each {|x| expire_cache(x)}
end
end
end

View File

@ -2,15 +2,5 @@ class PostVote < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :is_positive
validates_uniqueness_of :ip_addr, :scope => :post_id
after_save :update_post_score
belongs_to :post
def update_post_score
if is_positive
post.increment!(:score)
else
post.decrement!(:score)
end
end
end

View File

@ -1,4 +1,6 @@
class RemovedPost < ActiveRecord::Base
has_one :unapproval, :dependent => :destroy, :foreign_key => "post_id"
module RemovalMethods
def unremove!
Post.transaction do
@ -8,6 +10,17 @@ class RemovedPost < ActiveRecord::Base
end
end
def fast_count(tags)
count = Cache.get("rpfc:#{Cache.sanitize(tags)}")
if count.nil?
count = RemovedPost.find_by_tags("#{tags}").count
if count > Danbooru.config.posts_per_page * 10
Cache.put("rpfc:#{Cache.sanitize(tags)}", count, (count * 4).minutes)
end
end
count
end
include Post::FileMethods
include Post::ImageMethods
include Post::TagMethods

View File

@ -92,7 +92,7 @@ class Tag < ActiveRecord::Base
tag
else
returning Tag.new do |tag|
Tag.new.tap do |tag|
tag.name = name
tag.category = category
tag.save
@ -217,22 +217,22 @@ class Tag < ActiveRecord::Base
if token =~ /\A(-uploader|uploader|-pool|pool|-fav|fav|sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|status|tagcount|gentags|arttags|chartags|copytags):(.+)\Z/
case $1
when "-uploader"
q[:tags][:exclude] << "uploader:#{User.name_to_id(token[1..-1])}"
q[:tags][:exclude] << "uploader:#{User.name_to_id($2)}"
when "uploader"
q[:tags][:related] << "uploader:#{User.name_to_id(token)}"
q[:tags][:related] << "uploader:#{User.name_to_id($2)}"
when "-pool"
q[:tags][:exclude] << "pool:#{Pool.name_to_id(token[1..-1])}"
q[:tags][:exclude] << "pool:#{Pool.name_to_id($2)}"
when "pool"
q[:tags][:related] << "pool:#{Pool.name_to_id(token)}"
q[:tags][:related] << "pool:#{Pool.name_to_id($2)}"
when "-fav"
q[:tags][:exclude] << "fav:#{User.name_to_id(token[1..-1])}"
q[:tags][:exclude] << "fav:#{User.name_to_id($2)}"
when "fav"
q[:tags][:related] << "fav:#{User.name_to_id(token)}"
q[:tags][:related] << "fav:#{User.name_to_id($2)}"
when "sub"
q[:subscriptions] << $2

View File

@ -49,6 +49,9 @@ class Upload < ActiveRecord::Base
module ConversionMethods
def process!
CurrentUser.user = uploader
CurrentUser.ip_addr = uploader_ip_addr
update_attribute(:status, "processing")
if is_downloadable?
download_from_source(temp_file_path)
@ -70,10 +73,13 @@ class Upload < ActiveRecord::Base
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))
end
rescue RuntimeError => x
ensure
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
def convert_to_post
returning Post.new do |p|
Post.new.tap do |p|
p.tag_string = tag_string
p.md5 = md5
p.file_ext = file_ext
@ -81,8 +87,6 @@ class Upload < ActiveRecord::Base
p.image_height = image_height
p.uploader_id = uploader_id
p.uploader_ip_addr = uploader_ip_addr
p.updater_id = uploader_id
p.updater_ip_addr = uploader_ip_addr
p.rating = rating
p.source = source
p.file_size = file_size

View File

@ -42,7 +42,7 @@ class User < ActiveRecord::Base
module ClassMethods
def name_to_id(name)
Cache.get("uni:#{Cache.sanitize(name)}") do
select_value_sql("SELECT id FROM users WHERE name = ?", name.downcase)
select_value_sql("SELECT id FROM users WHERE lower(name) = ?", name.downcase)
end
end
@ -111,7 +111,7 @@ class User < ActiveRecord::Base
module FavoriteMethods
def favorite_posts(options = {})
favorites_table = Favorite.table_name_for(self)
favorites_table = Favorite.table_name_for(id)
before_id = options[:before]
before_id_sql_fragment = "AND favorites.id < #{before_id.to_i}" if before_id
limit = options[:limit] || 20
@ -227,9 +227,9 @@ class User < ActiveRecord::Base
end
def upload_limit
deleted_count = Post.where("is_deleted = true and user_id = ?", id).count
deleted_count = RemovedPost.where("user_id = ?", id).count
unapproved_count = Post.where("is_pending = true and user_id = ?", id).count
approved_count = Post.where("is_flagged = false and is_deleted = false and is_pending = false and user_id = ?", id).count
approved_count = Post.where("is_flagged = false and is_pending = false and user_id = ?", id).count
limit = base_upload_limit + (approved_count / 10) - (deleted_count / 4) - unapproved_count

View File

@ -1125,7 +1125,6 @@ CREATE TABLE posts (
is_rating_locked boolean DEFAULT false NOT NULL,
is_pending boolean DEFAULT false NOT NULL,
is_flagged boolean DEFAULT false NOT NULL,
is_deleted boolean DEFAULT false NOT NULL,
uploader_string character varying(255) NOT NULL,
uploader_ip_addr inet NOT NULL,
approver_string character varying(255) DEFAULT ''::character varying NOT NULL,
@ -1144,7 +1143,9 @@ CREATE TABLE posts (
file_ext character varying(255) NOT NULL,
file_size integer NOT NULL,
image_width integer NOT NULL,
image_height integer NOT NULL
image_height integer NOT NULL,
parent_id integer,
has_children boolean DEFAULT false NOT NULL
);
@ -1167,6 +1168,65 @@ CREATE SEQUENCE posts_id_seq
ALTER SEQUENCE posts_id_seq OWNED BY posts.id;
--
-- Name: removed_posts; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE removed_posts (
id integer NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
score integer DEFAULT 0 NOT NULL,
source character varying(255),
md5 character varying(255) NOT NULL,
rating character(1) DEFAULT 'q'::bpchar NOT NULL,
is_note_locked boolean DEFAULT false NOT NULL,
is_rating_locked boolean DEFAULT false NOT NULL,
is_pending boolean DEFAULT false NOT NULL,
is_flagged boolean DEFAULT false NOT NULL,
uploader_string character varying(255) NOT NULL,
uploader_ip_addr inet NOT NULL,
approver_string character varying(255) DEFAULT ''::character varying NOT NULL,
fav_string text DEFAULT ''::text NOT NULL,
pool_string text DEFAULT ''::text NOT NULL,
view_count integer DEFAULT 0 NOT NULL,
last_noted_at timestamp without time zone,
last_commented_at timestamp without time zone,
tag_string text DEFAULT ''::text NOT NULL,
tag_index tsvector,
tag_count integer DEFAULT 0 NOT NULL,
tag_count_general integer DEFAULT 0 NOT NULL,
tag_count_artist integer DEFAULT 0 NOT NULL,
tag_count_character integer DEFAULT 0 NOT NULL,
tag_count_copyright integer DEFAULT 0 NOT NULL,
file_ext character varying(255) NOT NULL,
file_size integer NOT NULL,
image_width integer NOT NULL,
image_height integer NOT NULL,
parent_id integer,
has_children boolean DEFAULT false NOT NULL
);
--
-- Name: removed_posts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE removed_posts_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
--
-- Name: removed_posts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE removed_posts_id_seq OWNED BY removed_posts.id;
--
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@ -1771,6 +1831,13 @@ ALTER TABLE post_votes ALTER COLUMN id SET DEFAULT nextval('post_votes_id_seq'::
ALTER TABLE posts ALTER COLUMN id SET DEFAULT nextval('posts_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE removed_posts ALTER COLUMN id SET DEFAULT nextval('removed_posts_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@ -2097,6 +2164,14 @@ ALTER TABLE ONLY posts
ADD CONSTRAINT posts_pkey PRIMARY KEY (id);
--
-- Name: removed_posts_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY removed_posts
ADD CONSTRAINT removed_posts_pkey PRIMARY KEY (id);
--
-- Name: tag_aliases_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@ -2317,6 +2392,13 @@ CREATE INDEX index_dmails_on_parent_id ON dmails USING btree (parent_id);
CREATE INDEX index_favorites_0_on_post_id ON favorites_0 USING btree (post_id);
--
-- Name: index_favorites_0_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_0_on_post_id_and_user_id ON favorites_0 USING btree (post_id, user_id);
--
-- Name: index_favorites_0_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2331,6 +2413,13 @@ CREATE INDEX index_favorites_0_on_user_id ON favorites_0 USING btree (user_id);
CREATE INDEX index_favorites_1_on_post_id ON favorites_1 USING btree (post_id);
--
-- Name: index_favorites_1_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_1_on_post_id_and_user_id ON favorites_1 USING btree (post_id, user_id);
--
-- Name: index_favorites_1_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2345,6 +2434,13 @@ CREATE INDEX index_favorites_1_on_user_id ON favorites_1 USING btree (user_id);
CREATE INDEX index_favorites_2_on_post_id ON favorites_2 USING btree (post_id);
--
-- Name: index_favorites_2_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_2_on_post_id_and_user_id ON favorites_2 USING btree (post_id, user_id);
--
-- Name: index_favorites_2_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2359,6 +2455,13 @@ CREATE INDEX index_favorites_2_on_user_id ON favorites_2 USING btree (user_id);
CREATE INDEX index_favorites_3_on_post_id ON favorites_3 USING btree (post_id);
--
-- Name: index_favorites_3_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_3_on_post_id_and_user_id ON favorites_3 USING btree (post_id, user_id);
--
-- Name: index_favorites_3_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2373,6 +2476,13 @@ CREATE INDEX index_favorites_3_on_user_id ON favorites_3 USING btree (user_id);
CREATE INDEX index_favorites_4_on_post_id ON favorites_4 USING btree (post_id);
--
-- Name: index_favorites_4_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_4_on_post_id_and_user_id ON favorites_4 USING btree (post_id, user_id);
--
-- Name: index_favorites_4_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2387,6 +2497,13 @@ CREATE INDEX index_favorites_4_on_user_id ON favorites_4 USING btree (user_id);
CREATE INDEX index_favorites_5_on_post_id ON favorites_5 USING btree (post_id);
--
-- Name: index_favorites_5_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_5_on_post_id_and_user_id ON favorites_5 USING btree (post_id, user_id);
--
-- Name: index_favorites_5_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2401,6 +2518,13 @@ CREATE INDEX index_favorites_5_on_user_id ON favorites_5 USING btree (user_id);
CREATE INDEX index_favorites_6_on_post_id ON favorites_6 USING btree (post_id);
--
-- Name: index_favorites_6_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_6_on_post_id_and_user_id ON favorites_6 USING btree (post_id, user_id);
--
-- Name: index_favorites_6_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2415,6 +2539,13 @@ CREATE INDEX index_favorites_6_on_user_id ON favorites_6 USING btree (user_id);
CREATE INDEX index_favorites_7_on_post_id ON favorites_7 USING btree (post_id);
--
-- Name: index_favorites_7_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_7_on_post_id_and_user_id ON favorites_7 USING btree (post_id, user_id);
--
-- Name: index_favorites_7_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2429,6 +2560,13 @@ CREATE INDEX index_favorites_7_on_user_id ON favorites_7 USING btree (user_id);
CREATE INDEX index_favorites_8_on_post_id ON favorites_8 USING btree (post_id);
--
-- Name: index_favorites_8_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_8_on_post_id_and_user_id ON favorites_8 USING btree (post_id, user_id);
--
-- Name: index_favorites_8_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2443,6 +2581,13 @@ CREATE INDEX index_favorites_8_on_user_id ON favorites_8 USING btree (user_id);
CREATE INDEX index_favorites_9_on_post_id ON favorites_9 USING btree (post_id);
--
-- Name: index_favorites_9_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_favorites_9_on_post_id_and_user_id ON favorites_9 USING btree (post_id, user_id);
--
-- Name: index_favorites_9_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2639,6 +2784,13 @@ CREATE UNIQUE INDEX index_posts_on_md5 ON posts USING btree (md5);
CREATE INDEX index_posts_on_mpixels ON posts USING btree (((((image_width * image_height))::numeric / 1000000.0)));
--
-- Name: index_posts_on_parent_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_posts_on_parent_id ON posts USING btree (parent_id);
--
-- Name: index_posts_on_source; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2660,6 +2812,90 @@ CREATE INDEX index_posts_on_tags_index ON posts USING gin (tag_index);
CREATE INDEX index_posts_on_view_count ON posts USING btree (view_count);
--
-- Name: index_removed_posts_on_created_at; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_created_at ON removed_posts USING btree (created_at);
--
-- Name: index_removed_posts_on_file_size; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_file_size ON removed_posts USING btree (file_size);
--
-- Name: index_removed_posts_on_image_height; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_image_height ON removed_posts USING btree (image_height);
--
-- Name: index_removed_posts_on_image_width; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_image_width ON removed_posts USING btree (image_width);
--
-- Name: index_removed_posts_on_last_commented_at; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_last_commented_at ON removed_posts USING btree (last_commented_at);
--
-- Name: index_removed_posts_on_last_noted_at; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_last_noted_at ON removed_posts USING btree (last_noted_at);
--
-- Name: index_removed_posts_on_md5; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_removed_posts_on_md5 ON removed_posts USING btree (md5);
--
-- Name: index_removed_posts_on_mpixels; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_mpixels ON posts USING btree (((((image_width * image_height))::numeric / 1000000.0)));
--
-- Name: index_removed_posts_on_parent_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_parent_id ON removed_posts USING btree (parent_id);
--
-- Name: index_removed_posts_on_source; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_source ON removed_posts USING btree (source);
--
-- Name: index_removed_posts_on_tags_index; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_tags_index ON posts USING gin (tag_index);
--
-- Name: index_removed_posts_on_view_count; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_removed_posts_on_view_count ON removed_posts USING btree (view_count);
--
-- Name: index_tag_aliases_on_antecedent_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2835,6 +3071,16 @@ CREATE TRIGGER trigger_posts_on_tag_index_update
EXECUTE PROCEDURE tsvector_update_trigger('tag_index', 'public.danbooru', 'tag_string', 'fav_string', 'pool_string', 'uploader_string', 'approver_string');
--
-- Name: trigger_removed_posts_on_tag_index_update; Type: TRIGGER; Schema: public; Owner: -
--
CREATE TRIGGER trigger_removed_posts_on_tag_index_update
BEFORE INSERT OR UPDATE ON removed_posts
FOR EACH ROW
EXECUTE PROCEDURE tsvector_update_trigger('tag_index', 'public.danbooru', 'tag_string', 'fav_string', 'pool_string', 'uploader_string', 'approver_string');
--
-- Name: trigger_wiki_pages_on_update; Type: TRIGGER; Schema: public; Owner: -
--
@ -2911,4 +3157,6 @@ INSERT INTO schema_migrations (version) VALUES ('20100307073438');
INSERT INTO schema_migrations (version) VALUES ('20100309211553');
INSERT INTO schema_migrations (version) VALUES ('20100318213503');
INSERT INTO schema_migrations (version) VALUES ('20100318213503');
INSERT INTO schema_migrations (version) VALUES ('20100818180317');

View File

@ -6,7 +6,7 @@ module Danbooru
end
def reduce_to(size, max_size)
returning size.dup do |new_size|
size.dup.tap do |new_size|
if new_size[:width] > max_size[:width]
scale = max_size[:width].to_f / new_size[:width].to_f
new_size[:width] = new_size[:width] * scale

View File

@ -0,0 +1,13 @@
Factory.define(:removed_post) do |f|
f.md5 {|x| Time.now.to_f.to_s}
f.uploader {|x| x.association(:user)}
f.uploader_ip_addr "127.0.0.1"
f.tag_string "tag1 tag2"
f.tag_count 2
f.tag_count_general 2
f.file_ext "jpg"
f.image_width 100
f.image_height 200
f.file_size 2000
f.rating "q"
end

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)