Skip to content

Commit ba63c41

Browse files
committed
Coerce tests.
ActiveRecord::AdapterTest#test_update_prepared_statement BelongsToAssociationsTest#test_belongs_to_does_not_use_order_by BelongsToAssociationsTest#test_belongs_to_with_primary_key_joins_on_correct_column EagerAssociationTest#test_0013_including association based on sql condition and no database column EagerAssociationTest#test_count_with_include: AttributeMethodsTest#test_typecast_attribute_from_select_to_false AttributeMethodsTest#test_typecast_attribute_from_select_to_true BasicsTest#test_column_names_are_escaped: 29 failures, 17 errors
1 parent 8bfd152 commit ba63c41

File tree

1 file changed

+177
-73
lines changed

1 file changed

+177
-73
lines changed

test/cases/coerced_tests.rb

Lines changed: 177 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
11
require 'cases/helper_sqlserver'
22

33

4-
require 'models/post'
5-
class SanitizeTest < ActiveRecord::TestCase
4+
module ActiveRecord
5+
class AdapterTest < ActiveRecord::TestCase
6+
7+
# As far as I can tell, SQL Server does not support null bytes in strings.
8+
coerce_tests! :test_update_prepared_statement
69

7-
coerce_tests! :test_sanitize_sql_like_example_use_case
8-
def test_sanitize_sql_like_example_use_case_coerced
9-
searchable_post = Class.new(Post) do
10-
def self.search(term)
11-
where("title LIKE ?", sanitize_sql_like(term, '!'))
12-
end
13-
end
14-
assert_sql(/\(title LIKE N''20!% !_reduction!_!!''\)/) do
15-
searchable_post.search("20% _reduction_!").to_a
16-
end
10+
end
11+
end
12+
13+
14+
15+
16+
require 'models/topic'
17+
class AttributeMethodsTest < ActiveRecord::TestCase
18+
19+
coerce_tests! :test_typecast_attribute_from_select_to_false
20+
def test_typecast_attribute_from_select_to_false_coerced
21+
Topic.create(:title => 'Budget')
22+
topic = Topic.all.merge!(:select => "topics.*, IIF (1 = 2, 1, 0) as is_test").first
23+
assert !topic.is_test?
24+
end
25+
26+
coerce_tests! :test_typecast_attribute_from_select_to_true
27+
def test_typecast_attribute_from_select_to_true_coerced
28+
Topic.create(:title => 'Budget')
29+
topic = Topic.all.merge!(:select => "topics.*, IIF (1 = 1, 1, 0) as is_test").first
30+
assert topic.is_test?
1731
end
1832

1933
end
2034

2135

2236

23-
require 'models/author'
24-
class YamlSerializationTest < ActiveRecord::TestCase
2537

26-
coerce_tests! :test_types_of_virtual_columns_are_not_changed_on_round_trip
27-
def test_types_of_virtual_columns_are_not_changed_on_round_trip_coerced
28-
author = Author.select('authors.*, 5 as posts_count').first
29-
dumped = YAML.load(YAML.dump(author))
30-
assert_equal 5, author.posts_count
31-
assert_equal 5, dumped.posts_count
38+
class BasicsTest < ActiveRecord::TestCase
39+
40+
coerce_tests! :test_column_names_are_escaped
41+
def test_column_names_are_escaped_coerced
42+
conn = ActiveRecord::Base.connection
43+
classname = conn.class.name[/[^:]*$/]
44+
badchar = "'"
45+
quoted = conn.quote_column_name "foo#{badchar}bar"
46+
assert_equal("#{badchar}foo#{badchar * 2}bar#{badchar}", quoted)
3247
end
3348

3449
end
3550

3651

3752

53+
54+
class BelongsToAssociationsTest < ActiveRecord::TestCase
55+
56+
# Since @client.firm is a single first/top, and we use FETCH the order clause is used.
57+
coerce_tests! :test_belongs_to_does_not_use_order_by
58+
59+
coerce_tests! :test_belongs_to_with_primary_key_joins_on_correct_column
60+
def test_belongs_to_with_primary_key_joins_on_correct_column_coerced
61+
sql = Client.joins(:firm_with_primary_key).to_sql
62+
assert_no_match(/\[firm_with_primary_keys_companies\]\.\[id\]/, sql)
63+
assert_match(/\[firm_with_primary_keys_companies\]\.\[name\]/, sql)
64+
end
65+
66+
end
67+
68+
69+
70+
3871
module ActiveRecord
3972
module ConnectionAdapters
4073

