From 8233f8314ba00838e8ef6b2d95cdfb7d58c8dece Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Wed, 24 Sep 2008 16:44:56 +1200 Subject: wrote a test showing eager loading's misbehavior (sanitizing against the wrong table) when the association has a :conditions hash Signed-off-by: Michael Koziarski --- activerecord/test/cases/associations/eager_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index e78624a98d..3dd2cb028a 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -116,6 +116,13 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal 2, posts.first.comments.size end + def test_loading_from_an_association_that_has_a_hash_of_conditions + assert_nothing_raised do + Author.find(:all, :include => :hello_posts_with_hash_conditions) + end + assert !Author.find(authors(:david).id, :include => :hello_posts_with_hash_conditions).hello_posts.empty? + end + def test_loading_with_no_associations assert_nil Post.find(posts(:authorless).id, :include => :author).author end -- cgit v1.2.3 From 8d337e9ec2e25007d557150dbe7557ab3c3bd05f Mon Sep 17 00:00:00 2001 From: Eloy Duran Date: Fri, 3 Oct 2008 13:55:35 +0200 Subject: Dynamic finders should use the ActiveRecord::Base::find method instead of ::find_initial, :find_last, and ::find_all. This is so when people override ActiveRecord::Base::find, the new ::find method will also be invoked by the dynamic finders. Associations for instance do go through ::find, so this makes it more consistent. Also removed the unnecessary deprecation silence blocks. Signed-off-by: Michael Koziarski [#1162 state:committed] --- activerecord/test/cases/finder_test.rb | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 292b88edbc..853474916c 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -21,7 +21,7 @@ class DynamicFinderMatchTest < ActiveRecord::TestCase match = ActiveRecord::DynamicFinderMatch.match("find_by_age_and_sex_and_location") assert_not_nil match assert match.finder? - assert_equal :find_initial, match.finder + assert_equal :first, match.finder assert_equal %w(age sex location), match.attribute_names end @@ -30,7 +30,7 @@ class DynamicFinderMatchTest < ActiveRecord::TestCase assert_not_nil match assert match.finder? assert match.bang? - assert_equal :find_initial, match.finder + assert_equal :first, match.finder assert_equal %w(age sex location), match.attribute_names end @@ -38,7 +38,7 @@ class DynamicFinderMatchTest < ActiveRecord::TestCase match = ActiveRecord::DynamicFinderMatch.match("find_all_by_age_and_sex_and_location") assert_not_nil match assert match.finder? - assert_equal :find_every, match.finder + assert_equal :all, match.finder assert_equal %w(age sex location), match.attribute_names end @@ -47,7 +47,7 @@ class DynamicFinderMatchTest < ActiveRecord::TestCase assert_not_nil match assert !match.finder? assert match.instantiator? - assert_equal :find_initial, match.finder + assert_equal :first, match.finder assert_equal :new, match.instantiator assert_equal %w(age sex location), match.attribute_names end @@ -57,7 +57,7 @@ class DynamicFinderMatchTest < ActiveRecord::TestCase assert_not_nil match assert !match.finder? assert match.instantiator? - assert_equal :find_initial, match.finder + assert_equal :first, match.finder assert_equal :create, match.instantiator assert_equal %w(age sex location), match.attribute_names end @@ -500,6 +500,23 @@ class FinderTest < ActiveRecord::TestCase assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1])) end + uses_mocha('test_dynamic_finder_should_go_through_the_find_class_method') do + def test_dynamic_finders_should_go_through_the_find_class_method + Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' }) + Topic.find_by_title("The First Topic!") + + Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' }) + Topic.find_last_by_title("The Last Topic!") + + Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' }) + Topic.find_all_by_title("A Topic.") + + Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2) + Topic.find_or_initialize_by_title('Does not exist yet for sure!') + Topic.find_or_create_by_title('Does not exist yet for sure!') + end + end + def test_find_by_one_attribute assert_equal topics(:first), Topic.find_by_title("The First Topic") assert_nil Topic.find_by_title("The First Topic!") -- cgit v1.2.3 From 1bc267d21679408b3624d1c697656ec250c01972 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Fri, 3 Oct 2008 16:08:17 +0200 Subject: Make sure recreate MySQL test database with the proper encoding and collation [#1165 state:resolved] Signed-off-by: Michael Koziarski [#1165 state:committed] --- activerecord/test/cases/active_schema_test_mysql.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/active_schema_test_mysql.rb b/activerecord/test/cases/active_schema_test_mysql.rb index 2a42dc3517..9aff538ce9 100644 --- a/activerecord/test/cases/active_schema_test_mysql.rb +++ b/activerecord/test/cases/active_schema_test_mysql.rb @@ -25,6 +25,11 @@ class ActiveSchemaTest < ActiveRecord::TestCase assert_equal "CREATE DATABASE `aimonetti` DEFAULT CHARACTER SET `latin1`", create_database(:aimonetti, {:charset => 'latin1'}) assert_equal "CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `big5` COLLATE `big5_chinese_ci`", create_database(:matt_aimonetti, {:charset => :big5, :collation => :big5_chinese_ci}) end + + def test_recreate_mysql_database_with_encoding + create_database(:luca, {:charset => 'latin1'}) + assert_equal "CREATE DATABASE `luca` DEFAULT CHARACTER SET `latin1`", recreate_database(:luca, {:charset => 'latin1'}) + end end def test_add_column -- cgit v1.2.3 From 7659fb6a2b638703a99a63033d947d19089a6b85 Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Sat, 4 Oct 2008 15:31:04 +0100 Subject: Try reloading model on class mismatch [#229 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/reload_models_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 activerecord/test/cases/reload_models_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/reload_models_test.rb b/activerecord/test/cases/reload_models_test.rb new file mode 100644 index 0000000000..411b5f6afa --- /dev/null +++ b/activerecord/test/cases/reload_models_test.rb @@ -0,0 +1,20 @@ +require "cases/helper" +require 'models/owner' +require 'models/pet' + +class ReloadModelsTest < ActiveRecord::TestCase + def test_has_one_with_reload + pet = Pet.find_by_name('parrot') + pet.owner = Owner.find_by_name('ashley') + + # Reload the class Owner, simulating auto-reloading of model classes in a + # development environment. Note that meanwhile the class Pet is not + # reloaded, simulating a class that is present in a plugin. + Object.class_eval { remove_const :Owner } + Kernel.load(File.expand_path(File.join(File.dirname(__FILE__), "../models/owner.rb"))) + + pet = Pet.find_by_name('parrot') + pet.owner = Owner.find_by_name('ashley') + assert_equal pet.owner, Owner.find_by_name('ashley') + end +end \ No newline at end of file -- cgit v1.2.3 From 95e1cf4812d4b964d7ab0fdf4bfa31177d27909c Mon Sep 17 00:00:00 2001 From: Zach Dennis Date: Sat, 4 Oct 2008 15:42:36 +0100 Subject: Fix has_many :through when the source is a belongs_to association. [#323 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/associations/eager_test.rb | 9 +++++++++ .../cases/associations/has_many_associations_test.rb | 1 + .../associations/has_many_through_associations_test.rb | 17 ++++++++++++++++- activerecord/test/models/post.rb | 2 ++ 4 files changed, 28 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3dd2cb028a..7f42577ab0 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -275,6 +275,15 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author } end + def test_eager_with_has_many_through_a_belongs_to_association + author = authors(:mary) + post = Post.create!(:author => author, :title => "TITLE", :body => "BODY") + author.author_favorites.create(:favorite_author_id => 1) + author.author_favorites.create(:favorite_author_id => 2) + posts_with_author_favorites = author.posts.find(:all, :include => :author_favorites) + assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id } + end + def test_eager_with_has_many_through_an_sti_join_model author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id') assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments } diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 315d77de07..1bc9c39c19 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1081,3 +1081,4 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + 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 12cce98c26..a07f4bcbdd 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -5,7 +5,7 @@ require 'models/reader' require 'models/comment' class HasManyThroughAssociationsTest < ActiveRecord::TestCase - fixtures :posts, :readers, :people, :comments + fixtures :posts, :readers, :people, :comments, :authors def test_associate_existing assert_queries(2) { posts(:thinking);people(:david) } @@ -229,4 +229,19 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end end end + + def test_has_many_association_through_a_belongs_to_association_where_the_association_doesnt_exist + author = authors(:mary) + post = Post.create!(:title => "TITLE", :body => "BODY") + assert_equal [], post.author_favorites + end + + def test_has_many_association_through_a_belongs_to_association + author = authors(:mary) + post = Post.create!(:author => author, :title => "TITLE", :body => "BODY") + author.author_favorites.create(:favorite_author_id => 1) + author.author_favorites.create(:favorite_author_id => 2) + author.author_favorites.create(:favorite_author_id => 3) + assert_equal post.author.author_favorites, post.author_favorites + end end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 3adbc0ce1f..6da37c31ff 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -22,6 +22,8 @@ class Post < ActiveRecord::Base end end + has_many :author_favorites, :through => :author + has_many :comments_with_interpolated_conditions, :class_name => 'Comment', :conditions => ['#{"#{aliased_table_name}." rescue ""}body = ?', 'Thank you for the welcome'] -- cgit v1.2.3 From 25ca21ae21d49f06708357a5ce0670103ced2d58 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 4 Oct 2008 16:53:13 +0100 Subject: Introduce ActiveRecord::Reflection::ThroughReflection to simplify hm:t reflection logic --- activerecord/test/cases/reflection_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index e339ef41ab..e0ed3e5886 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -170,6 +170,10 @@ class ReflectionTest < ActiveRecord::TestCase assert_nothing_raised { Firm.reflections[:clients] == Object.new } end + def test_has_many_through_reflection + assert_kind_of ActiveRecord::Reflection::ThroughReflection, Subscriber.reflect_on_association(:books) + end + private def assert_reflection(klass, association, options) assert reflection = klass.reflect_on_association(association) -- cgit v1.2.3 From 9599948fbcd67c1c2c5fecc2dca148e998479e33 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 4 Oct 2008 20:03:42 +0100 Subject: Ensure Model.sum and Model.avg typecast appropriately. [#1066 state:resolved] Model.sum delegates typecasting to the column being summed. If that's not feasible, returns a string. Model.avg always returns big decimal. --- activerecord/test/cases/calculations_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 754fd58f35..0fa61500c0 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -18,8 +18,8 @@ class CalculationsTest < ActiveRecord::TestCase def test_should_average_field value = Account.average(:credit_limit) - assert_kind_of Float, value - assert_in_delta 53.0, value, 0.001 + assert_kind_of BigDecimal, value + assert_equal BigDecimal.new('53.0'), value end def test_should_return_nil_as_average @@ -273,7 +273,7 @@ class CalculationsTest < ActiveRecord::TestCase end def test_should_sum_expression - assert_equal 636, Account.sum("2 * credit_limit") + assert_equal '636', Account.sum("2 * credit_limit") end def test_count_with_from_option -- cgit v1.2.3