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

@@ -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