@@ -51,22 +84,84 @@ module ConnectionAdapters
5184

5285

5386

87+
5488
require 'models/post'
55-
module ActiveRecord
56-
class WhereChainTest < ActiveRecord::TestCase
89+
require 'models/subscriber'
90+
class EachTest < ActiveRecord::TestCase
5791

58-
coerce_tests! :test_not_eq_with_array_parameter
59-
def test_not_eq_with_array_parameter_coerced
60-
expected = Arel::Nodes::Not.new("title = N'hello'")
61-
relation = Post.where.not(['title = ?', 'hello'])
62-
assert_equal([expected], relation.where_values)
92+
coerce_tests! :test_find_in_batches_should_quote_batch_order
93+
def test_find_in_batches_should_quote_batch_order_coerced
94+
c = Post.connection
95+
assert_sql(/ORDER BY \[posts\]\.\[id\]/) do
96+
Post.find_in_batches(:batch_size => 1) do |batch|
97+
assert_kind_of Array, batch
98+
assert_kind_of Post, batch.first
99+
end
100+
end
101+
end
102+
103+
end
104+
105+
106+
107+
108+
require 'models/owner'
109+
class Owner < ActiveRecord::Base
110+
scope :including_last_pet, -> {
111+
select('owners.*, (select TOP (1) p.pet_id from pets p where p.owner_id = owners.owner_id order by p.name desc ) as last_pet_id').
112+
includes(:last_pet)
113+
}
114+
end
115+
class EagerAssociationTest < ActiveRecord::TestCase
116+
117+
# Use LEN() vs length() function.
118+
coerce_tests! :test_count_with_include
119+
def test_count_with_include_coerced
120+
assert_equal 3, authors(:david).posts_with_comments.where("LEN(comments.body) > 15").references(:comments).count
121+
end
122+
123+
# Use TOP (1) in scope vs limit 1.
124+
coerce_tests! %r{including association based on sql condition and no database column}
125+
it "including association based on sql condition and no database column coerced" do
126+
assert_equal pets(:parrot), Owner.including_last_pet.first.last_pet
127+
end
128+
129+
end
130+
131+
132+
133+
134+
require 'models/topic'
135+
class FinderTest < ActiveRecord::TestCase
136+
137+
coerce_tests! %r{doesn't have implicit ordering},
138+
:test_find_doesnt_have_implicit_ordering # We have implicit ordering, via FETCH.
139+
140+
coerce_tests! :test_exists_does_not_select_columns_without_alias
141+
def test_exists_does_not_select_columns_without_alias_coerced
142+
assert_sql(/SELECT\s+1 AS one FROM \[topics\].*OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY/i) do
143+
Topic.exists?
63144
end
145+
end
146+
147+
coerce_tests! :test_string_sanitation
148+
def test_string_sanitation_coerced
149+
assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
150+
assert_equal "N'something; select table'", ActiveRecord::Base.sanitize("something; select table")
151+
end
64152

153+
coerce_tests! :test_take_and_first_and_last_with_integer_should_use_sql_limit
154+
def test_take_and_first_and_last_with_integer_should_use_sql_limit_coerced
155+
assert_sql(/OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY/) { Topic.take(3).entries }
156+
assert_sql(/OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY/) { Topic.first(2).entries }
157+
assert_sql(/OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY/) { Topic.last(5).entries }
65158
end
159+
66160
end
67161

68162

69163

164+
70165
require 'models/company'
71166
class InheritanceTest < ActiveRecord::TestCase
72167

@@ -90,6 +185,22 @@ def test_eager_load_belongs_to_primary_key_quoting_coerced
90185

91186

92187

188+
189+
module ActiveRecord
190+
class Migration
191+
class ChangeSchemaTest < ActiveRecord::TestCase
192+
193+
# We test these.
194+
coerce_tests! :test_create_table_with_bigint,
195+
:test_create_table_with_defaults
196+
197+
end
198+
end
199+
end
200+
201+
202+
203+
93204
require 'models/developer'
94205
require 'models/computer'
95206
class NestedRelationScopingTest < ActiveRecord::TestCase
@@ -110,24 +221,6 @@ def test_merge_options_coerced
110221

111222

112223

113-
require 'models/post'
114-
require 'models/subscriber'
115-
class EachTest < ActiveRecord::TestCase
116-
117-
coerce_tests! :test_find_in_batches_should_quote_batch_order
118-
def test_find_in_batches_should_quote_batch_order_coerced
119-
c = Post.connection
120-
assert_sql(/ORDER BY \[posts\]\.\[id\]/) do
121-
Post.find_in_batches(:batch_size => 1) do |batch|
122-
assert_kind_of Array, batch
123-
assert_kind_of Post, batch.first
124-
end
125-
end
126-
end
127-
128-
end
129-
130-
131224

132225
require 'models/topic'
133226
module ActiveRecord
@@ -146,52 +239,63 @@ def test_registering_new_handlers_coerced
146239

147240

148241

149-
module ActiveRecord
150-
class Migration
151-
class ChangeSchemaTest < ActiveRecord::TestCase
152242

153-
coerce_tests! :test_create_table_with_bigint,
154-
:test_create_table_with_defaults # We test these.
243+
class RelationTest < ActiveRecord::TestCase
244+
245+
coerce_tests! %r{doesn't have implicit ordering} # We have implicit ordering, via FETCH.
155246

156-
end
157-
end
158247
end
159248

160249

161250

162-
require 'models/topic'
163-
class FinderTest < ActiveRecord::TestCase
164251

165-
coerce_tests! %r{doesn't have implicit ordering},
166-
:test_find_doesnt_have_implicit_ordering # We have implicit ordering, via FETCH.
252+
require 'models/post'
253+
class SanitizeTest < ActiveRecord::TestCase
167254

168-
coerce_tests! :test_exists_does_not_select_columns_without_alias
169-
def test_exists_does_not_select_columns_without_alias_coerced
170-
assert_sql(/SELECT\s+1 AS one FROM \[topics\].*OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY/i) do
171-
Topic.exists?
255+
coerce_tests! :test_sanitize_sql_like_example_use_case
256+
def test_sanitize_sql_like_example_use_case_coerced
257+
searchable_post = Class.new(Post) do
258+
def self.search(term)
259+
where("title LIKE ?", sanitize_sql_like(term, '!'))
260+
end
261+
end
262+
assert_sql(/\(title LIKE N''20!% !_reduction!_!!''\)/) do
263+
searchable_post.search("20% _reduction_!").to_a
172264
end
173265
end
174266

175-
coerce_tests! :test_string_sanitation
176-
def test_string_sanitation_coerced
177-
assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
178-
assert_equal "N'something; select table'", ActiveRecord::Base.sanitize("something; select table")
179-
end
267+
end
268+
180269

181-
coerce_tests! :test_take_and_first_and_last_with_integer_should_use_sql_limit
182-
def test_take_and_first_and_last_with_integer_should_use_sql_limit_coerced
183-
assert_sql(/OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY/) { Topic.take(3).entries }
184-
assert_sql(/OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY/) { Topic.first(2).entries }
185-
assert_sql(/OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY/) { Topic.last(5).entries }
186-
end
187270

271+
272+
require 'models/post'
273+
module ActiveRecord
274+
class WhereChainTest < ActiveRecord::TestCase
275+
276+
coerce_tests! :test_not_eq_with_array_parameter
277+
def test_not_eq_with_array_parameter_coerced
278+
expected = Arel::Nodes::Not.new("title = N'hello'")
279+
relation = Post.where.not(['title = ?', 'hello'])
280+
assert_equal([expected], relation.where_values)
281+
end
282+
283+
end
188284
end
189285

190286

191287

192-
class RelationTest < ActiveRecord::TestCase
193288

194-
coerce_tests! %r{doesn't have implicit ordering} # We have implicit ordering, via FETCH.
289+
require 'models/author'
290+
class YamlSerializationTest < ActiveRecord::TestCase
291+
292+
coerce_tests! :test_types_of_virtual_columns_are_not_changed_on_round_trip
293+
def test_types_of_virtual_columns_are_not_changed_on_round_trip_coerced
294+
author = Author.select('authors.*, 5 as posts_count').first
295+
dumped = YAML.load(YAML.dump(author))
296+
assert_equal 5, author.posts_count
297+
assert_equal 5, dumped.posts_count
298+
end
195299

196300
end
197301

0 commit comments

Comments
 (0)