ibm_db 2.5.5 → 2.5.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,8 @@ require "cases/helper"
2
2
  require 'models/company'
3
3
  require 'models/topic'
4
4
  require 'models/edge'
5
- require 'models/owner'
6
- require 'models/pet'
7
- require 'models/toy'
5
+ require 'models/club'
6
+ require 'models/organization'
8
7
 
9
8
  Company.has_many :accounts
10
9
 
@@ -13,7 +12,7 @@ class NumericData < ActiveRecord::Base
13
12
  end
14
13
 
15
14
  class CalculationsTest < ActiveRecord::TestCase
16
- fixtures :companies, :accounts, :topics, :owners, :pets, :toys
15
+ fixtures :companies, :accounts, :topics
17
16
 
18
17
  def test_should_sum_field
19
18
  assert_equal 318, Account.sum(:credit_limit)
@@ -21,17 +20,16 @@ class CalculationsTest < ActiveRecord::TestCase
21
20
 
22
21
  def test_should_average_field
23
22
  value = Account.average(:credit_limit)
24
- assert_kind_of BigDecimal, value
25
- assert_equal BigDecimal.new('53.0'), value
23
+ assert_equal 53.0, value
26
24
  end
27
25
 
28
26
  def test_should_return_nil_as_average
29
27
  assert_nil NumericData.average(:bank_balance)
30
28
  end
31
-
29
+
32
30
  def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
33
- assert_equal 0, NumericData.send(:type_cast_calculated_value, 0, nil, 'avg')
34
- assert_equal 53.0, NumericData.send(:type_cast_calculated_value, 53, nil, 'avg')
31
+ assert_equal 0, NumericData.scoped.send(:type_cast_calculated_value, 0, nil, 'avg')
32
+ assert_equal 53.0, NumericData.scoped.send(:type_cast_calculated_value, 53, nil, 'avg')
35
33
  end
36
34
 
37
35
  def test_should_get_maximum_of_field
@@ -39,12 +37,12 @@ class CalculationsTest < ActiveRecord::TestCase
39
37
  end
40
38
 
41
39
  def test_should_get_maximum_of_field_with_include
42
- assert_equal 50, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
40
+ assert_equal 55, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
43
41
  end
44
42
 
45
43
  def test_should_get_maximum_of_field_with_scoped_include
46
- Account.with_scope :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
47
- assert_equal 50, Account.maximum(:credit_limit)
44
+ Account.send :with_scope, :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
45
+ assert_equal 55, Account.maximum(:credit_limit)
48
46
  end
49
47
  end
50
48
 
@@ -210,7 +208,7 @@ class CalculationsTest < ActiveRecord::TestCase
210
208
  c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
211
209
  assert_equal 2, c[nil]
212
210
  assert_equal 1, c['DEPENDENTFIRM']
213
- assert_equal 3, c['CLIENT']
211
+ assert_equal 4, c['CLIENT']
214
212
  assert_equal 2, c['FIRM']
215
213
  end
216
214
 
@@ -218,7 +216,7 @@ class CalculationsTest < ActiveRecord::TestCase
218
216
  c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})")
219
217
  assert_equal 2, c[nil]
220
218
  assert_equal 1, c['DEPENDENTFIRM']
221
- assert_equal 3, c['CLIENT']
219
+ assert_equal 4, c['CLIENT']
222
220
  assert_equal 2, c['FIRM']
223
221
  end
224
222
 
@@ -230,6 +228,10 @@ class CalculationsTest < ActiveRecord::TestCase
230
228
  assert_equal 15, companies(:rails_core).companies.sum(:id)
231
229
  end
232
230
 
231
+ def test_should_sum_scoped_field_with_from
232
+ assert_equal Club.count, Organization.clubs.count
233
+ end
234
+
233
235
  def test_should_sum_scoped_field_with_conditions
234
236
  assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
235
237
  end
@@ -240,37 +242,31 @@ class CalculationsTest < ActiveRecord::TestCase
240
242
  assert_equal 8, c['Jadedpixel']
241
243
  end
242
244
 
243
- def test_should_group_by_summed_field_with_conditions_and_having
245
+ def test_should_group_by_summed_field_through_association_and_having
244
246
  c = companies(:rails_core).companies.sum(:id, :group => :name,
245
247
  :having => 'sum(id) > 7')
246
248
  assert_nil c['Leetsoft']
247
249
  assert_equal 8, c['Jadedpixel']
248
250
  end
249
251
 
250
- def test_should_reject_invalid_options
251
- assert_nothing_raised do
252
- [:count, :sum].each do |func|
253
- # empty options are valid
254
- Company.send(:validate_calculation_options, func)
255
- # these options are valid for all calculations
256
- [:select, :conditions, :joins, :order, :group, :having, :distinct].each do |opt|
257
- Company.send(:validate_calculation_options, func, opt => true)
258
- end
259
- end
260
-
261
- # :include is only valid on :count
262
- Company.send(:validate_calculation_options, :count, :include => true)
263
- end
264
-
265
- assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
266
- assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
267
- end
268
-
269
252
  def test_should_count_selected_field_with_include
270
253
  assert_equal 6, Account.count(:distinct => true, :include => :firm)
271
254
  assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
272
255
  end
273
256
 
257
+ def test_should_count_scoped_select
258
+ Account.update_all("credit_limit = NULL")
259
+ assert_equal 0, Account.scoped(:select => "credit_limit").count
260
+ end
261
+
262
+ def test_should_count_scoped_select_with_options
263
+ Account.update_all("credit_limit = NULL")
264
+ Account.last.update_attribute('credit_limit', 49)
265
+ Account.first.update_attribute('credit_limit', 51)
266
+
267
+ assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
268
+ end
269
+
274
270
  def test_should_count_manual_select_with_include
275
271
  assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
276
272
  end
@@ -280,7 +276,18 @@ class CalculationsTest < ActiveRecord::TestCase
280
276
  end
281
277
 
282
278
  def test_count_with_column_and_options_parameter
