Sha256: 090834d6e2a6a3507171201ea900782e570b92a409472187b49931476c84c3db

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require "spec_helper"

describe Mongoid::Relations::Constraint do

  describe "#convert" do

    context "when the id's class stores object ids" do

      before(:all) do
        Person.field(
          :_id,
          type: BSON::ObjectId,
          pre_processed: true,
          default: ->{ BSON::ObjectId.new }
        )
      end

      let(:metadata) do
        Mongoid::Relations::Metadata.new(
          class_name: "Person",
          name: :person,
          inverse_class_name: "Post",
          relation: Mongoid::Relations::Referenced::In
        )
      end

      let(:constraint) do
        described_class.new(metadata)
      end

      context "when provided an object id" do

        let(:object) do
          BSON::ObjectId.new
        end

        it "returns the object id" do
          expect(constraint.convert(object)).to eq(object)
        end
      end

      context "when provided a string" do

        let(:object) do
          BSON::ObjectId.new
        end

        it "returns an object id from the string" do
          expect(constraint.convert(object.to_s)).to eq(object)
        end
      end
    end

    context "when the id's class does not store object ids" do

      let(:metadata) do
        Mongoid::Relations::Metadata.new(
          class_name: "Account",
          name: :account,
          inverse_class_name: "Alert",
          relation: Mongoid::Relations::Referenced::In
        )
      end

      let(:constraint) do
        described_class.new(metadata)
      end

      it "returns the object" do
        expect(constraint.convert("testing")).to eq("testing")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sepastian-mongoid-rails4-4.0.1.alpha spec/mongoid/relations/constraint_spec.rb
sepastian-mongoid-rails4-4.0.0.alpha spec/mongoid/relations/constraint_spec.rb