forked from e621ng/e621ng
added tag alias unit test
This commit is contained in:
parent
d388b1b3bd
commit
f0ed612042
@ -1,7 +1,7 @@
|
|||||||
class Tag < ActiveRecord::Base
|
class Tag < ActiveRecord::Base
|
||||||
attr_accessible :category
|
attr_accessible :category
|
||||||
after_save :update_category_cache
|
after_save :update_category_cache
|
||||||
named_scope :by_pattern, lambda {|name| where(["name LIKE ? ESCAPE E'\\\\'", name.to_escaped_for_sql_like])}
|
scope :by_pattern, lambda {|name| where(["name LIKE ? ESCAPE E'\\\\'", name.to_escaped_for_sql_like])}
|
||||||
|
|
||||||
class CategoryMapping
|
class CategoryMapping
|
||||||
Danbooru.config.reverse_tag_category_mapping.each do |value, category|
|
Danbooru.config.reverse_tag_category_mapping.each do |value, category|
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
class TagAlias < ActiveRecord::Base
|
class TagAlias < ActiveRecord::Base
|
||||||
|
attr_accessor :updater_id, :updater_ip_addr
|
||||||
after_save :update_posts
|
after_save :update_posts
|
||||||
|
after_save :update_cache
|
||||||
|
validates_presence_of :updater_id, :updater_ip_addr
|
||||||
|
validates_uniqueness_of :antecedent_name
|
||||||
|
belongs_to :updater, :class_name => "User"
|
||||||
|
belongs_to :creator, :class_name => "User"
|
||||||
|
|
||||||
def self.to_aliased(names)
|
def self.to_aliased(names)
|
||||||
alias_hash = Cache.get_multi(names, "ta") do |name|
|
alias_hash = Cache.get_multi(names.flatten, "ta") do |name|
|
||||||
ta = TagAlias.find_by_antecedent_name(name)
|
ta = TagAlias.find_by_antecedent_name(name)
|
||||||
if ta
|
if ta
|
||||||
ta.consequent_name
|
ta.consequent_name
|
||||||
@ -11,9 +17,22 @@ class TagAlias < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_hash.values
|
alias_hash.values.uniq
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_cache
|
||||||
|
Cache.put("ta:#{Cache.sanitize(antecedent_name)}", consequent_name, 24.hours)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_posts
|
def update_posts
|
||||||
|
Post.find_by_tags(antecedent_name).find_each do |post|
|
||||||
|
escaped_antecedent_name = Regexp.escape(antecedent_name)
|
||||||
|
fixed_tags = post.tag_string.sub(/\A#{escaped_antecedent_name} | #{escaped_antecedent_name} | #{escaped_antecedent_name}\Z/, " #{consequent_name} ").strip
|
||||||
|
post.update_attributes(
|
||||||
|
:tag_string => fixed_tags,
|
||||||
|
:updater_id => updater_id,
|
||||||
|
:updater_ip_addr => updater_ip_addr
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
5
test/factories/tag_alias.rb
Normal file
5
test/factories/tag_alias.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Factory.define(:tag_alias) do |f|
|
||||||
|
f.creator {|x| x.association(:user)}
|
||||||
|
f.updater_id {|x| x.creator_id}
|
||||||
|
f.updater_ip_addr "127.0.0.1"
|
||||||
|
end
|
@ -1,8 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
class DeletedPostTest < ActiveSupport::TestCase
|
|
||||||
# Replace this with your real tests.
|
|
||||||
test "the truth" do
|
|
||||||
assert true
|
|
||||||
end
|
|
||||||
end
|
|
@ -83,9 +83,21 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
post1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
|
post1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
|
||||||
post2 = Factory.create(:post, :tag_string => "bbb ccc ddd")
|
post2 = Factory.create(:post, :tag_string => "bbb ccc ddd")
|
||||||
post3 = Factory.create(:post, :tag_string => "ccc ddd eee")
|
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(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("bbb").post_count)
|
||||||
assert_equal(3, Tag.find_by_name("ccc").post_count)
|
assert_equal(3, Tag.find_by_name("ccc").post_count)
|
||||||
|
post3.update_attributes(
|
||||||
|
:tag_string => "xxx",
|
||||||
|
:updater_id => user.id,
|
||||||
|
:updater_ip_addr => "127.0.0.1"
|
||||||
|
)
|
||||||
|
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
|
end
|
||||||
|
|
||||||
should "be counted" do
|
should "be counted" do
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
class PostVersionTest < ActiveSupport::TestCase
|
|
||||||
# Replace this with your real tests.
|
|
||||||
test "the truth" do
|
|
||||||
assert true
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,8 +1,37 @@
|
|||||||
require 'test_helper'
|
require File.dirname(__FILE__) + '/../test_helper'
|
||||||
|
|
||||||
class TagAliasTest < ActiveSupport::TestCase
|
class TagAliasTest < ActiveSupport::TestCase
|
||||||
# Replace this with your real tests.
|
context "A tag alias" do
|
||||||
test "the truth" do
|
setup do
|
||||||
assert true
|
MEMCACHE.flush_all
|
||||||
|
end
|
||||||
|
|
||||||
|
should "convert a tag to its normalized version" do
|
||||||
|
tag1 = Factory.create(:tag, :name => "aaa")
|
||||||
|
tag2 = Factory.create(:tag, :name => "bbb")
|
||||||
|
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
|
||||||
|
normalized_tags = TagAlias.to_aliased(["aaa", "ccc"])
|
||||||
|
assert_equal(["bbb", "ccc"], normalized_tags.sort)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "update the cache" do
|
||||||
|
tag1 = Factory.create(:tag, :name => "aaa")
|
||||||
|
tag2 = Factory.create(:tag, :name => "bbb")
|
||||||
|
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
|
||||||
|
assert_equal("bbb", MEMCACHE.get("ta:aaa"))
|
||||||
|
end
|
||||||
|
|
||||||
|
should "update any affected posts when saved" do
|
||||||
|
assert_equal(0, TagAlias.count)
|
||||||
|
post1 = Factory.create(:post, :tag_string => "aaa bbb")
|
||||||
|
post2 = Factory.create(:post, :tag_string => "ccc ddd")
|
||||||
|
assert_equal("aaa bbb", post1.tag_string)
|
||||||
|
assert_equal("ccc ddd", post2.tag_string)
|
||||||
|
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "ccc")
|
||||||
|
post1.reload
|
||||||
|
post2.reload
|
||||||
|
assert_equal("ccc bbb", post1.tag_string)
|
||||||
|
assert_equal("ccc ddd", post2.tag_string)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
class PostUnapprovalTest < ActiveSupport::TestCase
|
|
||||||
# Replace this with your real tests.
|
|
||||||
test "the truth" do
|
|
||||||
assert true
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue
Block a user