283
- assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50")
279
+ assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50 AND firm_id IS NOT NULL")
280
+ end
281
+
282
+ def test_should_count_field_in_joined_table
283
+ assert_equal 5, Account.count('companies.id', :joins => :firm)
284
+ assert_equal 4, Account.count('companies.id', :joins => :firm, :distinct => true)
285
+ end
286
+
287
+ def test_should_count_field_in_joined_table_with_group_by
288
+ c = Account.count('companies.id', :group => 'accounts.firm_id', :joins => :firm)
289
+
290
+ [1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
284
291
  end
285
292
 
286
293
  def test_count_with_no_parameters_isnt_deprecated
@@ -291,16 +298,13 @@ class CalculationsTest < ActiveRecord::TestCase
291
298
  assert_raise(ArgumentError) { Account.count(1, 2, 3) }
292
299
  end
293
300
 
294
- def test_count_with_scoped_has_many_through_association
295
- assert_equal 1, owners(:blackbeard).toys.with_name('Bone').count
296
- end
297
-
298
301
  def test_should_sum_expression
299
- if current_adapter?(:IBM_DBAdapter) && !ActiveRecord::Base.connection.servertype.class.name.include?('::IBM_IDS')
300
- assert_equal 636, Account.sum("2 * credit_limit")
302
+ # Oracle adapter returns floating point value 636.0 after SUM
303
+ if current_adapter?(:OracleAdapter)
304
+ assert_equal 636, Account.sum("2 * credit_limit")
301
305
  else
302
- assert_equal '636', Account.sum("2 * credit_limit")
303
- end
306
+ assert_equal 636, Account.sum("2 * credit_limit").to_i
307
+ end
304
308
  end
305
309
 
306
310
  def test_count_with_from_option
@@ -347,4 +351,12 @@ class CalculationsTest < ActiveRecord::TestCase
347
351
  assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
348
352
  end
349
353
 
354
+ def test_distinct_is_honored_when_used_with_count_operation_after_group
355
+ # Count the number of authors for approved topics
356
+ approved_topics_count = Topic.group(:approved).count(:author_name)[true]
357
+ assert_equal approved_topics_count, 3
358
+ # Count the number of distinct authors for approved Topics
359
+ distinct_authors_for_approved_count = Topic.group(:approved).count(:author_name, :distinct => true)[true]
360
+ assert_equal distinct_authors_for_approved_count, 2
361
+ end
350
362
  end
@@ -1,4 +1,5 @@
1
1
  require "cases/helper"
2
+ require 'models/post'
2
3
  require 'models/author'
3
4
  require 'models/categorization'
4
5
  require 'models/comment'
@@ -6,11 +7,9 @@ require 'models/company'
6
7
  require 'models/topic'
7
8
  require 'models/reply'
8
9
  require 'models/entrant'
10
+ require 'models/project'
9
11
  require 'models/developer'
10
- require 'models/post'
11
12
  require 'models/customer'
12
- require 'models/job'
13
- require 'models/categorization'
14
13
 
15
14
  class DynamicFinderMatchTest < ActiveRecord::TestCase
16
15
  def test_find_no_match
@@ -64,18 +63,18 @@ class DynamicFinderMatchTest < ActiveRecord::TestCase
64
63
  end
65
64
 
66
65
  class FinderTest < ActiveRecord::TestCase
67
- fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers
68
-
66
+ fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations
67
+
69
68
  def test_find
70
69
  assert_equal(topics(:first).title, Topic.find(1).title)
71
70
  end
72
-
71
+
73
72
  # find should handle strings that come from URLs
74
73
  # (example: Category.find(params[:id]))
75
74
  def test_find_with_string
76
75
  assert_equal(Topic.find(1).title,Topic.find("1").title)
77
76
  end
78
-
77
+
79
78
  def test_exists
80
79
  assert Topic.exists?(1)
81
80
  assert Topic.exists?("1")
@@ -83,7 +82,7 @@ class FinderTest < ActiveRecord::TestCase
83
82
  assert Topic.exists?(:author_name => "Mary", :approved => true)
84
83
  assert Topic.exists?(["parent_id = ?", 1])
85
84
  assert !Topic.exists?(45)
86
-
85
+
87
86
  begin
88
87
  assert !Topic.exists?("foo")
89
88
  rescue ActiveRecord::StatementInvalid
@@ -91,24 +90,24 @@ class FinderTest < ActiveRecord::TestCase
91
90
  rescue Exception
92
91
  flunk
93
92
  end
94
-
93
+
95
94
  assert_raise(NoMethodError) { Topic.exists?([1,2]) }
96
95
  end
97
-
96
+
98
97
  def test_exists_returns_true_with_one_record_and_no_args
99
98
  assert Topic.exists?
100
99
  end
101
-
100
+
102
101
  def test_does_not_exist_with_empty_table_and_no_args_given
103
102
  Topic.delete_all
104
103
  assert !Topic.exists?
105
104
  end
106
-
105
+
107
106
  def test_exists_with_aggregate_having_three_mappings
108
107
  existing_address = customers(:david).address
109
108
  assert Customer.exists?(:address => existing_address)
110
109
  end
111
-
110
+
112
111
  def test_exists_with_aggregate_having_three_mappings_with_one_difference
113
112
  existing_address = customers(:david).address
114
113
  assert !Customer.exists?(:address =>
@@ -120,7 +119,7 @@ class FinderTest < ActiveRecord::TestCase
120
119
  end
121
120
 
122
121
  def test_exists_with_scoped_include
123
- Developer.with_scope(:find => { :include => :projects, :order => "projects.name" }) do
122
+ Developer.send(:with_scope, :find => { :include => :projects, :order => "projects.name" }) do
124
123
  assert Developer.exists?
125
124
  end
126
125
  end
@@ -129,16 +128,16 @@ class FinderTest < ActiveRecord::TestCase
129
128
  assert_kind_of(Array, Topic.find([ 1 ]))
130
129
  assert_equal(1, Topic.find([ 1 ]).length)
131
130
  end
132
-
131
+
133
132
  def test_find_by_ids
134
133
  assert_equal 2, Topic.find(1, 2).size
135
134
  assert_equal topics(:second).title, Topic.find([2]).first.title
136
135
  end
137
-
136
+
138
137
  def test_find_by_ids_with_limit_and_offset
139
138
  assert_equal 2, Entrant.find([1,3,2], :limit => 2).size
140
139
  assert_equal 1, Entrant.find([1,3,2], :limit => 3, :offset => 2).size
141
-
140
+
142
141
  # Also test an edge case: If you have 11 results, and you set a
143
142
  # limit of 3 and offset of 9, then you should find that there
144
143
  # will be only 2 results, regardless of the limit.
@@ -146,44 +145,43 @@ class FinderTest < ActiveRecord::TestCase
146
145
  last_devs = Developer.find devs.map(&:id), :limit => 3, :offset => 9
147
146
  assert_equal 2, last_devs.size
148
147
  end
149
-
148
+
150
149
  def test_find_an_empty_array
151
150
  assert_equal [], Topic.find([])
152
151
  end
153
-
152
+
154
153
  def test_find_by_ids_missing_one
155
154
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
156
155
  end
157
-
156
+
158
157
  def test_find_all_with_limit
159
- entrants = Entrant.find(:all, :order => "id ASC", :limit => 2)
160
-
161
- assert_equal(2, entrants.size)
162
- assert_equal(entrants(:first).name, entrants.first.name)
158
+ assert_equal(2, Entrant.find(:all, :limit => 2).size)
159
+ assert_equal(0, Entrant.find(:all, :limit => 0).size)
163
160
  end
164
-
161
+
165
162
  def test_find_all_with_prepared_limit_and_offset
166
163
  entrants = Entrant.find(:all, :order => "id ASC", :limit => 2, :offset => 1)
167
-
164
+
168
165
  assert_equal(2, entrants.size)
169
166
  assert_equal(entrants(:second).name, entrants.first.name)
170
-
167
+
168
+ assert_equal 3, Entrant.count
171
169
  entrants = Entrant.find(:all, :order => "id ASC", :limit => 2, :offset => 2)
172
170
  assert_equal(1, entrants.size)
173
171
  assert_equal(entrants(:third).name, entrants.first.name)
174
172
  end
175
-
176
- def test_find_all_with_limit_and_offset_and_multiple_orderings
177
- developers = Developer.find(:all, :order => "salary ASC, id DESC", :limit => 3, :offset => 1)
178
- assert_equal ["David", "fixture_10", "fixture_9"], developers.collect {|d| d.name}
179
- end
180
-
181
- def test_find_with_limit_and_condition
182
- developers = Developer.find(:all, :order => "id DESC", :conditions => "salary = 100000", :limit => 3, :offset =>7)
183
- assert_equal(1, developers.size)
184
- assert_equal("fixture_3", developers.first.name)
173
+
174
+ def test_find_all_with_limit_and_offset_and_multiple_order_clauses
175
+ first_three_posts = Post.find :all, :order => 'author_id, id', :limit => 3, :offset => 0
176
+ second_three_posts = Post.find :all, :order => ' author_id,id ', :limit => 3, :offset => 3
177
+ last_posts = Post.find :all, :order => ' author_id, id ', :limit => 3, :offset => 6
178
+
179
+ assert_equal [[0,3],[1,1],[1,2]], first_three_posts.map { |p| [p.author_id, p.id] }
180
+ assert_equal [[1,4],[1,5],[1,6]], second_three_posts.map { |p| [p.author_id, p.id] }
181
+ assert_equal [[2,7]], last_posts.map { |p| [p.author_id, p.id] }
185
182
  end
186
183
 
184
+
187
185
  def test_find_with_group
188
186
  developers = Developer.find(:all, :group => "salary", :select => "salary")
189
187
  assert_equal 4, developers.size
@@ -206,81 +204,81 @@ class FinderTest < ActiveRecord::TestCase
206
204
 
207
205
  def test_find_with_entire_select_statement
208
206
  topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
209
-
207
+
210
208
  assert_equal(1, topics.size)
211
209
  assert_equal(topics(:second).title, topics.first.title)
212
210
  end
213
-
211
+
214
212
  def test_find_with_prepared_select_statement
215
213
  topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"]
216
-
214
+
217
215
  assert_equal(1, topics.size)
218
216
  assert_equal(topics(:second).title, topics.first.title)
219
217
  end
220
-
218
+
221
219
  def test_find_by_sql_with_sti_on_joined_table
222
220
  accounts = Account.find_by_sql("SELECT * FROM accounts INNER JOIN companies ON companies.id = accounts.firm_id")
223
221
  assert_equal [Account], accounts.collect(&:class).uniq
224
222
  end
225
-
223
+
226
224
  def test_find_first
227
225
  first = Topic.find(:first, :conditions => "title = 'The First Topic'")
228
226
  assert_equal(topics(:first).title, first.title)
229
227
  end
230
-
228
+
231
229
  def test_find_first_failing
232
230
  first = Topic.find(:first, :conditions => "title = 'The First Topic!'")
233
231
  assert_nil(first)
234
232
  end
235
-
233
+
236
234
  def test_first
237
- assert_equal topics(:second).title, Topic.first(:conditions => "title = 'The Second Topic of the day'").title
235
+ assert_equal topics(:second).title, Topic.where("title = 'The Second Topic of the day'").first.title
238
236
  end
239
-
237
+
240
238
  def test_first_failing
241
- assert_nil Topic.first(:conditions => "title = 'The Second Topic of the day!'")
239
+ assert_nil Topic.where("title = 'The Second Topic of the day!'").first
242
240
  end
243
-
241
+
244
242
  def test_unexisting_record_exception_handling
245
243
  assert_raise(ActiveRecord::RecordNotFound) {
246
244
  Topic.find(1).parent
247
245
  }
248
-
246
+
249
247
  Topic.find(2).topic
250
248
  end
251
-
249
+
252
250
  def test_find_only_some_columns
253
251
  topic = Topic.find(1, :select => "author_name")
254
- assert_raise(ActiveRecord::MissingAttributeError) {topic.title}
252
+ assert_raise(ActiveModel::MissingAttributeError) {topic.title}
255
253
  assert_equal "David", topic.author_name
256
254
  assert !topic.attribute_present?("title")
257
255
  #assert !topic.respond_to?("title")
258
256
  assert topic.attribute_present?("author_name")
259
- assert topic.respond_to?("author_name")
257
+ assert_respond_to topic, "author_name"
260
258
  end
261
-
259
+
262
260
  def test_find_on_blank_conditions
263
261
  [nil, " ", [], {}].each do |blank|
264
262
  assert_nothing_raised { Topic.find(:first, :conditions => blank) }
265
263
  end
266
264
  end
267
-
265
+
268
266
  def test_find_on_blank_bind_conditions
269
267
  [ [""], ["",{}] ].each do |blank|
270
268
  assert_nothing_raised { Topic.find(:first, :conditions => blank) }
271
269
  end
272
270
  end
273
-
271
+
274
272
  def test_find_on_array_conditions
275
273
  assert Topic.find(1, :conditions => ["approved = ?", false])
276
274
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
277
275
  end
278
-
276
+
279
277
  def test_find_on_hash_conditions
280
278
  assert Topic.find(1, :conditions => { :approved => false })
281
279
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
282
280
  end
283
-
281
+
284
282
  def test_find_on_hash_conditions_with_explicit_table_name
285
283
  assert Topic.find(1, :conditions => { 'topics.approved' => false })
286
284
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) }
@@ -292,7 +290,7 @@ class FinderTest < ActiveRecord::TestCase
292
290
  end
