-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathparameters_spec.rb
286 lines (234 loc) · 9.18 KB
/
parameters_spec.rb
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# frozen_string_literal: true
describe Grape::DSL::Parameters do
subject { dummy_class.new }
let(:dummy_class) do
Class.new do
include Grape::DSL::Parameters
attr_accessor :api, :element, :parent
def initialize
@validate_attributes = []
end
def validate_attributes(*args)
@validate_attributes.push(*args)
end
def validate_attributes_reader
@validate_attributes
end
def push_declared_params(args, _opts)
@push_declared_params = args
end
def push_declared_params_reader
@push_declared_params
end
def validates(*args)
@validates = *args
end
def validates_reader
@validates
end
def new_scope(args, _, &block)
nested_scope = self.class.new
nested_scope.new_group_scope(args, &block)
nested_scope
end
def new_group_scope(args)
prev_group = @group
@group = args.clone.first
yield
@group = prev_group
end
def extract_message_option(attrs)
return nil unless attrs.is_a?(Array)
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
opts.key?(:message) && !opts[:message].nil? ? opts.delete(:message) : nil
end
end
end
describe '#use' do
before do
allow_message_expectations_on_nil
allow(subject.api).to receive(:namespace_stackable).with(:named_params)
end
let(:options) { { option: 'value' } }
let(:named_params) { { params_group: proc {} } }
it 'calls processes associated with named params' do
allow(subject.api).to receive(:namespace_stackable_with_hash).and_return(named_params)
expect(subject).to receive(:instance_exec).with(options).and_yield
subject.use :params_group, options
end
it 'raises error when non-existent named param is called' do
allow(subject.api).to receive(:namespace_stackable_with_hash).and_return({})
expect { subject.use :params_group }.to raise_error('Params :params_group not found!')
end
end
describe '#use_scope' do
it 'is alias to #use' do
expect(subject.method(:use_scope)).to eq subject.method(:use)
end
end
describe '#includes' do
it 'is alias to #use' do
expect(subject.method(:includes)).to eq subject.method(:use)
end
end
describe '#requires' do
it 'adds a required parameter' do
subject.requires :id, type: Integer, desc: 'Identity.'
expect(subject.validate_attributes_reader).to eq([[:id], { type: Integer, desc: 'Identity.', presence: { value: true, message: nil } }])
expect(subject.push_declared_params_reader).to eq([:id])
end
end
describe '#optional' do
it 'adds an optional parameter' do
subject.optional :id, type: Integer, desc: 'Identity.'
expect(subject.validate_attributes_reader).to eq([[:id], { type: Integer, desc: 'Identity.' }])
expect(subject.push_declared_params_reader).to eq([:id])
end
end
describe '#with' do
it 'creates a scope with group attributes' do
subject.with(type: Integer) { subject.optional :id, desc: 'Identity.' }
expect(subject.validate_attributes_reader).to eq([[:id], { type: Integer, desc: 'Identity.' }])
expect(subject.push_declared_params_reader).to eq([:id])
end
it 'merges the group attributes' do
subject.with(documentation: { in: 'body' }) { subject.optional :vault, documentation: { default: 33 } }
expect(subject.validate_attributes_reader).to eq([[:vault], { documentation: { in: 'body', default: 33 } }])
expect(subject.push_declared_params_reader).to eq([:vault])
end
it 'overrides the group attribute when values not mergable' do
subject.with(type: Integer, documentation: { in: 'body', default: 33 }) do
subject.optional :vault
subject.optional :allowed_vaults, type: [Integer], documentation: { default: [31, 32, 33], is_array: true }
end
expect(subject.validate_attributes_reader).to eq(
[
[:vault], { type: Integer, documentation: { in: 'body', default: 33 } },
[:allowed_vaults], { type: [Integer], documentation: { in: 'body', default: [31, 32, 33], is_array: true } }
]
)
end
it 'allows a primitive type attribite to overwrite a complex type group attribute' do
subject.with(documentation: { x: { nullable: true } }) do
subject.optional :vault, type: Integer, documentation: { x: nil }
end
expect(subject.validate_attributes_reader).to eq(
[
[:vault], { type: Integer, documentation: { x: nil } }
]
)
end
it 'does not nest primitives inside existing complex types erroneously' do
subject.with(type: Hash, documentation: { default: { vault: '33' } }) do
subject.optional :info
subject.optional :role, type: String, documentation: { default: 'resident' }
end
expect(subject.validate_attributes_reader).to eq(
[
[:info], { type: Hash, documentation: { default: { vault: '33' } } },
[:role], { type: String, documentation: { default: 'resident' } }
]
)
end
it 'merges deeply nested attributes' do
subject.with(documentation: { details: { in: 'body', hidden: false } }) do
subject.optional :vault, documentation: { details: { desc: 'The vault number' } }
end
expect(subject.validate_attributes_reader).to eq(
[
[:vault], { documentation: { details: { in: 'body', hidden: false, desc: 'The vault number' } } }
]
)
end
it "supports nested 'with' calls" do
subject.with(type: Integer, documentation: { in: 'body' }) do
subject.optional :pipboy_id
subject.with(documentation: { default: 33 }) do
subject.optional :vault
subject.with(type: String) do
subject.with(documentation: { default: 'resident' }) do
subject.optional :role
end
end
subject.optional :age, documentation: { default: 42 }
end
end
expect(subject.validate_attributes_reader).to eq(
[
[:pipboy_id], { type: Integer, documentation: { in: 'body' } },
[:vault], { type: Integer, documentation: { in: 'body', default: 33 } },
[:role], { type: String, documentation: { in: 'body', default: 'resident' } },
[:age], { type: Integer, documentation: { in: 'body', default: 42 } }
]
)
end
it "supports Hash parameter inside the 'with' calls" do
subject.with(documentation: { in: 'body' }) do
subject.optional :info, type: Hash, documentation: { x: { nullable: true }, desc: 'The info' } do
subject.optional :vault, type: Integer, documentation: { default: 33, desc: 'The vault number' }
end
end
expect(subject.validate_attributes_reader).to eq(
[
[:info], { type: Hash, documentation: { in: 'body', desc: 'The info', x: { nullable: true } } },
[:vault], { type: Integer, documentation: { in: 'body', default: 33, desc: 'The vault number' } }
]
)
end
end
describe '#mutually_exclusive' do
it 'adds an mutally exclusive parameter validation' do
subject.mutually_exclusive :media, :audio
expect(subject.validates_reader).to eq([%i[media audio], { mutual_exclusion: { value: true, message: nil } }])
end
end
describe '#exactly_one_of' do
it 'adds an exactly of one parameter validation' do
subject.exactly_one_of :media, :audio
expect(subject.validates_reader).to eq([%i[media audio], { exactly_one_of: { value: true, message: nil } }])
end
end
describe '#at_least_one_of' do
it 'adds an at least one of parameter validation' do
subject.at_least_one_of :media, :audio
expect(subject.validates_reader).to eq([%i[media audio], { at_least_one_of: { value: true, message: nil } }])
end
end
describe '#all_or_none_of' do
it 'adds an all or none of parameter validation' do
subject.all_or_none_of :media, :audio
expect(subject.validates_reader).to eq([%i[media audio], { all_or_none_of: { value: true, message: nil } }])
end
end
describe '#group' do
it 'is alias to #requires' do
expect(subject.method(:group)).to eq subject.method(:requires)
end
end
describe '#params' do
it 'inherits params from parent' do
parent_params = { foo: 'bar' }
subject.parent = Object.new
allow(subject.parent).to receive_messages(params: parent_params, params_meeting_dependency: nil)
expect(subject.params({})).to eq parent_params
end
describe 'when params argument is an array of hashes' do
it 'returns values of each hash for @element key' do
subject.element = :foo
expect(subject.params([{ foo: 'bar' }, { foo: 'baz' }])).to eq(%w[bar baz])
end
end
describe 'when params argument is a hash' do
it 'returns value for @element key' do
subject.element = :foo
expect(subject.params(foo: 'bar')).to eq('bar')
end
end
describe 'when params argument is not a array or a hash' do
it 'returns empty hash' do
subject.element = Object.new
expect(subject.params(Object.new)).to eq({})
end
end
end
end