File: create_index_spec.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (59 lines) | stat: -rw-r--r-- 1,382 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'spec_helper'

describe Mongo::Operation::Write::Command::CreateIndex do

  describe '#execute' do

    context 'when the index is created' do

      let(:spec) do
        { key: { random: 1 }, name: 'random_1', unique: true }
      end

      let(:operation) do
        described_class.new(indexes: [ spec ], db_name: TEST_DB, coll_name: TEST_COLL)
      end

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

      after do
        authorized_collection.indexes.drop_one('random_1')
      end

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

    context 'when index creation fails' do

      let(:spec) do
        { key: { random: 1 }, name: 'random_1', unique: true }
      end

      let(:operation) do
        described_class.new(indexes: [ spec ], db_name: TEST_DB, coll_name: TEST_COLL)
      end

      let(:second_operation) do
        described_class.new(indexes: [ spec.merge(unique: false) ], db_name: TEST_DB, coll_name: TEST_COLL)
      end

      before do
        operation.execute(authorized_primary)
      end

      after do
        authorized_collection.indexes.drop_one('random_1')
      end

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