293
291
 
294
292
  def test_find_with_hash_conditions_on_joined_table
295
- firms = Firm.all :joins => :account, :conditions => {:accounts => { :credit_limit => 50 }}
293
+ firms = Firm.joins(:account).where(:accounts => { :credit_limit => 50 })
296
294
  assert_equal 1, firms.size
297
295
  assert_equal companies(:first_firm), firms.first
298
296
  end
@@ -310,11 +308,11 @@ class FinderTest < ActiveRecord::TestCase
310
308
  Customer.find(david.id, :conditions => { 'customers.name' => david.name + "1", :address => david.address })
311
309
  }
312
310
  end
313
-
311
+
314
312
  def test_find_on_association_proxy_conditions
315
313
  assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10], Comment.find_all_by_post_id(authors(:david).posts).map(&:id).sort
316
314
  end
317
-
315
+
318
316
  def test_find_on_hash_conditions_with_range
319
317
  assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1..2 }).map(&:id).sort
320
318
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
@@ -325,105 +323,140 @@ class FinderTest < ActiveRecord::TestCase
325
323
  assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1...3 }).map(&:id).sort
326
324
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(3, :conditions => { :id => 2...3 }) }
327
325
  end
328
-
326
+
329
327
  def test_find_on_hash_conditions_with_multiple_ranges
