Skip to content

Commit 5947bc8

Browse files
committed
Add tests for how select quotes strings and symbols
Follow up to ba468db.
1 parent 61104c3 commit 5947bc8

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

activerecord/test/cases/relation/select_test.rb

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,58 @@ class SelectTest < ActiveRecord::TestCase
99
fixtures :posts, :comments
1010

1111
def test_select_with_nil_argument
12-
expected = Post.select(:title).to_sql
13-
assert_equal expected, Post.select(nil).select(:title).to_sql
12+
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("posts.title"))} FROM/
13+
assert_match expected, Post.select(nil).select(:title).to_sql
14+
end
15+
16+
def test_select_with_non_field_values
17+
expected = %r/\ASELECT 1, foo\(\), #{Regexp.escape(quote_table_name("bar"))} FROM/
18+
assert_match expected, Post.select("1", "foo()", :bar).to_sql
19+
end
20+
21+
def test_select_with_non_field_hash_values
22+
expected = %r/\ASELECT 1 AS a, foo\(\) AS b, #{Regexp.escape(quote_table_name("bar"))} AS c FROM/
23+
assert_match expected, Post.select("1" => :a, "foo()" => :b, :bar => :c).to_sql
1424
end
1525

1626
def test_select_with_hash_argument
17-
post = Post.select(:title, posts: { title: :post_title }).take
18-
assert_not_nil post.title
19-
assert_not_nil post.post_title
20-
assert_equal post.title, post.post_title
27+
post = Post.select("UPPER(title)" => :title, posts: { title: :post_title }).first
28+
29+
assert_equal "WELCOME TO THE WEBLOG", post.title
30+
assert_equal "Welcome to the weblog", post.post_title
2131
end
2232

2333
def test_select_with_one_level_hash_argument
24-
post = Post.select(:title, title: :post_title).take
25-
assert_not_nil post.title
26-
assert_not_nil post.post_title
27-
assert_equal post.title, post.post_title
34+
post = Post.select("UPPER(title)" => :title, title: :post_title).first
35+
36+
assert_equal "WELCOME TO THE WEBLOG", post.title
37+
assert_equal "Welcome to the weblog", post.post_title
2838
end
2939

3040
def test_select_with_not_exists_field
41+
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("foo"))} AS post_title FROM/
42+
assert_match expected, Post.select(foo: :post_title).to_sql
43+
44+
skip if sqlite3_adapter_strict_strings_disabled?
45+
3146
assert_raises(ActiveRecord::StatementInvalid) do
3247
Post.select(foo: :post_title).take
3348
end
3449
end
3550

3651
def test_select_with_hash_with_not_exists_field
52+
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("posts.bar"))} AS post_title FROM/
53+
assert_match expected, Post.select(posts: { bar: :post_title }).to_sql
54+
3755
assert_raises(ActiveRecord::StatementInvalid) do
3856
Post.select(posts: { boo: :post_title }).take
3957
end
4058
end
4159

4260
def test_select_with_hash_array_value_with_not_exists_field
61+
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("posts.bar"))}, #{Regexp.escape(quote_table_name("posts.id"))} FROM/
62+
assert_match expected, Post.select(posts: [:bar, :id]).to_sql
63+
4364
assert_raises(ActiveRecord::StatementInvalid) do
4465
Post.select(posts: [:bar, :id]).take
4566
end

activerecord/test/support/adapter_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ def current_adapter?(*types)
99
end
1010

1111
def in_memory_db?
12-
current_adapter?(:SQLite3Adapter) &&
13-
ActiveRecord::Base.connection_pool.db_config.database == ":memory:"
12+
current_adapter?(:SQLite3Adapter) && ActiveRecord::Base.connection_pool.db_config.database == ":memory:"
13+
end
14+
15+
def sqlite3_adapter_strict_strings_disabled?
16+
current_adapter?(:SQLite3Adapter) && !ActiveRecord::Base.connection_pool.db_config.configuration_hash[:strict]
1417
end
1518

1619
def mysql_enforcing_gtid_consistency?

0 commit comments

Comments
 (0)