diff options
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/associations_test.rb | 18 | ||||
-rwxr-xr-x | activerecord/test/base_test.rb | 42 |
2 files changed, 47 insertions, 13 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 59951d47fb..85df401fb1 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1175,6 +1175,24 @@ class BelongsToAssociationsTest < Test::Unit::TestCase topic.update_attributes(:title => "37signals") assert_equal 1, Topic.find(topic.id)[:replies_count] end + + def test_belongs_to_counter_after_save + topic = Topic.create("title" => "monday night") + topic.replies.create("title" => "re: monday night", "content" => "football") + assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count") + + topic.save + assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count") + end + + def test_belongs_to_counter_after_update_attributes + topic = Topic.create("title" => "37s") + topic.replies.create("title" => "re: 37s", "content" => "rails") + assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count") + + topic.update_attributes("title" => "37signals") + assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count") + end def test_assignment_before_parent_saved client = Client.find(:first) diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 1883f369c3..4418b8d9e6 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -47,6 +47,10 @@ class TightDescendant < TightPerson attr_accessible :phone_number end +class ReadonlyTitlePost < Post + attr_readonly :title +end + class Booleantest < ActiveRecord::Base; end class Task < ActiveRecord::Base @@ -840,6 +844,19 @@ class BasicsTest < Test::Unit::TestCase assert_nil TightDescendant.protected_attributes assert_equal [ :name, :address, :phone_number ], TightDescendant.accessible_attributes end + + def test_readonly_attributes + assert_equal [ :title ], ReadonlyTitlePost.readonly_attributes + + post = ReadonlyTitlePost.create(:title => "cannot change this", :body => "changeable") + post.reload + assert_equal "cannot change this", post.title + + post.update_attributes(:title => "try to change", :body => "changed") + post.reload + assert_equal "cannot change this", post.title + assert_equal "changed", post.body + end def test_multiparameter_attributes_on_date attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" } @@ -1222,12 +1239,12 @@ class BasicsTest < Test::Unit::TestCase end def test_increment_attribute - assert_equal 1, topics(:first).replies_count - topics(:first).increment! :replies_count - assert_equal 2, topics(:first, :reload).replies_count - - topics(:first).increment(:replies_count).increment!(:replies_count) - assert_equal 4, topics(:first, :reload).replies_count + assert_equal 50, accounts(:signals37).credit_limit + accounts(:signals37).increment! :credit_limit + assert_equal 51, accounts(:signals37, :reload).credit_limit + + accounts(:signals37).increment(:credit_limit).increment!(:credit_limit) + assert_equal 53, accounts(:signals37, :reload).credit_limit end def test_increment_nil_attribute @@ -1237,14 +1254,13 @@ class BasicsTest < Test::Unit::TestCase end def test_decrement_attribute - topics(:first).increment(:replies_count).increment!(:replies_count) - assert_equal 3, topics(:first).replies_count - - topics(:first).decrement!(:replies_count) - assert_equal 2, topics(:first, :reload).replies_count + assert_equal 50, accounts(:signals37).credit_limit - topics(:first).decrement(:replies_count).decrement!(:replies_count) - assert_equal 0, topics(:first, :reload).replies_count + accounts(:signals37).decrement!(:credit_limit) + assert_equal 49, accounts(:signals37, :reload).credit_limit + + accounts(:signals37).decrement(:credit_limit).decrement!(:credit_limit) + assert_equal 47, accounts(:signals37, :reload).credit_limit end def test_toggle_attribute |