ibm_db 2.5.26-universal-darwin-14 → 2.6.1-universal-darwin-14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +11 -0
  3. data/MANIFEST +14 -14
  4. data/README +225 -225
  5. data/ext/Makefile.nt32 +181 -181
  6. data/ext/Makefile.nt32.191 +212 -212
  7. data/ext/extconf.rb +264 -261
  8. data/ext/extconf_MacOS.rb +269 -0
  9. data/ext/ibm_db.c +11879 -11793
  10. data/ext/ruby_ibm_db.h +241 -240
  11. data/ext/ruby_ibm_db_cli.c +851 -845
  12. data/ext/ruby_ibm_db_cli.h +500 -489
  13. data/init.rb +41 -41
  14. data/lib/IBM_DB.rb +27 -19
  15. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3339 -3289
  16. data/lib/active_record/connection_adapters/ibmdb_adapter.rb +1 -1
  17. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -328
  18. data/test/cases/adapter_test.rb +207 -207
  19. data/test/cases/associations/belongs_to_associations_test.rb +711 -711
  20. data/test/cases/associations/cascaded_eager_loading_test.rb +181 -181
  21. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +851 -851
  22. data/test/cases/associations/join_model_test.rb +743 -743
  23. data/test/cases/attribute_methods_test.rb +822 -822
  24. data/test/cases/base_test.rb +2133 -2133
  25. data/test/cases/calculations_test.rb +482 -482
  26. data/test/cases/migration_test.rb +2408 -2408
  27. data/test/cases/persistence_test.rb +642 -642
  28. data/test/cases/query_cache_test.rb +257 -257
  29. data/test/cases/relations_test.rb +1182 -1182
  30. data/test/cases/schema_dumper_test.rb +256 -256
  31. data/test/cases/transaction_callbacks_test.rb +300 -300
  32. data/test/cases/validations/uniqueness_validation_test.rb +299 -299
  33. data/test/cases/xml_serialization_test.rb +408 -408
  34. data/test/config.yml +154 -154
  35. data/test/connections/native_ibm_db/connection.rb +43 -43
  36. data/test/ibm_db_test.rb +24 -24
  37. data/test/models/warehouse_thing.rb +4 -4
  38. data/test/schema/schema.rb +751 -751
  39. metadata +6 -8
  40. data/lib/linux/rb18x/ibm_db.bundle +0 -0
  41. data/lib/linux/rb19x/ibm_db.bundle +0 -0
  42. data/lib/linux/rb20x/ibm_db.bundle +0 -0
  43. data/lib/linux/rb21x/ibm_db.bundle +0 -0
