mirror of
https://github.com/github/codeql.git
synced 2025-12-18 01:33:15 +01:00
ruby: add tests
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
| DatabaseQueryInLoop.rb:11:13:11:52 | call to first | This call to a database query operation happens inside $@, and could be hoisted to a single call outside the loop. | DatabaseQueryInLoop.rb:10:9:12:11 | call to map | this loop |
|
||||||
|
| DatabaseQueryInLoop.rb:16:20:16:59 | call to first | This call to a database query operation happens inside $@, and could be hoisted to a single call outside the loop. | DatabaseQueryInLoop.rb:15:9:21:11 | call to map | this loop |
|
||||||
|
| DatabaseQueryInLoop.rb:19:17:19:56 | call to first | This call to a database query operation happens inside $@, and could be hoisted to a single call outside the loop. | DatabaseQueryInLoop.rb:18:13:20:15 | call to map | this loop |
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
queries/performance/DatabaseQueryInLoop.ql
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
class User < ActiveRecord::Base
|
||||||
|
end
|
||||||
|
|
||||||
|
class DatabaseQueryInLoopTest
|
||||||
|
def test
|
||||||
|
### These are bad
|
||||||
|
|
||||||
|
# simple query in loop
|
||||||
|
names.map do |name|
|
||||||
|
User.where(login: name).pluck(:id).first
|
||||||
|
end
|
||||||
|
|
||||||
|
# nested loop
|
||||||
|
names.map do |name|
|
||||||
|
user = User.where(login: name).pluck(:id).first
|
||||||
|
|
||||||
|
ids.map do |user_id|
|
||||||
|
User.where(id: user_id).pluck(:id).first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
### These are OK
|
||||||
|
|
||||||
|
# Not in loop
|
||||||
|
User.where(login: owner_slug).pluck(:id).first
|
||||||
|
|
||||||
|
# Loops over constant array
|
||||||
|
%w(first-name second-name).map { |name| User.where(login: name).pluck(:id).first }
|
||||||
|
|
||||||
|
constant_names = [first-name, second-name]
|
||||||
|
constant_names.each do |name|
|
||||||
|
User.where(login: name).pluck(:id).first
|
||||||
|
end
|
||||||
|
|
||||||
|
# Loop traversal is influenced by query result
|
||||||
|
# raising an exception if the user is not found
|
||||||
|
names.map do |name|
|
||||||
|
user = User.where(login: name).pluck(:id).first
|
||||||
|
unless user
|
||||||
|
raise Error.new("User '#{name}' not found")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# skipping through the loop when users are not relevant
|
||||||
|
names.map do |name|
|
||||||
|
user = User.where(login: name).pluck(:id).first
|
||||||
|
if not isRelevant(user)
|
||||||
|
next
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user