Repository: rails/rails
File: activerecord/test/cases/persistence_test.rb (Ruby)

Code before:

    t_reloaded = Topic.find(t.id)

    assert_equal "New Topic", t_reloaded.title
  end

  def test_update_column
    topic = Topic.find(1)
    topic.update_column("approved", true)
    assert_predicate topic, :approved?
    topic.reload
    assert_predicate topic, :approved?

    topic.update_column(:approved, false)
    assert_not_predicate topic, :approved?
    topic.reload
    assert_not_predicate topic, :approved?
  end

  def test_update_column_touch_option
    topic = Topic.find(1)
    updated_at = topic.updated_at
    travel(1.second) do
      topic.update_column(:title, "super_title", touch: true)
    end
    assert_not_equal updated_at, topic.updated_at
  end

  def test_update_column_should_not_use_setter_method
    dev = Developer.find(1)
    dev.instance_eval { def salary=(value); write_attribute(:salary, value * 2); end }

    dev.update_column(:salary, 80000)
    assert_equal 80000, dev.salary

    dev.reload
    assert_equal 80000, dev.salary
  end

  def test_update_column_should_raise_exception_if_new_record
    topic = Topic.new
    assert_raises(ActiveRecord::ActiveRecordError) { topic.update_column("approved", false) }
  end

  def test_update_column_should_not_leave_the_object_dirty
    topic = Topic.find(1)
    topic.update_column("content", "--- Have a nice day\n...\n")

    topic.reload
    topic.update_column(:content, "--- You too\n...\n")
    assert_equal [], topic.changed

Review comment (refactor):
I think, in this and the following tests we can utilise the `assert_changes` test helper.

Code after:

    developer.reload
    assert_not_equal prev_month, developer.updated_at
  end

  def test_update_attribute!
    assert_not_predicate Topic.find(1), :approved?
    Topic.find(1).update_attribute!("approved", true)
    assert_predicate Topic.find(1), :approved?

    Topic.find(1).update_attribute!(:approved, false)
    assert_not_predicate Topic.find(1), :approved?

    Topic.find(1).update_attribute!(:change_approved_before_save, true)
    assert_predicate Topic.find(1), :approved?
  end

  def test_update_attribute_for_readonly_attribute!
    minivan = Minivan.find("m1")
    assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute!(:color, "black") }
  end

  def test_update_attribute_with_one_updated!
    t = Topic.first
    t.update_attribute!(:title, "super_title")
    assert_equal "super_title", t.title
    assert_not t.changed?, "topic should not have changed"
    assert_not t.title_changed?, "title should not have changed"
    assert_nil t.title_change, "title change should be nil"

    t.reload
    assert_equal "super_title", t.title
  end

  def test_update_attribute_for_updated_at_on!
    developer = Developer.find(1)
    prev_month = Time.now.prev_month.change(usec: 0)

    developer.update_attribute!(:updated_at, prev_month)
    assert_equal prev_month, developer.updated_at

    developer.update_attribute!(:salary, 80001)
    assert_not_equal prev_month, developer.updated_at

    developer.reload
    assert_not_equal prev_month, developer.updated_at
  end

  def test_update_attribute_for_aborted_callback!
    klass = Class.new(Topic) do
      def self.name; "Topic"; end