search: move misc search parsing helpers to PostQueryBuilder.

* Move various search parser helper methods (`has_metatag?`,
  `is_single_tag?` et al) from PostSets and the Tag model to
  PostQueryBuilder.

* Fix various minor bugs stemming from trying to check if a search query
  contains certain metatags using regexes or other adhoc techniques.
This commit is contained in:
evazion
2020-04-23 01:51:30 -05:00
parent 3dab648d0e
commit dd0d9dff4a
15 changed files with 114 additions and 147 deletions

View File

@@ -850,6 +850,41 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
end
end
context "Parsing:" do
should "split a query" do
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(~aaa -bbb* -bbb*), PostQueryBuilder.new("~AAa -BBB* -bbb*").split_query)
end
should "not strip out valid characters when scanning" do
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%), PostQueryBuilder.new("favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%").split_query)
end
should "parse single tags correctly" do
assert_equal(true, PostQueryBuilder.new("foo").is_single_tag?)
assert_equal(true, PostQueryBuilder.new("-foo").is_single_tag?)
assert_equal(true, PostQueryBuilder.new("~foo").is_single_tag?)
assert_equal(true, PostQueryBuilder.new("foo*").is_single_tag?)
assert_equal(false, PostQueryBuilder.new("fav:1234").is_single_tag?)
assert_equal(false, PostQueryBuilder.new("pool:1234").is_single_tag?)
assert_equal(false, PostQueryBuilder.new('source:"foo bar baz"').is_single_tag?)
assert_equal(false, PostQueryBuilder.new("foo bar").is_single_tag?)
end
should "parse simple tags correctly" do
assert_equal(true, PostQueryBuilder.new("foo").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("-foo").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("~foo").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("foo*").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("fav:1234").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("FAV:1234").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("pool:1234").is_simple_tag?)
assert_equal(false, PostQueryBuilder.new('source:"foo bar baz"').is_simple_tag?)
assert_equal(false, PostQueryBuilder.new("foo bar").is_simple_tag?)
end
end
context "The normalize_query method" do
should "work" do
create(:tag_alias, antecedent_name: "gray", consequent_name: "grey")

View File

@@ -65,16 +65,6 @@ module PostSets
end
end
context "a set for the 'a b' tag query" do
setup do
@set = PostSets::Post.new("a b")
end
should "know it isn't a single tag" do
assert(!@set.is_single_tag?)
end
end
context "a set going to the 1,001st page" do
setup do
@set = PostSets::Post.new("a", 1_001)
@@ -118,10 +108,6 @@ module PostSets
@set = PostSets::Post.new("a")
end
should "know it is a single tag" do
assert(@set.is_single_tag?)
end
should "normalize its tag query" do
assert_equal("a", @set.tag_string)
end

View File

@@ -91,49 +91,6 @@ class TagTest < ActiveSupport::TestCase
end
end
context "A tag parser" do
should "scan a query" do
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(~aaa -bbb* -bbb*), PostQueryBuilder.new("~AAa -BBB* -bbb*").split_query)
end
should "not strip out valid characters when scanning" do
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%), PostQueryBuilder.new("favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%").split_query)
end
should "cast values" do
assert_equal(2048, PostQueryBuilder.parse_cast("2kb", :filesize))
assert_equal(2097152, PostQueryBuilder.parse_cast("2m", :filesize))
assert_nothing_raised {PostQueryBuilder.parse_cast("2009-01-01", :date)}
assert_nothing_raised {PostQueryBuilder.parse_cast("1234", :integer)}
assert_nothing_raised {PostQueryBuilder.parse_cast("1234.56", :float)}
end
should "parse single tags correctly" do
assert_equal(true, Tag.is_single_tag?("foo"))
assert_equal(true, Tag.is_single_tag?("-foo"))
assert_equal(true, Tag.is_single_tag?("~foo"))
assert_equal(true, Tag.is_single_tag?("foo*"))
assert_equal(true, Tag.is_single_tag?("fav:1234"))
assert_equal(true, Tag.is_single_tag?("pool:1234"))
assert_equal(true, Tag.is_single_tag?('source:"foo bar baz"'))
assert_equal(false, Tag.is_single_tag?("foo bar"))
end
should "parse simple tags correctly" do
assert_equal(true, Tag.is_simple_tag?("foo"))
assert_equal(false, Tag.is_simple_tag?("-foo"))
assert_equal(false, Tag.is_simple_tag?("~foo"))
assert_equal(false, Tag.is_simple_tag?("foo*"))
assert_equal(false, Tag.is_simple_tag?("fav:1234"))
assert_equal(false, Tag.is_simple_tag?("FAV:1234"))
assert_equal(false, Tag.is_simple_tag?("pool:1234"))
assert_equal(false, Tag.is_simple_tag?('source:"foo bar baz"'))
assert_equal(false, Tag.is_simple_tag?("foo bar"))
end
end
context "A tag" do
should "be found when one exists" do
tag = FactoryBot.create(:tag)