posts: move fast_count to PostQueryBuilder.

This commit is contained in:
evazion
2020-05-06 14:47:16 -05:00
parent d3bd0a9cb5
commit a753ebbea9
9 changed files with 206 additions and 234 deletions

View File

@@ -9,7 +9,7 @@ class CountsControllerTest < ActionDispatch::IntegrationTest
end
should "render an error during a timeout" do
Post.stubs(:fast_count).raises(Post::TimeoutError.new)
PostQueryBuilder.any_instance.stubs(:fast_count).raises(Post::TimeoutError.new)
get posts_counts_path
assert_response :error
end

View File

@@ -5,6 +5,10 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
assert_equal(posts.map(&:id), Post.tag_match(query).pluck(:id))
end
def assert_fast_count(count, query, **options)
assert_equal(count, PostQueryBuilder.new(query).fast_count(**options))
end
setup do
CurrentUser.user = create(:user)
CurrentUser.ip_addr = "127.0.0.1"
@@ -1062,4 +1066,109 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
assert_equal('-commentary:"true" bbb', PostQueryBuilder.new("bbb -commentary:'true'").normalize_query)
end
end
context "#fast_count" do
setup do
Danbooru.config.stubs(:blank_tag_search_fast_count).returns(nil)
Danbooru.config.stubs(:estimate_post_counts).returns(false)
create(:tag, name: "grey_skirt", post_count: 100)
create(:tag_alias, antecedent_name: "gray_skirt", consequent_name: "grey_skirt")
create(:post, tag_string: "aaa", score: 42)
end
context "for a single basic tag" do
should "return the post_count from the tags table" do
assert_fast_count(100, "grey_skirt")
end
end
context "for a aliased tag" do
should "return the post count of the consequent tag" do
assert_fast_count(100, "gray_skirt")
end
end
context "for a single metatag" do
should "return the correct cached count" do
build(:tag, name: "score:42", post_count: -100).save(validate: false)
PostQueryBuilder.new(nil).set_count_in_cache("score:42", 100)
assert_fast_count(100, "score:42")
end
should "return the correct cached count for a pool:<id> search" do
build(:tag, name: "pool:1234", post_count: -100).save(validate: false)
PostQueryBuilder.new(nil).set_count_in_cache("pool:1234", 100)
assert_fast_count(100, "pool:1234")
end
end
context "for a multi-tag search" do
should "return the cached count, if it exists" do
PostQueryBuilder.new(nil).set_count_in_cache("aaa score:42", 100)
assert_fast_count(100, "aaa score:42")
end
should "return the true count, if not cached" do
assert_fast_count(1, "aaa score:42")
end
should "set the expiration time" do
Cache.expects(:put).with(PostQueryBuilder.new(nil).count_cache_key("aaa score:42"), 1, 180)
PostQueryBuilder.new("aaa score:42").fast_count
end
should "work with the hide_deleted_posts option turned on" do
user = create(:user, hide_deleted_posts: true)
as(user) do
assert_fast_count(1, "aaa score:42")
end
end
end
context "a blank search" do
should "should execute a search" do
assert_fast_count(1, "")
end
context "with a primed cache" do
should "fetch the value from the cache" do
PostQueryBuilder.new(nil).set_count_in_cache("", 100)
assert_fast_count(100, "")
end
end
should "return 0 for a nonexisting tag" do
assert_fast_count(0, "bbb")
end
context "in safe mode" do
setup do
CurrentUser.stubs(:safe_mode?).returns(true)
create(:post, rating: "s")
end
should "work for a blank search" do
assert_fast_count(1, "")
end
should "work for a nil search" do
assert_fast_count(1, nil)
end
should "not fail for a two tag search by a member" do
post1 = create(:post, tag_string: "aaa bbb rating:s")
post2 = create(:post, tag_string: "aaa bbb rating:e")
assert_fast_count(1, "aaa bbb")
end
context "with a primed cache" do
should "fetch the value from the cache" do
PostQueryBuilder.new(nil).set_count_in_cache("rating:s", 100)
assert_fast_count(100, "")
end
end
end
end
end
end

