FIX: TopicQuery for NULL category.topic_id (#20664)

Our schema allows `category.topic_id` to be NULL. Null values shouldn't actually happen in production, but it is very common in tests because `Fabricate(:category)` skips creating the definition topic to improve performance. Before this commit, a NULL category.topic_id would cause all subcategory topics to be excluded from a TopicQuery result. This is because, in postgres, `NULL <> anything` is falsy. Instead, we can use `IS DISTINCT FROM`, which will return true when NULL is compared to a non-NULL value.
This commit is contained in:
David Taylor
2023-03-13 19:33:26 +00:00
committed by GitHub
parent 0a5b078ac7
commit 964f37476d
3 changed files with 40 additions and 3 deletions

View File

@ -1909,4 +1909,31 @@ RSpec.describe TopicQuery do
)
end
end
describe "show_category_definitions_in_topic_lists setting" do
fab!(:category) { Fabricate(:category_with_definition) }
fab!(:subcategory) { Fabricate(:category_with_definition, parent_category: category) }
fab!(:subcategory_regular_topic) { Fabricate(:topic, category: subcategory) }
it "excludes subcategory definition topics by default" do
expect(
TopicQuery.new(nil, category: category.id).list_latest.topics.map(&:id),
).to contain_exactly(category.topic_id, subcategory_regular_topic.id)
end
it "works when topic_id is null" do
subcategory.topic.destroy!
subcategory.update!(topic_id: nil)
expect(
TopicQuery.new(nil, category: category.id).list_latest.topics.map(&:id),
).to contain_exactly(category.topic_id, subcategory_regular_topic.id)
end
it "includes subcategory definition when setting enabled" do
SiteSetting.show_category_definitions_in_topic_lists = true
expect(
TopicQuery.new(nil, category: category.id).list_latest.topics.map(&:id),
).to contain_exactly(category.topic_id, subcategory.topic_id, subcategory_regular_topic.id)
end
end
end