Sha256: 1d0cbab7df009e2782be7a519a8d96a24687a16edd259c3bdaa88d7824c7d513

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 KB

Contents

require "spec_helper"

describe Mongoid::Identity do

  describe ".create" do

    let(:name) { Name.new }

    it "sets the document _type to the class name" do
      Mongoid::Identity.create(name)
      name._type.should == "Name"
    end

    it "returns the document" do
      Mongoid::Identity.create(name).should == name
    end

    context "when the document has a primary key" do

      before do
        @address = Address.allocate
        @address.instance_variable_set(:@attributes, { "street" => "Market St"})
      end

      it "sets the id to the composite key" do
        Mongoid::Identity.create(@address)
        @address.id.should == "market-st"
      end

    end

    context "when the document has no primary key" do

      context "when the document has no id" do

        before do
          @person = Person.allocate
          @person.instance_variable_set(:@attributes, {})
          @object_id = stub(:to_s => "1")
          Mongo::ObjectID.expects(:new).returns(@object_id)
        end

        it "sets the id to a mongo object id" do
          Mongoid::Identity.create(@person)
          @person.id.should == "1"
        end

      end

      context "when the document has an id" do

        before do
          @person = Person.allocate
          @person.instance_variable_set(:@attributes, { "_id" => "5" })
        end

        it "returns the existing id" do
          Mongoid::Identity.create(@person)
          @person.id.should == "5"
        end

      end

    end

  end

end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.pre spec/unit/mongoid/identity_spec.rb
mongoid-1.2.6 spec/unit/mongoid/identity_spec.rb
mongoid-1.2.5 spec/unit/mongoid/identity_spec.rb
mongoid-1.2.4 spec/unit/mongoid/identity_spec.rb
mongoid-1.2.3 spec/unit/mongoid/identity_spec.rb
mongoid-1.2.2 spec/unit/mongoid/identity_spec.rb
mongoid-1.2.1 spec/unit/mongoid/identity_spec.rb
mongoid-1.2.0 spec/unit/mongoid/identity_spec.rb
mongoid-1.1.4 spec/unit/mongoid/identity_spec.rb
mongoid-1.1.3 spec/unit/mongoid/identity_spec.rb
mongoid-1.1.2 spec/unit/mongoid/identity_spec.rb
mongoid-1.1.1 spec/unit/mongoid/identity_spec.rb
mongoid-1.1.0 spec/unit/mongoid/identity_spec.rb