forked from stablekernel/aqueduct
/
validate_value_test.dart
632 lines (508 loc) · 16.1 KB
/
validate_value_test.dart
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import '../not_tests/helpers.dart';
import 'package:conduit_core/conduit_core.dart';
import 'package:test/test.dart';
void main() {
final ctx = ManagedContext(
ManagedDataModel([
T,
U,
V,
EnumObject,
Constant,
ConstantRef,
FK,
Parent,
PresenceHas,
PresenceBelongsTo,
AbsenceBelongsTo,
AbsenceHas,
NonDefaultPK,
MultiValidate
]),
DefaultPersistentStore(),
);
tearDownAll(() async {
await ctx.close();
});
group("Primary key defaults", () {
test("@primaryKey defaults to using Validate.constant()", () {
final t = T()..id = 1;
expect(t.validate().isValid, true);
expect(t.validate(forEvent: Validating.update).isValid, false);
});
test(
"A primary key that isn't @primaryKey does not have Validate.constant()",
() {
final t = NonDefaultPK()..id = 1;
expect(t.validate().isValid, true);
expect(t.validate(forEvent: Validating.update).isValid, true);
});
});
group("Foreign keys", () {
test("Validator applies to foreign key value", () {
final fk = FK();
fk.parent = Parent()..id = 1;
expect(fk.validate().isValid, false);
expect(fk.validate().errors.first, contains("FK.parent.id"));
fk.parent.id = 2;
expect(fk.validate().isValid, true);
});
test("If foreign key object is null, validator is not run", () {
final fk = FK();
expect(fk.validate().isValid, true);
});
test(
"If foreign key object doesn't contain primary key, validator is not run",
() {
final fk = FK();
fk.parent = Parent();
expect(fk.validate().isValid, true);
});
test(
"If primary key has a validator, it is not run when evaluated as a foreign key",
() {
final fk = FK();
fk.parent = Parent()..id = 10;
expect(fk.validate().isValid, true);
expect(fk.parent.validate().isValid, false);
expect(fk.parent.validate().errors.first, contains("Parent.id"));
});
});
group("Validate.matches", () {
test("Valid regex reports match", () {
final t = T()
..regex = "OIASDJKASD"
..contain = "XcontainY";
expect(t.validate().isValid, true);
});
test("Invalid regex reports failure", () {
var t = T()..regex = "OiASDJKASD";
expect(t.validate().isValid, false);
t = T()..contain = "abcde";
expect(t.validate().isValid, false);
});
});
group("Validate.compare", () {
test("lessThan/int", () {
final t = T()..compareIntLessThan1 = 0;
expect(t.validate().isValid, true);
t.compareIntLessThan1 = 1;
expect(t.validate().isValid, false);
t.compareIntLessThan1 = 10;
expect(t.validate().isValid, false);
});
test("lessThanEqual/string", () {
final t = T()..compareStringLessThanEqualToBar = "abc";
expect(t.validate().isValid, true);
t.compareStringLessThanEqualToBar = "bar";
expect(t.validate().isValid, true);
t.compareStringLessThanEqualToBar = "baz";
expect(t.validate().isValid, false);
});
test("greaterThan/double", () {
final t = T()..compareDoubleGreaterThan1 = 2.0;
expect(t.validate().isValid, true);
t.compareDoubleGreaterThan1 = 1.0;
expect(t.validate().isValid, false);
t.compareDoubleGreaterThan1 = 0.0;
expect(t.validate().isValid, false);
});
test("greaterThanEqual/date", () {
final t = T()..compareDateGreaterThanEqualTo1990 = DateTime.utc(2000);
expect(t.validate().isValid, true);
t.compareDateGreaterThanEqualTo1990 = DateTime.utc(1990);
expect(t.validate().isValid, true);
t.compareDateGreaterThanEqualTo1990 = DateTime.utc(1980);
expect(t.validate().isValid, false);
});
test("equal", () {
final t = T()..compareIntEqualTo5 = 5;
expect(t.validate().isValid, true);
t.compareIntEqualTo5 = 4;
expect(t.validate().isValid, false);
});
test("Combine two yields and of both", () {
final t = T()..compareIntBetween6And10 = 6;
expect(t.validate().isValid, true);
t.compareIntBetween6And10 = 10;
expect(t.validate().isValid, true);
t.compareIntBetween6And10 = 11;
expect(t.validate().isValid, false);
t.compareIntBetween6And10 = 5;
expect(t.validate().isValid, false);
});
});
group("Validate.length", () {
test("lessThan", () {
final t = T()..lengthLessThan5 = "abc";
expect(t.validate().isValid, true);
t.lengthLessThan5 = "abcde";
expect(t.validate().isValid, false);
t.lengthLessThan5 = "abcdefgh";
expect(t.validate().isValid, false);
});
test("lessThanEqual", () {
final t = T()..lengthLessThanEqualTo5 = "abc";
expect(t.validate().isValid, true);
t.lengthLessThanEqualTo5 = "abcde";
expect(t.validate().isValid, true);
t.lengthLessThanEqualTo5 = "abcdefg";
expect(t.validate().isValid, false);
});
test("greaterThan", () {
final t = T()..lengthGreaterThan5 = "abcdefghi";
expect(t.validate().isValid, true);
t.lengthGreaterThan5 = "abcde";
expect(t.validate().isValid, false);
t.lengthGreaterThan5 = "abc";
expect(t.validate().isValid, false);
});
test("greaterThanEqual", () {
final t = T()..lengthGreaterThanEqualTo5 = "abcdefgh";
expect(t.validate().isValid, true);
t.lengthGreaterThanEqualTo5 = "abcde";
expect(t.validate().isValid, true);
t.lengthGreaterThanEqualTo5 = "abc";
expect(t.validate().isValid, false);
});
test("equal", () {
final t = T()..lengthEqualTo2 = "ab";
expect(t.validate().isValid, true);
t.lengthEqualTo2 = "c";
expect(t.validate().isValid, false);
});
test("Combine two yields and of both", () {
final t = T()..lengthBetween6And10 = "abcdef";
expect(t.validate().isValid, true);
t.lengthBetween6And10 = "abcdefghij";
expect(t.validate().isValid, true);
t.lengthBetween6And10 = "abcdefghijk";
expect(t.validate().isValid, false);
t.lengthBetween6And10 = "abcde";
expect(t.validate().isValid, false);
});
});
group("Validate.present", () {
test("Ensures key exists", () {
final u = U();
expect(u.validate().isValid, false);
u.present = 1;
expect(u.validate().isValid, true);
});
test("Does not care about null, as long as present", () {
final u = U()..present = null;
expect(u.validate().isValid, true);
});
test("Relationship primary key must be present", () {
final o = PresenceBelongsTo();
expect(o.validate().isValid, false);
o.present = PresenceHas();
expect(o.validate().isValid, false);
o.present = PresenceHas()..id = 1;
expect(o.validate().isValid, true);
});
test("Ensure foreign object key exists", () {
final fk = PresenceBelongsTo();
expect(fk.validate().isValid, false);
});
test("If foreign key object is null, validator fails", () {
final fk = PresenceBelongsTo()..present = null;
expect(fk.validate().isValid, false);
});
test("If foreign key object doesn't contain primary key, validator fails",
() {
final fk = PresenceBelongsTo()..present = PresenceHas();
expect(fk.validate().isValid, false);
expect(
fk.validate().errors.first,
contains("PresenceBelongsTo.present.id"),
);
});
});
group("Validate.absent", () {
test("Ensures key is absent", () {
final u = U()..present = 1;
expect(u.validate().isValid, true);
u.absent = 1;
expect(u.validate().isValid, false);
});
test("Does not treat null as absent", () {
final u = U()
..present = 1
..absent = null;
expect(u.validate().isValid, false);
});
test("Relationship key must be absent", () {
final o = AbsenceBelongsTo();
expect(o.validate().isValid, true);
o.absent = AbsenceHas();
expect(o.validate().isValid, false);
expect(o.validate().errors.first, contains("AbsenceBelongsTo.absent.id"));
o.absent = AbsenceHas()..id = 1;
expect(o.validate().isValid, false);
expect(o.validate().errors.first, contains("AbsenceBelongsTo.absent.id"));
});
});
group("Validate.oneOf", () {
test("Works with String", () {
final t = T()..oneOfAB = "A";
expect(t.validate().isValid, true);
t.oneOfAB = "C";
expect(t.validate().isValid, false);
});
test("Works with int", () {
final t = T()..oneOf12 = 1;
expect(t.validate().isValid, true);
t.oneOf12 = 3;
expect(t.validate().isValid, false);
});
test("Implicitly added to enum types", () {
final e = EnumObject()..backing.contents!["enumValues"] = "foobar";
expect(e.validate().isValid, false);
e.enumValues = EnumValues.abcd;
expect(e.validate().isValid, true);
});
});
group("Validate.constant", () {
test("Allows attributes during insert, not update", () {
final t = Constant()..constantString = "A";
expect(t.validate().isValid, true);
expect(t.validate(forEvent: Validating.update).isValid, false);
});
test("Allows relationships during insert, not update", () {
final t = Constant()
..constantRef = ConstantRef()
..id = 1;
expect(t.validate().isValid, true);
expect(t.validate(forEvent: Validating.update).isValid, false);
});
});
group("Operation", () {
test("Specify update only, only runs on update", () {
final t = T()..mustBeZeroOnUpdate = 10;
expect(ManagedValidator.run(t).isValid, true);
t.mustBeZeroOnUpdate = 10;
expect(ManagedValidator.run(t, event: Validating.update).isValid, false);
t.mustBeZeroOnUpdate = 0;
expect(ManagedValidator.run(t, event: Validating.update).isValid, true);
});
test("Specify insert only, only runs on insert", () {
final t = T()..mustBeZeroOnInsert = 10;
expect(ManagedValidator.run(t, event: Validating.update).isValid, true);
t.mustBeZeroOnInsert = 10;
expect(ManagedValidator.run(t).isValid, false);
t.mustBeZeroOnInsert = 0;
expect(ManagedValidator.run(t).isValid, true);
});
test("More than one matcher", () {
final t = T()..mustBeBayOrBaz = "bay";
expect(t.validate().isValid, true);
t.mustBeBayOrBaz = "baz";
expect(t.validate().isValid, true);
t.mustBeBayOrBaz = "bar";
expect(t.validate().isValid, false);
t.mustBeBayOrBaz = "baa";
expect(t.validate().isValid, false);
});
test("ManagedObject can provide add'l validations by overriding validate",
() async {
final v = V()..aOrbButReallyOnlyA = "a";
expect(v.validate().isValid, true);
v.aOrbButReallyOnlyA = "b";
expect(v.validate().isValid, false);
});
});
group("Custom validator verify", () {
test("Custom validator correctly validates a value", () {
final t = T()..mustByXYZ = "XYZ";
expect(t.validate().isValid, true);
t.mustByXYZ = "not";
expect(t.validate().isValid, false);
});
});
test("Can combine both metadata and arguments to Column", () {
final x = MultiValidate()..canOnlyBe4 = 3;
expect(x.validate().isValid, false);
x.canOnlyBe4 = 5;
expect(x.validate().isValid, false);
x.canOnlyBe4 = 4;
expect(x.validate().isValid, true);
final allValidators = x.entity.attributes['canOnlyBe4']!.validators;
expect(allValidators.length, 4);
});
}
class T extends ManagedObject<_T> implements _T {}
class _T {
@primaryKey
int? id;
@Validate.matches(r"^[A-Z]+$")
String? regex;
@Validate.matches("contain")
String? contain;
@Validate.compare(lessThan: 1)
int? compareIntLessThan1;
@Validate.compare(greaterThan: 1.0)
double? compareDoubleGreaterThan1;
@Validate.compare(lessThanEqualTo: "bar")
String? compareStringLessThanEqualToBar;
@Validate.compare(greaterThanEqualTo: "1990-01-01T00:00:00Z")
DateTime? compareDateGreaterThanEqualTo1990;
@Validate.compare(equalTo: 5)
int? compareIntEqualTo5;
@Validate.compare(lessThan: 11, greaterThan: 5)
int? compareIntBetween6And10;
@Validate.length(lessThan: 5)
String? lengthLessThan5;
@Validate.length(lessThanEqualTo: 5)
String? lengthLessThanEqualTo5;
@Validate.length(greaterThan: 5)
String? lengthGreaterThan5;
@Validate.length(greaterThanEqualTo: 5)
String? lengthGreaterThanEqualTo5;
@Validate.length(equalTo: 2)
String? lengthEqualTo2;
@Validate.length(lessThan: 11, greaterThan: 5)
String? lengthBetween6And10;
@Validate.oneOf(["A", "B"])
String? oneOfAB;
@Validate.oneOf([1, 2])
int? oneOf12;
@Validate.compare(equalTo: 0, onInsert: true, onUpdate: false)
int? mustBeZeroOnInsert;
@Validate.compare(equalTo: 0, onInsert: false, onUpdate: true)
int? mustBeZeroOnUpdate;
@Validate.compare(greaterThan: "bar")
@Validate.oneOf(["baa", "bar", "bay", "baz"])
String? mustBeBayOrBaz;
@CustomValidate()
String? mustByXYZ;
}
class U extends ManagedObject<_U> implements _U {}
class _U {
@primaryKey
int? id;
@Validate.present()
int? present;
@Validate.absent()
int? absent;
}
class CustomValidate extends Validate {
const CustomValidate({super.onUpdate, super.onInsert});
@override
void validate(ValidationContext context, dynamic input) {
if (input != "XYZ") {
context.addError("not XYZ");
}
}
}
class V extends ManagedObject<_V> implements _V {
@override
ValidationContext validate({Validating forEvent = Validating.insert}) {
final context = super.validate(forEvent: forEvent);
if (aOrbButReallyOnlyA == "b") {
context.addError("can't be b");
}
return context;
}
}
class Constant extends ManagedObject<_Constant> implements _Constant {}
class _Constant {
@primaryKey
int? id;
@Validate.constant()
String? constantString;
@Validate.constant()
@Relate(#constant)
ConstantRef? constantRef;
}
class ConstantRef extends ManagedObject<_ConstantRef> implements _ConstantRef {}
class _ConstantRef {
@primaryKey
int? id;
Constant? constant;
}
class _V {
@primaryKey
int? id;
@Validate.oneOf(["a", "b"])
String? aOrbButReallyOnlyA;
}
class EnumObject extends ManagedObject<_EnumObject> implements _EnumObject {}
class _EnumObject {
@primaryKey
int? id;
EnumValues? enumValues;
}
enum EnumValues { abcd, efgh, other18 }
class FK extends ManagedObject<_FK> implements _FK {}
class _FK {
@primaryKey
int? id;
@Validate.compare(greaterThan: 1)
@Relate(#fk)
late Parent parent;
}
class Parent extends ManagedObject<_Parent> implements _Parent {}
class _Parent {
@Validate.compare(greaterThan: 100)
@primaryKey
int? id;
FK? fk;
}
class PresenceHas extends ManagedObject<_PresenceHas> implements _PresenceHas {}
class _PresenceHas {
@primaryKey
int? id;
ManagedSet<PresenceBelongsTo>? present;
}
class PresenceBelongsTo extends ManagedObject<_PresenceBelongsTo>
implements _PresenceBelongsTo {}
class _PresenceBelongsTo {
@primaryKey
int? id;
@Validate.present(onInsert: true)
@Relate(#present)
PresenceHas? present;
}
class AbsenceHas extends ManagedObject<_AbsenceHas> implements _AbsenceHas {}
class _AbsenceHas {
@primaryKey
int? id;
ManagedSet<AbsenceBelongsTo>? absent;
}
class AbsenceBelongsTo extends ManagedObject<_AbsenceBelongsTo>
implements _AbsenceBelongsTo {}
class _AbsenceBelongsTo {
@primaryKey
int? id;
@Validate.absent(onInsert: true)
@Relate(#absent)
AbsenceHas? absent;
}
class NonDefaultPK extends ManagedObject<_NonDefaultPK>
implements _NonDefaultPK {}
class _NonDefaultPK {
@Column(
primaryKey: true,
databaseType: ManagedPropertyType.bigInteger,
autoincrement: true,
)
int? id;
String? name;
}
class MultiValidate extends ManagedObject<_MultiValidate>
implements _MultiValidate {}
const validateReference = Validate.compare(lessThan: 100);
class _MultiValidate {
@primaryKey
int? id;
@validateReference
@Validate.compare(lessThan: 5)
@Column(
validators: [
Validate.compare(greaterThan: 3),
Validate.compare(equalTo: 4)
],
)
int? canOnlyBe4;
}