Skip to content

Commit

Permalink
Fixed. NPlusOneQuery duplicate offense
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Oct 23, 2021
1 parent 7c88e47 commit 16620f0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions spec/rubocop/cop/isucon/mysql2/n_plus_one_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@
end
end

context "with single line SQL (2)" do
it "registers an offense" do
# c.f. https://github.com/isucon/isucon11-final/blob/667be3ec70c025eadde541e21d5ab1167efa1dd3/webapp/ruby/app.rb#L171-L190
expect_offense(<<~RUBY)
courses = db.xquery(
"SELECT `courses`.*" \\
" FROM `courses`" \\
" JOIN `registrations` ON `courses`.`id` = `registrations`.`course_id`" \\
" WHERE `courses`.`status` != ? AND `registrations`.`user_id` = ?",
STATUS_CLOSED, user_id,
)
courses.map do |course|
teacher = db.xquery('SELECT * FROM `users` WHERE `id` = ?', course[:teacher_id]).first
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This looks like N+1 query.
raise unless teacher
{
id: course[:id],
name: course[:name],
teacher: teacher[:name],
period: course[:period],
day_of_week: course[:day_of_week],
}
end
RUBY
end
end

context "with multiple line SQL" do
it "registers an offense" do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 16620f0

Please sign in to comment.