~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/test/cases/serialization_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require "cases/helper"
2
 
require 'models/contact'
3
 
 
4
 
class SerializationTest < ActiveRecord::TestCase
5
 
  FORMATS = [ :xml, :json ]
6
 
 
7
 
  def setup
8
 
    @contact_attributes = {
9
 
      :name        => 'aaron stack',
10
 
      :age         => 25,
11
 
      :avatar      => 'binarydata',
12
 
      :created_at  => Time.utc(2006, 8, 1),
13
 
      :awesome     => false,
14
 
      :preferences => { :gem => '<strong>ruby</strong>' }
15
 
    }
16
 
 
17
 
    @contact = Contact.new(@contact_attributes)
18
 
  end
19
 
 
20
 
  def test_serialize_should_be_reversible
21
 
    for format in FORMATS
22
 
      @serialized = Contact.new.send("to_#{format}")
23
 
      contact = Contact.new.send("from_#{format}", @serialized)
24
 
 
25
 
      assert_equal @contact_attributes.keys.collect(&:to_s).sort, contact.attributes.keys.collect(&:to_s).sort, "For #{format}"
26
 
    end
27
 
  end
28
 
 
29
 
  def test_serialize_should_allow_attribute_only_filtering
30
 
    for format in FORMATS
31
 
      @serialized = Contact.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ])
32
 
      contact = Contact.new.send("from_#{format}", @serialized)
33
 
      assert_equal @contact_attributes[:name], contact.name, "For #{format}"
34
 
      assert_nil contact.avatar, "For #{format}"
35
 
    end
36
 
  end
37
 
 
38
 
  def test_serialize_should_allow_attribute_except_filtering
39
 
    for format in FORMATS
40
 
      @serialized = Contact.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ])
41
 
      contact = Contact.new.send("from_#{format}", @serialized)
42
 
      assert_nil contact.name, "For #{format}"
43
 
      assert_nil contact.age, "For #{format}"
44
 
      assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
45
 
    end
46
 
  end
47
 
end