aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test/cases/associations')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb67
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb20
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb32
3 files changed, 36 insertions, 83 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 04714f42e9..4b56037a08 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -46,10 +46,13 @@ class HasManyAssociationsTestForCountWithCountSql < ActiveRecord::TestCase
end
end
-class HasManyAssociationsTestForCountDistinctWithFinderSql < ActiveRecord::TestCase
+class HasManyAssociationsTestForCountWithVariousFinderSqls < ActiveRecord::TestCase
class Invoice < ActiveRecord::Base
ActiveSupport::Deprecation.silence do
has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT DISTINCT line_items.amount from line_items"
+ has_many :custom_full_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.invoice_id, line_items.amount from line_items"
+ has_many :custom_star_line_items, :class_name => 'LineItem', :finder_sql => "SELECT * from line_items"
+ has_many :custom_qualified_star_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.* from line_items"
end
end
@@ -61,6 +64,33 @@ class HasManyAssociationsTestForCountDistinctWithFinderSql < ActiveRecord::TestC
assert_equal 1, invoice.custom_line_items.count
end
+
+ def test_should_count_results_with_multiple_fields
+ invoice = Invoice.new
+ invoice.custom_full_line_items << LineItem.new(:amount => 0)
+ invoice.custom_full_line_items << LineItem.new(:amount => 0)
+ invoice.save!
+
+ assert_equal 2, invoice.custom_full_line_items.count
+ end
+
+ def test_should_count_results_with_star
+ invoice = Invoice.new
+ invoice.custom_star_line_items << LineItem.new(:amount => 0)
+ invoice.custom_star_line_items << LineItem.new(:amount => 0)
+ invoice.save!
+
+ assert_equal 2, invoice.custom_star_line_items.count
+ end
+
+ def test_should_count_results_with_qualified_star
+ invoice = Invoice.new
+ invoice.custom_qualified_star_line_items << LineItem.new(:amount => 0)
+ invoice.custom_qualified_star_line_items << LineItem.new(:amount => 0)
+ invoice.save!
+
+ assert_equal 2, invoice.custom_qualified_star_line_items.count
+ end
end
class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
@@ -158,28 +188,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal invoice.id, line_item.invoice_id
end
- def test_association_conditions_bypass_attribute_protection
- car = Car.create(:name => 'honda')
-
- bulb = car.frickinawesome_bulbs.new
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.new(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.build
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.build(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.create
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.create(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
- end
-
# When creating objects on the association, we must not do it within a scope (even though it
# would be convenient), because this would cause that scope to be applied to any callbacks etc.
def test_build_and_create_should_not_happen_within_scope
@@ -1550,19 +1558,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal "RED!", car.bulbs.to_a.first.color
end
- def test_new_is_called_with_attributes_and_options
- car = Car.create(:name => 'honda')
-
- bulb = car.bulbs.build
- assert_equal Bulb, bulb.class
-
- bulb = car.bulbs.build(:bulb_type => :custom)
- assert_equal Bulb, bulb.class
-
- bulb = car.bulbs.build({ :bulb_type => :custom }, :as => :admin)
- assert_equal CustomBulb, bulb.class
- end
-
def test_abstract_class_with_polymorphic_has_many
post = SubStiPost.create! :title => "fooo", :body => "baa"
tagging = Tagging.create! :taggable => post
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 36e5ba9660..d4ceae6f80 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -58,21 +58,6 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert post.reload.people(true).include?(person)
end
- def test_associate_existing_with_strict_mass_assignment_sanitizer
- SecureReader.mass_assignment_sanitizer = :strict
-
- SecureReader.new
-
- post = posts(:thinking)
- person = people(:david)
-
- assert_queries(1) do
- post.secure_people << person
- end
- ensure
- SecureReader.mass_assignment_sanitizer = :logger
- end
-
def test_associate_existing_record_twice_should_add_to_target_twice
post = posts(:thinking)
person = people(:david)
@@ -838,6 +823,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_assign_array_to_new_record_builds_join_records
+ c = Category.new(:name => 'Fishing', :authors => [Author.first])
+ assert_equal 1, c.categorizations.size
+ end
+
def test_create_bang_should_raise_exception_when_join_record_has_errors
repair_validations(Categorization) do
Categorization.validate { |r| r.errors[:base] << 'Invalid Categorization' }
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 8bc633f2b5..2d3cb654df 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -446,38 +446,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal pirate.id, ship.pirate_id
end
- def test_association_conditions_bypass_attribute_protection
- car = Car.create(:name => 'honda')
-
- bulb = car.build_frickinawesome_bulb
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.build_frickinawesome_bulb(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.create_frickinawesome_bulb
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.create_frickinawesome_bulb(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
- end
-
- def test_new_is_called_with_attributes_and_options
- car = Car.create(:name => 'honda')
-
- bulb = car.build_bulb
- assert_equal Bulb, bulb.class
-
- bulb = car.build_bulb
- assert_equal Bulb, bulb.class
-
- bulb = car.build_bulb(:bulb_type => :custom)
- assert_equal Bulb, bulb.class
-
- bulb = car.build_bulb({ :bulb_type => :custom }, :as => :admin)
- assert_equal CustomBulb, bulb.class
- end
-
def test_build_with_block
car = Car.create(:name => 'honda')