diff options
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/dirty_test.rb | 43 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 7 |
2 files changed, 44 insertions, 6 deletions
diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb index 562fadbb85..db2cd885e2 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -43,11 +43,11 @@ class DirtyTest < ActiveModel::TestCase end def reload - reset_changes + clear_changes_information end - def rollback - undo_changes + def deprecated_reload + reset_changes end end @@ -111,7 +111,7 @@ class DirtyTest < ActiveModel::TestCase test "resetting attribute" do @model.name = "Bob" - @model.reset_name! + @model.restore_name! assert_nil @model.name assert !@model.name_changed? end @@ -181,17 +181,48 @@ class DirtyTest < ActiveModel::TestCase assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.changed_attributes end - test "undo_changes should restore all previous data" do + test "reset_changes is deprecated" do + @model.name = 'Dmitry' + @model.name_changed? + @model.save + @model.name = 'Bob' + + assert_equal [nil, 'Dmitry'], @model.previous_changes['name'] + assert_equal 'Dmitry', @model.changed_attributes['name'] + + assert_deprecated do + @model.deprecated_reload + end + + assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.previous_changes + assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.changed_attributes + end + + test "restore_attributes should restore all previous data" do @model.name = 'Dmitry' @model.color = 'Red' @model.save @model.name = 'Bob' @model.color = 'White' - @model.rollback + @model.restore_attributes assert_not @model.changed? assert_equal 'Dmitry', @model.name assert_equal 'Red', @model.color end + + test "restore_attributes can restore only some attributes" do + @model.name = 'Dmitry' + @model.color = 'Red' + @model.save + @model.name = 'Bob' + @model.color = 'White' + + @model.restore_attributes(['name']) + + assert @model.changed? + assert_equal 'Dmitry', @model.name + assert_equal 'White', @model.color + end end diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 4fee704ef5..38e54b7f17 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -167,6 +167,13 @@ class ValidationsTest < ActiveModel::TestCase end end + def test_invalid_options_to_validate + assert_raises(ArgumentError) do + # A common mistake -- we meant to call 'validates' + Topic.validate :title, presence: true + end + end + def test_errors_conversions Topic.validates_presence_of %w(title content) t = Topic.new |