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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe Mongo::Lint do
before(:all) do
# Since we are installing an expectation on ENV, close any open clients
# which may have background threads reading ENV
ClientRegistry.instance.close_all_clients
end
before do
expect(ENV).to receive(:[]).with('MONGO_RUBY_DRIVER_LINT').at_least(:once).and_return('1')
end
describe '.validate_underscore_read_preference' do
%w(primary primary_preferred secondary secondary_preferred nearest).each do |mode|
it "accepts #{mode} as string" do
expect do
described_class.validate_underscore_read_preference(mode: mode)
end.to_not raise_error
end
it "accepts #{mode} with string mode key" do
expect do
described_class.validate_underscore_read_preference('mode' => mode)
end.to_not raise_error
end
it "accepts #{mode} as symbol" do
expect do
described_class.validate_underscore_read_preference(mode: mode.to_sym)
end.to_not raise_error
end
end
%w(primaryPreferred secondaryPreferred).each do |mode|
it "rejects #{mode} as string" do
expect do
described_class.validate_underscore_read_preference(mode: mode)
end.to raise_error(Mongo::Error::LintError)
end
it "rejects #{mode} with string mode key" do
expect do
described_class.validate_underscore_read_preference('mode' => mode)
end.to raise_error(Mongo::Error::LintError)
end
it "rejects #{mode} as symbol" do
expect do
described_class.validate_underscore_read_preference(mode: mode.to_sym)
end.to raise_error(Mongo::Error::LintError)
end
end
end
describe '.validate_underscore_read_preference_mode' do
%w(primary primary_preferred secondary secondary_preferred nearest).each do |mode|
it "accepts #{mode} as string" do
expect do
described_class.validate_underscore_read_preference_mode(mode)
end.to_not raise_error
end
it "accepts #{mode} as symbol" do
expect do
described_class.validate_underscore_read_preference_mode(mode.to_sym)
end.to_not raise_error
end
end
%w(primaryPreferred secondaryPreferred).each do |mode|
it "rejects #{mode} as string" do
expect do
described_class.validate_underscore_read_preference_mode(mode)
end.to raise_error(Mongo::Error::LintError)
end
it "rejects #{mode} as symbol" do
expect do
described_class.validate_underscore_read_preference_mode(mode.to_sym)
end.to raise_error(Mongo::Error::LintError)
end
end
end
describe '.validate_camel_case_read_preference' do
%w(primary primaryPreferred secondary secondaryPreferred nearest).each do |mode|
it "accepts #{mode} as string" do
expect do
described_class.validate_camel_case_read_preference(mode: mode)
end.to_not raise_error
end
it "accepts #{mode} with string mode key" do
expect do
described_class.validate_camel_case_read_preference('mode' => mode)
end.to_not raise_error
end
it "accepts #{mode} as symbol" do
expect do
described_class.validate_camel_case_read_preference(mode: mode.to_sym)
end.to_not raise_error
end
end
%w(primary_preferred secondary_preferred).each do |mode|
it "rejects #{mode} as string" do
expect do
described_class.validate_camel_case_read_preference(mode: mode)
end.to raise_error(Mongo::Error::LintError)
end
it "rejects #{mode} with string mode key" do
expect do
described_class.validate_camel_case_read_preference('mode' => mode)
end.to raise_error(Mongo::Error::LintError)
end
it "rejects #{mode} as symbol" do
expect do
described_class.validate_camel_case_read_preference(mode: mode.to_sym)
end.to raise_error(Mongo::Error::LintError)
end
end
end
describe '.validate_camel_case_read_preference_mode' do
%w(primary primaryPreferred secondary secondaryPreferred nearest).each do |mode|
it "accepts #{mode} as string" do
expect do
described_class.validate_camel_case_read_preference_mode(mode)
end.to_not raise_error
end
it "accepts #{mode} as symbol" do
expect do
described_class.validate_camel_case_read_preference_mode(mode.to_sym)
end.to_not raise_error
end
end
%w(primary_preferred secondary_preferred).each do |mode|
it "rejects #{mode} as string" do
expect do
described_class.validate_camel_case_read_preference_mode(mode)
end.to raise_error(Mongo::Error::LintError)
end
it "rejects #{mode} as symbol" do
expect do
described_class.validate_camel_case_read_preference_mode(mode.to_sym)
end.to raise_error(Mongo::Error::LintError)
end
end
end
describe '.validate_read_concern_option' do
it 'accepts nil' do
expect do
described_class.validate_read_concern_option(nil)
end.to_not raise_error
end
it 'accepts empty hash' do
expect do
described_class.validate_read_concern_option({})
end.to_not raise_error
end
it "rejects an object which is not a hash" do
expect do
described_class.validate_read_concern_option(:local)
end.to raise_error(Mongo::Error::LintError)
end
[:local, :majority, :snapshot].each do |level|
it "accepts :#{level}" do
expect do
described_class.validate_read_concern_option({level: level})
end.to_not raise_error
end
it "rejects #{level} as string" do
expect do
described_class.validate_read_concern_option({level: level.to_s})
end.to raise_error(Mongo::Error::LintError)
end
end
it "rejects a bogus level" do
expect do
described_class.validate_read_concern_option({level: :bogus})
end.to raise_error(Mongo::Error::LintError)
end
it "rejects level given as a string key" do
expect do
described_class.validate_read_concern_option({'level' => :snapshot})
end.to raise_error(Mongo::Error::LintError)
end
it "rejects a bogus key as symbol" do
expect do
described_class.validate_read_concern_option({foo: 'bar'})
end.to raise_error(Mongo::Error::LintError)
end
it "rejects a bogus key as string" do
expect do
described_class.validate_read_concern_option({'foo' => 'bar'})
end.to raise_error(Mongo::Error::LintError)
end
%w(afterClusterTime after_cluster_time).each do |key|
[:to_s, :to_sym].each do |conv|
key = key.send(conv)
it "rejects #{key.inspect}" do
expect do
described_class.validate_read_concern_option({key => 123})
end.to raise_error(Mongo::Error::LintError)
end
end
end
end
end
|