View File

@@ -1931,137 +1931,6 @@ class PostTest < ActiveSupport::TestCase
end
end
context "Counting:" do
context "Creating a post" do
setup do
Danbooru.config.stubs(:blank_tag_search_fast_count).returns(nil)
Danbooru.config.stubs(:estimate_post_counts).returns(false)
FactoryBot.create(:tag_alias, :antecedent_name => "alias", :consequent_name => "aaa")
FactoryBot.create(:post, :tag_string => "aaa", "score" => 42)
end
context "a single basic tag" do
should "return the cached count" do
Tag.find_or_create_by_name("aaa").update_columns(post_count: 100)
assert_equal(100, Post.fast_count("aaa"))
end
end
context "an aliased tag" do
should "return the count of the consequent tag" do
assert_equal(Post.fast_count("aaa"), Post.fast_count("alias"))
end
end
context "a single metatag" do
should "return the correct cached count" do
FactoryBot.build(:tag, name: "score:42", post_count: -100).save(validate: false)
Post.set_count_in_cache("score:42", 100)
assert_equal(100, Post.fast_count("score:42"))
end
should "return the correct cached count for a pool:<id> search" do
FactoryBot.build(:tag, name: "pool:1234", post_count: -100).save(validate: false)
Post.set_count_in_cache("pool:1234", 100)
assert_equal(100, Post.fast_count("pool:1234"))
end
end
context "a multi-tag search" do
should "return the cached count, if it exists" do
Post.set_count_in_cache("aaa score:42", 100)
assert_equal(100, Post.fast_count("aaa score:42"))
end
should "return the true count, if not cached" do
assert_equal(1, Post.fast_count("aaa score:42"))
end
should "set the expiration time" do
Cache.expects(:put).with(Post.count_cache_key("aaa score:42"), 1, 180)
Post.fast_count("aaa score:42")
end
should "work with the hide_deleted_posts option turned on" do
user = create(:user, hide_deleted_posts: true)
as(user) do
assert_equal(1, Post.fast_count("aaa score:42"))
end
end
end
context "a blank search" do
should "should execute a search" do
Cache.delete(Post.count_cache_key(''))
Post.expects(:fast_count_search).with("", kind_of(Hash)).once.returns(1)
assert_equal(1, Post.fast_count(""))
end
should "set the value in cache" do
Post.expects(:set_count_in_cache).with("", kind_of(Integer)).once
Post.fast_count("")
end
context "with a primed cache" do
setup do
Cache.put(Post.count_cache_key(''), "100")
end
should "fetch the value from the cache" do
assert_equal(100, Post.fast_count(""))
end
end
should_eventually "translate an alias" do
assert_equal(1, Post.fast_count("alias"))
end
should "return 0 for a nonexisting tag" do
assert_equal(0, Post.fast_count("bbb"))
end
context "in safe mode" do
setup do
CurrentUser.stubs(:safe_mode?).returns(true)
FactoryBot.create(:post, "rating" => "s")
end
should "work for a blank search" do
assert_equal(1, Post.fast_count(""))
end
should "work for a nil search" do
assert_equal(1, Post.fast_count(nil))
end
should "not fail for a two tag search by a member" do
post1 = FactoryBot.create(:post, tag_string: "aaa bbb rating:s")
post2 = FactoryBot.create(:post, tag_string: "aaa bbb rating:e")
assert_equal(1, Post.fast_count("aaa bbb"))
end
should "set the value in cache" do
Post.expects(:set_count_in_cache).with("rating:s", kind_of(Integer)).once
Post.fast_count("")
end
context "with a primed cache" do
setup do
Cache.put(Post.count_cache_key('rating:s'), "100")
end
should "fetch the value from the cache" do
assert_equal(100, Post.fast_count(""))
end
end
end
end
end
end
context "Reverting: " do
context "a post that is rating locked" do
setup do