330
328
  assert_equal [1,2,3], Comment.find(:all, :conditions => { :id => 1..3, :post_id => 1..2 }).map(&:id).sort
331
329
  assert_equal [1], Comment.find(:all, :conditions => { :id => 1..1, :post_id => 1..10 }).map(&:id).sort
332
330
  end
333
-
331
+
334
332
  def test_find_on_multiple_hash_conditions
335
333
  assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false })
336
334
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
337
335
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }) }
338
336
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
339
337
  end
340
-
341
-
338
+
342
339
  def test_condition_interpolation
343
340
  assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
344
341
  assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])
345
342
  assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!' OR 1=1"])
346
343
  assert_kind_of Time, Topic.find(:first, :conditions => ["id = %d", 1]).written_on
347
344
  end
348
-
345
+
349
346
  def test_condition_array_interpolation
350
347
  assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
351
348
  assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])
352
349
  assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!' OR 1=1"])
353
350
  assert_kind_of Time, Topic.find(:first, :conditions => ["id = %d", 1]).written_on
354
351
  end
355
-
352
+
356
353
  def test_condition_hash_interpolation
357
354
  assert_kind_of Firm, Company.find(:first, :conditions => { :name => "37signals"})
358
355
  assert_nil Company.find(:first, :conditions => { :name => "37signals!"})
359
356
  assert_kind_of Time, Topic.find(:first, :conditions => {:id => 1}).written_on
360
357
  end
361
-
358
+
362
359
  def test_hash_condition_find_malformed
363
360
  assert_raise(ActiveRecord::StatementInvalid) {
364
361
  Company.find(:first, :conditions => { :id => 2, :dhh => true })
365
362
  }
366
363
  end
367
-
364
+
368
365
  def test_hash_condition_find_with_escaped_characters
369
366
  Company.create("name" => "Ain't noth'n like' \#stuff")
370
367
  assert Company.find(:first, :conditions => { :name => "Ain't noth'n like' \#stuff" })
371
368
  end
372
-
369
+
373
370
  def test_hash_condition_find_with_array
374
371
  p1, p2 = Post.find(:all, :limit => 2, :order => 'id asc')
375
372
  assert_equal [p1, p2], Post.find(:all, :conditions => { :id => [p1, p2] }, :order => 'id asc')
376
373
  assert_equal [p1, p2], Post.find(:all, :conditions => { :id => [p1, p2.id] }, :order => 'id asc')
377
374
  end
378
-
375
+
379
376
  def test_hash_condition_find_with_nil
380
377
  topic = Topic.find(:first, :conditions => { :last_read => nil } )
381
378
  assert_not_nil topic
382
379
  assert_nil topic.last_read
383
380
  end
384
-
381
+
385
382
  def test_hash_condition_find_with_aggregate_having_one_mapping
386
383
  balance = customers(:david).balance
387
384
  assert_kind_of Money, balance
388
385
  found_customer = Customer.find(:first, :conditions => {:balance => balance})
389
386
  assert_equal customers(:david), found_customer
390
387
  end
391
-
388
+
392
389
  def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_aggregate
393
390
  gps_location = customers(:david).gps_location
394
391
  assert_kind_of GpsLocation, gps_location
395
392
  found_customer = Customer.find(:first, :conditions => {:gps_location => gps_location})
396
393
  assert_equal customers(:david), found_customer
397
394
  end
398
-
395
+
399
396
  def test_hash_condition_find_with_aggregate_having_one_mapping_and_key_value_being_attribute_value
400
397
  balance = customers(:david).balance
401
398
  assert_kind_of Money, balance
402
399
  found_customer = Customer.find(:first, :conditions => {:balance => balance.amount})
403
400
  assert_equal customers(:david), found_customer
404
401
  end
405
-
402
+
406
403
  def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_attribute_value
407
404
  gps_location = customers(:david).gps_location
408
405
  assert_kind_of GpsLocation, gps_location
409
406
  found_customer = Customer.find(:first, :conditions => {:gps_location => gps_location.gps_location})
410
407
  assert_equal customers(:david), found_customer
411
408
  end
412
-
409
+
413
410
  def test_hash_condition_find_with_aggregate_having_three_mappings
414
411
  address = customers(:david).address
415
412
  assert_kind_of Address, address
416
413
  found_customer = Customer.find(:first, :conditions => {:address => address})
417
414
  assert_equal customers(:david), found_customer
418
415
  end
419
-
416
+
420
417
  def test_hash_condition_find_with_one_condition_being_aggregate_and_another_not
421
418
  address = customers(:david).address
422
419
  assert_kind_of Address, address
423
420
  found_customer = Customer.find(:first, :conditions => {:address => address, :name => customers(:david).name})
424
421
  assert_equal customers(:david), found_customer
425
422
  end
