Sha256: 1c2189f0e4da31a322eef9fdd41de79017f00e5e7c40727fd10e64f848202fff

Contents?: true

Size: 1.54 KB

Versions: 44

Compression:

Stored size: 1.54 KB

Contents

require 'test_helper'

class SerializationTest < Test::Unit::TestCase
  def setup
    @document = Class.new do
      include MongoMapper::EmbeddedDocument
      key :name, String
      key :age, Integer
      key :awesome, Boolean
      key :preferences, Hash
      key :created_at, Time
    end
    
    @instance = @document.new(
      :name        => 'John Doe',
      :age         => 25,
      :awesome     => true,
      :preferences => {:language => 'Ruby'},
      :created_at  => Time.now.change(:usec => 0)
    )
  end
  
  [:json].each do |format|
    context format do
      should "be reversable" do
        serialized = @instance.send("to_#{format}")
        unserialized = @document.new.send("from_#{format}", serialized)

        assert_equal @instance, unserialized
      end
      
      should "allow attribute only filtering" do
        serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
        unserialized = @document.new.send("from_#{format}", serialized)

        assert_equal @instance.name, unserialized.name
        assert_equal @instance.age, unserialized.age
        assert ! unserialized.awesome
        assert_nil unserialized.created_at
      end
      
      should "allow attribute except filtering" do
        serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
        unserialized = @document.new.send("from_#{format}", serialized)
        
        assert_nil unserialized.name
        assert_nil unserialized.age
        assert_equal @instance.awesome, unserialized.awesome
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 7 rubygems

Version Path
mongo_mapper-0.6.2 test/unit/test_serializations.rb
mongo_mapper-0.6.1 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.11.18 test/unit/test_serializations.rb
mongo_mapper-0.6.0 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.11.8 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.11.6 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.11.2 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.10.31 test/unit/test_serializations.rb
mongo_mapper-0.5.8 test/unit/test_serializations.rb
mongo_mapper-0.5.7 test/unit/test_serializations.rb
mongo_mapper-0.5.6 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.10.16 test/unit/test_serializations.rb
mongo_mapper-0.5.5 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.10.12 test/unit/test_serializations.rb
djsun-mongo_mapper-0.5.4.1 test/unit/test_serializations.rb
djsun-mongo_mapper-0.5.3.1 test/unit/test_serializations.rb
mongo_mapper-0.5.4 test/unit/test_serializations.rb
mongo_mapper-0.5.3 test/unit/test_serializations.rb
mongo_mapper-unstable-2009.10.11 test/unit/test_serializations.rb
djsun-mongo_mapper-0.5.2.1 test/unit/test_serializations.rb