tag unit test updates

This commit is contained in:
Albert Yi 2010-02-07 01:47:40 -05:00
parent 341a24e22e
commit df9c1c4efe
3 changed files with 96 additions and 12 deletions

View File

@ -43,10 +43,11 @@ class Tag < ActiveRecord::Base
def self.find_or_create_by_name(name, options = {})
name = normalize_name(name)
category = self.class.types.general
category = categories.general
if name =~ /\A(#{categories.regexp}):(.+)\Z/
category = self.class.types.value_for($1)
category = categories.value_for($1)
name = $2
end
tag = find_by_name(name)
@ -90,7 +91,7 @@ class Tag < ActiveRecord::Base
end
def self.scan_tags(tags)
tags.to_s.downcase.gsub(/[&,;]/, "").scan(/\S+/).uniq
tags.to_s.downcase.gsub(/[,;*]/, "_").scan(/\S+/).uniq
end
def self.parse_cast(object, type)
@ -157,11 +158,7 @@ class Tag < ActiveRecord::Base
scan_query(query).each do |token|
if token =~ /^(sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|change|status|tagcount|gentagcount|arttagcount|chartagcount|copytagcount):(.+)$/
if $1 == "user"
q[:user] = $2
elsif $1 == "fav"
q[:fav] = $2
elsif $1 == "sub"
if $1 == "sub"
q[:subscriptions] = $2
elsif $1 == "md5"
q[:md5] = $2
@ -215,10 +212,14 @@ class Tag < ActiveRecord::Base
end
end
q[:exclude] = TagAlias.to_aliased(q[:exclude], :strip_prefix => true) if q.has_key?(:exclude)
q[:include] = TagAlias.to_aliased(q[:include], :strip_prefix => true) if q.has_key?(:include)
q[:related] = TagAlias.to_aliased(q[:related]) if q.has_key?(:related)
normalize_tags_in_query(q)
return q
end
def self.normalize_tags_in_query(query_hash)
query_hash[:exclude] = TagAlias.to_aliased(query_hash[:exclude], :strip_prefix => true) if query_hash.has_key?(:exclude)
query_hash[:include] = TagAlias.to_aliased(query_hash[:include], :strip_prefix => true) if query_hash.has_key?(:include)
query_hash[:related] = TagAlias.to_aliased(query_hash[:related]) if query_hash.has_key?(:related)
end
end

View File

@ -1,5 +1,5 @@
Factory.define(:tag) do |f|
f.name {Faker::Name.first_name}
f.name {Faker::Name.first_name.downcase}
f.post_count 0
f.category Tag.categories.general
f.related_tags ""

View File

@ -50,5 +50,88 @@ class TagTest < ActiveSupport::TestCase
@tag = Factory.create(:artist_tag)
assert_equal("Artist", @tag.category_name)
end
should "know its cache safe name" do
tag = Tag.new
tag.name = "tag"
assert_equal("tag", tag.cache_safe_name)
tag.name = "tag%"
assert_equal("tag_", tag.cache_safe_name)
tag.name = "tag%%"
assert_equal("tag__", tag.cache_safe_name)
end
end
context "A tag parser" do
should "scan a query" do
assert_equal(%w(aaa bbb), Tag.scan_query("aaa bbb"))
assert_equal(%w(~aaa -bbb*), Tag.scan_query("~AAa -BBB* -bbb*"))
end
should "strip out invalid characters when scanning" do
assert_equal(%w(aaa bbb), Tag.scan_tags("aaa bbb"))
assert_equal(%w(-b_b_b_), Tag.scan_tags("-B,B;B* -b_b_b_"))
end
should "cast values" do
assert_equal(2048, Tag.parse_cast("2kb", :filesize))
assert_equal(2097152, Tag.parse_cast("2m", :filesize))
assert_nothing_raised {Tag.parse_cast("2009-01-01", :date)}
assert_nothing_raised {Tag.parse_cast("1234", :integer)}
assert_nothing_raised {Tag.parse_cast("1234.56", :float)}
end
should "parse a query" do
tag1 = Factory.create(:tag, :name => "abc")
tag2 = Factory.create(:tag, :name => "acb")
assert_equal({md5: "abc"}, Tag.parse_query("md5:abc"))
assert_equal({:post_id => [:between, 1, 2]}, Tag.parse_query("id:1..2"))
assert_equal({:post_id => [:gte, 1]}, Tag.parse_query("id:1.."))
assert_equal({:post_id => [:lte, 2]}, Tag.parse_query("id:..2"))
assert_equal({:post_id => [:gt, 2]}, Tag.parse_query("id:>2"))
assert_equal({:post_id => [:lt, 3]}, Tag.parse_query("id:<3"))
Tag.expects(:normalize_tags_in_query).returns(nil)
assert_equal({:include => ["acb"]}, Tag.parse_query("a*b"))
end
end
context "A tag" do
should "be found when one exists" do
tag = Factory.create(:tag)
assert_difference("Tag.count", 0) do
Tag.find_or_create_by_name(tag.name)
end
end
should "change the type for an existing tag" do
tag = Factory.create(:tag)
assert_difference("Tag.count", 0) do
assert_equal(Tag.categories.general, tag.category)
Tag.find_or_create_by_name("artist:#{tag.name}")
tag.reload
assert_equal(Tag.categories.artist, tag.category)
end
end
should "be created when one doesn't exist" do
assert_difference("Tag.count", 1) do
tag = Tag.find_or_create_by_name("hoge")
assert_equal("hoge", tag.name)
assert_equal(Tag.categories.general, tag.category)
end
end
should "be created with the type when one doesn't exist" do
assert_difference("Tag.count", 1) do
tag = Tag.find_or_create_by_name("artist:hoge")
assert_equal("hoge", tag.name)
assert_equal(Tag.categories.artist, tag.category)
end
end
end
end