Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/readings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index
@readings = @readings.where(toread: true)
@page_subtitle = t(".marked_for_later_page_title")
end
@readings = @readings.order("last_viewed DESC")
@readings = @readings.order("last_viewed DESC").includes(work: :pseuds)
@pagy, @readings = pagy(@readings)
end

Expand Down
53 changes: 53 additions & 0 deletions spec/requests/readings_n_plus_one_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "spec_helper"

describe "n+1 queries in the readings controller" do
include LoginMacros

describe "#index", n_plus_one: true do
context "when displaying a user's reading history" do
let!(:user) { create(:user) }

subject do
proc do
get user_readings_path(user)
end
end

before do
fake_login_known_user(user)
end

context "when all works are cached" do
populate do |n|
create_list(:reading, n, user: user)
subject.call # this caches the blurbs
end

it "produces a constant number of queries" do
expect do
subject.call
expect(response.body.scan('<li id="work_').size).to eq(current_scale.to_i)
end.to perform_constant_number_of_queries
end
end

context "when nothing is cached" do
populate do |n|
create_list(:reading, n, user: user)
end

it "performs around 12 queries per reading" do
# TODO: Ideally, we'd like the uncached reading listings to also have a
# constant number of queries, instead of the linear number of queries
# we're checking for here. But we also don't want to put too much
# unnecessary load on the databases when we have a bunch of cache hits,
# so this requires some care & tuning.
expect do
subject.call
expect(response.body.scan('<li id="work_').size).to eq(current_scale.to_i)
end.to perform_linear_number_of_queries(slope: 12).with_warming_up
end
end
end
end
end
Loading