calculations_test.rb 19.9 KB
Newer Older
1
require "cases/helper"
2
require 'models/club'
J
Jeremy Kemper 已提交
3
require 'models/company'
4
require "models/contract"
5
require 'models/edge'
6
require 'models/organization'
7 8
require 'models/possession'
require 'models/topic'
9 10
require 'models/minivan'
require 'models/speedometer'
A
Aaron Patterson 已提交
11
require 'models/ship_part'
12 13 14

Company.has_many :accounts

15 16 17 18
class NumericData < ActiveRecord::Base
  self.table_name = 'numeric_data'
end

19
class CalculationsTest < ActiveRecord::TestCase
20 21 22
  fixtures :companies, :accounts, :topics

  def test_should_sum_field
23
    assert_equal 318, Account.sum(:credit_limit)
24 25 26 27
  end

  def test_should_average_field
    value = Account.average(:credit_limit)
28
    assert_equal 53.0, value
29 30
  end

31 32 33 34 35
  def test_should_return_decimal_average_of_integer_field
    value = Account.average(:id)
    assert_equal 3.5, value
  end

36
  def test_should_return_integer_average_if_db_returns_such
A
Aaron Patterson 已提交
37 38 39
    ShipPart.delete_all
    ShipPart.create!(:id => 3, :name => 'foo')
    value = ShipPart.average(:id)
40 41 42
    assert_equal 3, value
  end

43 44 45
  def test_should_return_nil_as_average
    assert_nil NumericData.average(:bank_balance)
  end
46

47
  def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
48 49
    assert_equal 0, NumericData.all.send(:type_cast_calculated_value, 0, nil, 'avg')
    assert_equal 53.0, NumericData.all.send(:type_cast_calculated_value, 53, nil, 'avg')
50
  end
51

52 53 54 55
  def test_should_get_maximum_of_field
    assert_equal 60, Account.maximum(:credit_limit)
  end

56
  def test_should_get_maximum_of_field_with_include
J
Jon Leighton 已提交
57
    assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(:credit_limit)
58 59
  end

60 61 62 63 64
  def test_should_get_minimum_of_field
    assert_equal 50, Account.minimum(:credit_limit)
  end

  def test_should_group_by_field
J
Jon Leighton 已提交
65
    c = Account.group(:firm_id).sum(:credit_limit)
66 67 68
    [1,6,2].each do |firm_id|
      assert c.keys.include?(firm_id), "Group #{c.inspect} does not contain firm_id #{firm_id}"
    end
69
  end
70

71
  def test_should_group_by_arel_attribute
72 73 74 75
    c = Account.group(Account.arel_table[:firm_id]).sum(:credit_limit)
    [1,6,2].each do |firm_id|
      assert c.keys.include?(firm_id), "Group #{c.inspect} does not contain firm_id #{firm_id}"
    end
76 77
  end

78
  def test_should_group_by_multiple_fields
J
Jon Leighton 已提交
79
    c = Account.group('firm_id', :credit_limit).count(:all)
80 81 82 83 84 85 86 87 88 89
    [ [nil, 50], [1, 50], [6, 50], [6, 55], [9, 53], [2, 60] ].each { |firm_and_limit| assert c.keys.include?(firm_and_limit) }
  end

  def test_should_group_by_multiple_fields_having_functions
    c = Topic.group(:author_name, 'COALESCE(type, title)').count(:all)
    assert_equal 1, c[["Carl", "The Third Topic of the day"]]
    assert_equal 1, c[["Mary", "Reply"]]
    assert_equal 1, c[["David", "The First Topic"]]
    assert_equal 1, c[["Carl", "Reply"]]
  end
90 91

  def test_should_group_by_summed_field
J
Jon Leighton 已提交
92
    c = Account.group(:firm_id).sum(:credit_limit)
93 94 95
    assert_equal 50,   c[1]
    assert_equal 105,  c[6]
    assert_equal 60,   c[2]
96 97
  end

98
  def test_should_order_by_grouped_field
99
    c = Account.all.merge!(:group => :firm_id, :order => "firm_id").sum(:credit_limit)
100
    assert_equal [1, 2, 6, 9], c.keys.compact
101 102 103
  end

  def test_should_order_by_calculation
