mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Should Regexp.escape
quoted table name in regex
It is for agnostic test case, since quoted table name may include `.` for all adapters, and `[` / `]` for sqlserver adapter.
This commit is contained in:
parent
74ef67b16d
commit
7d699dad33
7 changed files with 51 additions and 115 deletions
|
@ -1,37 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "cases/helper"
|
||||
require "models/post"
|
||||
|
||||
class Mysql2AnnotateTest < ActiveRecord::Mysql2TestCase
|
||||
fixtures :posts
|
||||
|
||||
def test_annotate_wraps_content_in_an_inline_comment
|
||||
assert_sql(%r{\ASELECT `posts`\.`id` FROM `posts` /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("foo")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
|
||||
def test_annotate_is_sanitized
|
||||
assert_sql(%r{\ASELECT `posts`\.`id` FROM `posts` /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("*/foo/*")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT `posts`\.`id` FROM `posts` /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("**//foo//**")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT `posts`\.`id` FROM `posts` /\* foo \*/ /\* bar \*/}) do
|
||||
posts = Post.select(:id).annotate("*/foo/*").annotate("*/bar")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT `posts`\.`id` FROM `posts` /\* \+ MAX_EXECUTION_TIME\(1\) \*/}) do
|
||||
posts = Post.select(:id).annotate("+ MAX_EXECUTION_TIME(1)")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,37 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "cases/helper"
|
||||
require "models/post"
|
||||
|
||||
class PostgresqlAnnotateTest < ActiveRecord::PostgreSQLTestCase
|
||||
fixtures :posts
|
||||
|
||||
def test_annotate_wraps_content_in_an_inline_comment
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("foo")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
|
||||
def test_annotate_is_sanitized
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("*/foo/*")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("**//foo//**")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/ /\* bar \*/}) do
|
||||
posts = Post.select(:id).annotate("*/foo/*").annotate("*/bar")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* \+ MAX_EXECUTION_TIME\(1\) \*/}) do
|
||||
posts = Post.select(:id).annotate("+ MAX_EXECUTION_TIME(1)")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,37 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "cases/helper"
|
||||
require "models/post"
|
||||
|
||||
class SQLite3AnnotateTest < ActiveRecord::SQLite3TestCase
|
||||
fixtures :posts
|
||||
|
||||
def test_annotate_wraps_content_in_an_inline_comment
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("foo")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
|
||||
def test_annotate_is_sanitized
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("*/foo/*")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/}) do
|
||||
posts = Post.select(:id).annotate("**//foo//**")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* foo \*/ /\* bar \*/}) do
|
||||
posts = Post.select(:id).annotate("*/foo/*").annotate("*/bar")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT "posts"\."id" FROM "posts" /\* \+ MAX_EXECUTION_TIME\(1\) \*/}) do
|
||||
posts = Post.select(:id).annotate("+ MAX_EXECUTION_TIME(1)")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
end
|
46
activerecord/test/cases/annotate_test.rb
Normal file
46
activerecord/test/cases/annotate_test.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "cases/helper"
|
||||
require "models/post"
|
||||
|
||||
class AnnotateTest < ActiveRecord::TestCase
|
||||
fixtures :posts
|
||||
|
||||
def test_annotate_wraps_content_in_an_inline_comment
|
||||
quoted_posts_id, quoted_posts = regexp_escape_table_name("posts.id"), regexp_escape_table_name("posts")
|
||||
|
||||
assert_sql(%r{\ASELECT #{quoted_posts_id} FROM #{quoted_posts} /\* foo \*/}i) do
|
||||
posts = Post.select(:id).annotate("foo")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
|
||||
def test_annotate_is_sanitized
|
||||
quoted_posts_id, quoted_posts = regexp_escape_table_name("posts.id"), regexp_escape_table_name("posts")
|
||||
|
||||
assert_sql(%r{\ASELECT #{quoted_posts_id} FROM #{quoted_posts} /\* foo \*/}i) do
|
||||
posts = Post.select(:id).annotate("*/foo/*")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT #{quoted_posts_id} FROM #{quoted_posts} /\* foo \*/}i) do
|
||||
posts = Post.select(:id).annotate("**//foo//**")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT #{quoted_posts_id} FROM #{quoted_posts} /\* foo \*/ /\* bar \*/}i) do
|
||||
posts = Post.select(:id).annotate("*/foo/*").annotate("*/bar")
|
||||
assert posts.first
|
||||
end
|
||||
|
||||
assert_sql(%r{\ASELECT #{quoted_posts_id} FROM #{quoted_posts} /\* \+ MAX_EXECUTION_TIME\(1\) \*/}i) do
|
||||
posts = Post.select(:id).annotate("+ MAX_EXECUTION_TIME(1)")
|
||||
assert posts.first
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def regexp_escape_table_name(name)
|
||||
Regexp.escape(Post.connection.quote_table_name(name))
|
||||
end
|
||||
end
|
|
@ -146,7 +146,7 @@ class EachTest < ActiveRecord::TestCase
|
|||
|
||||
def test_find_in_batches_should_quote_batch_order
|
||||
c = Post.connection
|
||||
assert_sql(/ORDER BY #{c.quote_table_name('posts')}\.#{c.quote_column_name('id')}/) do
|
||||
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("posts.id"))}/i) do
|
||||
Post.find_in_batches(batch_size: 1) do |batch|
|
||||
assert_kind_of Array, batch
|
||||
assert_kind_of Post, batch.first
|
||||
|
|
|
@ -245,7 +245,8 @@ class FinderTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_exists_does_not_select_columns_without_alias
|
||||
assert_sql(/SELECT\W+1 AS one FROM ["`]topics["`]/i) do
|
||||
c = Topic.connection
|
||||
assert_sql(/SELECT 1 AS one FROM #{Regexp.escape(c.quote_table_name("topics"))}/i) do
|
||||
Topic.exists?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -471,9 +471,9 @@ class InheritanceTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_eager_load_belongs_to_primary_key_quoting
|
||||
con = Account.connection
|
||||
c = Account.connection
|
||||
bind_param = Arel::Nodes::BindParam.new(nil)
|
||||
assert_sql(/#{con.quote_table_name('companies')}\.#{con.quote_column_name('id')} = (?:#{Regexp.quote(bind_param.to_sql)}|1)/) do
|
||||
assert_sql(/#{Regexp.escape(c.quote_table_name("companies.id"))} = (?:#{Regexp.escape(bind_param.to_sql)}|1)/i) do
|
||||
Account.all.merge!(includes: :firm).find(1)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue