[Tests] More performance improvements

Most of the gained time is because some post tests don't recreate the
elastic index twice now.
Tests go from 107 seconds to 95 on my machine.
This commit is contained in:
Earlopain 2023-09-08 19:51:35 +02:00
parent 37ff30db3a
commit c53b2eff60
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
8 changed files with 13 additions and 18 deletions

View File

@ -83,6 +83,12 @@ class ActiveSupport::TestCase
mod.send(:remove_const, constant)
mod.const_set(constant, old_value)
end
def reset_post_index
# This seems slightly faster than deleting and recreating the index
Post.__elasticsearch__.client.delete_by_query(index: Post.index_name, q: "*", body: {})
Post.__elasticsearch__.refresh_index!
end
end
class ActionDispatch::IntegrationTest

View File

@ -22,7 +22,7 @@ class BulkUpdateRequestImporterTest < ActiveSupport::TestCase
context "#estimate_update_count" do
setup do
Post.__elasticsearch__.create_index! force: true
reset_post_index
create(:post, tag_string: "aaa")
create(:post, tag_string: "bbb")
create(:post, tag_string: "ccc")

View File

@ -9,7 +9,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
context "#estimate_update_count" do
setup do
Post.__elasticsearch__.create_index! force: true
reset_post_index
create(:post, tag_string: "aaa")
create(:post, tag_string: "bbb")
create(:post, tag_string: "ccc")

View File

@ -8,7 +8,7 @@ class PostTest < ActiveSupport::TestCase
setup do
@user = create(:user, created_at: 2.weeks.ago)
CurrentUser.user = @user
Post.__elasticsearch__.create_index!
reset_post_index
end
context "Deletion:" do
@ -1488,14 +1488,6 @@ class PostTest < ActiveSupport::TestCase
end
context "Searching:" do
setup do
Post.__elasticsearch__.create_index!
end
teardown do
Post.__elasticsearch__.delete_index!
end
should "return posts for the age:<1minute tag" do
post = create(:post)
assert_tag_match([post], "age:<1minute")
@ -2033,8 +2025,6 @@ class PostTest < ActiveSupport::TestCase
# context "Counting:" do
# context "Creating a post" do
# setup do
# Post.__elasticsearch__.delete_index!
# Post.__elasticsearch__.create_index!
# create(:tag_alias, antecedent_name: "alias", consequent_name: "aaa")
# create(:post, tag_string: "aaa", score: 42)
# end
@ -2105,7 +2095,6 @@ class PostTest < ActiveSupport::TestCase
# setup do
# CurrentUser.stubs(:safe_mode?).returns(true)
# create(:post, rating: "s")
# Post.__elasticsearch__.refresh_index!
# end
#
# should "work for a blank search" do

View File

@ -4,7 +4,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
setup do
user = create(:user)
CurrentUser.user = user
Post.__elasticsearch__.create_index! force: true
reset_post_index
end
context "A related tag calculator" do

View File

@ -48,7 +48,7 @@ class TagAliasTest < ActiveSupport::TestCase
context "#estimate_update_count" do
setup do
Post.__elasticsearch__.create_index! force: true
reset_post_index
create(:post, tag_string: "aaa bbb ccc")
@alias = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "pending")
end

View File

@ -45,7 +45,7 @@ class TagImplicationTest < ActiveSupport::TestCase
context "#estimate_update_count" do
setup do
Post.__elasticsearch__.create_index! force: true
reset_post_index
create(:post, tag_string: "aaa bbb ccc")
@implication = create(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", status: "pending")
end

View File

@ -188,7 +188,7 @@ class TagTest < ActiveSupport::TestCase
context "A tag with a negative post count" do
should "be fixed" do
Post.__elasticsearch__.create_index! force: true
reset_post_index
tag = create(:tag, name: "touhou", post_count: -10)
post = create(:post, tag_string: "touhou")