104
    c = Account.all.merge!(:group => :firm_id, :order => "sum_credit_limit desc, firm_id").sum(:credit_limit)
105 106
    assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
    assert_equal [6, 2, 9, 1], c.keys.compact
107 108
  end

109
  def test_should_limit_calculation
110
    c = Account.all.merge!(:where => "firm_id IS NOT NULL",
J
Jon Leighton 已提交
111
                       :group => :firm_id, :order => "firm_id", :limit => 2).sum(:credit_limit)
112 113 114 115
    assert_equal [1, 2], c.keys.compact
  end

  def test_should_limit_calculation_with_offset
116
    c = Account.all.merge!(:where => "firm_id IS NOT NULL", :group => :firm_id,
J
Jon Leighton 已提交
117
                       :order => "firm_id", :limit => 2, :offset => 1).sum(:credit_limit)
118 119 120
    assert_equal [2, 6], c.keys.compact
  end

121 122 123 124 125 126 127 128 129 130 131 132 133 134
  def test_limit_should_apply_before_count
    accounts = Account.limit(3).where('firm_id IS NOT NULL')

    assert_equal 3, accounts.count(:firm_id)
    assert_equal 3, accounts.select(:firm_id).count
  end

  def test_count_should_shortcut_with_limit_zero
    accounts = Account.limit(0)

    assert_no_queries { assert_equal 0, accounts.count }
  end

  def test_limit_is_kept
A
Aaron Patterson 已提交
135 136
    return if current_adapter?(:OracleAdapter)

137
    queries = assert_sql { Account.limit(1).count }
138 139 140 141
    assert_equal 1, queries.length
    assert_match(/LIMIT/, queries.first)
  end

142 143 144
  def test_offset_is_kept
    return if current_adapter?(:OracleAdapter)

145 146
    queries = assert_sql { Account.offset(1).count }
    assert_equal 1, queries.length
147
    assert_match(/OFFSET/, queries.first)
148 149
  end

150 151 152 153
  def test_limit_with_offset_is_kept
    return if current_adapter?(:OracleAdapter)

    queries = assert_sql { Account.limit(1).offset(1).count }
154
    assert_equal 1, queries.length
155 156
    assert_match(/LIMIT/, queries.first)
    assert_match(/OFFSET/, queries.first)
157 158 159 160 161 162 163 164 165
  end

  def test_no_limit_no_offset
    queries = assert_sql { Account.count }
    assert_equal 1, queries.length
    assert_no_match(/LIMIT/, queries.first)
    assert_no_match(/OFFSET/, queries.first)
  end

166
  def test_should_group_by_summed_field_having_condition
167
    c = Account.all.merge!(:group => :firm_id,
J
Jon Leighton 已提交
168
                       :having => 'sum(credit_limit) > 50').sum(:credit_limit)
169 170 171 172 173
    assert_nil        c[1]
    assert_equal 105, c[6]
    assert_equal 60,  c[2]
  end

174
  def test_should_group_by_summed_field_having_condition_from_select
175
    c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
176 177 178 179 180
    assert_nil       c[1]
    assert_equal 60, c[2]
    assert_equal 53, c[9]
  end

181
  def test_should_group_by_summed_association
J
Jon Leighton 已提交
182
    c = Account.group(:firm).sum(:credit_limit)
183 184 185 186
    assert_equal 50,   c[companies(:first_firm)]
    assert_equal 105,  c[companies(:rails_core)]
    assert_equal 60,   c[companies(:first_client)]
  end
J
Jeremy Kemper 已提交
187

188
  def test_should_sum_field_with_conditions
J
Jon Leighton 已提交
189
    assert_equal 105, Account.where('firm_id = 6').sum(:credit_limit)
190 191
  end

192
  def test_should_return_zero_if_sum_conditions_return_nothing
J
Jon Leighton 已提交
193 194
    assert_equal 0, Account.where('1 = 2').sum(:credit_limit)
    assert_equal 0, companies(:rails_core).companies.where('1 = 2').sum(:id)
195 196
  end

197 198 199 200 201
  def test_sum_should_return_valid_values_for_decimals
    NumericData.create(:bank_balance => 19.83)
    assert_equal 19.83, NumericData.sum(:bank_balance)
  end

202
  def test_should_group_by_summed_field_with_conditions
