aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-05-01 17:59:24 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-05-01 17:59:24 -0700
commitdd1f36078eff18721eed76d236796c8d26d81e58 (patch)
treed3d358d2625ac1b00af3b55c91bb66e436728a32 /activerecord/test/cases
parent14a75a54a802b35db5a7802eca90dae9aa8e3518 (diff)
parent54122067acaad39b277a5363c6d11d6804c7bf6b (diff)
downloadrails-dd1f36078eff18721eed76d236796c8d26d81e58.tar.gz
rails-dd1f36078eff18721eed76d236796c8d26d81e58.tar.bz2
rails-dd1f36078eff18721eed76d236796c8d26d81e58.zip
Merge pull request #7839 from chancancode/handle_aliased_attributes_in_ar_relation
Handle aliased attributes in AR::Relation
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/calculations_test.rb12
-rw-r--r--activerecord/test/cases/finder_test.rb3
-rw-r--r--activerecord/test/cases/relation/where_test.rb8
3 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index b0b647cbf7..f49bef2346 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -28,6 +28,10 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 53.0, value
end
+ def test_should_resolve_aliased_attributes
+ assert_equal 318, Account.sum(:available_credit)
+ end
+
def test_should_return_decimal_average_of_integer_field
value = Account.average(:id)
assert_equal 3.5, value
@@ -352,6 +356,10 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 4, Account.select(:credit_limit).uniq.count
end
+ def test_count_with_aliased_attribute
+ assert_equal 6, Account.count(:available_credit)
+ end
+
def test_count_with_column_and_options_parameter
assert_equal 2, Account.where("credit_limit = 50 AND firm_id IS NOT NULL").count(:firm_id)
end
@@ -488,6 +496,10 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal [contract.id], company.contracts.pluck(:id)
end
+ def test_pluck_on_aliased_attribute
+ assert_equal 'The First Topic', Topic.order(:id).pluck(:heading).first
+ end
+
def test_pluck_with_serialization
t = Topic.create!(:content => { :foo => :bar })
assert_equal [{:foo => :bar}], Topic.where(:id => t.id).pluck(:content)
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 557cc7e7a0..7db9aef218 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -47,7 +47,8 @@ class FinderTest < ActiveRecord::TestCase
def test_exists
assert Topic.exists?(1)
assert Topic.exists?("1")
- assert Topic.exists?(:author_name => "David")
+ assert Topic.exists?(title: "The First Topic")
+ assert Topic.exists?(heading: "The First Topic")
assert Topic.exists?(:author_name => "Mary", :approved => true)
assert Topic.exists?(["parent_id = ?", 1])
assert !Topic.exists?(45)
diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb
index c43c7601a2..d333be3560 100644
--- a/activerecord/test/cases/relation/where_test.rb
+++ b/activerecord/test/cases/relation/where_test.rb
@@ -5,6 +5,7 @@ require 'models/treasure'
require 'models/post'
require 'models/comment'
require 'models/edge'
+require 'models/topic'
module ActiveRecord
class WhereTest < ActiveRecord::TestCase
@@ -80,6 +81,13 @@ module ActiveRecord
assert_equal expected.to_sql, actual.to_sql
end
+ def test_aliased_attribute
+ expected = Topic.where(heading: 'The First Topic')
+ actual = Topic.where(title: 'The First Topic')
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
def test_where_error
assert_raises(ActiveRecord::StatementInvalid) do
Post.where(:id => { 'posts.author_id' => 10 }).first