Sha256: d1de031060f105b9b450ee195dc4ac4086960cd03fa7a3150e385070afe1e424

Contents?: true

Size: 2 KB

Versions: 9

Compression:

Stored size: 2 KB

Contents

require File.expand_path('../abstract_unit', __FILE__)

class TestUpdate < ActiveSupport::TestCase
  fixtures :reference_types, :reference_codes

  CLASSES = {
    :single => {
      :class => ReferenceType,
      :primary_keys => :reference_type_id,
      :update => { :description => 'RT Desc' },
    },
    :dual   => {
      :class => ReferenceCode,
      :primary_keys => [:reference_type_id, :reference_code],
      :update => { :description => 'RT Desc' },
    },
  }

  def setup
    self.class.classes = CLASSES
  end

  def test_setup
    testing_with do
      assert_not_nil @klass_info[:update]
    end
  end

  def test_update_attributes
    testing_with do
      assert(@first.update_attributes(@klass_info[:update]))
      assert(@first.reload)
      @klass_info[:update].each_pair do |attr_name, new_value|
        assert_equal(new_value, @first[attr_name])
      end
    end
  end

  def test_update_primary_key
    obj = ReferenceCode.find([1,1])
    obj.reference_type_id = 2
    obj.reference_code = 3
    assert(obj.primary_key_changed?)
    assert_equal({"reference_type_id" => 1, "reference_code" => 1}, obj.primary_key_was)
    assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
    assert(obj.save)
    assert(obj.reload)
    assert_equal(2, obj.reference_type_id)
    assert_equal(3, obj.reference_code)
    assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
    assert_equal([2, 3], obj.id)
  end

  def test_update_attribute
    obj = ReferenceType.find(1)
    obj[:abbreviation] = 'a'
    obj['abbreviation'] = 'b'
    assert(obj.save)
    assert(obj.reload)
    assert_equal('b', obj.abbreviation)
  end

  def test_update_all
    assert_nothing_raised do
      reference_code = ReferenceCode.create
      primary_key = reference_code.class.primary_key
      ReferenceCode.where(primary_key => reference_code[primary_key]).
        update_all(description: 'random value')
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
composite_primary_keys-9.0.4 test/test_update.rb
composite_primary_keys-9.0.2 test/test_update.rb
composite_primary_keys-9.0.1 test/test_update.rb
composite_primary_keys-8.1.4 test/test_update.rb
composite_primary_keys-9.0.0 test/test_update.rb
composite_primary_keys-9.0.0.beta1 test/test_update.rb
composite_primary_keys-8.1.3 test/test_update.rb
composite_primary_keys-8.1.2 test/test_update.rb
composite_primary_keys-7.0.16 test/test_update.rb