203
    c = Account.all.merge!(:where => 'firm_id > 1',
J
Jon Leighton 已提交
204
                       :group => :firm_id).sum(:credit_limit)
205 206 207
    assert_nil        c[1]
    assert_equal 105, c[6]
    assert_equal 60,  c[2]
208
  end
J
Jeremy Kemper 已提交
209

210
  def test_should_group_by_summed_field_with_conditions_and_having
211
    c = Account.all.merge!(:where => 'firm_id > 1',
J
Jon Leighton 已提交
212 213
                       :group => :firm_id,
                       :having => 'sum(credit_limit) > 60').sum(:credit_limit)
214 215 216
    assert_nil        c[1]
    assert_equal 105, c[6]
    assert_nil        c[2]
217 218 219
  end

  def test_should_group_by_fields_with_table_alias
J
Jon Leighton 已提交
220
    c = Account.group('accounts.firm_id').sum(:credit_limit)
221 222 223
    assert_equal 50,  c[1]
    assert_equal 105, c[6]
    assert_equal 60,  c[2]
224
  end
J
Jeremy Kemper 已提交
225

226
  def test_should_calculate_with_invalid_field
227 228
    assert_equal 6, Account.calculate(:count, '*')
    assert_equal 6, Account.calculate(:count, :all)
229
  end
J
Jeremy Kemper 已提交
230

231
  def test_should_calculate_grouped_with_invalid_field
J
Jon Leighton 已提交
232
    c = Account.group('accounts.firm_id').count(:all)
233 234 235
    assert_equal 1, c[1]
    assert_equal 2, c[6]
    assert_equal 1, c[2]
236
  end
J
Jeremy Kemper 已提交
237

238
  def test_should_calculate_grouped_association_with_invalid_field
J
Jon Leighton 已提交
239
    c = Account.group(:firm).count(:all)
240 241 242 243
    assert_equal 1, c[companies(:first_firm)]
    assert_equal 2, c[companies(:rails_core)]
    assert_equal 1, c[companies(:first_client)]
  end
244

245
  def test_should_group_by_association_with_non_numeric_foreign_key
A
Aaron Patterson 已提交
246 247
    Speedometer.create! id: 'ABC'
    Minivan.create! id: 'OMG', speedometer_id: 'ABC'
248

249
    c = Minivan.group(:speedometer).count(:all)
250
    first_key = c.keys.first
251
    assert_equal Speedometer, first_key.class
252
    assert_equal 1, c[first_key]
253
  end
J
Jeremy Kemper 已提交
254

255 256
  def test_should_calculate_grouped_association_with_foreign_key_option
    Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
J
Jon Leighton 已提交
257
    c = Account.group(:another_firm).count(:all)
258 259 260 261 262
    assert_equal 1, c[companies(:first_firm)]
    assert_equal 2, c[companies(:rails_core)]
    assert_equal 1, c[companies(:first_client)]
  end

263
  def test_should_calculate_grouped_by_function
J
Jon Leighton 已提交
264
    c = Company.group("UPPER(#{QUOTED_TYPE})").count(:all)
265 266
    assert_equal 2, c[nil]
    assert_equal 1, c['DEPENDENTFIRM']
267
    assert_equal 4, c['CLIENT']
268 269
    assert_equal 2, c['FIRM']
  end
J
Jeremy Kemper 已提交
270

271
  def test_should_calculate_grouped_by_function_with_table_alias
J
Jon Leighton 已提交
272
    c = Company.group("UPPER(companies.#{QUOTED_TYPE})").count(:all)
273 274
    assert_equal 2, c[nil]
    assert_equal 1, c['DEPENDENTFIRM']
275
    assert_equal 4, c['CLIENT']
276
    assert_equal 2, c['FIRM']
277
  end
J
Jeremy Kemper 已提交
278

279 280 281
  def test_should_not_overshadow_enumerable_sum
    assert_equal 6, [1, 2, 3].sum(&:abs)
  end
282 283 284 285 286

  def test_should_sum_scoped_field
    assert_equal 15, companies(:rails_core).companies.sum(:id)
  end

287 288 289 290
  def test_should_sum_scoped_field_with_from
    assert_equal Club.count, Organization.clubs.count
  end

291
  def test_should_sum_scoped_field_with_conditions
