File: result_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (141 lines) | stat: -rw-r--r-- 3,301 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# frozen_string_literal: true
# rubocop:todo all

require 'lite_spec_helper'

describe Mongo::BulkWrite::Result do
  let(:results_document) do
    {'n_inserted' => 2, 'n' => 3, 'inserted_ids' => [1, 2]}
  end

  let(:subject) { described_class.new(results_document, true) }

  describe 'construction' do
    it 'works' do
      expect(subject).to be_a(described_class)
    end
  end

  describe '#inserted_count' do
    it 'is taken from results document' do
      expect(subject.inserted_count).to eql(2)
    end
  end

  describe '#inserted_ids' do
    it 'is taken from results document' do
      expect(subject.inserted_ids).to eql([1, 2])
    end
  end

  describe '#deleted_count' do
    let(:results_document) do
      {'n_removed' => 2, 'n' => 3}
    end

    it 'is taken from results document' do
      expect(subject.deleted_count).to eql(2)
    end
  end

  describe '#matched_count' do
    let(:results_document) do
      {'n_modified' => 1, 'n_matched' => 2, 'n' => 3}
    end

    it 'is taken from results document' do
      expect(subject.matched_count).to eql(2)
    end
  end

  describe '#modified_count' do
    let(:results_document) do
      {'n_modified' => 1, 'n_matched' => 2, 'n' => 3}
    end

    it 'is taken from results document' do
      expect(subject.modified_count).to eql(1)
    end
  end

  describe '#upserted_count' do
    let(:results_document) do
      {'n_upserted' => 2, 'n' => 3, 'upserted_ids' => [1, 2]}
    end

    it 'is taken from results document' do
      expect(subject.upserted_count).to eql(2)
    end
  end

  describe '#upserted_ids' do
    let(:results_document) do
      {'n_upserted' => 2, 'n' => 3, 'upserted_ids' => [1, 2]}
    end

    it 'is taken from results document' do
      expect(subject.upserted_ids).to eql([1, 2])
    end
  end

  describe '#validate!' do
    context 'no errors' do
      it 'returns self' do
        expect(subject.validate!).to eql(subject)
      end
    end

    context 'with top level error' do
      let(:results_document) do
        {
          'writeErrors' => [
            {
              'ok' => 0,
              'errmsg' => 'not master',
              'code' => 10107,
              'codeName' => 'NotMaster',
            }
          ]
        }
      end

      it 'raises BulkWriteError' do
        expect do
          subject.validate!
        # BulkWriteErrors don't have any messages on them
        end.to raise_error(Mongo::Error::BulkWriteError, /not master/)
      end
    end

    context 'with write concern error' do
      let(:results_document) do
        {'n' => 1, 'writeConcernErrors' => {
          'errmsg' => 'Not enough data-bearing nodes',
          'code' => 100,
          'codeName' => 'CannotSatisfyWriteConcern',
        }}
      end

      it 'raises BulkWriteError' do
        expect do
          subject.validate!
        # BulkWriteErrors don't have any messages on them
        end.to raise_error(Mongo::Error::BulkWriteError, nil)
      end
    end
  end

  describe "#acknowledged?" do

    [true, false].each do |b|
      context "when acknowledged is passed as #{b}" do

        let(:result) { described_class.new(results_document, b) }

        it "acknowledged? is #{b}" do
          expect(result.acknowledged?).to be b
        end
      end
    end
  end
end