Sha256: 8b23d28245f50df3e01c2458ac0633e4a15f16e15c8e8ce8474839677552ce74

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

require "helper"

class TestOhmToHash < Test::Unit::TestCase
  setup do
    Ohm.flush
  end

  context "a Person with name: matz, age: nil, skills: 10" do
    class Person < Ohm::Model
      include Ohm::ToHash

      attribute :name
      attribute :age
      attribute :skills
    end

    setup do
      @person = Person.create(:name => 'matz', :skills => 10)
      @person = Person[@person.id]
    end

    should "have a to_hash of { id: 1, name: 'matz', age: nil, skills: 10 }" do
      assert_equal(
        { :id => '1', :name => "matz", :age => nil, :skills => '10' },
        @person.to_hash
      )
    end
  end

  context "when a Post has a votes counter" do
    class Post < Ohm::Model
      include Ohm::ToHash

      counter :votes
    end

    setup do
      @post = Post.create
      @post.incr :votes
      @post.incr :votes
      @post.incr :votes
    end

    should "include the votes in the hash" do
      assert_equal({ :id => '1', :votes => 3 }, @post.to_hash)
    end
  end

  context "when a comment has a reference to a person" do
    self::Person = Class.new(Ohm::Model)

    class Comment < Ohm::Model
      include Ohm::ToHash

      reference :person, Person
    end

    setup do
      @person = Person.create
      @comment = Comment.create(:person => @person)
    end

    should "have the person_id in the hash" do
      assert_equal({ :id => '1', :person_id => '1' }, @comment.to_hash)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ohm-contrib-0.0.31 test/test_ohm_to_hash.rb
ohm-contrib-0.0.30 test/test_ohm_to_hash.rb
ohm-contrib-0.0.29 test/test_ohm_to_hash.rb
ohm-contrib-0.0.28 test/test_ohm_to_hash.rb
ohm-contrib-0.0.27 test/test_ohm_to_hash.rb