-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.exs
More file actions
557 lines (486 loc) · 17.2 KB
/
decoder_test.exs
File metadata and controls
557 lines (486 loc) · 17.2 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
defmodule DecoderTest do
use ExUnit.Case, async: true
alias Decoder, as: D
test "decode/2 should decode value" do
# unknown derive
assert {:error, [%{message: "unknown derive :unknown"}]} =
D.decode(D.string(derive: [:unknown]), "Bruce")
# decodes string
assert {:ok, "Bruce"} = D.decode(D.string(), "Bruce")
assert {:ok, "abc"} = D.decode(D.string(~r/abc/), "abc")
# custom string format
assert {:ok, ["value1", "value2"]} =
D.decode(
D.string(
derive: [{:format, fn val, _path -> {:ok, val |> String.split(",")} end}]
),
"value1,value2"
)
# decodes date-time
assert {:ok, DateTime.from_iso8601("2021-08-10T00:00:00Z") |> elem(1)} ==
D.decode(D.string(derive: [{:format, "date-time"}]), "2021-08-10T00:00:00Z")
assert {:ok, DateTime.from_iso8601("2023-11-14T22:13:20Z") |> elem(1)} ==
D.decode(D.integer(derive: [{:format, "date-time"}]), 1_700_000_000)
assert {:ok, DateTime.from_iso8601("2023-11-14T22:13:20.000Z") |> elem(1)} ==
D.decode(
D.integer(derive: [{:format, "date-time", :millisecond}]),
1_700_000_000_000
)
# decodes & trims string
assert {:ok, "Bruce"} = D.decode(D.string(derive: [:trim]), " Bruce ")
# decodes integer
assert {:ok, 30} = D.decode(D.integer(), 30)
assert {:ok, 30} =
D.decode(D.integer(derive: [{:gt, 15}, {:lt, 35}, {:gte, 30}, {:lte, 30}]), 30)
# decodes map
assert {:ok, %{"name" => "Bruce", "age" => 30}} ===
D.decode(
D.map(%{
"name" => D.string(),
"age" => D.integer()
}),
%{
"name" => "Bruce",
"age" => 30
}
)
# decodes map and omits extra props
assert {:ok, %{"name" => "Bruce", "age" => 30}} ===
D.decode(
D.map(
%{
"name" => D.string(),
"age" => D.integer()
},
extra_props: false
),
%{
"name" => "Bruce",
"age" => 30,
"extra" => "extra"
}
)
# decodes map in strict mode
assert {:ok, %{"name" => "Bruce", "age" => 30}} ===
D.decode(
D.map(
%{
"name" => D.string(),
"age" => D.integer()
},
strict: true
),
%{
"name" => "Bruce",
"age" => 30
}
)
# decodes empty map in strict mode
assert {:ok, %{}} =
D.decode(
D.map(
%{},
strict: true
),
%{}
)
# decodes record
assert {:ok,
%{
"k1" => "v1",
"k2" => "v2"
}} =
D.decode(
D.record(
D.string(),
D.string()
),
%{
"k1" => "v1",
"k2" => "v2"
}
)
# decodes tuple, support both list and tuple
assert {:ok, {"Bruce", 30}} = D.decode(D.tuple([D.string(), D.integer()]), {"Bruce", 30})
assert {:ok, ["Bruce", 30]} = D.decode(D.tuple([D.string(), D.integer()]), ["Bruce", 30])
# decodes optionals
assert {:ok, nil} = D.decode(D.optional(D.string()), nil)
assert {:ok, ""} = D.decode(D.optional(D.string()), "")
assert {:ok, []} = D.decode(D.optional(D.list(D.string())), [])
assert {:ok, nil} = D.decode(D.optional(D.list(D.string())), nil)
assert {:ok, %{"k" => "v"}} =
D.decode(D.optional(D.map(%{"k" => D.string()})), %{"k" => "v"})
# decodes list
assert {:ok, ["Bruce", "Wayne"]} = D.decode(D.list(D.string()), ["Bruce", "Wayne"])
# decodes nested list
assert {:ok,
[
%{
"name" => "Bruce Wayne",
"activities" => ["fighting", "detective", "billionaire", "philanthropist"]
}
]} =
D.decode(
D.list(D.map(%{"name" => D.string(), "activities" => D.list(D.string())})),
[
%{
"name" => "Bruce Wayne",
"activities" => ["fighting", "detective", "billionaire", "philanthropist"]
}
]
)
# decodes union
assert {:ok, "Bruce"} = D.decode(D.union([D.string(), D.integer()]), "Bruce")
# decodes boolean
assert {:ok, true} = D.decode(D.boolean(), true)
assert {:ok, false} = D.decode(D.boolean(), false)
# decodes literal
assert {:ok, "Bruce"} = D.decode(D.literal("Bruce"), "Bruce")
# decodes any
assert {:ok, "Bruce"} = D.decode(D.any(), "Bruce")
# decodes nil
assert {:ok, nil} = D.decode(D.nil!(), nil)
# decodes atom
assert {:ok, :test} = D.decode(D.atom(), :test)
end
test "decode/2 should return decode error" do
# fails decoding string
assert {:error, [%{message: "not a string"}]} = D.decode(D.string(), 30)
assert {:error, [%{message: "should match ~r/deaf/"}]} = D.decode(D.string(~r/deaf/), "abc")
assert {:error, [%{message: "should match ~r/deaf/", description: "test"}]} =
D.decode(D.string(~r/deaf/, description: "test"), "abc")
assert {:error, [%{message: "should be longer than 5"}]} =
D.decode(D.string(derive: [:trim, {:min, 5}]), "f")
assert {:error, [%{message: "should be shorter than 5"}]} =
D.decode(D.string(derive: [:trim, {:max, 5}]), "fairly long string")
assert {:error, [%{message: "should not be empty"}]} =
D.decode(D.string(derive: [:trim, :not_empty]), " ")
assert {:error, [%{message: "unknown options" <> _}]} =
D.decode(D.string(extra_props: false), " ")
# fails with custom string format
assert {:error, [%{message: "custom format failed"}]} =
D.decode(
D.string(
derive: [{:format, fn _val, _path -> {:error, "custom format failed"} end}]
),
"value1,value2"
)
assert {:error,
[
%{
message: "error 1"
},
%{message: "error 2"}
]} =
D.decode(
D.string(
derive: [
{:format,
fn _val, _path -> {:error, [{:error, "error 1"}, {:error, "error 2"}]} end}
]
),
"value1,value2"
)
assert {:error,
[
%{
message: "format error: %ArgumentError{message: \"argument error\"}"
}
]} =
D.decode(
D.string(derive: [{:format, fn _val, _path -> raise ArgumentError end}]),
"value1,value2"
)
# fails decoding integer
assert {:error, [%{message: "not an integer"}]} = D.decode(D.integer(), "Bruce")
assert {:error, [%{message: "should be greater than 10"}]} =
D.decode(D.integer(derive: [{:gt, 10}]), 5)
assert {:error, [%{message: "should be lower than 5", description: "test"}]} =
D.decode(D.integer(derive: [{:lt, 5}], description: "test"), 10)
# fails decoding map
assert {:error, [%{path: ["age"], message: "not an integer", description: "test"}]} =
D.decode(
D.map(%{
"name" => D.string(),
"age" => D.integer(description: "test")
}),
%{
"name" => "Bruce",
"age" => "30"
}
)
# fails decoding map in strict mode
assert {:error, [%{message: "extra properties not allowed in strict mode", path: ["extra"]}]} =
D.decode(
D.map(
%{
"name" => D.string(),
"age" => D.integer()
},
strict: true
),
%{
"name" => "Bruce",
"age" => 30,
"extra" => "extra"
}
)
# extra props and strict mode cannot be combined
assert {:error,
[
%{
message:
"unknown options: [:extra_props] allowed [:strict, :derive, :description]"
}
]} =
D.decode(
D.map(
%{
"name" => D.string(),
"age" => D.integer()
},
strict: true,
extra_props: true
),
%{
"name" => "Bruce",
"age" => 30,
"extra" => "extra"
}
)
# fails decoding map in strict mode
assert {:error,
[
%{message: "extra properties not allowed in strict mode", path: ["extra"]}
]} = D.decode(D.map(%{}, strict: true), %{"extra" => "extra"})
# fails decoding map and reports errors recursively
assert {:error,
[
%{path: ["age"], message: "not a string"},
%{path: ["name", "last_name"], message: "not a string", description: "test"}
]} =
D.decode(
D.map(%{
"name" =>
D.map(%{
"first_name" => D.string(),
"last_name" => D.string(description: "test")
}),
"age" => D.string()
}),
%{
"name" => %{
"first_name" => "Bruce",
"last_name" => 0xDEADBEEF
},
"age" => 30
}
)
assert {:error, [%{message: "not a map", description: "test"}]} =
D.decode(D.optional(D.map(%{"k" => D.string()}, description: "test")), 30)
assert {:error,
[
%{message: "map properties should be a map"}
]} =
D.decode(D.map("gibberish"), 30)
assert {:error,
[%{message: "string params should be a regex or a list of options", path: ["name"]}]} ===
D.decode(
D.map(%{
"name" => D.string("gibberish"),
"age" => D.integer()
}),
%{
"name" => "Bruce",
"age" => 30
}
)
assert {:error, [%{message: "should be longer than 3"}]} =
D.decode(
D.map(
%{
"k1" => D.string(),
"k2" => D.string()
},
derive: [{:min, 3}]
),
%{
"k1" => "v1",
"k2" => "v2"
}
)
assert {:error, [%{message: "should be shorter than 2"}]} =
D.decode(
D.map(
%{
"k1" => D.string(),
"k2" => D.string()
},
derive: [{:max, 2}]
),
%{
"k1" => "v1",
"k2" => "v2",
"k3" => "v3"
}
)
assert {:error, [%{message: "should not be empty"}]} =
D.decode(
D.map(%{},
derive: [:not_empty]
),
%{}
)
# fails decoding record
assert {:error, [%{path: ["k2"], message: "not a string"}]} =
D.decode(
D.record(
D.string(),
D.string()
),
%{
"k1" => "v1",
"k2" => 30
}
)
assert {:error,
[
%{
message: "record key and value should be valid schemas"
}
]} =
D.decode(
D.record(
"gibberish",
"gibberish"
),
%{}
)
assert {:error, [%{message: "should be shorter than 1", description: "test"}]} =
D.decode(
D.record(
D.string(),
D.string(),
derive: [{:max, 1}],
description: "test"
),
%{
"k1" => "v1",
"k2" => "v2"
}
)
assert {:error, [%{message: "should be longer than 3"}]} =
D.decode(
D.record(
D.string(),
D.string(),
derive: [{:min, 3}]
),
%{
"k1" => "v1",
"k2" => "v2"
}
)
assert {:error, [%{message: "should not be empty"}]} =
D.decode(
D.record(
D.string(),
D.string(),
derive: [:trim, :not_empty]
),
%{}
)
# fails decoding optionals
assert {:error, [%{message: "not a string"}]} = D.decode(D.optional(D.string()), 30)
assert {:error, [%{message: "not a list"}]} = D.decode(D.optional(D.list(D.string())), 30)
# fails decoding optional with derive
assert {:error, [%{message: "should not be empty", description: "test"}]} =
D.decode(D.optional(D.string(derive: [:not_empty], description: "test")), "")
# fails decoding list
assert {:error, [%{message: "not a list", description: "test"}]} =
D.decode(D.list(D.string(), description: "test"), "Bruce")
# fails decoding nested list
assert {:error, [%{path: [0, "activities", 0], message: "not a string", description: "test"}]} =
D.decode(
D.list(
D.map(%{
"name" => D.string(),
"activities" => D.list(D.string(description: "test"))
})
),
[
%{
"name" => "Bruce Wayne",
"activities" => [30, "detective", "billionaire", "philanthropist"]
}
]
)
# fails decoding tuple
assert {:error,
[
%{
message: "not an integer",
path: [1]
}
]} = D.decode(D.tuple([D.string(), D.integer()]), ["Bruce", "30"])
# fails decoding tuple with wrong schema
assert {:error,
[
%{
message: "tuple items should be a list"
}
]} = D.decode(D.tuple(D.string(), D.integer()), ["Bruce", "30"])
# fails decoding tuple with wrong number of items
assert {:error,
[
%{
message: "tuple length mismatch (actual) 1 != (expected) 2",
description: "test"
}
]} = D.decode(D.tuple([D.string(), D.integer()], description: "test"), ["Bruce"])
assert {:error,
[
%{
message: "tuple length mismatch (actual) 1 != (expected) 2"
}
]} = D.decode(D.tuple([D.string(), D.integer()]), {"Bruce"})
# fails decoding union
assert {:error, [%{message: "not a string"}, %{message: "not an integer"}]} =
D.decode(D.union([D.string(), D.integer()]), nil)
# fails decoding boolean
assert {:error,
[
%{
message: "not a boolean",
description: "test"
}
]} = D.decode(D.boolean(description: "test"), "true")
# fails decoding literal
assert {:error,
[
%{
message: "literal mismatch (actual) \"Wayne\" != (expected) \"Bruce\"",
description: "test"
}
]} =
D.decode(D.literal("Bruce", description: "test"), "Wayne")
# fails decoding nil
assert {:error, [%{message: "not a nil"}]} = D.decode(D.nil!(), "nil")
# fails when unknown options passed
assert {:error, [%{message: "unknown options" <> _}]} =
D.decode(D.string(unknown: 123), "Bruce")
# fails when unknown options passed in nested map
assert {:error, [%{message: "unknown options" <> _}]} =
D.decode(D.map(%{"k1" => D.map(%{"k2" => D.string(unknown: 123)})}), %{
"k1" => %{"k2" => "v2"}
})
# fails decoding date-time
assert {:error, [%{message: "should be a valid date-time"}]} =
D.decode(D.string(derive: [{:format, "date-time"}]), "2023-99-XYT25:61:90")
# fails with unknown options
assert {:error, [%{message: "unknown options: [:unknown] allowed [:derive, :description]"}]} =
D.decode(D.string(unknown: :unknown), "Bruce")
# fails decoding atom
assert {:error, [%{message: "not an atom"}]} = D.decode(D.atom(), "test")
end
end