Sha256: e72b5ae6af4a1bea27e272d3857d6fc4e7e0e3414d0bbebb9103cf474cb4986a

Contents?: true

Size: 1.63 KB

Versions: 55

Compression:

Stored size: 1.63 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 },
          overwrite: true
        )
      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

55 entries across 50 versions & 3 rubygems

Version Path
mongoid-6.4.8 spec/mongoid/relations/constraint_spec.rb
mongoid-6.4.7 spec/mongoid/relations/constraint_spec.rb
mongoid-6.4.5 spec/mongoid/relations/constraint_spec.rb
mongoid-5.4.1 spec/mongoid/relations/constraint_spec.rb
mongoid-6.4.4 spec/mongoid/relations/constraint_spec.rb
mongoid-6.4.2 spec/mongoid/relations/constraint_spec.rb
mongoid-6.4.1 spec/mongoid/relations/constraint_spec.rb
mongoid-5.4.0 spec/mongoid/relations/constraint_spec.rb
tdiary-5.0.8 vendor/bundle/gems/mongoid-6.4.0/spec/mongoid/relations/constraint_spec.rb
mongoid-6.4.0 spec/mongoid/relations/constraint_spec.rb
mongoid-6.3.0 spec/mongoid/relations/constraint_spec.rb
mongoid-6.2.1 spec/mongoid/relations/constraint_spec.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mongoid-6.1.0/spec/mongoid/relations/constraint_spec.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.2.0/spec/mongoid/relations/constraint_spec.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.1.0/spec/mongoid/relations/constraint_spec.rb
mongoid-6.2.0 spec/mongoid/relations/constraint_spec.rb
mongoid-6.1.1 spec/mongoid/relations/constraint_spec.rb
mongoid-5.2.1 spec/mongoid/relations/constraint_spec.rb
tdiary-5.0.4 vendor/bundle/gems/mongoid-6.1.0/spec/mongoid/relations/constraint_spec.rb
mongoid-6.1.0 spec/mongoid/relations/constraint_spec.rb