@@ -9,37 +9,58 @@ class SelectTest < ActiveRecord::TestCase
9
9
fixtures :posts , :comments
10
10
11
11
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/\A SELECT #{ 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/\A SELECT 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/\A SELECT 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
14
24
end
15
25
16
26
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
21
31
end
22
32
23
33
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
28
38
end
29
39
30
40
def test_select_with_not_exists_field
41
+ expected = %r/\A SELECT #{ 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
+
31
46
assert_raises ( ActiveRecord ::StatementInvalid ) do
32
47
Post . select ( foo : :post_title ) . take
33
48
end
34
49
end
35
50
36
51
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/
53
+ assert_match expected , Post . select ( posts : { bar : :post_title } ) . to_sql
54
+
37
55
assert_raises ( ActiveRecord ::StatementInvalid ) do
38
56
Post . select ( posts : { boo : :post_title } ) . take
39
57
end
40
58
end
41
59
42
60
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/
62
+ assert_match expected , Post . select ( posts : [ :bar , :id ] ) . to_sql
63
+
43
64
assert_raises ( ActiveRecord ::StatementInvalid ) do
44
65
Post . select ( posts : [ :bar , :id ] ) . take
45
66
end
0 commit comments