-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsumo_changeset_SUITE.erl
482 lines (393 loc) · 13.6 KB
/
sumo_changeset_SUITE.erl
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
-module(sumo_changeset_SUITE).
-compile({parse_transform, fancyflow_trans}).
-include_lib("common_test/include/ct.hrl").
-import(sumo_test_utils, [assert_error/2]).
%% Common Test
-export([
all/0,
init_per_suite/1,
end_per_suite/1
]).
%% Test Cases
-export([
t_add_error/1,
t_cast/1,
t_change/1,
t_put_change/1,
t_get_change/1,
t_delete_change/1,
t_apply_changes/1,
t_validate_change/1,
t_validate_required/1,
t_validate_inclusion/1,
t_validate_number/1,
t_validate_length/1,
t_validate_format/1,
t_nested_changeset_validations/1
]).
-define(EXCLUDED_FUNS, [
module_info,
all,
init_per_suite,
end_per_suite
]).
-define(ALLOWED, [
id,
name,
last_name,
age,
address,
height,
description,
status,
birthdate
]).
-define(REQUIRED, [id, name, last_name, age]).
-define(PERSON, #{
id => 1,
last_name => <<"other">>,
age => 33,
height => 1.85,
birthdate => <<"1980-09-22">>,
created_at => {{2012, 2, 16}, {1, 6, 48}},
is_blocked => false,
status => "active"
}).
-type config() :: [{atom(), term()}].
%%%=============================================================================
%%% CT
%%%=============================================================================
-spec all() -> [atom()].
all() ->
Exports = ?MODULE:module_info(exports),
[F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)].
-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(sumo_db),
Config.
-spec end_per_suite(config()) -> config().
end_per_suite(Config) ->
_ = application:stop(sumo_db),
Config.
%%%=============================================================================
%%% Test Cases
%%%=============================================================================
-spec t_add_error(config()) -> ok.
t_add_error(_Config) ->
%% run changeset pipeline adding an error
CS = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), ?PERSON, ?ALLOWED),
sumo_changeset:add_error(_, status, <<"Invalid">>)),
%% validate errors
false = sumo_changeset:is_valid(CS),
1 = length(sumo_changeset:errors(CS)),
_ = validate_cs_errors(CS, [status]),
ok.
-spec t_cast(config()) -> ok.
t_cast(_Config) ->
%% create a person doc
Person = default_person_doc(),
PersonModel = sumo_internal:doc_fields(sumo_internal:from_user_doc(people, Person)),
%% create params to be cast adding some intentional errors
Params = ?PERSON#{age => '33', missing => 1},
Allowed = [missing | ?ALLOWED],
%% run changeset pipeline
ExpectedChanges = #{
birthdate => {{1980, 9, 22}, {0, 0, 0}},
height => 1.85,
id => 1,
last_name => <<"other">>,
status => <<"active">>
},
CS = sumo_changeset:cast(people, Person, Params, Allowed),
_ = validate_cs(CS, #{
schema => people,
store => sumo_test_mnesia,
data => PersonModel,
params => maps:with(Allowed, Params),
changes => ExpectedChanges,
types => {true, fun(M) -> maps:size(M) > 0 end},
required => {[], fun(L) -> L end}
}),
%% validate errors
false = sumo_changeset:is_valid(CS),
1 = length(sumo_changeset:errors(CS)),
_ = validate_cs_errors(CS, [age]),
CS1 = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), #{}, ?ALLOWED),
sumo_changeset:cast(_, #{last_name => <<"other">>}, Allowed)),
%% validate errors
true = sumo_changeset:is_valid(CS1),
0 = length(sumo_changeset:errors(CS1)),
ok.
-spec t_change(config()) -> ok.
t_change(_Config) ->
CS1 = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), #{}, ?ALLOWED),
sumo_changeset:change(_, #{last_name => <<"other">>})),
1 = maps:size(sumo_changeset:changes(CS1)),
CS2 = [pipe](people,
sumo_changeset:change(_, default_person_doc(), #{last_name => <<"Darwin">>}),
sumo_changeset:change(_, #{last_name => <<"other">>})),
1 = maps:size(sumo_changeset:changes(CS2)),
CS3 = [pipe](people,
sumo_changeset:change(_, default_person_doc(), #{last_name => <<"Darwin">>}),
sumo_changeset:cast(_, #{}, ?ALLOWED)),
1 = maps:size(sumo_changeset:changes(CS3)),
ok.
-spec t_put_change(config()) -> ok.
t_put_change(_Config) ->
#{last_name := <<"other">>} = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), #{}, ?ALLOWED),
sumo_changeset:put_change(_, last_name, <<"other">>),
sumo_changeset:changes(_)),
0 = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), #{}, ?ALLOWED),
sumo_changeset:put_change(_, last_name, <<"Doe">>),
sumo_changeset:changes(_),
maps:size(_)),
0 = [pipe](people,
sumo_changeset:cast(_, sumo_test_people:new(<<"other">>, <<"other">>), #{}, ?ALLOWED),
sumo_changeset:put_change(_, last_name, <<"other">>),
sumo_changeset:cast(_, #{last_name => <<"other">>}, ?ALLOWED),
sumo_changeset:put_change(_, last_name, <<"other">>),
sumo_changeset:changes(_),
maps:size(_)),
ok.
-spec t_get_change(config()) -> ok.
t_get_change(_Config) ->
CS1 = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), #{}, ?ALLOWED),
sumo_changeset:put_change(_, last_name, <<"other">>)),
1 = maps:size(sumo_changeset:changes(CS1)),
<<"other">> = sumo_changeset:get_change(CS1, last_name),
undefined = sumo_changeset:get_change(CS1, name),
<<"default">> = sumo_changeset:get_change(CS1, name, <<"default">>),
ok.
-spec t_delete_change(config()) -> ok.
t_delete_change(_Config) ->
CS1 = [pipe](people,
sumo_changeset:cast(_, default_person_doc(), #{}, ?ALLOWED),
sumo_changeset:put_change(_, last_name, <<"other">>)),
1 = maps:size(sumo_changeset:changes(CS1)),
CS2 = sumo_changeset:delete_change(CS1, last_name),
0 = maps:size(sumo_changeset:changes(CS2)),
ok.
-spec t_apply_changes(config()) -> ok.
t_apply_changes(_Config) ->
%% create a person doc
Person = default_person_doc(),
PersonModel = sumo_internal:doc_fields(sumo_internal:from_user_doc(people, Person)),
%% run changeset pipeline
CS1 = sumo_changeset:cast(people, Person, ?PERSON#{missing => 1}, ?ALLOWED),
Data = sumo_changeset:data(CS1),
true = Data == PersonModel,
undefined = sumo_test_people:id(Person),
<<"Doe">> = sumo_test_people:last_name(Person),
undefined = sumo_test_people:age(Person),
%% apply changes
NewData = sumo_changeset:apply_changes(CS1),
false = NewData == PersonModel,
NewPerson = sumo_test_people:sumo_wakeup(NewData),
1 = sumo_test_people:id(NewPerson),
<<"other">> = sumo_test_people:last_name(NewPerson),
33 = sumo_test_people:age(NewPerson),
%% run changeset pipeline
CS2 = sumo_changeset:cast(people, Person, #{}, ?ALLOWED),
0 = maps:size(sumo_changeset:changes(CS2)),
PersonModel = sumo_changeset:apply_changes(CS2),
%% run changeset pipeline
CS3 = [pipe](people,
sumo_changeset:cast(_, Person, #{}, ?ALLOWED),
sumo_changeset:put_change(_, missing, 2)
),
1 = maps:size(sumo_changeset:changes(CS3)),
PersonModel = sumo_changeset:apply_changes(CS3),
ok.
-spec t_validate_change(config()) -> ok.
t_validate_change(_Config) ->
%% create a person doc
Person = default_person_doc(),
%% run changeset pipeline
CS1 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_change(_, age, fun(age, Age) ->
case Age > 30 of
true -> [{age, <<"cannot be greater than 30">>}];
false -> []
end
end)),
%% validate errors
false = sumo_changeset:is_valid(CS1),
[{age, {<<"cannot be greater than 30">>, []}}] = sumo_changeset:errors(CS1),
ok.
-spec t_validate_required(config()) -> ok.
t_validate_required(_Config) ->
%% create a person doc
Person = default_person_doc(),
%% run changeset pipeline
CS1 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_required(_, ?REQUIRED)),
%% validate errors
true = sumo_changeset:is_valid(CS1),
0 = length(sumo_changeset:errors(CS1)),
%% run changeset pipeline
CS2 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON#{age => nil}, ?ALLOWED),
sumo_changeset:validate_required(_, [address | ?REQUIRED])),
%% validate errors
false = sumo_changeset:is_valid(CS2),
2 = length(sumo_changeset:errors(CS2)),
_ = validate_cs_errors(CS2, [address, age]),
%% should fails
_ = assert_error({badarg, invalid}, fun() ->
[pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_required(_, [invalid | ?REQUIRED]))
end),
ok.
-spec t_validate_inclusion(config()) -> ok.
t_validate_inclusion(_Config) ->
%% create a person doc
Person = default_person_doc(),
%% valid statuses
Statuses = [<<"active">>, <<"blocked">>],
%% run changeset pipeline
CS1 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_required(_, ?REQUIRED),
sumo_changeset:validate_inclusion(_, status, Statuses)),
%% validate errors
true = sumo_changeset:is_valid(CS1),
0 = length(sumo_changeset:errors(CS1)),
%% run changeset pipeline
CS2 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON#{status => <<"invalid">>}, ?ALLOWED),
sumo_changeset:validate_required(_, ?REQUIRED),
sumo_changeset:validate_inclusion(_, status, Statuses)),
%% validate errors
false = sumo_changeset:is_valid(CS2),
1 = length(sumo_changeset:errors(CS2)),
_ = validate_cs_errors(CS2, [status]),
ok.
-spec t_validate_number(config()) -> ok.
t_validate_number(_Config) ->
%% create a person doc
Person = default_person_doc(),
%% run changeset pipeline
CS1 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_number(_, age, [
{less_than, 34},
{less_than_or_equal_to, 33},
{greater_than, 32},
{greater_than_or_equal_to, 33},
{equal_to, 33}
])),
%% validate errors
true = sumo_changeset:is_valid(CS1),
0 = length(sumo_changeset:errors(CS1)),
ValidationSet = [
[{less_than, 30}],
[{less_than_or_equal_to, 30}],
[{greater_than, 40}],
[{greater_than_or_equal_to, 40}],
[{equal_to, 30}],
[{less_than, 30}, {equal_to, 30}]
],
_ = lists:foreach(fun(Validations) ->
%% run changeset pipeline
CS = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_number(_, age, Validations)),
%% validate errors
false = sumo_changeset:is_valid(CS),
1 = length(sumo_changeset:errors(CS)),
_ = validate_cs_errors(CS, [age])
end, ValidationSet),
%% should fails
_ = assert_error({badarg, invalid_validation}, fun() ->
[pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_number(_, age, [{invalid_validation, 33}]))
end),
ok.
-spec t_validate_length(config()) -> ok.
t_validate_length(_Config) ->
%% create a person doc
Person = default_person_doc(),
%% run changeset pipeline
CS1 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_length(_, last_name, [{is, 5}, {min, 2}, {max, 10}])),
%% validate errors
true = sumo_changeset:is_valid(CS1),
0 = length(sumo_changeset:errors(CS1)),
ValidationSet = [
[{is, 3}],
[{min, 10}],
[{max, 3}],
[{is, 5}, {min, 2}, {max, 3}]
],
_ = lists:foreach(fun(Validations) ->
%% run changeset pipeline
CS = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_length(_, last_name, Validations)),
%% validate errors
false = sumo_changeset:is_valid(CS),
[{last_name, {_, [{validation, length}]}}] = sumo_changeset:errors(CS)
end, ValidationSet),
ok.
-spec t_validate_format(config()) -> ok.
t_validate_format(_Config) ->
%% create a person doc
Person = default_person_doc(),
%% run changeset pipeline
CS1 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_required(_, ?REQUIRED),
sumo_changeset:validate_format(_, last_name, <<"^oth">>)),
%% validate errors
true = sumo_changeset:is_valid(CS1),
0 = length(sumo_changeset:errors(CS1)),
%% run changeset pipeline
CS2 = [pipe](people,
sumo_changeset:cast(_, Person, ?PERSON, ?ALLOWED),
sumo_changeset:validate_required(_, ?REQUIRED),
sumo_changeset:validate_format(_, last_name, <<"^Doe">>)),
%% validate errors
false = sumo_changeset:is_valid(CS2),
[{last_name, {<<"has invalid format">>, [{validation, format}]}}] = sumo_changeset:errors(CS2),
ok.
-spec t_nested_changeset_validations(config()) -> ok.
t_nested_changeset_validations(_Config) ->
Person = sumo_test_people:new(<<"John">>, <<"Doe">>),
Params = #{age => 33, id => 1, <<"last_name">> => <<"other">>},
_ = [pipe](people,
sumo_changeset:cast(_, Person, Params, ?ALLOWED),
sumo_changeset:validate_required(_, ?REQUIRED),
sumo_changeset:validate_inclusion(_, status, [<<"active">>, <<"blocked">>]),
sumo_changeset:validate_number(_, age, [{less_than_or_equal_to, 33}]),
sumo_changeset:validate_length(_, last_name, [{min, 3}]),
sumo_changeset:validate_format(_, last_name, <<"^oth">>)),
ok.
%%%=============================================================================
%%% Internal functions
%%%=============================================================================
%% @private
default_person_doc() ->
sumo_test_people:new(<<"John">>, <<"Doe">>).
%% @private
validate_cs(CS, ParamsToCheck) ->
maps:fold(fun
(K, {Expected, Fun}, _Acc) when is_function(Fun) ->
Expected = Fun(sumo_changeset:K(CS));
(K, V, _Acc) ->
V = sumo_changeset:K(CS)
end, ok, ParamsToCheck).
%% @private
validate_cs_errors(CS, ErrorKeys) ->
Errors = sumo_changeset:errors(CS),
[true = sumo_utils:is_key(K, Errors) || K <- ErrorKeys].