mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
ruby: add tests
This commit is contained in:
@@ -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