aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/test/cases/calculations_test.rb84
-rw-r--r--activerecord/test/cases/dirty_test.rb6
2 files changed, 90 insertions, 0 deletions
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index dbd1d03c4c..abce4565a4 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -952,6 +952,90 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal({ "proposed" => 2, "published" => 2 }, Book.group(:status).count)
end
+ def test_select_avg_with_group_by_as_virtual_attribute_with_sql
+ rails_core = companies(:rails_core)
+
+ sql = <<~SQL
+ SELECT firm_id, AVG(credit_limit) AS avg_credit_limit
+ FROM accounts
+ WHERE firm_id = ?
+ GROUP BY firm_id
+ LIMIT 1
+ SQL
+
+ account = Account.find_by_sql([sql, rails_core]).first
+
+ # id was not selected, so it should be nil
+ # (cannot select id because it wasn't used in the GROUP BY clause)
+ assert_nil account.id
+
+ # firm_id was explicitly selected, so it should be present
+ assert_equal(rails_core, account.firm)
+
+ # avg_credit_limit should be present as a virtual attribute
+ assert_equal(52.5, account.avg_credit_limit)
+ end
+
+ def test_select_avg_with_group_by_as_virtual_attribute_with_ar
+ rails_core = companies(:rails_core)
+
+ account = Account
+ .select(:firm_id, "AVG(credit_limit) AS avg_credit_limit")
+ .where(firm: rails_core)
+ .group(:firm_id)
+ .take!
+
+ # id was not selected, so it should be nil
+ # (cannot select id because it wasn't used in the GROUP BY clause)
+ assert_nil account.id
+
+ # firm_id was explicitly selected, so it should be present
+ assert_equal(rails_core, account.firm)
+
+ # avg_credit_limit should be present as a virtual attribute
+ assert_equal(52.5, account.avg_credit_limit)
+ end
+
+ def test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_sql
+ rails_core = companies(:rails_core)
+
+ sql = <<~SQL
+ SELECT companies.*, AVG(accounts.credit_limit) AS avg_credit_limit
+ FROM companies
+ INNER JOIN accounts ON companies.id = accounts.firm_id
+ WHERE companies.id = ?
+ GROUP BY companies.id
+ LIMIT 1
+ SQL
+
+ firm = DependentFirm.find_by_sql([sql, rails_core]).first
+
+ # all the DependentFirm attributes should be present
+ assert_equal rails_core, firm
+ assert_equal rails_core.name, firm.name
+
+ # avg_credit_limit should be present as a virtual attribute
+ assert_equal(52.5, firm.avg_credit_limit)
+ end
+
+ def test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_ar
+ rails_core = companies(:rails_core)
+
+ firm = DependentFirm
+ .select("companies.*", "AVG(accounts.credit_limit) AS avg_credit_limit")
+ .where(id: rails_core)
+ .joins(:account)
+ .group(:id)
+ .take!
+
+ # all the DependentFirm attributes should be present
+ assert_equal rails_core, firm
+ assert_equal rails_core.name, firm.name
+
+ # avg_credit_limit should be present as a virtual attribute
+ assert_equal(52.5, firm.avg_credit_limit)
+ end
+
def test_count_with_block_and_column_name_raises_an_error
assert_raises(ArgumentError) do
Account.count(:firm_id) { true }
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index a2a501a794..6aca7d93ad 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -491,6 +491,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal 4, pirate.previous_changes.size
assert_equal [nil, "arrr"], pirate.previous_changes["catchphrase"]
+ assert_equal nil, pirate.catchphrase_previously_was
assert_equal [nil, pirate.id], pirate.previous_changes["id"]
assert_nil pirate.previous_changes["updated_on"][0]
assert_not_nil pirate.previous_changes["updated_on"][1]
@@ -507,6 +508,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal 4, pirate.previous_changes.size
assert_equal [nil, "arrr"], pirate.previous_changes["catchphrase"]
+ assert_equal nil, pirate.catchphrase_previously_was
assert_equal [nil, pirate.id], pirate.previous_changes["id"]
assert_includes pirate.previous_changes, "updated_on"
assert_includes pirate.previous_changes, "created_on"
@@ -525,6 +527,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal 2, pirate.previous_changes.size
assert_equal ["arrr", "Me Maties!"], pirate.previous_changes["catchphrase"]
+ assert_equal "arrr", pirate.catchphrase_previously_was
assert_not_nil pirate.previous_changes["updated_on"][0]
assert_not_nil pirate.previous_changes["updated_on"][1]
assert_not pirate.previous_changes.key?("parrot_id")
@@ -539,6 +542,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal 2, pirate.previous_changes.size
assert_equal ["Me Maties!", "Thar She Blows!"], pirate.previous_changes["catchphrase"]
+ assert_equal "Me Maties!", pirate.catchphrase_previously_was
assert_not_nil pirate.previous_changes["updated_on"][0]
assert_not_nil pirate.previous_changes["updated_on"][1]
assert_not pirate.previous_changes.key?("parrot_id")
@@ -551,6 +555,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal 2, pirate.previous_changes.size
assert_equal ["Thar She Blows!", "Ahoy!"], pirate.previous_changes["catchphrase"]
+ assert_equal "Thar She Blows!", pirate.catchphrase_previously_was
assert_not_nil pirate.previous_changes["updated_on"][0]
assert_not_nil pirate.previous_changes["updated_on"][1]
assert_not pirate.previous_changes.key?("parrot_id")
@@ -563,6 +568,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal 2, pirate.previous_changes.size
assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes["catchphrase"]
+ assert_equal "Ahoy!", pirate.catchphrase_previously_was
assert_not_nil pirate.previous_changes["updated_on"][0]
assert_not_nil pirate.previous_changes["updated_on"][1]
assert_not pirate.previous_changes.key?("parrot_id")