J
Jon Leighton 已提交
292
    assert_equal 8,  companies(:rails_core).companies.where('id > 7').sum(:id)
293 294 295
  end

  def test_should_group_by_scoped_field
J
Jon Leighton 已提交
296
    c = companies(:rails_core).companies.group(:name).sum(:id)
297 298 299 300
    assert_equal 7, c['Leetsoft']
    assert_equal 8, c['Jadedpixel']
  end

E
Emilio Tagua 已提交
301
  def test_should_group_by_summed_field_through_association_and_having
J
Jon Leighton 已提交
302
    c = companies(:rails_core).companies.group(:name).having('sum(id) > 7').sum(:id)
303 304 305
    assert_nil      c['Leetsoft']
    assert_equal 8, c['Jadedpixel']
  end
306

307
  def test_should_count_selected_field_with_include
308 309
    assert_equal 6, Account.includes(:firm).distinct.count
    assert_equal 4, Account.includes(:firm).distinct.select(:credit_limit).count
310
  end
J
Jeremy Kemper 已提交
311

312 313 314 315 316 317 318
  def test_should_not_perform_joined_include_by_default
    assert_equal Account.count, Account.includes(:firm).count
    queries = assert_sql { Account.includes(:firm).count }
    assert_no_match(/join/i, queries.last)
  end

  def test_should_perform_joined_include_when_referencing_included_tables
319
    joined_count = Account.includes(:firm).where(:companies => {:name => '37signals'}).count
320 321 322
    assert_equal 1, joined_count
  end

323
  def test_should_count_scoped_select
324
    Account.update_all("credit_limit = NULL")
325
    assert_equal 0, Account.all.merge!(:select => "credit_limit").count
326 327 328
  end

  def test_should_count_scoped_select_with_options
329
    Account.update_all("credit_limit = NULL")
330 331
    Account.last.update_columns('credit_limit' => 49)
    Account.first.update_columns('credit_limit' => 51)
332

333
    assert_equal 1, Account.all.merge!(:select => "credit_limit").where('credit_limit >= 50').count
334 335
  end

336
  def test_should_count_manual_select_with_include
337
    assert_equal 6, Account.all.merge!(:select => "DISTINCT accounts.id", :includes => :firm).count
338 339
  end

340 341 342
  def test_count_with_column_parameter
    assert_equal 5, Account.count(:firm_id)
  end
J
Jeremy Kemper 已提交
343

344 345 346 347 348 349 350 351 352 353
  def test_count_distinct_option_is_deprecated
    assert_deprecated do
      assert_equal 4, Account.select(:credit_limit).count(distinct: true)
    end

    assert_deprecated do
      assert_equal 6, Account.select(:credit_limit).count(distinct: false)
    end
  end

354 355
  def test_count_with_distinct
    assert_equal 4, Account.select(:credit_limit).distinct.count
356 357 358
    assert_equal 4, Account.select(:credit_limit).uniq.count
  end

359
  def test_count_with_column_and_options_parameter
J
Jon Leighton 已提交
360
    assert_equal 2, Account.where("credit_limit = 50 AND firm_id IS NOT NULL").count(:firm_id)
361
  end
J
Jeremy Kemper 已提交
362

O
oleg dashevskii 已提交
363
  def test_should_count_field_in_joined_table
J
Jon Leighton 已提交
364
    assert_equal 5, Account.joins(:firm).count('companies.id')
365
    assert_equal 4, Account.joins(:firm).distinct.count('companies.id')
O
oleg dashevskii 已提交
366 367 368
  end

  def test_should_count_field_in_joined_table_with_group_by
369
    c = Account.all.merge!(:group => 'accounts.firm_id', :joins => :firm).count('companies.id')
