Sha256: 3cc33a1f5ef11d44c995ffa9bde1f0284c0d75a62b5fcf31252a70ea616b09d0

Contents?: true

Size: 1.99 KB

Versions: 14

Compression:

Stored size: 1.99 KB

Contents

require 'test_helper'

class Superstore::AttributeMethods::DirtyTest < Superstore::TestCase
  test 'save clears dirty' do
    record = temp_object do
      string :name
    end.new name: 'foo'

    assert record.changed?

    record.save!

    assert_equal [nil, 'foo'], record.previous_changes['name']
    assert !record.changed?
  end

  test 'reload clears dirty' do
    record = temp_object do
      string :name
    end.create! name: 'foo'

    record.name = 'bar'
    assert record.changed?

    record.reload

    assert !record.changed?
  end

  test 'typecast float before dirty check' do
    record = temp_object do
      float :price
    end.create(price: 5.01)

    record.price = '5.01'
    assert !record.changed?

    record.price = '7.12'
    assert record.changed?
  end

  test 'typecast boolean before dirty check' do
    record = temp_object do
      boolean :awesome
    end.create(awesome: false)

    record.awesome = false
    assert !record.changed?

    record.awesome = true
    assert record.changed?
  end

  test 'typecast json with times' do
    record = temp_object do
      json :stuff
    end.create(stuff: {'time' => Time.new(2004, 12, 24, 6, 42, 21)})

    record.reload

    record.stuff = {'time' => Time.new(2004, 12, 24, 6, 42, 21)}
    assert !record.changed?

    record.stuff = {'time' => Time.new(2004, 12, 24, 6, 42, 22)}
    assert record.changed?
  end

  test 'unapplied_changes' do
    record = temp_object do
      float :price
      string :color
    end.create(price: 5.01, color: 'green')

    record.color = 'blue'

    assert_equal({'color' => 'blue'}, record.unapplied_changes)
  end

  test 'write_attribute' do
    object = temp_object do
      string :name
    end

    expected = {"name"=>[nil, "foo"]}

    object.new.tap do |record|
      record.name = 'foo'
      assert_equal expected, record.changes
    end

    object.new.tap do |record|
      record[:name] = 'foo'
      # record.write_attribute(:name, 'foo')
      assert_equal expected, record.changes
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
superstore-1.2.0 test/unit/attribute_methods/dirty_test.rb
superstore-1.1.4 test/unit/attribute_methods/dirty_test.rb
superstore-1.1.3 test/unit/attribute_methods/dirty_test.rb
superstore-1.1.2 test/unit/attribute_methods/dirty_test.rb
superstore-1.1.1 test/unit/attribute_methods/dirty_test.rb
superstore-1.1.0 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.12 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.11 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.10 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.9 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.8 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.7 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.6 test/unit/attribute_methods/dirty_test.rb
superstore-1.0.5 test/unit/attribute_methods/dirty_test.rb