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 20, 2021
1 parent ef122c8 commit e2f9439
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 e2f9439

Please sign in to comment.