O
oleg dashevskii 已提交
370 371 372 373

    [1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
  end

374 375 376 377 378 379 380
  def test_count_with_no_parameters_isnt_deprecated
    assert_not_deprecated { Account.count }
  end

  def test_count_with_too_many_parameters_raises
    assert_raise(ArgumentError) { Account.count(1, 2, 3) }
  end
381 382

  def test_should_sum_expression
383 384 385 386
    # Oracle adapter returns floating point value 636.0 after SUM
    if current_adapter?(:OracleAdapter)
      assert_equal 636, Account.sum("2 * credit_limit")
    else
387
      assert_equal 636, Account.sum("2 * credit_limit").to_i
388
    end
389
  end
390

391 392 393 394
  def test_sum_expression_returns_zero_when_no_records_to_sum
    assert_equal 0, Account.where('1 = 2').sum("2 * credit_limit")
  end

395
  def test_count_with_from_option
J
Jon Leighton 已提交
396 397 398 399 400
    assert_equal Company.count(:all), Company.from('companies').count(:all)
    assert_equal Account.where("credit_limit = 50").count(:all),
        Account.from('accounts').where("credit_limit = 50").count(:all)
    assert_equal Company.where(:type => "Firm").count(:type),
        Company.where(:type => "Firm").from('companies').count(:type)
401 402 403
  end

  def test_sum_with_from_option
J
Jon Leighton 已提交
404 405 406
    assert_equal Account.sum(:credit_limit), Account.from('accounts').sum(:credit_limit)
    assert_equal Account.where("credit_limit > 50").sum(:credit_limit),
        Account.where("credit_limit > 50").from('accounts').sum(:credit_limit)
407 408
  end

409 410 411 412
  def test_sum_array_compatibility_deprecation
    assert_deprecated do
      assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
    end
413 414
  end

415
  def test_average_with_from_option
J
Jon Leighton 已提交
416 417 418
    assert_equal Account.average(:credit_limit), Account.from('accounts').average(:credit_limit)
    assert_equal Account.where("credit_limit > 50").average(:credit_limit),
        Account.where("credit_limit > 50").from('accounts').average(:credit_limit)
419 420 421
  end

  def test_minimum_with_from_option
J
Jon Leighton 已提交
422 423 424
    assert_equal Account.minimum(:credit_limit), Account.from('accounts').minimum(:credit_limit)
    assert_equal Account.where("credit_limit > 50").minimum(:credit_limit),
        Account.where("credit_limit > 50").from('accounts').minimum(:credit_limit)
425 426 427
  end

  def test_maximum_with_from_option
J
Jon Leighton 已提交
428 429 430
    assert_equal Account.maximum(:credit_limit), Account.from('accounts').maximum(:credit_limit)
    assert_equal Account.where("credit_limit > 50").maximum(:credit_limit),
        Account.where("credit_limit > 50").from('accounts').maximum(:credit_limit)
431 432
  end

433 434 435
  def test_maximum_with_not_auto_table_name_prefix_if_column_included
    Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])

A
Aaron Patterson 已提交
436
    assert_equal 7, Company.includes(:contracts).maximum(:developer_id)
437 438 439 440 441
  end

  def test_minimum_with_not_auto_table_name_prefix_if_column_included
    Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])

A
Aaron Patterson 已提交
442
    assert_equal 7, Company.includes(:contracts).minimum(:developer_id)
443 444 445 446 447
  end

  def test_sum_with_not_auto_table_name_prefix_if_column_included
    Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])

A
Aaron Patterson 已提交
448
    assert_equal 7, Company.includes(:contracts).sum(:developer_id)
449 450 451
  end


452
  def test_from_option_with_specified_index
B
Brian Lopez 已提交
453
    if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2'
J
Jon Leighton 已提交
454 455 456
      assert_equal Edge.count(:all), Edge.from('edges USE INDEX(unique_edge_index)').count(:all)
      assert_equal Edge.where('sink_id < 5').count(:all),
          Edge.from('edges USE INDEX(unique_edge_index)').where('sink_id < 5').count(:all)
457 458 459 460
    end
  end

  def test_from_option_with_table_different_than_class
J
Jon Leighton 已提交
461
    assert_equal Account.count(:all), Company.from('accounts').count(:all)
462
  end
463 464 465 466 467 468

  def test_distinct_is_honored_when_used_with_count_operation_after_group
    # Count the number of authors for approved topics
    approved_topics_count = Topic.group(:approved).count(:author_name)[true]
    assert_equal approved_topics_count, 3
    # Count the number of distinct authors for approved Topics
469
    distinct_authors_for_approved_count = Topic.group(:approved).distinct.count(:author_name)[true]
