aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-12-11 13:34:46 -0700
committerSean Griffin <sean@thoughtbot.com>2014-12-11 13:34:46 -0700
commit39542fba54328ca048fb75a5d5b37f8e1d4c1f37 (patch)
tree1d41414fe6b6208e075ce6016d8ef03f3c7cd00f /activerecord
parent387065a023b3883576a581af79b0a202bdcc07a8 (diff)
downloadrails-39542fba54328ca048fb75a5d5b37f8e1d4c1f37.tar.gz
rails-39542fba54328ca048fb75a5d5b37f8e1d4c1f37.tar.bz2
rails-39542fba54328ca048fb75a5d5b37f8e1d4c1f37.zip
Improve the test case introduced by bd0d47e
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/test/cases/calculations_test.rb29
1 files changed, 10 insertions, 19 deletions
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 1264251418..6cf18bc3c6 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -612,25 +612,16 @@ class CalculationsTest < ActiveRecord::TestCase
end
def test_calculation_with_polymorphic_relation
- sp = ShipPart.create! name: "no trinket"
- sp_with_trinket = ShipPart.create! name: "has_trinket"
- sp_with_trinket.trinkets.create!
- ship_parts = [sp.id,sp_with_trinket.id]
-
- sp_with_trinket_relation = ShipPart.where(id:ship_parts).joins(:trinkets)
- sp_with_trinket_relation2 = sp_with_trinket_relation.where(name:"has_trinket")
- sp_with_trinket_relation3 = sp_with_trinket_relation.where(treasures: {looter_type:"ShipPart"})
- sp_with_trinket_relation4 = ShipPart.where(id:ship_parts).eager_load(:trinkets).where.not(treasures:{id:nil})
-
- [sp_with_trinket_relation,sp_with_trinket_relation2,
- sp_with_trinket_relation3,sp_with_trinket_relation4].each do |with_trinket_rel|
- assert_equal 1, with_trinket_rel.count
- assert_equal [sp_with_trinket.id], with_trinket_rel.pluck(:id)
- assert_equal sp_with_trinket.id, with_trinket_rel.sum(:id)
- assert_equal ({"has_trinket" => sp_with_trinket.id}), with_trinket_rel.group('ship_parts.name').sum(:id)
- assert_equal ({"has_trinket" => sp_with_trinket.id}), with_trinket_rel.where(id:sp_with_trinket.id).
- group('ship_parts.name').sum(:id)
- end
+ part = ShipPart.create!(name: "has trinket")
+ part.trinkets.create!
+
+ assert_equal part.id, ShipPart.joins(:trinkets).sum(:id)
end
+ def test_grouped_calculation_with_polymorphic_relation
+ part = ShipPart.create!(name: "has trinket")
+ part.trinkets.create!
+
+ assert_equal({ "has trinket" => part.id }, ShipPart.joins(:trinkets).group("ship_parts.name").sum(:id))
+ end
end