parametric 0.0.1 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,430 @@
1
+ require "spec_helper"
2
+
3
+ describe Parametric::Field do
4
+ let(:registry) { Parametric.registry }
5
+ let(:context) { Parametric::Context.new}
6
+ subject { described_class.new(:a_key, registry) }
7
+
8
+ def register_coercion(name, block)
9
+ registry.policy name do
10
+ coerce &block
11
+ end
12
+ end
13
+
14
+ def resolve(subject, payload)
15
+ subject.resolve(payload, context)
16
+ end
17
+
18
+ def has_errors
19
+ expect(context.errors.keys).not_to be_empty
20
+ end
21
+
22
+ def no_errors
23
+ expect(context.errors.keys).to be_empty
24
+ end
25
+
26
+ def has_error(key, message)
27
+ expect(context.errors[key]).to include(message)
28
+ end
29
+
30
+ let(:payload) { {a_key: "Joe"} }
31
+
32
+ describe "#resolve" do
33
+ it "returns value" do
34
+ resolve(subject, payload).tap do |r|
35
+ expect(r.eligible?).to be true
36
+ no_errors
37
+ expect(r.value).to eq "Joe"
38
+ end
39
+ end
40
+ end
41
+
42
+ BUILT_IN_COERCIONS = [:string, :integer, :number, :array, :object, :boolean]
43
+
44
+ describe "#meta_data" do
45
+ BUILT_IN_COERCIONS.each do |t|
46
+ it "policy #{t} adds #{t} to meta data" do
47
+ subject.policy(t)
48
+ expect(subject.meta_data[:type]).to eq t
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "#type" do
54
+ it "is an alias for #policy" do
55
+ subject.type(:integer)
56
+ resolve(subject, a_key: "10.0").tap do |r|
57
+ expect(r.value).to eq 10
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#policy" do
63
+ it "coerces integer" do
64
+ subject.policy(:integer)
65
+ resolve(subject, a_key: "10.0").tap do |r|
66
+ expect(r.eligible?).to be true
67
+ no_errors
68
+ expect(r.value).to eq 10
69
+ end
70
+ end
71
+
72
+ it "coerces number" do
73
+ subject.policy(:number)
74
+ resolve(subject, a_key: "10.0").tap do |r|
75
+ expect(r.eligible?).to be true
76
+ no_errors
77
+ expect(r.value).to eq 10.0
78
+ end
79
+ end
80
+
81
+ it "coerces string" do
82
+ subject.policy(:string)
83
+ resolve(subject, a_key: 10.0).tap do |r|
84
+ expect(r.eligible?).to be true
85
+ no_errors
86
+ expect(r.value).to eq "10.0"
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "#default" do
92
+ it "is default if missing key" do
93
+ resolve(subject.default("AA"), foobar: 1).tap do |r|
94
+ expect(r.eligible?).to be true
95
+ no_errors
96
+ expect(r.value).to eq "AA"
97
+ end
98
+ end
99
+
100
+ it "returns value if key is present" do
101
+ resolve(subject.default("AA"), a_key: nil).tap do |r|
102
+ expect(r.eligible?).to be true
103
+ no_errors
104
+ expect(r.value).to eq nil
105
+ end
106
+
107
+ resolve(subject.default("AA"), a_key: "abc").tap do |r|
108
+ expect(r.eligible?).to be true
109
+ no_errors
110
+ expect(r.value).to eq "abc"
111
+ end
112
+ end
113
+ end
114
+
115
+ describe "#present" do
116
+ it "is valid if value is present" do
117
+ resolve(subject.present, a_key: "abc").tap do |r|
118
+ expect(r.eligible?).to be true
119
+ no_errors
120
+ expect(r.value).to eq "abc"
121
+ end
122
+ end
123
+
124
+ it "is invalid if value is empty" do
125
+ resolve(subject.present, a_key: "").tap do |r|
126
+ expect(r.eligible?).to be true
127
+ has_errors
128
+ expect(r.value).to eq ""
129
+ end
130
+
131
+ resolve(subject.present, a_key: nil).tap do |r|
132
+ expect(r.eligible?).to be true
133
+ has_errors
134
+ expect(r.value).to eq nil
135
+ end
136
+ end
137
+
138
+ it "is invalid if key is missing" do
139
+ resolve(subject.present, foo: "abc").tap do |r|
140
+ expect(r.eligible?).to be true
141
+ has_errors
142
+ expect(r.value).to eq nil
143
+ end
144
+ end
145
+ end
146
+
147
+ describe "#required" do
148
+ it "is valid if key is present" do
149
+ resolve(subject.required, a_key: "abc").tap do |r|
150
+ expect(r.eligible?).to be true
151
+ no_errors
152
+ expect(r.value).to eq "abc"
153
+ end
154
+ end
155
+
156
+ it "is valid if key is present and value empty" do
157
+ resolve(subject.required, a_key: "").tap do |r|
158
+ expect(r.eligible?).to be true
159
+ no_errors
160
+ expect(r.value).to eq ""
161
+ end
162
+ end
163
+
164
+ it "is invalid if key is missing" do
165
+ resolve(subject.required, foobar: "lala").tap do |r|
166
+ expect(r.eligible?).to be true
167
+ has_errors
168
+ expect(r.value).to eq nil
169
+ end
170
+ end
171
+ end
172
+
173
+ describe "#options" do
174
+ before do
175
+ subject.options(['a', 'b', 'c'])
176
+ end
177
+
178
+ it "resolves if value within options" do
179
+ resolve(subject, a_key: "b").tap do |r|
180
+ expect(r.eligible?).to be true
181
+ no_errors
182
+ expect(r.value).to eq "b"
183
+ end
184
+ end
185
+
186
+ it "resolves if value is array within options" do
187
+ resolve(subject, a_key: ["b", "c"]).tap do |r|
188
+ expect(r.eligible?).to be true
189
+ no_errors
190
+ expect(r.value).to eq ["b", "c"]
191
+ end
192
+ end
193
+
194
+ it "does not resolve if missing key" do
195
+ resolve(subject, foobar: ["b", "c"]).tap do |r|
196
+ expect(r.eligible?).to be false
197
+ no_errors
198
+ expect(r.value).to be_nil
199
+ end
200
+ end
201
+
202
+ it "does resolve if missing key and default set" do
203
+ subject.default("Foobar")
204
+ resolve(subject, foobar: ["b", "c"]).tap do |r|
205
+ expect(r.eligible?).to be true
206
+ no_errors
207
+ expect(r.value).to eq "Foobar"
208
+ end
209
+ end
210
+
211
+ it "is invalid if missing key and required" do
212
+ subject = described_class.new(:a_key).required.options(%w(a b c))
213
+ resolve(subject, foobar: ["b", "c"]).tap do |r|
214
+ expect(r.eligible?).to be true
215
+ has_errors
216
+ expect(r.value).to be_nil
217
+ end
218
+ end
219
+
220
+ it "is invalid if value outside options" do
221
+ resolve(subject, a_key: "x").tap do |r|
222
+ expect(r.eligible?).to be true
223
+ has_errors
224
+ expect(r.value).to eq "x"
225
+ end
226
+
227
+ resolve(subject, a_key: ["x", "b"]).tap do |r|
228
+ expect(r.eligible?).to be true
229
+ has_errors
230
+ expect(r.value).to eq ["x", "b"]
231
+ end
232
+ end
233
+ end
234
+
235
+ describe ":split policy" do
236
+ it "splits by comma" do
237
+ resolve(subject.policy(:split), a_key: "tag1,tag2").tap do |r|
238
+ expect(r.eligible?).to be true
239
+ no_errors
240
+ expect(r.value).to eq ["tag1", "tag2"]
241
+ end
242
+ end
243
+ end
244
+
245
+ describe ":declared policy" do
246
+ it "is eligible if key exists" do
247
+ resolve(subject.policy(:declared).present, a_key: "").tap do |r|
248
+ expect(r.eligible?).to be true
249
+ has_errors
250
+ expect(r.value).to eq ""
251
+ end
252
+ end
253
+
254
+ it "is available as method" do
255
+ resolve(subject.declared.present, a_key: "").tap do |r|
256
+ expect(r.eligible?).to be true
257
+ has_errors
258
+ expect(r.value).to eq ""
259
+ end
260
+ end
261
+
262
+ it "is not eligible if key does not exist" do
263
+ resolve(subject.policy(:declared).present, foo: "").tap do |r|
264
+ expect(r.eligible?).to be false
265
+ no_errors
266
+ expect(r.value).to eq nil
267
+ end
268
+ end
269
+
270
+ it "returns default" do
271
+ resolve(subject.policy(:declared).default('aa'), foo: "").tap do |r|
272
+ expect(r.eligible?).to be true
273
+ no_errors
274
+ expect(r.value).to eq 'aa'
275
+ end
276
+ end
277
+ end
278
+
279
+ describe ':declared_no_default' do
280
+ it "does not return default" do
281
+ resolve(subject.policy(:declared_no_default).default('aa'), lala: "").tap do |r|
282
+ expect(r.eligible?).to be false
283
+ no_errors
284
+ expect(r.value).to eq nil
285
+ end
286
+ end
287
+ end
288
+
289
+ describe ":noop policy" do
290
+ it "does not do anything" do
291
+ resolve(subject.policy(:noop).present, a_key: "").tap do |r|
292
+ expect(r.eligible?).to be true
293
+ has_errors
294
+ expect(r.value).to eq ""
295
+ end
296
+
297
+ resolve(subject.policy(:noop).present, foo: "").tap do |r|
298
+ expect(r.eligible?).to be true
299
+ has_errors
300
+ expect(r.value).to eq nil
301
+ end
302
+ end
303
+ end
304
+
305
+ describe "#schema" do
306
+ it "runs sub-schema" do
307
+ subject.schema do
308
+ field(:name).policy(:string)
309
+ field(:tags).policy(:split).policy(:array)
310
+ end
311
+
312
+ payload = {a_key: [{name: "n1", tags: "t1,t2"}, {name: "n2", tags: ["t3"]}]}
313
+
314
+ resolve(subject, payload).tap do |r|
315
+ expect(r.eligible?).to be true
316
+ no_errors
317
+ expect(r.value).to eq([
318
+ {name: "n1", tags: ["t1", "t2"]},
319
+ {name: "n2", tags: ["t3"]},
320
+ ])
321
+ end
322
+ end
323
+ end
324
+
325
+ describe '#policy' do
326
+ let(:custom_klass) do
327
+ Class.new do
328
+ def initialize(title = 'Sr.')
329
+ @title = title
330
+ end
331
+
332
+ def eligible?(*_)
333
+ true
334
+ end
335
+
336
+ def valid?(*_)
337
+ true
338
+ end
339
+
340
+ def coerce(value, key, context)
341
+ "#{@title} #{value}"
342
+ end
343
+
344
+ def meta_data
345
+ {foo: "bar"}
346
+ end
347
+ end
348
+ end
349
+
350
+ it 'works with policy in registry' do
351
+ register_coercion :foo, ->(v, k, c){ "Hello #{v}" }
352
+ subject.policy(:foo)
353
+ resolve(subject, a_key: "Ismael").tap do |r|
354
+ expect(r.eligible?).to be true
355
+ no_errors
356
+ expect(r.value).to eq "Hello Ismael"
357
+ end
358
+ end
359
+
360
+ it 'raises if policy not found' do
361
+ expect{
362
+ subject.policy(:foobar)
363
+ }.to raise_exception Parametric::ConfigurationError
364
+ end
365
+
366
+ it 'chains policies' do
367
+ registry.policy :general, custom_klass.new("General")
368
+ registry.policy :commander, custom_klass.new("Commander")
369
+
370
+ subject
371
+ .policy(:general)
372
+ .policy(:commander)
373
+
374
+ resolve(subject, a_key: "Ismael").tap do |r|
375
+ expect(r.eligible?).to be true
376
+ no_errors
377
+ expect(r.value).to eq "Commander General Ismael"
378
+ end
379
+ end
380
+
381
+ it "can instantiate policy class and pass arguments" do
382
+ registry.policy :job_title, custom_klass
383
+
384
+ subject.policy(:job_title, "Developer")
385
+
386
+ resolve(subject, a_key: "Ismael").tap do |r|
387
+ expect(r.eligible?).to be true
388
+ no_errors
389
+ expect(r.value).to eq "Developer Ismael"
390
+ end
391
+ end
392
+
393
+ it "can take a class not in the registry" do
394
+ subject.policy(custom_klass, "Developer")
395
+
396
+ resolve(subject, a_key: "Ismael").tap do |r|
397
+ expect(r.eligible?).to be true
398
+ no_errors
399
+ expect(r.value).to eq "Developer Ismael"
400
+ end
401
+ end
402
+
403
+ it "adds policy meta data" do
404
+ subject.policy(custom_klass, "Developer")
405
+ expect(subject.meta_data[:foo]).to eq "bar"
406
+ end
407
+
408
+ it "can take an instance not in the registry" do
409
+ subject.policy(custom_klass.new("Developer"), "ignore this")
410
+
411
+ resolve(subject, a_key: "Ismael").tap do |r|
412
+ expect(r.eligible?).to be true
413
+ no_errors
414
+ expect(r.value).to eq "Developer Ismael"
415
+ end
416
+ end
417
+
418
+ it 'add policy exceptions to #errors' do
419
+ register_coercion :error, ->(v, k, c){ raise "This is an error" }
420
+
421
+ subject.policy(:error)
422
+
423
+ resolve(subject, a_key: "b").tap do |r|
424
+ expect(r.eligible?).to be true
425
+ has_error("$", "This is an error")
426
+ expect(r.value).to eq "b"
427
+ end
428
+ end
429
+ end
430
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'default coercions' do
4
+ def test_coercion(key, value, expected)
5
+ coercion = Parametric.registry.policies[key]
6
+ expect(coercion.new.coerce(value, nil, nil)).to eq expected
7
+ end
8
+
9
+ describe ':datetime' do
10
+ it {
11
+ coercion = Parametric.registry.coercions[:datetime]
12
+ coercion.new.coerce("2016-11-05T14:23:34Z", nil, nil).tap do |d|
13
+ expect(d).to be_a Date
14
+ expect(d.year).to eq 2016
15
+ expect(d.month).to eq 11
16
+ expect(d.day).to eq 5
17
+ expect(d.hour).to eq 14
18
+ expect(d.minute).to eq 23
19
+ expect(d.second).to eq 34
20
+ expect(d.zone).to eq "+00:00"
21
+ end
22
+ }
23
+ end
24
+
25
+ describe ':integer' do
26
+ it {
27
+ test_coercion(:integer, '10', 10)
28
+ test_coercion(:integer, '10.20', 10)
29
+ test_coercion(:integer, 10.20, 10)
30
+ test_coercion(:integer, 10, 10)
31
+ }
32
+ end
33
+
34
+ describe ':number' do
35
+ it {
36
+ test_coercion(:number, '10', 10.0)
37
+ test_coercion(:number, '10.20', 10.20)
38
+ test_coercion(:number, 10.20, 10.20)
39
+ test_coercion(:number, 10, 10.0)
40
+ }
41
+ end
42
+
43
+ describe ':string' do
44
+ it {
45
+ test_coercion(:string, '10', '10')
46
+ test_coercion(:string, '10.20', '10.20')
47
+ test_coercion(:string, 10.20, '10.2')
48
+ test_coercion(:string, 10, '10')
49
+ test_coercion(:string, true, 'true')
50
+ test_coercion(:string, 'hello', 'hello')
51
+ }
52
+ end
53
+
54
+ describe ':boolean' do
55
+ it {
56
+ test_coercion(:boolean, true, true)
57
+ test_coercion(:boolean, '10', true)
58
+ test_coercion(:boolean, '', true)
59
+ test_coercion(:boolean, nil, false)
60
+ test_coercion(:boolean, false, false)
61
+ }
62
+ end
63
+
64
+ describe ':split' do
65
+ it {
66
+ test_coercion(:split, 'aaa,bb,cc', ['aaa', 'bb', 'cc'])
67
+ test_coercion(:split, 'aaa ,bb, cc', ['aaa', 'bb', 'cc'])
68
+ test_coercion(:split, 'aaa', ['aaa'])
69
+ test_coercion(:split, ['aaa', 'bb', 'cc'], ['aaa', 'bb', 'cc'])
70
+ }
71
+ end
72
+ end
@@ -0,0 +1,133 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parametric::Schema do
4
+ describe '#before_resolve' do
5
+ it 'passes payload through before_resolve block, if defined' do
6
+ schema = described_class.new do
7
+ before_resolve do |payload, _context|
8
+ payload[:slug] = payload[:name].to_s.downcase.gsub(/\s+/, '-') unless payload[:slug]
9
+ payload
10
+ end
11
+
12
+ field(:name).policy(:string).present
13
+ field(:slug).policy(:string).present
14
+ field(:variants).policy(:array).schema do
15
+ before_resolve do |payload, _context|
16
+ payload[:slug] = "v: #{payload[:name].to_s.downcase}"
17
+ payload
18
+ end
19
+ field(:name).policy(:string).present
20
+ field(:slug).type(:string).present
21
+ end
22
+ end
23
+
24
+ result = schema.resolve({ name: 'A name', variants: [{ name: 'A variant' }] })
25
+ expect(result.valid?).to be true
26
+ expect(result.output[:slug]).to eq 'a-name'
27
+ expect(result.output[:variants].first[:slug]).to eq 'v: a variant'
28
+ end
29
+
30
+ it 'collects errors added in before_resolve blocks' do
31
+ schema = described_class.new do
32
+ field(:variants).type(:array).schema do
33
+ before_resolve do |payload, context|
34
+ context.add_error 'nope!' if payload[:name] == 'with errors'
35
+ payload
36
+ end
37
+ field(:name).type(:string)
38
+ end
39
+ end
40
+
41
+ results = schema.resolve({ variants: [ {name: 'no errors'}, {name: 'with errors'}]})
42
+ expect(results.valid?).to be false
43
+ expect(results.errors['$.variants[1]']).to eq ['nope!']
44
+ end
45
+
46
+ it 'copies before_resolve hooks to merged schemas' do
47
+ schema1 = described_class.new do
48
+ before_resolve do |payload, _context|
49
+ payload[:slug] = payload[:name].to_s.downcase.gsub(/\s+/, '-') unless payload[:slug]
50
+ payload
51
+ end
52
+ field(:name).present.type(:string)
53
+ field(:slug).present.type(:string)
54
+ end
55
+
56
+ schema2 = described_class.new do
57
+ before_resolve do |payload, _context|
58
+ payload[:slug] = "slug-#{payload[:slug]}" if payload[:slug]
59
+ payload
60
+ end
61
+
62
+ field(:age).type(:integer)
63
+ end
64
+
65
+ schema3 = schema1.merge(schema2)
66
+
67
+ results = schema3.resolve({ name: 'Ismael Celis', age: 41 })
68
+ expect(results.output[:slug]).to eq 'slug-ismael-celis'
69
+ end
70
+
71
+ it 'works with any callable' do
72
+ slug_maker = Class.new do
73
+ def initialize(slug_field, from:)
74
+ @slug_field, @from = slug_field, from
75
+ end
76
+
77
+ def call(payload, _context)
78
+ payload.merge(
79
+ @slug_field => payload[@from].to_s.downcase.gsub(/\s+/, '-')
80
+ )
81
+ end
82
+ end
83
+
84
+ schema = described_class.new do |sc, _opts|
85
+ sc.before_resolve slug_maker.new(:slug, from: :name)
86
+
87
+ sc.field(:name).type(:string)
88
+ sc.field(:slug).type(:string)
89
+ end
90
+
91
+ results = schema.resolve(name: 'Ismael Celis')
92
+ expect(results.output[:slug]).to eq 'ismael-celis'
93
+ end
94
+ end
95
+
96
+ describe '#after_resolve' do
97
+ let!(:schema) do
98
+ described_class.new do
99
+ after_resolve do |payload, ctx|
100
+ ctx.add_base_error('deposit', 'cannot be greater than house price') if payload[:deposit] > payload[:house_price]
101
+ payload.merge(desc: 'hello')
102
+ end
103
+
104
+ field(:deposit).policy(:integer).present
105
+ field(:house_price).policy(:integer).present
106
+ field(:desc).policy(:string)
107
+ end
108
+ end
109
+
110
+ it 'passes payload through after_resolve block, if defined' do
111
+ result = schema.resolve({ deposit: 1100, house_price: 1000 })
112
+ expect(result.valid?).to be false
113
+ expect(result.output[:deposit]).to eq 1100
114
+ expect(result.output[:house_price]).to eq 1000
115
+ expect(result.output[:desc]).to eq 'hello'
116
+ end
117
+
118
+ it 'copies after hooks when merging schemas' do
119
+ child_schema = described_class.new do
120
+ field(:name).type(:string)
121
+ end
122
+
123
+ union = schema.merge(child_schema)
124
+
125
+ result = union.resolve({ name: 'Joe', deposit: 1100, house_price: 1000 })
126
+ expect(result.valid?).to be false
127
+ expect(result.output[:deposit]).to eq 1100
128
+ expect(result.output[:house_price]).to eq 1000
129
+ expect(result.output[:desc]).to eq 'hello'
130
+ expect(result.output[:name]).to eq 'Joe'
131
+ end
132
+ end
133
+ end