Sha256: d406891d95a19afe0fceeb122c7ff14c0f0cc2fe5e757c43cbad95334044c744

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require 'helper'

describe Toy::Identity do
  uses_constants('User', 'Piece')

  describe "setting the key" do
    it "should set key factory to UUIDKeyFactory" do
      User.key(:uuid).should be_instance_of(Toy::Identity::UUIDKeyFactory)
    end

    it "should use ObjectIdKeyFactory if :object_id" do
      User.key(:object_id).should be_instance_of(Toy::Identity::ObjectIdKeyFactory)
    end

    it "should set key factory passed in factory" do
      factory = Toy::Identity::UUIDKeyFactory.new
      User.key(factory).should == factory
    end

    it "should use Toy.key_factory by default" do
      key_factory     = Toy::Identity::UUIDKeyFactory.new
      Toy.key_factory = key_factory
      klass           = Class.new { include Toy::Store }

      key_factory.should_receive(:next_key).and_return('some_key')
      klass.next_key

      Toy.key_factory = nil
    end
  end

  describe ".next_key" do
    it "should call the next key on the key factory" do
      factory = Toy::Identity::UUIDKeyFactory.new
      factory.should_receive(:next_key).and_return('some_key')
      User.key(factory)
      User.next_key.should == 'some_key'
    end

    it "should raise an exception for nil key" do
      factory = Toy::Identity::UUIDKeyFactory.new
      factory.should_receive(:next_key).and_return(nil)
      User.key(factory)
      lambda { User.next_key }.should raise_error
    end
  end

  describe ".key_type" do
    it "returns the type based on the key factory" do
      User.key(Toy::Identity::UUIDKeyFactory.new)
      User.key_type.should be(String)
    end
  end

  describe "initializing the id" do
    it "should pass use pass the new object" do
      Piece.attribute(:name, String)
      Piece.attribute(:number, Integer)
      Piece.key(NameAndNumberKeyFactory.new)
      Piece.new(:name => 'Rook', :number => 1).id.should == 'Rook-1'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toystore-0.6.5 spec/toy/identity_spec.rb
toystore-0.6.4 spec/toy/identity_spec.rb