Sha256: a70f1d0c4559dedf06fdc97d532925e8eff77524580fe542835fbfd0e623b4b9

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'spec_helper'

describe Mongo::Operation::Write::EnsureIndex do

  describe '#execute' do

    context 'when the index is created' do

      let(:spec) do
        { random: 1 }
      end

      let(:operation) do
        described_class.new(
          index: spec,
          db_name: TEST_DB,
          coll_name: TEST_COLL,
          index_name: 'random_1',
          options: { unique: true }
        )
      end

      let(:response) do
        operation.execute(authorized_primary.context)
      end

      after do
        authorized_collection.indexes.drop(spec)
      end

      it 'returns ok' do
        expect(response).to be_successful
      end
    end

    context 'when index creation fails' do

      let(:spec) do
        { random: 1 }
      end

      let(:operation) do
        described_class.new(
          index: spec,
          db_name: TEST_DB,
          coll_name: TEST_COLL,
          index_name: 'random_1',
          options: { unique: true }
        )
      end

      let(:second_operation) do
        described_class.new(
          index: spec,
          db_name: TEST_DB,
          coll_name: TEST_COLL,
          index_name: 'random_1',
          options: { unique: false }
        )
      end

      before do
        operation.execute(authorized_primary.context)
      end

      after do
        authorized_collection.indexes.drop(spec)
      end

      it 'raises an exception', if: write_command_enabled? do
        expect {
          second_operation.execute(authorized_primary.context)
        }.to raise_error(Mongo::Error::OperationFailure)
      end

      it 'does not raise an exception', unless: write_command_enabled? do
        expect(second_operation.execute(authorized_primary.context)).to be_successful
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo-2.0.0.beta spec/mongo/operation/write/ensure_index_spec.rb