2024-09-10 23:41:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe PostsController do
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, user: admin) }
|
|
|
|
|
|
|
|
fab!(:post1) { Fabricate(:post, topic:, raw: "[poll]\n- A\n- B\n[/poll]") }
|
|
|
|
fab!(:post2) { Fabricate(:post, topic:, raw: "[poll results=on_vote]\n- A\n- B\n[/poll]") }
|
|
|
|
fab!(:post3) { Fabricate(:post, topic:, raw: "[poll results=on_vote]\n- A\n- B\n[/poll]") }
|
|
|
|
fab!(:post4) { Fabricate(:post, topic:, raw: "[poll results=on_vote]\n- A\n- B\n[/poll]") }
|
2024-09-11 15:14:53 +08:00
|
|
|
fab!(:post5) do
|
|
|
|
Fabricate(:post, topic:, raw: "[poll public=true results=staff_only]\n- A\n- B\n[/poll]")
|
|
|
|
end
|
|
|
|
fab!(:post6) do
|
|
|
|
Fabricate(:post, topic:, raw: "[poll public=true results=staff_only]\n- A\n- B\n[/poll]")
|
|
|
|
end
|
|
|
|
fab!(:post7) { Fabricate(:post, topic:, raw: "[poll public=true]\n- A\n- B\n[/poll]") }
|
2024-09-10 23:41:08 +08:00
|
|
|
|
|
|
|
describe "#show" do
|
|
|
|
context "when not logged in" do
|
|
|
|
it "does not create N+1 queries to load polls" do
|
|
|
|
queries = track_sql_queries { get "/t/#{topic.id}.json" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
poll_queries = queries.filter { |q| q =~ /FROM "?poll/ }
|
|
|
|
# Expected queries:
|
|
|
|
#
|
|
|
|
# - load all polls
|
|
|
|
# - load all options
|
|
|
|
# - count votes for each poll
|
|
|
|
# - count votes for each option
|
2024-09-11 15:14:53 +08:00
|
|
|
# - voters for poll in post7
|
|
|
|
expect(poll_queries.size).to eq(5)
|
2024-09-10 23:41:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "does not create N+1 queries to load polls" do
|
|
|
|
queries = track_sql_queries { get "/t/#{topic.id}.json" }
|
|
|
|
|
|
|
|
poll_queries = queries.filter { |q| q =~ /FROM "?poll/ }
|
|
|
|
|
|
|
|
# Expected queries:
|
|
|
|
#
|
|
|
|
# - all queries listed for "when not logged in"
|
|
|
|
# - query to find out if the user has voted in each poll
|
|
|
|
# - queries to get "serialized voters" (NOT TRACKED)
|
2024-09-11 15:14:53 +08:00
|
|
|
# - voters for poll in post5
|
|
|
|
# - voters for poll in post6
|
|
|
|
# - voters for poll in post7
|
|
|
|
expect(poll_queries.size).to eq(8)
|
2024-09-10 23:41:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|