@@ -1,482 +1,482 @@
1
- require "cases/helper"
2
- require 'models/company'
3
- require "models/contract"
4
- require 'models/topic'
5
- require 'models/edge'
6
- require 'models/club'
7
- require 'models/organization'
8
-
9
- Company.has_many :accounts
10
-
11
- class NumericData < ActiveRecord::Base
12
- self.table_name = 'numeric_data'
13
- end
14
-
15
- class CalculationsTest < ActiveRecord::TestCase
16
- fixtures :companies, :accounts, :topics
17
-
18
- def test_should_sum_field
19
- assert_equal 318, Account.sum(:credit_limit)
20
- end
21
-
22
- def test_should_average_field
23
- value = Account.average(:credit_limit)
24
- assert_equal 53.0, value
25
- end
26
-
27
- def test_should_return_decimal_average_of_integer_field
28
- return if current_adapter?(:IBM_DBAdapter) #average cannot be a decimal value when applied on integer field
29
- value = Account.average(:id)
30
- assert_equal 3.5, value
31
- end
32
-
33
- def test_should_return_integer_average_if_db_returns_such
34
- Account.connection.stubs :select_value => 3
35
- value = Account.average(:id)
36
- assert_equal 3, value
37
- end
38
-
39
- def test_should_return_nil_as_average
40
- assert_nil NumericData.average(:bank_balance)
41
- end
42
-
43
- def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
44
- assert_equal 0, NumericData.scoped.send(:type_cast_calculated_value, 0, nil, 'avg')
45
- assert_equal 53.0, NumericData.scoped.send(:type_cast_calculated_value, 53, nil, 'avg')
46
- end
47
-
48
- def test_should_get_maximum_of_field
49
- assert_equal 60, Account.maximum(:credit_limit)
50
- end
51
-
52
- def test_should_get_maximum_of_field_with_include
53
- assert_equal 55, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
54
- end
55
-
56
- def test_should_get_maximum_of_field_with_scoped_include
57
- Account.send :with_scope, :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
58
- assert_equal 55, Account.maximum(:credit_limit)
59
- end
60
- end
61
-
62
- def test_should_get_minimum_of_field
63
- assert_equal 50, Account.minimum(:credit_limit)
64
- end
65
-
66
- def test_should_group_by_field
67
- c = Account.sum(:credit_limit, :group => :firm_id)
68
- [1,6,2].each { |firm_id| assert c.keys.include?(firm_id) }
69
- end
70
-
71
- def test_should_group_by_multiple_fields
72
- c = Account.count(:all, :group => ['firm_id', :credit_limit])
73
- [ [nil, 50], [1, 50], [6, 50], [6, 55], [9, 53], [2, 60] ].each { |firm_and_limit| assert c.keys.include?(firm_and_limit) }
74
- end
75
-
76
- def test_should_group_by_multiple_fields_having_functions
77
- c = Topic.group(:author_name, 'COALESCE(type, title)').count(:all)
78
- assert_equal 1, c[["Carl", "The Third Topic of the day"]]
79
- assert_equal 1, c[["Mary", "Reply"]]
80
- assert_equal 1, c[["David", "The First Topic"]]
81
- assert_equal 1, c[["Carl", "Reply"]]
82
- end
83
-
84
- def test_should_group_by_summed_field
85
- c = Account.sum(:credit_limit, :group => :firm_id)
86
- assert_equal 50, c[1]
87
- assert_equal 105, c[6]
88
- assert_equal 60, c[2]
89
- end
90
-
91
- def test_should_order_by_grouped_field
92
- c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id")
93
- assert_equal [1, 2, 6, 9], c.keys.compact
94
- end
95
-
96
- def test_should_order_by_calculation
97
- c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc, firm_id")
98
- assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
99
- assert_equal [6, 2, 9, 1], c.keys.compact
100
- end
101
-
102
- def test_should_limit_calculation
103
- c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
104
- :group => :firm_id, :order => "firm_id", :limit => 2)
105
- assert_equal [1, 2], c.keys.compact
106
- end
107
-
108
- def test_should_limit_calculation_with_offset
109
- c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
110
- :group => :firm_id, :order => "firm_id", :limit => 2, :offset => 1)
111
- assert_equal [2, 6], c.keys.compact
112
- end
113
-
114
- def test_limit_should_apply_before_count
115
- accounts = Account.limit(3).where('firm_id IS NOT NULL')
116
-
117
- assert_equal 3, accounts.count(:firm_id)
118
- assert_equal 3, accounts.select(:firm_id).count
119
- end
120
-
121
- def test_count_should_shortcut_with_limit_zero
122
- accounts = Account.limit(0)
123
-
124
- assert_no_queries { assert_equal 0, accounts.count }
125
- end
126
-
127
- def test_limit_is_kept
128
- return if current_adapter?(:OracleAdapter, :IBM_DBAdapter)
129
-
130
- queries = assert_sql { Account.limit(1).count }
131
- assert_equal 1, queries.length
132
- assert_match(/LIMIT/, queries.first)
133
- end
134
-
135
- def test_offset_is_kept
136
- return if current_adapter?(:OracleAdapter,:IBM_DBAdapter)
137
-
138
- queries = assert_sql { Account.offset(1).count }
139
- assert_equal 1, queries.length
140
- assert_match(/OFFSET/, queries.first)
141
- end
142
-
143
- def test_limit_with_offset_is_kept
144
- return if current_adapter?(:OracleAdapter,:IBM_DBAdapter)
145
-
146
- queries = assert_sql { Account.limit(1).offset(1).count }
147
- assert_equal 1, queries.length
148
- assert_match(/LIMIT/, queries.first)
149
- assert_match(/OFFSET/, queries.first)
150
- end
151
-
152
- def test_no_limit_no_offset
153
- queries = assert_sql { Account.count }
154
- assert_equal 1, queries.length
155
- assert_no_match(/LIMIT/, queries.first)
156
- assert_no_match(/OFFSET/, queries.first)
157
- end
158
-
159
- def test_should_group_by_summed_field_having_condition
160
- c = Account.sum(:credit_limit, :group => :firm_id,
161
- :having => 'sum(credit_limit) > 50')
162
- assert_nil c[1]
163
- assert_equal 105, c[6]
164
- assert_equal 60, c[2]
165
- end
166
-
167
- def test_should_group_by_summed_field_having_sanitized_condition
168
- c = Account.sum(:credit_limit, :group => :firm_id,
169
- :having => ['sum(credit_limit) > ?', 50])
170
- assert_nil c[1]
171
- assert_equal 105, c[6]
172
- assert_equal 60, c[2]
173
- end
174
-
175
- def test_should_group_by_summed_field_having_condition_from_select
176
- c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
177
- assert_nil c[1]
178
- assert_equal 60, c[2]
179
- assert_equal 53, c[9]
180
- end
181
-
182
- def test_should_group_by_summed_association
183
- c = Account.sum(:credit_limit, :group => :firm)
184
- assert_equal 50, c[companies(:first_firm)]
185
- assert_equal 105, c[companies(:rails_core)]
186
- assert_equal 60, c[companies(:first_client)]
187
- end
188
-
189
- def test_should_sum_field_with_conditions
190
- assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
191
- end
192
-
193
- def test_should_return_zero_if_sum_conditions_return_nothing
194
- assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
195
- assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
196
- end
197
-
198
- def test_sum_should_return_valid_values_for_decimals
199
- NumericData.create(:bank_balance => 19.83)
200
- assert_equal 19.83, NumericData.sum(:bank_balance)
201
- end
202
-
203
- def test_should_group_by_summed_field_with_conditions
204
- c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
205
- :group => :firm_id)
206
- assert_nil c[1]
207
- assert_equal 105, c[6]
208
- assert_equal 60, c[2]
209
- end
210
-
211
- def test_should_group_by_summed_field_with_conditions_and_having
212
- c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
213
- :group => :firm_id,
214
- :having => 'sum(credit_limit) > 60')
215
- assert_nil c[1]
216
- assert_equal 105, c[6]
217
- assert_nil c[2]
218
- end
219
-
220
- def test_should_group_by_fields_with_table_alias
221
- c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
222
- assert_equal 50, c[1]
223
- assert_equal 105, c[6]
224
- assert_equal 60, c[2]
225
- end
226
-
227
- def test_should_calculate_with_invalid_field
228
- assert_equal 6, Account.calculate(:count, '*')
229
- assert_equal 6, Account.calculate(:count, :all)
230
- end
231
-
232
- def test_should_calculate_grouped_with_invalid_field
233
- c = Account.count(:all, :group => 'accounts.firm_id')
234
- assert_equal 1, c[1]
235
- assert_equal 2, c[6]
236
- assert_equal 1, c[2]
237
- end
238
-
239
- def test_should_calculate_grouped_association_with_invalid_field
240
- c = Account.count(:all, :group => :firm)
241
- assert_equal 1, c[companies(:first_firm)]
242
- assert_equal 2, c[companies(:rails_core)]
243
- assert_equal 1, c[companies(:first_client)]
244
- end
245
-
246
- def test_should_group_by_association_with_non_numeric_foreign_key
247
- ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
248
-
249
- firm = mock()
250
- firm.expects(:id).returns("ABC")
251
- firm.expects(:class).returns(Firm)
252
- Company.expects(:find).with(["ABC"]).returns([firm])
253
-
254
- column = mock()
255
- column.expects(:name).at_least_once.returns(:firm_id)
256
- column.expects(:type_cast).with("ABC").returns("ABC")
257
- Account.expects(:columns).at_least_once.returns([column])
258
-
259
- c = Account.count(:all, :group => :firm)
260
- first_key = c.keys.first
261
- assert_equal Firm, first_key.class
262
- assert_equal 1, c[first_key]
263
- end
264
-
265
- def test_should_calculate_grouped_association_with_foreign_key_option
266
- Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
267
- c = Account.count(:all, :group => :another_firm)
268
- assert_equal 1, c[companies(:first_firm)]
269
- assert_equal 2, c[companies(:rails_core)]
270
- assert_equal 1, c[companies(:first_client)]
271
- end
272
-
273
- def test_should_not_modify_options_when_using_includes
274
- options = {:conditions => 'companies.id > 1', :include => :firm}
275
- options_copy = options.dup
276
-
277
- Account.count(:all, options)
278
- assert_equal options_copy, options
279
- end
280
-
281
- def test_should_calculate_grouped_by_function
282
- c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
283
- assert_equal 2, c[nil]
284
- assert_equal 1, c['DEPENDENTFIRM']
285
- assert_equal 4, c['CLIENT']
286
- assert_equal 2, c['FIRM']
287
- end
288
-
289
- def test_should_calculate_grouped_by_function_with_table_alias
290
- c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})")
291
- assert_equal 2, c[nil]
292
- assert_equal 1, c['DEPENDENTFIRM']
293
- assert_equal 4, c['CLIENT']
294
- assert_equal 2, c['FIRM']
295
- end
296
-
297
- def test_should_not_overshadow_enumerable_sum
298
- assert_equal 6, [1, 2, 3].sum(&:abs)
299
- end
300
-
301
- def test_should_sum_scoped_field
302
- assert_equal 15, companies(:rails_core).companies.sum(:id)
303
- end
304
-
305
- def test_should_sum_scoped_field_with_from
306
- assert_equal Club.count, Organization.clubs.count
307
- end
308
-
309
- def test_should_sum_scoped_field_with_conditions
310
- assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
311
- end
312
-
313
- def test_should_group_by_scoped_field
314
- c = companies(:rails_core).companies.sum(:id, :group => :name)
315
- assert_equal 7, c['Leetsoft']
316
- assert_equal 8, c['Jadedpixel']
317
- end
318
-
319
- def test_should_group_by_summed_field_through_association_and_having
320
- c = companies(:rails_core).companies.sum(:id, :group => :name,
321
- :having => 'sum(id) > 7')
322
- assert_nil c['Leetsoft']
323
- assert_equal 8, c['Jadedpixel']
324
- end
325
-
326
- def test_should_count_selected_field_with_include
327
- assert_equal 6, Account.count(:distinct => true, :include => :firm)
328
- assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
329
- end
330
-
331
- def test_should_not_perform_joined_include_by_default
332
- assert_equal Account.count, Account.includes(:firm).count
333
- queries = assert_sql { Account.includes(:firm).count }
334
- assert_no_match(/join/i, queries.last)
335
- end
336
-
337
- def test_should_perform_joined_include_when_referencing_included_tables
338
- joined_count = Account.includes(:firm).where(:companies => {:name => '37signals'}).count
339
- assert_equal 1, joined_count
340
- end
341
-
342
- def test_should_count_scoped_select
343
- Account.update_all("credit_limit = NULL")
344
- assert_equal 0, Account.scoped(:select => "credit_limit").count
345
- end
346
-
347
- def test_should_count_scoped_select_with_options
348
- Account.update_all("credit_limit = NULL")
349
- Account.last.update_column('credit_limit', 49)
350
- Account.first.update_column('credit_limit', 51)
351
-
352
- assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
353
- end
354
-
355
- def test_should_count_manual_select_with_include
356
- assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
357
- end
358
-
359
- def test_count_with_column_parameter
360
- assert_equal 5, Account.count(:firm_id)
361
- end
362
-
363
- def test_count_with_column_and_options_parameter
364
- assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50 AND firm_id IS NOT NULL")
365
- end
366
-
367
- def test_should_count_field_in_joined_table
368
- assert_equal 5, Account.count('companies.id', :joins => :firm)
369
- assert_equal 4, Account.count('companies.id', :joins => :firm, :distinct => true)
370
- end
371
-
372
- def test_should_count_field_in_joined_table_with_group_by
373
- c = Account.count('companies.id', :group => 'accounts.firm_id', :joins => :firm)
374
-
375
- [1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
376
- end
377
-
378
- def test_count_with_no_parameters_isnt_deprecated
379
- assert_not_deprecated { Account.count }
380
- end
381
-
382
- def test_count_with_too_many_parameters_raises
383
- assert_raise(ArgumentError) { Account.count(1, 2, 3) }
384
- end
385
-
386
- def test_should_sum_expression
387
- # Oracle adapter returns floating point value 636.0 after SUM
388
- if current_adapter?(:OracleAdapter)
389
- assert_equal 636, Account.sum("2 * credit_limit")
390
- else
391
- assert_equal 636, Account.sum("2 * credit_limit").to_i
392
- end
393
- end
394
-
395
- def test_count_with_from_option
396
- assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
397
- assert_equal Account.count(:all, :conditions => "credit_limit = 50"),
398
- Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50")
399
- assert_equal Company.count(:type, :conditions => {:type => "Firm"}),
400
- Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies')
401
- end
402
-
403
- def test_sum_with_from_option
404
- assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
405
- assert_equal Account.sum(:credit_limit, :conditions => "credit_limit > 50"),
406
- Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
407
- end
408
-
409
- def test_sum_array_compatibility
410
- assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
411
- end
412
-
413
- def test_average_with_from_option
414
- assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
415
- assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"),
416
- Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
417
- end
418
-
419
- def test_minimum_with_from_option
420
- assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
421
- assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit > 50"),
422
- Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
423
- end
424
-
425
- def test_maximum_with_from_option
426
- assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
427
- assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit > 50"),
428
- Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
429
- end
430
-
431
- def test_from_option_with_specified_index
432
- if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2'
433
- assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
434
- assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
435
- Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
436
- end
437
- end
438
-
439
- def test_from_option_with_table_different_than_class
440
- assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
441
- end
442
-
443
- def test_distinct_is_honored_when_used_with_count_operation_after_group
444
- # Count the number of authors for approved topics
445
- approved_topics_count = Topic.group(:approved).count(:author_name)[true]
446
- assert_equal approved_topics_count, 3
447
- # Count the number of distinct authors for approved Topics
448
- distinct_authors_for_approved_count = Topic.group(:approved).count(:author_name, :distinct => true)[true]
449
- assert_equal distinct_authors_for_approved_count, 2
450
- end
451
-
452
- def test_pluck
453
- assert_equal [1,2,3,4], Topic.order(:id).pluck(:id)
454
- end
455
-
456
- def test_pluck_type_cast
457
- topic = topics(:first)
458
- relation = Topic.where(:id => topic.id)
459
- assert_equal [ topic.approved ], relation.pluck(:approved)
460
- assert_equal [ topic.last_read ], relation.pluck(:last_read)
461
- assert_equal [ topic.written_on ], relation.pluck(:written_on)
462
- end
463
-
464
- def test_pluck_and_uniq
465
- assert_equal [50, 53, 55, 60], Account.order(:credit_limit).uniq.pluck(:credit_limit)
466
- end
467
-
468
- def test_pluck_in_relation
469
- company = Company.first
470
- contract = company.contracts.create!
471
- assert_equal [contract.id], company.contracts.pluck(:id)
472
- end
473
-
474
- def test_pluck_with_serialization
475
- t = Topic.create!(:content => { :foo => :bar })
476
- assert_equal [{:foo => :bar}], Topic.where(:id => t.id).pluck(:content)
477
- end
478
-
479
- def test_pluck_with_qualified_column_name
480
- assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
481
- end
482
- end
1
+ require "cases/helper"
2
+ require 'models/company'
3
+ require "models/contract"
4
+ require 'models/topic'
5
+ require 'models/edge'
6
+ require 'models/club'
7
+ require 'models/organization'
8
+
9
+ Company.has_many :accounts
10
+
11
+ class NumericData < ActiveRecord::Base
12
+ self.table_name = 'numeric_data'
13
+ end
14
+
15
+ class CalculationsTest < ActiveRecord::TestCase
16
+ fixtures :companies, :accounts, :topics
17
+
18
+ def test_should_sum_field
19
+ assert_equal 318, Account.sum(:credit_limit)
20
+ end
21
+
22
+ def test_should_average_field
23
+ value = Account.average(:credit_limit)
24
+ assert_equal 53.0, value
25
+ end
26
+
27
+ def test_should_return_decimal_average_of_integer_field
28
+ return if current_adapter?(:IBM_DBAdapter) #average cannot be a decimal value when applied on integer field
29
+ value = Account.average(:id)
30
+ assert_equal 3.5, value
31
+ end
32
+
33
+ def test_should_return_integer_average_if_db_returns_such
34
+ Account.connection.stubs :select_value => 3
35
+ value = Account.average(:id)
36
+ assert_equal 3, value
37
+ end
38
+
39
+ def test_should_return_nil_as_average
40
+ assert_nil NumericData.average(:bank_balance)
41
+ end
42
+
43
+ def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
44
+ assert_equal 0, NumericData.scoped.send(:type_cast_calculated_value, 0, nil, 'avg')
45
+ assert_equal 53.0, NumericData.scoped.send(:type_cast_calculated_value, 53, nil, 'avg')
46
+ end
47
+
48
+ def test_should_get_maximum_of_field
49
+ assert_equal 60, Account.maximum(:credit_limit)
50
+ end
51
+
52
+ def test_should_get_maximum_of_field_with_include
53
+ assert_equal 55, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
54
+ end
55
+
56
+ def test_should_get_maximum_of_field_with_scoped_include
57
+ Account.send :with_scope, :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
58
+ assert_equal 55, Account.maximum(:credit_limit)
59
+ end
60
+ end
61
+
62
+ def test_should_get_minimum_of_field
63
+ assert_equal 50, Account.minimum(:credit_limit)
64
+ end
65
+
66
+ def test_should_group_by_field
67
+ c = Account.sum(:credit_limit, :group => :firm_id)
68
+ [1,6,2].each { |firm_id| assert c.keys.include?(firm_id) }
69
+ end
70
+
71
+ def test_should_group_by_multiple_fields
72
+ c = Account.count(:all, :group => ['firm_id', :credit_limit])
73
+ [ [nil, 50], [1, 50], [6, 50], [6, 55], [9, 53], [2, 60] ].each { |firm_and_limit| assert c.keys.include?(firm_and_limit) }
74
+ end
75
+
76
+ def test_should_group_by_multiple_fields_having_functions
77
+ c = Topic.group(:author_name, 'COALESCE(type, title)').count(:all)
78
+ assert_equal 1, c[["Carl", "The Third Topic of the day"]]
79
+ assert_equal 1, c[["Mary", "Reply"]]
80
+ assert_equal 1, c[["David", "The First Topic"]]
81
+ assert_equal 1, c[["Carl", "Reply"]]
82
+ end
83
+
84
+ def test_should_group_by_summed_field
85
+ c = Account.sum(:credit_limit, :group => :firm_id)
86
+ assert_equal 50, c[1]
87
+ assert_equal 105, c[6]
88
+ assert_equal 60, c[2]
89
+ end
90
+
91
+ def test_should_order_by_grouped_field
92
+ c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id")
93
+ assert_equal [1, 2, 6, 9], c.keys.compact
94
+ end
95
+
96
+ def test_should_order_by_calculation
97
+ c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc, firm_id")
98
+ assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
99
+ assert_equal [6, 2, 9, 1], c.keys.compact
100
+ end
101
+
102
+ def test_should_limit_calculation
103
+ c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
104
+ :group => :firm_id, :order => "firm_id", :limit => 2)
105
+ assert_equal [1, 2], c.keys.compact
106
+ end
107
+
108
+ def test_should_limit_calculation_with_offset
109
+ c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
110
+ :group => :firm_id, :order => "firm_id", :limit => 2, :offset => 1)
111
+ assert_equal [2, 6], c.keys.compact
112
+ end
113
+
114
+ def test_limit_should_apply_before_count
115
+ accounts = Account.limit(3).where('firm_id IS NOT NULL')
116
+
117
+ assert_equal 3, accounts.count(:firm_id)
118
+ assert_equal 3, accounts.select(:firm_id).count
119
+ end
120
+
121
+ def test_count_should_shortcut_with_limit_zero
122
+ accounts = Account.limit(0)
123
+
124
+ assert_no_queries { assert_equal 0, accounts.count }
125
+ end
126
+
127
+ def test_limit_is_kept
128
+ return if current_adapter?(:OracleAdapter, :IBM_DBAdapter)
129
+
130
+ queries = assert_sql { Account.limit(1).count }
131
+ assert_equal 1, queries.length
132
+ assert_match(/LIMIT/, queries.first)
133
+ end
134
+
135
+ def test_offset_is_kept
136
+ return if current_adapter?(:OracleAdapter,:IBM_DBAdapter)
137
+
138
+ queries = assert_sql { Account.offset(1).count }
139
+ assert_equal 1, queries.length
140
+ assert_match(/OFFSET/, queries.first)
141
+ end
142
+
143
+ def test_limit_with_offset_is_kept
144
+ return if current_adapter?(:OracleAdapter,:IBM_DBAdapter)
145
+
146
+ queries = assert_sql { Account.limit(1).offset(1).count }
147
+ assert_equal 1, queries.length
148
+ assert_match(/LIMIT/, queries.first)
149
+ assert_match(/OFFSET/, queries.first)
150
+ end
151
+
152
+ def test_no_limit_no_offset
153
+ queries = assert_sql { Account.count }
154
+ assert_equal 1, queries.length
155
+ assert_no_match(/LIMIT/, queries.first)
156
+ assert_no_match(/OFFSET/, queries.first)
157
+ end
158
+
159
+ def test_should_group_by_summed_field_having_condition
160
+ c = Account.sum(:credit_limit, :group => :firm_id,
161
+ :having => 'sum(credit_limit) > 50')
162
+ assert_nil c[1]
163
+ assert_equal 105, c[6]
164
+ assert_equal 60, c[2]
165
+ end
166
+
167
+ def test_should_group_by_summed_field_having_sanitized_condition
168
+ c = Account.sum(:credit_limit, :group => :firm_id,
169
+ :having => ['sum(credit_limit) > ?', 50])
170
+ assert_nil c[1]
171
+ assert_equal 105, c[6]
172
+ assert_equal 60, c[2]
173
+ end
174
+
175
+ def test_should_group_by_summed_field_having_condition_from_select
176
+ c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
177
+ assert_nil c[1]
178
+ assert_equal 60, c[2]
179
+ assert_equal 53, c[9]
180
+ end
181
+
182
+ def test_should_group_by_summed_association
183
+ c = Account.sum(:credit_limit, :group => :firm)
184
+ assert_equal 50, c[companies(:first_firm)]
185
+ assert_equal 105, c[companies(:rails_core)]
186
+ assert_equal 60, c[companies(:first_client)]
187
+ end
188
+
189
+ def test_should_sum_field_with_conditions
190
+ assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
191
+ end
192
+
193
+ def test_should_return_zero_if_sum_conditions_return_nothing
194
+ assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
195
+ assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
196
+ end
197
+
198
+ def test_sum_should_return_valid_values_for_decimals
199
+ NumericData.create(:bank_balance => 19.83)
200
+ assert_equal 19.83, NumericData.sum(:bank_balance)
201
+ end
202
+
203
+ def test_should_group_by_summed_field_with_conditions
204
+ c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
205
+ :group => :firm_id)
206
+ assert_nil c[1]
207
+ assert_equal 105, c[6]
208
+ assert_equal 60, c[2]
209
+ end
210
+
211
+ def test_should_group_by_summed_field_with_conditions_and_having
212
+ c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
213
+ :group => :firm_id,
214
+ :having => 'sum(credit_limit) > 60')
215
+ assert_nil c[1]
216
+ assert_equal 105, c[6]
217
+ assert_nil c[2]
218
+ end
219
+
220
+ def test_should_group_by_fields_with_table_alias
221
+ c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
222
+ assert_equal 50, c[1]
223
+ assert_equal 105, c[6]
224
+ assert_equal 60, c[2]
225
+ end
226
+
227
+ def test_should_calculate_with_invalid_field
228
+ assert_equal 6, Account.calculate(:count, '*')
229
+ assert_equal 6, Account.calculate(:count, :all)
230
+ end
231
+
232
+ def test_should_calculate_grouped_with_invalid_field
233
+ c = Account.count(:all, :group => 'accounts.firm_id')
234
+ assert_equal 1, c[1]
235
+ assert_equal 2, c[6]
236
+ assert_equal 1, c[2]
237
+ end
238
+
239
+ def test_should_calculate_grouped_association_with_invalid_field
240
+ c = Account.count(:all, :group => :firm)
241
+ assert_equal 1, c[companies(:first_firm)]
242
+ assert_equal 2, c[companies(:rails_core)]
243
+ assert_equal 1, c[companies(:first_client)]
244
+ end
245
+
246
+ def test_should_group_by_association_with_non_numeric_foreign_key
247
+ ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
248
+
249
+ firm = mock()
250
+ firm.expects(:id).returns("ABC")
251
+ firm.expects(:class).returns(Firm)
252
+ Company.expects(:find).with(["ABC"]).returns([firm])
253
+
254
+ column = mock()
255
+ column.expects(:name).at_least_once.returns(:firm_id)
256
+ column.expects(:type_cast).with("ABC").returns("ABC")
257
+ Account.expects(:columns).at_least_once.returns([column])
258
+
259
+ c = Account.count(:all, :group => :firm)
260
+ first_key = c.keys.first
261
+ assert_equal Firm, first_key.class
262
+ assert_equal 1, c[first_key]
263
+ end
264
+
265
+ def test_should_calculate_grouped_association_with_foreign_key_option
266
+ Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
267
+ c = Account.count(:all, :group => :another_firm)
268
+ assert_equal 1, c[companies(:first_firm)]
269
+ assert_equal 2, c[companies(:rails_core)]
270
+ assert_equal 1, c[companies(:first_client)]
271
+ end
272
+
273
+ def test_should_not_modify_options_when_using_includes
274
+ options = {:conditions => 'companies.id > 1', :include => :firm}
275
+ options_copy = options.dup
276
+
277
+ Account.count(:all, options)
278
+ assert_equal options_copy, options
279
+ end
280
+
281
+ def test_should_calculate_grouped_by_function
282
+ c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
283
+ assert_equal 2, c[nil]
284
+ assert_equal 1, c['DEPENDENTFIRM']
285
+ assert_equal 4, c['CLIENT']
286
+ assert_equal 2, c['FIRM']
287
+ end
288
+
289
+ def test_should_calculate_grouped_by_function_with_table_alias
290
+ c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})")
291
+ assert_equal 2, c[nil]
292
+ assert_equal 1, c['DEPENDENTFIRM']
293
+ assert_equal 4, c['CLIENT']
294
+ assert_equal 2, c['FIRM']
295
+ end
296
+
297
+ def test_should_not_overshadow_enumerable_sum
298
+ assert_equal 6, [1, 2, 3].sum(&:abs)
299
+ end
300
+
301
+ def test_should_sum_scoped_field
302
+ assert_equal 15, companies(:rails_core).companies.sum(:id)
303
+ end
304
+
305
+ def test_should_sum_scoped_field_with_from
306
+ assert_equal Club.count, Organization.clubs.count
307
+ end
308
+
309
+ def test_should_sum_scoped_field_with_conditions
310
+ assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
311
+ end
312
+
313
+ def test_should_group_by_scoped_field
314
+ c = companies(:rails_core).companies.sum(:id, :group => :name)
315
+ assert_equal 7, c['Leetsoft']
316
+ assert_equal 8, c['Jadedpixel']
317
+ end
318
+
319
+ def test_should_group_by_summed_field_through_association_and_having
320
+ c = companies(:rails_core).companies.sum(:id, :group => :name,
321
+ :having => 'sum(id) > 7')
322
+ assert_nil c['Leetsoft']
323
+ assert_equal 8, c['Jadedpixel']
324
+ end
325
+
326
+ def test_should_count_selected_field_with_include
327
+ assert_equal 6, Account.count(:distinct => true, :include => :firm)
328
+ assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
329
+ end
330
+
331
+ def test_should_not_perform_joined_include_by_default
332
+ assert_equal Account.count, Account.includes(:firm).count
333
+ queries = assert_sql { Account.includes(:firm).count }
334
+ assert_no_match(/join/i, queries.last)
335
+ end
336
+
337
+ def test_should_perform_joined_include_when_referencing_included_tables
338
+ joined_count = Account.includes(:firm).where(:companies => {:name => '37signals'}).count
339
+ assert_equal 1, joined_count
340
+ end
341
+
342
+ def test_should_count_scoped_select
343
+ Account.update_all("credit_limit = NULL")
344
+ assert_equal 0, Account.scoped(:select => "credit_limit").count
345
+ end
346
+
347
+ def test_should_count_scoped_select_with_options
348
+ Account.update_all("credit_limit = NULL")
349
+ Account.last.update_column('credit_limit', 49)
350
+ Account.first.update_column('credit_limit', 51)
351
+
352
+ assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
353
+ end
354
+
355
+ def test_should_count_manual_select_with_include
356
+ assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
357
+ end
358
+
359
+ def test_count_with_column_parameter
360
+ assert_equal 5, Account.count(:firm_id)
361
+ end
362
+
363
+ def test_count_with_column_and_options_parameter
364
+ assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50 AND firm_id IS NOT NULL")
365
+ end
366
+
367
+ def test_should_count_field_in_joined_table
368
+ assert_equal 5, Account.count('companies.id', :joins => :firm)
369
+ assert_equal 4, Account.count('companies.id', :joins => :firm, :distinct => true)
370
+ end
371
+
372
+ def test_should_count_field_in_joined_table_with_group_by
373
+ c = Account.count('companies.id', :group => 'accounts.firm_id', :joins => :firm)
374
+
375
+ [1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
376
+ end
377
+
378
+ def test_count_with_no_parameters_isnt_deprecated
379
+ assert_not_deprecated { Account.count }
380
+ end
381
+
382
+ def test_count_with_too_many_parameters_raises
383
+ assert_raise(ArgumentError) { Account.count(1, 2, 3) }
384
+ end
385
+
386
+ def test_should_sum_expression
387
+ # Oracle adapter returns floating point value 636.0 after SUM
388
+ if current_adapter?(:OracleAdapter)
389
+ assert_equal 636, Account.sum("2 * credit_limit")
390
+ else
391
+ assert_equal 636, Account.sum("2 * credit_limit").to_i
392
+ end
393
+ end
394
+
395
+ def test_count_with_from_option
396
+ assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
397
+ assert_equal Account.count(:all, :conditions => "credit_limit = 50"),
398
+ Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50")
399
+ assert_equal Company.count(:type, :conditions => {:type => "Firm"}),
400
+ Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies')
401
+ end
402
+
403
+ def test_sum_with_from_option
404
+ assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
405
+ assert_equal Account.sum(:credit_limit, :conditions => "credit_limit > 50"),
406
+ Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
407
+ end
408
+
409
+ def test_sum_array_compatibility
410
+ assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
411
+ end
412
+
413
+ def test_average_with_from_option
414
+ assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
415
+ assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"),
416
+ Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
417
+ end
418
+
419
+ def test_minimum_with_from_option
420
+ assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
421
+ assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit > 50"),
422
+ Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
423
+ end
424
+
425
+ def test_maximum_with_from_option
426
+ assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
427
+ assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit > 50"),
428
+ Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
429
+ end
430
+
431
+ def test_from_option_with_specified_index
432
+ if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2'
433
+ assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
434
+ assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
435
+ Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
436
+ end
437
+ end
438
+
439
+ def test_from_option_with_table_different_than_class
440
+ assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
441
+ end
442
+
443
+ def test_distinct_is_honored_when_used_with_count_operation_after_group
444
+ # Count the number of authors for approved topics
445
+ approved_topics_count = Topic.group(:approved).count(:author_name)[true]
446
+ assert_equal approved_topics_count, 3
447
+ # Count the number of distinct authors for approved Topics
448
+ distinct_authors_for_approved_count = Topic.group(:approved).count(:author_name, :distinct => true)[true]
449
+ assert_equal distinct_authors_for_approved_count, 2
450
+ end
451
+
452
+ def test_pluck
453
+ assert_equal [1,2,3,4], Topic.order(:id).pluck(:id)
454
+ end
455
+
456
+ def test_pluck_type_cast
457
+ topic = topics(:first)
458
+ relation = Topic.where(:id => topic.id)
459
+ assert_equal [ topic.approved ], relation.pluck(:approved)
460
+ assert_equal [ topic.last_read ], relation.pluck(:last_read)
461
+ assert_equal [ topic.written_on ], relation.pluck(:written_on)
462
+ end
463
+
464
+ def test_pluck_and_uniq
465
+ assert_equal [50, 53, 55, 60], Account.order(:credit_limit).uniq.pluck(:credit_limit)
466
+ end
467
+
468
+ def test_pluck_in_relation
469
+ company = Company.first
470
+ contract = company.contracts.create!
471
+ assert_equal [contract.id], company.contracts.pluck(:id)
472
+ end
473
+
474
+ def test_pluck_with_serialization
475
+ t = Topic.create!(:content => { :foo => :bar })
476
+ assert_equal [{:foo => :bar}], Topic.where(:id => t.id).pluck(:content)
477
+ end
478
+
479
+ def test_pluck_with_qualified_column_name
480
+ assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
481
+ end
482
+ end