Sha256: cfb790e2c8ebd21c6132aa63cdd8d9d988fd65eeb251dab3a9c3604d73fcf2ad

Contents?: true

Size: 1.54 KB

Versions: 30

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_nil 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

30 entries across 30 versions & 6 rubygems

Version Path
crnixon-mongomapper-0.3.4 test/unit/test_serializations.rb
djsun-mongomapper-0.3.1.1 test/unit/test_serializations.rb
djsun-mongomapper-0.3.1 test/unit/test_serializations.rb
djsun-mongomapper-0.3.3 test/unit/test_serializations.rb
djsun-mongomapper-0.3.5.1 test/unit/test_serializations.rb
djsun-mongomapper-0.3.5.2 test/unit/test_serializations.rb
djsun-mongomapper-0.3.5.4 test/unit/test_serializations.rb
djsun-mongomapper-0.3.5.5 test/unit/test_serializations.rb
djsun-mongomapper-0.3.5 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.10 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.11 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.12 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.13 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.14 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.15 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.3 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.4.1 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.4 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.5 test/unit/test_serializations.rb
hashrocket-mongomapper-0.3.6 test/unit/test_serializations.rb