470 471
    assert_equal distinct_authors_for_approved_count, 2
  end
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

  def test_pluck
    assert_equal [1,2,3,4], Topic.order(:id).pluck(:id)
  end

  def test_pluck_type_cast
    topic = topics(:first)
    relation = Topic.where(:id => topic.id)
    assert_equal [ topic.approved ], relation.pluck(:approved)
    assert_equal [ topic.last_read ], relation.pluck(:last_read)
    assert_equal [ topic.written_on ], relation.pluck(:written_on)
  end

  def test_pluck_and_uniq
    assert_equal [50, 53, 55, 60], Account.order(:credit_limit).uniq.pluck(:credit_limit)
  end

  def test_pluck_in_relation
    company = Company.first
    contract = company.contracts.create!
    assert_equal [contract.id], company.contracts.pluck(:id)
  end

495 496 497 498 499 500 501 502
  def test_pluck_with_serialization
    t = Topic.create!(:content => { :foo => :bar })
    assert_equal [{:foo => :bar}], Topic.where(:id => t.id).pluck(:content)
  end

  def test_pluck_with_qualified_column_name
    assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
  end
503 504 505 506 507 508

  def test_pluck_auto_table_name_prefix
    c = Company.create!(:name => "test", :contracts => [Contract.new])
    assert_equal [c.id], Company.joins(:contracts).pluck(:id)
  end

509 510 511 512 513
  def test_pluck_if_table_included
    c = Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
    assert_equal [c.id], Company.includes(:contracts).where("contracts.id" => c.contracts.first).pluck(:id)
  end

514
  def test_pluck_not_auto_table_name_prefix_if_column_joined
515
    Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
516
    assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
517
  end
T
twinturbo 已提交
518

519 520
  def test_pluck_with_selection_clause
    assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT credit_limit').sort
521 522
    assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT accounts.credit_limit').sort
    assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT(credit_limit)').sort
523 524 525 526 527

    # MySQL returns "SUM(DISTINCT(credit_limit))" as the column name unless
    # an alias is provided.  Without the alias, the column cannot be found
    # and properly typecast.
    assert_equal [50 + 53 + 55 + 60], Account.pluck('SUM(DISTINCT(credit_limit)) as credit_limit')
528 529
  end

T
twinturbo 已提交
530
  def test_plucks_with_ids
J
Jon Leighton 已提交
531
    assert_equal Company.all.map(&:id).sort, Company.ids.sort
T
twinturbo 已提交
532
  end
533 534 535 536 537 538 539

  def test_pluck_not_auto_table_name_prefix_if_column_included
    Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
    ids = Company.includes(:contracts).pluck(:developer_id)
    assert_equal Company.count, ids.length
    assert_equal [7], ids.compact
  end
540 541 542 543 544

  def test_pluck_multiple_columns
    assert_equal [
      [1, "The First Topic"], [2, "The Second Topic of the day"],
      [3, "The Third Topic of the day"], [4, "The Fourth Topic of the day"]
545
    ], Topic.order(:id).pluck(:id, :title)
546 547 548
    assert_equal [
      [1, "The First Topic", "David"], [2, "The Second Topic of the day", "Mary"],
      [3, "The Third Topic of the day", "Carl"], [4, "The Fourth Topic of the day", "Carl"]
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    ], Topic.order(:id).pluck(:id, :title, :author_name)
  end

  def test_pluck_with_multiple_columns_and_selection_clause
    assert_equal [[1, 50], [2, 50], [3, 50], [4, 60], [5, 55], [6, 53]],
      Account.pluck('id, credit_limit')
  end

  def test_pluck_with_multiple_columns_and_includes
    Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
    companies_and_developers = Company.order('companies.id').includes(:contracts).pluck(:name, :developer_id)

    assert_equal Company.count, companies_and_developers.length
    assert_equal ["37signals", nil], companies_and_developers.first
    assert_equal ["test", 7], companies_and_developers.last
564
  end
565 566 567 568 569 570

  def test_pluck_with_reserved_words
    Possession.create!(:where => "Over There")

    assert_equal ["Over There"], Possession.pluck(:where)
  end
571 572 573 574 575 576

  def test_pluck_replaces_select_clause
    taks_relation = Topic.select(:approved, :id).order(:id)
    assert_equal [1,2,3,4], taks_relation.pluck(:id)
    assert_equal [false, true, true, true], taks_relation.pluck(:approved)
  end
577
end