426
-
423
+
424
+ def test_condition_utc_time_interpolation_with_default_timezone_local
425
+ with_env_tz 'America/New_York' do
426
+ with_active_record_default_timezone :local do
427
+ topic = Topic.first
428
+ assert_equal topic, Topic.find(:first, :conditions => ['written_on = ?', topic.written_on.getutc])
429
+ end
430
+ end
431
+ end
432
+
433
+ def test_hash_condition_utc_time_interpolation_with_default_timezone_local
434
+ with_env_tz 'America/New_York' do
435
+ with_active_record_default_timezone :local do
436
+ topic = Topic.first
437
+ assert_equal topic, Topic.find(:first, :conditions => {:written_on => topic.written_on.getutc})
438
+ end
439
+ end
440
+ end
441
+
442
+ def test_condition_local_time_interpolation_with_default_timezone_utc
443
+ with_env_tz 'America/New_York' do
444
+ with_active_record_default_timezone :utc do
445
+ topic = Topic.first
446
+ assert_equal topic, Topic.find(:first, :conditions => ['written_on = ?', topic.written_on.getlocal])
447
+ end
448
+ end
449
+ end
450
+
451
+ def test_hash_condition_local_time_interpolation_with_default_timezone_utc
452
+ with_env_tz 'America/New_York' do
453
+ with_active_record_default_timezone :utc do
454
+ topic = Topic.first
455
+ assert_equal topic, Topic.find(:first, :conditions => {:written_on => topic.written_on.getlocal})
456
+ end
457
+ end
458
+ end
459
+
427
460
  def test_bind_variables
428
461
  assert_kind_of Firm, Company.find(:first, :conditions => ["name = ?", "37signals"])
429
462
  assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!"])
@@ -436,17 +469,17 @@ class FinderTest < ActiveRecord::TestCase
436
469
  Company.find(:first, :conditions => ["id=?", 2, 3, 4])
437
470
  }
438
471
  end
439
-
472
+
440
473
  def test_bind_variables_with_quotes
441
474
  Company.create("name" => "37signals' go'es agains")
442
475
  assert Company.find(:first, :conditions => ["name = ?", "37signals' go'es agains"])
443
476
  end
444
-
477
+
445
478
  def test_named_bind_variables_with_quotes
446
479
  Company.create("name" => "37signals' go'es agains")
447
480
  assert Company.find(:first, :conditions => ["name = :name", {:name => "37signals' go'es agains"}])
448
481
  end
449
-
482
+
450
483
  def test_bind_arity
451
484
  assert_nothing_raised { bind '' }
452
485
  assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
@@ -455,45 +488,57 @@ class FinderTest < ActiveRecord::TestCase
455
488
  assert_nothing_raised { bind '?', 1 }
456
489
  assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
457
490
  end
458
-
491
+
459
492
  def test_named_bind_variables
460
493
  assert_equal '1', bind(':a', :a => 1) # ' ruby-mode
461
494
  assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode
462
-
495
+
463
496
  assert_nothing_raised { bind("'+00:00'", :foo => "bar") }
464
-
497
+
465
498
  assert_kind_of Firm, Company.find(:first, :conditions => ["name = :name", { :name => "37signals" }])
466
499
  assert_nil Company.find(:first, :conditions => ["name = :name", { :name => "37signals!" }])
467
500
  assert_nil Company.find(:first, :conditions => ["name = :name", { :name => "37signals!' OR 1=1" }])
468
501
  assert_kind_of Time, Topic.find(:first, :conditions => ["id = :id", { :id => 1 }]).written_on
469
502
  end
470
-
503
+
504
+ class SimpleEnumerable
505
+ include Enumerable
506
+
507
+ def initialize(ary)
508
+ @ary = ary
509
+ end
510
+
511
+ def each(&b)
512
+ @ary.each(&b)
513
+ end
514
+ end
515
+
471
516
  def test_bind_enumerable
