@@ -19,7 +19,8 @@ def test_select_with_non_field_values
19
19
end
20
20
21
21
def test_select_with_non_field_hash_values
22
- expected = %r/\A SELECT 1 AS a, foo\( \) AS b, #{ Regexp . escape ( quote_table_name ( "bar" ) ) } AS c FROM/
22
+ q = -> name { Regexp . escape ( quote_table_name ( name ) ) }
23
+ expected = %r/\A SELECT 1 AS #{ q [ "a" ] } , foo\( \) AS #{ q [ "b" ] } , #{ q [ "bar" ] } AS #{ q [ "c" ] } FROM/
23
24
assert_match expected , Post . select ( "1" => :a , "foo()" => :b , :bar => :c ) . to_sql
24
25
end
25
26
@@ -30,6 +31,13 @@ def test_select_with_hash_argument
30
31
assert_equal "Welcome to the weblog" , post . post_title
31
32
end
32
33
34
+ def test_select_with_reserved_words_aliases
35
+ post = Post . select ( "UPPER(title)" => :from , title : :group ) . first
36
+
37
+ assert_equal "WELCOME TO THE WEBLOG" , post . from
38
+ assert_equal "Welcome to the weblog" , post . group
39
+ end
40
+
33
41
def test_select_with_one_level_hash_argument
34
42
post = Post . select ( "UPPER(title)" => :title , title : :post_title ) . first
35
43
@@ -38,7 +46,8 @@ def test_select_with_one_level_hash_argument
38
46
end
39
47
40
48
def test_select_with_not_exists_field
41
- expected = %r/\A SELECT #{ Regexp . escape ( quote_table_name ( "foo" ) ) } AS post_title FROM/
49
+ q = -> name { Regexp . escape ( quote_table_name ( name ) ) }
50
+ expected = %r/\A SELECT #{ q [ "foo" ] } AS #{ q [ "post_title" ] } FROM/
42
51
assert_match expected , Post . select ( foo : :post_title ) . to_sql
43
52
44
53
skip if sqlite3_adapter_strict_strings_disabled?
@@ -49,7 +58,8 @@ def test_select_with_not_exists_field
49
58
end
50
59
51
60
def test_select_with_hash_with_not_exists_field
52
- expected = %r/\A SELECT #{ Regexp . escape ( quote_table_name ( "posts.bar" ) ) } AS post_title FROM/
61
+ q = -> name { Regexp . escape ( quote_table_name ( name ) ) }
62
+ expected = %r/\A SELECT #{ q [ "posts.bar" ] } AS #{ q [ "post_title" ] } FROM/
53
63
assert_match expected , Post . select ( posts : { bar : :post_title } ) . to_sql
54
64
55
65
assert_raises ( ActiveRecord ::StatementInvalid ) do
@@ -58,7 +68,8 @@ def test_select_with_hash_with_not_exists_field
58
68
end
59
69
60
70
def test_select_with_hash_array_value_with_not_exists_field
61
- expected = %r/\A SELECT #{ Regexp . escape ( quote_table_name ( "posts.bar" ) ) } , #{ Regexp . escape ( quote_table_name ( "posts.id" ) ) } FROM/
71
+ q = -> name { Regexp . escape ( quote_table_name ( name ) ) }
72
+ expected = %r/\A SELECT #{ q [ "posts.bar" ] } , #{ q [ "posts.id" ] } FROM/
62
73
assert_match expected , Post . select ( posts : [ :bar , :id ] ) . to_sql
63
74
64
75
assert_raises ( ActiveRecord ::StatementInvalid ) do
0 commit comments