Sha256: 2c90ece3b708fe8cf0b55ef9a4fb520fa57cf0c64c3c74d8fe90ec5532942e2e

Contents?: true

Size: 1.59 KB

Versions: 101

Compression:

Stored size: 1.59 KB

Contents

require "cases/helper"
require 'models/contact'

class SerializationTest < ActiveRecord::TestCase
  FORMATS = [ :xml, :json ]

  def setup
    @contact_attributes = {
      :name        => 'aaron stack',
      :age         => 25,
      :avatar      => 'binarydata',
      :created_at  => Time.utc(2006, 8, 1),
      :awesome     => false,
      :preferences => { :gem => '<strong>ruby</strong>' }
    }

    @contact = Contact.new(@contact_attributes)
  end

  def test_serialize_should_be_reversible
    for format in FORMATS
      @serialized = Contact.new.send("to_#{format}")
      contact = Contact.new.send("from_#{format}", @serialized)

      assert_equal @contact_attributes.keys.collect(&:to_s).sort, contact.attributes.keys.collect(&:to_s).sort, "For #{format}"
    end
  end

  def test_serialize_should_allow_attribute_only_filtering
    for format in FORMATS
      @serialized = Contact.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ])
      contact = Contact.new.send("from_#{format}", @serialized)
      assert_equal @contact_attributes[:name], contact.name, "For #{format}"
      assert_nil contact.avatar, "For #{format}"
    end
  end

  def test_serialize_should_allow_attribute_except_filtering
    for format in FORMATS
      @serialized = Contact.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ])
      contact = Contact.new.send("from_#{format}", @serialized)
      assert_nil contact.name, "For #{format}"
      assert_nil contact.age, "For #{format}"
      assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
    end
  end
end

Version data entries

101 entries across 98 versions & 20 rubygems

Version Path
activerecord-2.3.18 test/cases/serialization_test.rb
activerecord_csi-2.3.5.p8 test/cases/serialization_test.rb
activerecord-2.3.17 test/cases/serialization_test.rb
activerecord_csi-2.3.5.p7 test/cases/serialization_test.rb
activerecord_csi-2.3.5.p6 test/cases/serialization_test.rb
activerecord-2.3.16 test/cases/serialization_test.rb
activerecord-2.3.15 test/cases/serialization_test.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/activerecord-2.3.14/test/cases/serialization_test.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/activerecord-2.3.12/test/cases/serialization_test.rb
activerecord-2.3.14 test/cases/serialization_test.rb
kajam-1.0.3.rc2 vendor/rails/activerecord/test/cases/serialization_test.rb
activerecord-2.3.12 test/cases/serialization_test.rb
radiant-1.0.0.rc2 vendor/rails/activerecord/test/cases/serialization_test.rb
radiant-1.0.0.rc1 vendor/rails/activerecord/test/cases/serialization_test.rb
webroar-0.5.0 src/admin_panel/vendor/rails/activerecord/test/cases/serialization_test.rb
activerecord-2.3.11 test/cases/serialization_test.rb
radiantcms-couchrest_model-0.2.4 vendor/rails/activerecord/test/cases/serialization_test.rb
radiantcms-couchrest_model-0.2.2 vendor/rails/activerecord/test/cases/serialization_test.rb
radiantcms-couchrest_model-0.2.1 vendor/rails/activerecord/test/cases/serialization_test.rb
radiantcms-couchrest_model-0.2 vendor/rails/activerecord/test/cases/serialization_test.rb