Sha256: 87fcd0725685d6392b794cd7e9d9adb337dfe1ad0807d0fe27982cf8834ec307

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'helper'
require 'toy/identity/object_id_key_factory'

describe Toy::Identity::ObjectIdKeyFactory do
  uses_constants('User')

  it "should use BSON::ObjectId as key_type" do
    Toy::Identity::ObjectIdKeyFactory.new.key_type.should be(BSON::ObjectId)
  end

  it "should use object id for next key" do
    key = Toy::Identity::ObjectIdKeyFactory.new.next_key(nil)
    key.should be_instance_of(BSON::ObjectId)
  end

  describe "Declaring key to be object_id" do
    before(:each) do
      User.key(:object_id)
      User.attribute(:name, String)
    end

    it "returns BSON::ObjectId as .key_type" do
      User.key_type.should be(BSON::ObjectId)
    end

    it "sets id attribute to BSON::ObjectId type" do
      User.attributes['id'].type.should be(BSON::ObjectId)
    end

    it "correctly stores id in database" do
      user = User.create(:name => 'John')
      user.id.should be_instance_of(BSON::ObjectId)
      # key_for in memory adapter marshals non symbol/string keys
      # so we have to unmarshal to get the key type
      key =  Marshal.load(user.store.client.keys.first)
      key.should be_instance_of(BSON::ObjectId)
      user.id.should == key
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
toystore-0.7.0 spec/toy/identity/object_id_key_factory_spec.rb
toystore-0.6.6 spec/toy/identity/object_id_key_factory_spec.rb
toystore-0.6.5 spec/toy/identity/object_id_key_factory_spec.rb
toystore-0.6.4 spec/toy/identity/object_id_key_factory_spec.rb