related tags: refactor to take PostQuery instead of tag string.

Refactor RelatedTagCalculator and RelatedTagQuery to take a PostQuery
object instead of a raw tag string.

* Fixes the related tag sidebar on the post index page having to reparse
  the query and reevaluate aliases.
* Fixes related tags being affected by the current user's safe mode and
  hide deleted posts settings.
This commit is contained in:
evazion
2020-05-08 15:31:25 -05:00
parent 40e3ac14c0
commit 2749269d5b
4 changed files with 36 additions and 25 deletions

View File

@@ -1,6 +1,16 @@
require 'test_helper'
class RelatedTagCalculatorTest < ActiveSupport::TestCase
def frequent_tags_for_search(tag_search, user = CurrentUser.user, **options)
post_query = PostQueryBuilder.new(tag_search, user)
RelatedTagCalculator.frequent_tags_for_search(post_query, **options).pluck(:name)
end
def similar_tags_for_search(tag_search, user = CurrentUser.user, **options)
post_query = PostQueryBuilder.new(tag_search, user)
RelatedTagCalculator.similar_tags_for_search(post_query, **options).pluck(:name)
end
setup do
@user = create(:user)
end
@@ -23,7 +33,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
create(:post, tag_string: "aaa bbb ccc")
create(:post, tag_string: "aaa bbb")
assert_equal(%w[aaa bbb ccc ddd], RelatedTagCalculator.frequent_tags_for_search("aaa", @user).pluck(:name))
assert_equal(%w[aaa bbb ccc ddd], frequent_tags_for_search("aaa", @user))
end
should "calculate the most frequent tags for a multiple tag search" do
@@ -31,7 +41,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
create(:post, tag_string: "aaa bbb ccc ddd")
create(:post, tag_string: "aaa eee fff")
assert_equal(%w[aaa bbb ccc ddd], RelatedTagCalculator.frequent_tags_for_search("aaa bbb", @user).pluck(:name))
assert_equal(%w[aaa bbb ccc ddd], frequent_tags_for_search("aaa bbb", @user))
end
should "calculate the most frequent tags with a category constraint" do
@@ -39,20 +49,20 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
create(:post, tag_string: "aaa bbb ccc")
create(:post, tag_string: "aaa bbb")
assert_equal(%w[aaa bbb], RelatedTagCalculator.frequent_tags_for_search("aaa", @user, category: Tag.categories.general).pluck(:name))
assert_equal(%w[ccc], RelatedTagCalculator.frequent_tags_for_search("aaa", @user, category: Tag.categories.artist).pluck(:name))
assert_equal(%w[aaa bbb], frequent_tags_for_search("aaa", @user, category: Tag.categories.general))
assert_equal(%w[ccc], frequent_tags_for_search("aaa", @user, category: Tag.categories.artist))
end
end
context "#similar_tags_for_search" do
should "calculate the most similar tags for a search" do
create(:post, tag_string: "1girl solo", rating: "s")
create(:post, tag_string: "1girl solo", rating: "q")
create(:post, tag_string: "1girl 1boy", rating: "q")
create(:post, tag_string: "1girl solo", rating: "s", score: 1)
create(:post, tag_string: "1girl solo", rating: "q", score: 2)
create(:post, tag_string: "1girl 1boy", rating: "q", score: 2)
assert_equal(%w[1girl solo 1boy], RelatedTagCalculator.similar_tags_for_search("1girl", @user).pluck(:name))
assert_equal(%w[1girl 1boy solo], RelatedTagCalculator.similar_tags_for_search("rating:q", @user).pluck(:name))
assert_equal(%w[solo 1girl], RelatedTagCalculator.similar_tags_for_search("solo", @user).pluck(:name))
assert_equal(%w[1girl solo 1boy], similar_tags_for_search("1girl", @user))
assert_equal(%w[1girl 1boy solo], similar_tags_for_search("score:2", @user))
assert_equal(%w[solo 1girl], similar_tags_for_search("solo", @user))
end
should "calculate the similar tags for an aliased tag" do
@@ -60,7 +70,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
create(:post, tag_string: "bunny dog")
create(:post, tag_string: "bunny cat")
assert_equal(%w[bunny cat dog], RelatedTagCalculator.similar_tags_for_search("rabbit", @user).pluck(:name))
assert_equal(%w[bunny cat dog], similar_tags_for_search("rabbit", @user))
end
end
end