DEV: cleanup custom filters to prevent leaks

Ensures we clean up any custom filters added in the specs to prevent any leaks when running the specs.

Follow up to https://github.com/discourse/discourse/pull/26770#discussion_r1582464760
This commit is contained in:
Régis Hanol 2024-04-29 15:56:52 +02:00
parent 271ca2c968
commit f7a1272fa4
7 changed files with 22 additions and 13 deletions

View File

@ -349,19 +349,19 @@ class Search
end
def self.advanced_order(trigger, &block)
(@advanced_orders ||= {})[trigger] = block
advanced_orders[trigger] = block
end
def self.advanced_orders
@advanced_orders
@advanced_orders ||= {}
end
def self.advanced_filter(trigger, &block)
(@advanced_filters ||= {})[trigger] = block
advanced_filters[trigger] = block
end
def self.advanced_filters
@advanced_filters
@advanced_filters ||= {}
end
def self.custom_topic_eager_load(tables = nil, &block)

View File

@ -73,12 +73,11 @@ class TopicView
end
def self.add_custom_filter(key, &blk)
@custom_filters ||= {}
@custom_filters[key] = blk
custom_filters[key] = blk
end
def self.custom_filters
@custom_filters || {}
@custom_filters ||= {}
end
# Configure a default scope to be applied to @filtered_posts.

View File

@ -87,11 +87,11 @@ class TopicsFilter
end
def self.add_filter_by_status(status, &blk)
(@custom_status_filters ||= {})[status] = blk
custom_status_filters[status] = blk
end
def self.custom_status_filters
@custom_status_filters || {}
@custom_status_filters ||= {}
end
def filter_status(status:, category_id: nil)

View File

@ -2635,11 +2635,18 @@ RSpec.describe Search do
end
let!(:post1) { Fabricate(:post, raw: "this is the second post about advanced filter") }
after do
Search.advanced_filters.clear
Search.advanced_orders.clear
end
it "allows to define custom filter" do
expect(Search.new("advanced").execute.posts).to eq([post1, post0])
Search.advanced_filter(/^min_chars:(\d+)$/) do |posts, match|
posts.where("(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?", match.to_i)
end
expect(Search.new("advanced min_chars:50").execute.posts).to eq([post0])
end

View File

@ -72,6 +72,8 @@ RSpec.describe TopicView do
fab!(:p0) { Fabricate(:post, topic: topic) }
fab!(:p1) { Fabricate(:post, topic: topic, wiki: true) }
after { TopicView.custom_filters.clear }
it "allows to register custom filters" do
tv = TopicView.new(topic.id, evil_trout, { filter: "wiki" })
expect(tv.filter_posts({ filter: "wiki" })).to eq([p0, p1])
@ -83,8 +85,6 @@ RSpec.describe TopicView do
tv = TopicView.new(topic.id, evil_trout, { filter: "whatever" })
expect(tv.filter_posts).to eq([p0, p1])
ensure
TopicView.instance_variable_set(:@custom_filters, {})
end
end

View File

@ -619,6 +619,8 @@ RSpec.describe TopicsFilter do
fab!(:deleted_topic_id) { Fabricate(:topic, deleted_at: Time.zone.now).id }
fab!(:foobar_topic) { Fabricate(:topic, closed: true, word_count: 42) }
after { TopicsFilter.custom_status_filters.clear }
it "supports custom status filters" do
TopicsFilter.add_filter_by_status("foobar") { |scope| scope.where("word_count = 42") }

View File

@ -3336,6 +3336,9 @@ RSpec.describe TopicsController do
describe "custom filters" do
fab!(:post2) { Fabricate(:post, user: post_author2, topic: topic, percent_rank: 0.2) }
fab!(:post3) { Fabricate(:post, user: post_author3, topic: topic, percent_rank: 0.5) }
after { TopicView.custom_filters.clear }
it "should return the right posts" do
TopicView.add_custom_filter("percent") do |posts, topic_view|
posts.where(percent_rank: 0.5)
@ -3348,8 +3351,6 @@ RSpec.describe TopicsController do
body = response.parsed_body
expect(body["post_stream"]["posts"].map { |p| p["id"] }).to eq([post3.id])
ensure
TopicView.instance_variable_set(:@custom_filters, {})
end
end
end