472
517
  quoted_abc = %(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')})
473
-
518
+
474
519
  assert_equal '1,2,3', bind('?', [1, 2, 3])
475
520
  assert_equal quoted_abc, bind('?', %w(a b c))
476
-
521
+
477
522
  assert_equal '1,2,3', bind(':a', :a => [1, 2, 3])
478
523
  assert_equal quoted_abc, bind(':a', :a => %w(a b c)) # '
479
-
480
- require 'set'
481
- assert_equal '1,2,3', bind('?', Set.new([1, 2, 3]))
482
- assert_equal quoted_abc, bind('?', Set.new(%w(a b c)))
483
-
484
- assert_equal '1,2,3', bind(':a', :a => Set.new([1, 2, 3]))
485
- assert_equal quoted_abc, bind(':a', :a => Set.new(%w(a b c))) # '
486
- end
487
-
524
+
525
+ assert_equal '1,2,3', bind('?', SimpleEnumerable.new([1, 2, 3]))
526
+ assert_equal quoted_abc, bind('?', SimpleEnumerable.new(%w(a b c)))
527
+
528
+ assert_equal '1,2,3', bind(':a', :a => SimpleEnumerable.new([1, 2, 3]))
529
+ assert_equal quoted_abc, bind(':a', :a => SimpleEnumerable.new(%w(a b c))) # '
530
+ end
531
+
488
532
  def test_bind_empty_enumerable
489
533
  quoted_nil = ActiveRecord::Base.connection.quote(nil)
490
534
  assert_equal quoted_nil, bind('?', [])
491
535
  assert_equal " in (#{quoted_nil})", bind(' in (?)', [])
492
536
  assert_equal "foo in (#{quoted_nil})", bind('foo in (?)', [])
493
537
  end
494
-
495
- def test_bind_string
496
- assert_equal ActiveRecord::Base.connection.quote(''), bind('?', '')
538
+
539
+ def test_bind_empty_string
540
+ quoted_empty = ActiveRecord::Base.connection.quote('')
541
+ assert_equal quoted_empty, bind('?', '')
497
542
  end
498
543
 
499
544
  def test_bind_chars
@@ -508,7 +553,7 @@ class FinderTest < ActiveRecord::TestCase
508
553
  def test_bind_record
509
554
  o = Struct.new(:quoted_id).new(1)
510
555
  assert_equal '1', bind('?', o)
511
-
556
+
512
557
  os = [o] * 3
513
558
  assert_equal '1,1,1', bind('?', os)
514
559
  end
@@ -520,37 +565,22 @@ class FinderTest < ActiveRecord::TestCase
520
565
  end
521
566
 
522
567
  def test_string_sanitation
523
- assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
524
- assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table")
568
+ assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
569
+ assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
525
570
  end
526
-
571
+
527
572
  def test_count
528
573
  assert_equal(0, Entrant.count(:conditions => "id > 3"))
529
574
  assert_equal(1, Entrant.count(:conditions => ["id > ?", 2]))
530
575
  assert_equal(2, Entrant.count(:conditions => ["id > ?", 1]))
531
576
  end
532
-
577
+
533
578
  def test_count_by_sql
534
579
  assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3"))
535
580
  assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
536
581
  assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
537
582
  end
538
583
 
539
- def test_dynamic_finders_should_go_through_the_find_class_method
540
- Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
541
- Topic.find_by_title("The First Topic!")
542
-
543
- Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
544
- Topic.find_last_by_title("The Last Topic!")
545
-
546
- Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
547
- Topic.find_all_by_title("A Topic.")
548
-
549
- Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
550
- Topic.find_or_initialize_by_title('Does not exist yet for sure!')
551
- Topic.find_or_create_by_title('Does not exist yet for sure!')
552
- end
553
-
554
584
  def test_find_by_one_attribute
555
585
  assert_equal topics(:first), Topic.find_by_title("The First Topic")
556
586
  assert_nil Topic.find_by_title("The First Topic!")
@@ -561,21 +591,6 @@ class FinderTest < ActiveRecord::TestCase
561
591
  assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
562
592
  end
563
593
 
564
- def test_find_by_one_attribute_caches_dynamic_finder
565
- # ensure this test can run independently of order
566
- class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.public_methods.any? { |m| m.to_s == 'find_by_title' }
567
- assert !Topic.public_methods.any? { |m| m.to_s == 'find_by_title' }
568
- t = Topic.find_by_title("The First Topic")
569
- assert Topic.public_methods.any? { |m| m.to_s == 'find_by_title' }
570
- end
571
-
572
- def test_dynamic_finder_returns_same_results_after_caching
573
- # ensure this test can run independently of order
574
- class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.public_method_defined?(:find_by_title)
575
- t = Topic.find_by_title("The First Topic")
576
- assert_equal t, Topic.find_by_title("The First Topic") # find_by_title has been cached
577
- end
578
-
579
594
  def test_find_by_one_attribute_with_order_option
580
595
  assert_equal accounts(:signals37), Account.find_by_credit_limit(50, :order => 'id')
581
596
  assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC')
@@ -619,14 +634,6 @@ class FinderTest < ActiveRecord::TestCase
619
634
  assert_equal customers(:david), found_customer
620
635
  end
621
636
 
622
- def test_dynamic_finder_on_one_attribute_with_conditions_caches_method
623
- # ensure this test can run independently of order
624
- class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
625
- assert !Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
626
- a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
627
- assert Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
628
- end
629
-
630
637
  def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching
631
638
  # ensure this test can run independently of order
632
639
  class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' }
@@ -659,14 +666,6 @@ class FinderTest < ActiveRecord::TestCase
659
666
  assert_nil Topic.find_last_by_title("A title with no matches")
660
667
  end
661
668
 
662
- def test_find_last_by_one_attribute_caches_dynamic_finder
663
- # ensure this test can run independently of order
664
- class << Topic; self; end.send(:remove_method, :find_last_by_title) if Topic.public_methods.any? { |m| m.to_s == 'find_last_by_title' }
665
- assert !Topic.public_methods.any? { |m| m.to_s == 'find_last_by_title' }
666
- t = Topic.find_last_by_title(Topic.last.title)
667
- assert Topic.public_methods.any? { |m| m.to_s == 'find_last_by_title' }
668
- end
669
-
670
669
  def test_find_last_by_invalid_method_syntax
671
670
  assert_raise(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
672
671
  assert_raise(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
@@ -694,6 +693,14 @@ class FinderTest < ActiveRecord::TestCase
694
693
  assert_equal [], Topic.find_all_by_title("The First Topic!!")
695
694
  end
696
695
 
696
+ def test_find_all_by_one_attribute_which_is_a_symbol
697
+ topics = Topic.find_all_by_content("Have a nice day".to_sym)
698
+ assert_equal 2, topics.size
699
+ assert topics.include?(topics(:first))
700
+
701
+ assert_equal [], Topic.find_all_by_title("The First Topic!!")
702
+ end
703
+
697
704
  def test_find_all_by_one_attribute_that_is_an_aggregate
698
705
  balance = customers(:david).balance
699
706
  assert_kind_of Money, balance
@@ -722,10 +729,10 @@ class FinderTest < ActiveRecord::TestCase
722
729
 
723
730
  def test_find_all_by_one_attribute_with_options
724
731
  topics = Topic.find_all_by_content("Have a nice day", :order => "id DESC")
725
- assert topics(:first), topics.last
732
+ assert_equal topics(:first), topics.last
726
733
 
727
734
  topics = Topic.find_all_by_content("Have a nice day", :order => "id")
728
- assert topics(:first), topics.first
735
+ assert_equal topics(:first), topics.first
729
736
  end
730
737
 
731
738
  def test_find_all_by_array_attribute
@@ -770,7 +777,7 @@ class FinderTest < ActiveRecord::TestCase
770
777
  sig38 = Company.find_or_create_by_name("38signals")
771
778
  assert_equal number_of_companies + 1, Company.count
772
779
  assert_equal sig38, Company.find_or_create_by_name("38signals")
773
- assert !sig38.new_record?
780
+ assert sig38.persisted?
774
781
  end
775
782
 
776
783
  def test_find_or_create_from_two_attributes
@@ -778,7 +785,7 @@ class FinderTest < ActiveRecord::TestCase
778
785
  another = Topic.find_or_create_by_title_and_author_name("Another topic","John")
779
786
  assert_equal number_of_topics + 1, Topic.count
780
787
  assert_equal another, Topic.find_or_create_by_title_and_author_name("Another topic", "John")
781
- assert !another.new_record?
788
+ assert another.persisted?
782
789
  end
783
790
 
784
791
  def test_find_or_create_from_two_attributes_with_one_being_an_aggregate
@@ -786,7 +793,7 @@ class FinderTest < ActiveRecord::TestCase
786
793
  created_customer = Customer.find_or_create_by_balance_and_name(Money.new(123), "Elizabeth")
787
794
  assert_equal number_of_customers + 1, Customer.count
788
795
  assert_equal created_customer, Customer.find_or_create_by_balance(Money.new(123), "Elizabeth")
789
- assert !created_customer.new_record?
796
+ assert created_customer.persisted?
790
797
  end
791
798
 
792
799
  def test_find_or_create_from_one_attribute_and_hash
@@ -794,7 +801,7 @@ class FinderTest < ActiveRecord::TestCase
794
801
  sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
795
802
  assert_equal number_of_companies + 1, Company.count
796
803
  assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
797
- assert !sig38.new_record?
804
+ assert sig38.persisted?
798
805
  assert_equal "38signals", sig38.name
799
806
  assert_equal 17, sig38.firm_id
800
807
  assert_equal 23, sig38.client_of
@@ -805,7 +812,7 @@ class FinderTest < ActiveRecord::TestCase
805
812
  created_customer = Customer.find_or_create_by_balance(Money.new(123))
806
813
  assert_equal number_of_customers + 1, Customer.count
807
814
  assert_equal created_customer, Customer.find_or_create_by_balance(Money.new(123))
808
- assert !created_customer.new_record?
815
+ assert created_customer.persisted?
809
816
  end
810
817
 
811
818
  def test_find_or_create_from_one_aggregate_attribute_and_hash
@@ -815,7 +822,7 @@ class FinderTest < ActiveRecord::TestCase
815
822
  created_customer = Customer.find_or_create_by_balance({:balance => balance, :name => name})
816
823
  assert_equal number_of_customers + 1, Customer.count
817
824
  assert_equal created_customer, Customer.find_or_create_by_balance({:balance => balance, :name => name})
818
- assert !created_customer.new_record?
825
+ assert created_customer.persisted?
819
826
  assert_equal balance, created_customer.balance
820
827
  assert_equal name, created_customer.name
821
828
  end
@@ -823,13 +830,13 @@ class FinderTest < ActiveRecord::TestCase
823
830
  def test_find_or_initialize_from_one_attribute
824
831
  sig38 = Company.find_or_initialize_by_name("38signals")
825
832
  assert_equal "38signals", sig38.name
826
- assert sig38.new_record?
833
+ assert !sig38.persisted?
827
834
  end
828
835
 
829
836
  def test_find_or_initialize_from_one_aggregate_attribute
830
837
  new_customer = Customer.find_or_initialize_by_balance(Money.new(123))
831
838
  assert_equal 123, new_customer.balance.amount
832
- assert new_customer.new_record?
839
+ assert !new_customer.persisted?
833
840
  end
834
841
 
835
842
  def test_find_or_initialize_from_one_attribute_should_not_set_attribute_even_when_protected
@@ -837,15 +844,15 @@ class FinderTest < ActiveRecord::TestCase
837
844
  assert_equal "Fortune 1000", c.name
838
845
  assert_not_equal 1000, c.rating
839
846
  assert c.valid?
840
- assert c.new_record?
847
+ assert !c.persisted?
841
848
  end
842
849
 
843
- def test_find_or_create_from_one_attribute_should_set_not_attribute_even_when_protected
850
+ def test_find_or_create_from_one_attribute_should_not_set_attribute_even_when_protected
844
851
  c = Company.find_or_create_by_name({:name => "Fortune 1000", :rating => 1000})
845
852
  assert_equal "Fortune 1000", c.name
846
853
  assert_not_equal 1000, c.rating
847
854
  assert c.valid?
848
- assert !c.new_record?
855
+ assert c.persisted?
849
856
  end
850
857
 
851
858
  def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected
@@ -853,7 +860,7 @@ class FinderTest < ActiveRecord::TestCase
853
860
  assert_equal "Fortune 1000", c.name
854
861
  assert_equal 1000, c.rating
855
862
  assert c.valid?
856
- assert c.new_record?
863
+ assert !c.persisted?
857
864
  end
858
865
 
859
866
  def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected
@@ -861,7 +868,23 @@ class FinderTest < ActiveRecord::TestCase
861
868
  assert_equal "Fortune 1000", c.name
862
869
  assert_equal 1000, c.rating
863
870
  assert c.valid?
864
- assert !c.new_record?
871
+ assert c.persisted?
872
+ end
873
+
874
+ def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected_and_also_set_the_hash
875
+ c = Company.find_or_initialize_by_rating(1000, {:name => "Fortune 1000"})
876
+ assert_equal "Fortune 1000", c.name
877
+ assert_equal 1000, c.rating
878
+ assert c.valid?
879
+ assert !c.persisted?
880
+ end
881
+
882
+ def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected_and_also_set_the_hash
883
+ c = Company.find_or_create_by_rating(1000, {:name => "Fortune 1000"})
884
+ assert_equal "Fortune 1000", c.name
885
+ assert_equal 1000, c.rating
886
+ assert c.valid?
887
+ assert c.persisted?
865
888
  end
866
889
 
867
890
  def test_find_or_initialize_should_set_protected_attributes_if_given_as_block
@@ -869,7 +892,7 @@ class FinderTest < ActiveRecord::TestCase
869
892
  assert_equal "Fortune 1000", c.name
870
893
  assert_equal 1000.to_f, c.rating.to_f
871
894
  assert c.valid?
872
- assert c.new_record?
895
+ assert !c.persisted?
873
896
  end
874
897
 
875
898
  def test_find_or_create_should_set_protected_attributes_if_given_as_block
@@ -877,39 +900,32 @@ class FinderTest < ActiveRecord::TestCase
877
900
  assert_equal "Fortune 1000", c.name
878
901
  assert_equal 1000.to_f, c.rating.to_f
879
902
  assert c.valid?
880
- assert !c.new_record?
903
+ assert c.persisted?
881
904
  end
882
905
 
883
906
  def test_find_or_create_should_work_with_block_on_first_call
884
- class << Company
885
- undef_method(:find_or_create_by_name) if method_defined?(:find_or_create_by_name)
886
- end
907
+ class << Company
908
+ undef_method(:find_or_create_by_name) if method_defined?(:find_or_create_by_name)
909
+ end
887
910
  c = Company.find_or_create_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
888
911
  assert_equal "Fortune 1000", c.name
889
912
  assert_equal 1000.to_f, c.rating.to_f
890
913
  assert c.valid?
891
- assert !c.new_record?
892
- end
893
-
894
- def test_dynamic_find_or_initialize_from_one_attribute_caches_method
895
- class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
896
- assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
897
- sig38 = Company.find_or_initialize_by_name("38signals")
898
- assert Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' }
914
+ assert c.persisted?
899
915
  end
900
916
 
901
917
  def test_find_or_initialize_from_two_attributes
902
918
  another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John")
903
919
  assert_equal "Another topic", another.title
904
920
  assert_equal "John", another.author_name
905
- assert another.new_record?
921
+ assert !another.persisted?
906
922
  end
907
923
 
908
924
  def test_find_or_initialize_from_one_aggregate_attribute_and_one_not
909
925
  new_customer = Customer.find_or_initialize_by_balance_and_name(Money.new(123), "Elizabeth")
910
926
  assert_equal 123, new_customer.balance.amount
911
927
  assert_equal "Elizabeth", new_customer.name
912
- assert new_customer.new_record?
928
+ assert !new_customer.persisted?
913
929
  end
914
930
 
915
931
  def test_find_or_initialize_from_one_attribute_and_hash
@@ -917,7 +933,7 @@ class FinderTest < ActiveRecord::TestCase
917
933
  assert_equal "38signals", sig38.name
918
934
  assert_equal 17, sig38.firm_id
919
935
  assert_equal 23, sig38.client_of
920
- assert sig38.new_record?
936
+ assert !sig38.persisted?
921
937
  end
922
938
 
923
939
  def test_find_or_initialize_from_one_aggregate_attribute_and_hash
@@ -926,7 +942,7 @@ class FinderTest < ActiveRecord::TestCase
926
942
  new_customer = Customer.find_or_initialize_by_balance({:balance => balance, :name => name})
927
943
  assert_equal balance, new_customer.balance
928
944
  assert_equal name, new_customer.name
929
- assert new_customer.new_record?
945
+ assert !new_customer.persisted?
930
946
  end
931
947
 
932
948
  def test_find_with_bad_sql
@@ -942,40 +958,6 @@ class FinderTest < ActiveRecord::TestCase
942
958
  assert_raise(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
943
959
  end
944
960
 
945
- def test_find_all_with_limit
946
- first_five_developers = Developer.find :all, :order => 'id ASC', :limit => 5
947
- assert_equal 5, first_five_developers.length
948
- assert_equal 'David', first_five_developers.first.name
949
- assert_equal 'fixture_5', first_five_developers.last.name
950
-
951
- no_developers = Developer.find :all, :order => 'id ASC', :limit => 0
952
- assert_equal 0, no_developers.length
953
- end
954
-
955
- def test_find_all_with_limit_and_offset
956
- first_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 0
957
- second_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 3
958
- last_two_developers = Developer.find :all, :order => 'id ASC', :limit => 2, :offset => 8
959
-
960
- assert_equal 3, first_three_developers.length
961
- assert_equal 3, second_three_developers.length
962
- assert_equal 2, last_two_developers.length
963
-
964
- assert_equal 'David', first_three_developers.first.name
965
- assert_equal 'fixture_4', second_three_developers.first.name
966
- assert_equal 'fixture_9', last_two_developers.first.name
967
- end
968
-
969
- def test_find_all_with_limit_and_offset_and_multiple_order_clauses
970
- first_three_posts = Post.find :all, :order => 'author_id, id', :limit => 3, :offset => 0
971
- second_three_posts = Post.find :all, :order => ' author_id,id ', :limit => 3, :offset => 3
972
- last_posts = Post.find :all, :order => ' author_id, id ', :limit => 3, :offset => 6
973
-
974
- assert_equal [[0,3],[1,1],[1,2]], first_three_posts.map { |p| [p.author_id, p.id] }
975
- assert_equal [[1,4],[1,5],[1,6]], second_three_posts.map { |p| [p.author_id, p.id] }
976
- assert_equal [[2,7]], last_posts.map { |p| [p.author_id, p.id] }
977
- end
978
-
979
961
  def test_find_all_with_join
980
962
  developers_on_project_one = Developer.find(
981
963
  :all,
@@ -991,7 +973,7 @@ class FinderTest < ActiveRecord::TestCase
991
973
  def test_joins_dont_clobber_id
992
974
  first = Firm.find(
993
975
  :first,
994
- :joins => 'INNER JOIN companies AS clients ON clients.firm_id = companies.id',
976
+ :joins => 'INNER JOIN companies clients ON clients.firm_id = companies.id',
995
977
  :conditions => 'companies.id = 1'
996
978
  )
997
979
  assert_equal 1, first.id
@@ -1044,8 +1026,8 @@ class FinderTest < ActiveRecord::TestCase
1044
1026
  end
1045
1027
 
1046
1028
  def test_select_values
1047
- assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s }
1048
- assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
1029
+ assert_equal ["1","2","3","4","5","6","7","8","9", "10"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s }
1030
+ assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
1049
1031
  end
1050
1032
 
1051
1033
  def test_select_rows
@@ -1062,7 +1044,7 @@ class FinderTest < ActiveRecord::TestCase
1062
1044
  def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
1063
1045
  assert_equal 2, Post.find(:all, :include => { :authors => :author_address }, :order => ' author_addresses.id DESC ', :limit => 2).size
1064
1046
 
1065
- assert_equal 3, Post.find(:all, :include => { :author => :author_address, :authors => :author_address},
1047
+ assert_equal 3, Post.find(:all, :include => { :author => :author_address, :authors => :author_address},
1066
1048
  :order => ' author_addresses_authors.id DESC ', :limit => 3).size
1067
1049
  end
1068
1050
  end
@@ -1074,10 +1056,10 @@ class FinderTest < ActiveRecord::TestCase
1074
1056
  end
1075
1057
 
1076
1058
  def test_finder_with_scoped_from
1077
- all_topics = Topic.all
1059
+ all_topics = Topic.find(:all)
1078
1060
 
1079
- Topic.with_scope(:find => { :from => 'fake_topics' }) do
1080
- assert_equal all_topics, Topic.all(:from => 'topics')
1061
+ Topic.send(:with_scope, :find => { :from => 'fake_topics' }) do
1062
+ assert_equal all_topics, Topic.from('topics').to_a
1081
1063
  end
1082
1064
  end
1083
1065
 
@@ -1089,4 +1071,18 @@ class FinderTest < ActiveRecord::TestCase
1089
1071
  ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
1090
1072
  end
1091
1073
  end
1074
+
1075
+ def with_env_tz(new_tz = 'US/Eastern')
1076
+ old_tz, ENV['TZ'] = ENV['TZ'], new_tz
1077
+ yield
1078
+ ensure
1079
+ old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
1080
+ end
1081
+
1082
+ def with_active_record_default_timezone(zone)
1083
+ old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
1084
+ yield
1085
+ ensure
1086
+ ActiveRecord::Base.default_timezone = old_zone
1087
+ end
1092
1088
  end