forked from stablekernel/aqueduct
/
model_test.dart
1154 lines (942 loc) · 27.2 KB
/
model_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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'dart:convert';
import '../not_tests/helpers.dart';
import 'package:conduit_core/conduit_core.dart';
import 'package:test/test.dart';
void main() {
late ManagedContext context;
setUpAll(() {
final ps = DefaultPersistentStore();
final ManagedDataModel dm = ManagedDataModel([
TransientTest,
TransientTypeTest,
User,
Post,
PrivateField,
EnumObject,
TransientBelongsTo,
TransientOwner,
DocumentTest,
ConstructorOverride,
Top,
Middle,
Bottom,
OverrideField
]);
context = ManagedContext(dm, ps);
});
tearDownAll(() async {
await context.close();
});
test("Can set properties in constructor", () {
final obj = ConstructorOverride();
expect(obj.value, "foo");
});
test("Model object construction", () {
final user = User();
user.name = "Joe";
user.id = 1;
expect(user.name, "Joe");
expect(user.id, 1);
});
test("Mismatched type throws exception", () {
final user = User();
try {
user["name"] = 1;
expect(true, false);
} on ValidationException catch (e) {
expectError(e, contains("invalid input value for 'name'"));
}
try {
user["id"] = "foo";
} on ValidationException catch (e) {
expectError(e, contains("invalid input value for 'id'"));
}
});
test("Accessing model object without field should return null", () {
final user = User();
expect(user.name, isNull);
});
test("Can assign and read embedded objects", () {
final user = User();
user.id = 1;
user.name = "Bob";
final List<Post> posts = <Post>[
Post()
..text = "A"
..id = 1
..owner = user,
Post()
..text = "B"
..id = 2
..owner = user,
Post()
..text = "C"
..id = 3
..owner = user,
];
user.posts = ManagedSet<Post>.from(posts);
expect(user.posts!.length, 3);
expect(user.posts!.first.owner, user);
expect(user.posts!.first.text, "A");
expect(posts.first.owner, user);
expect(posts.first.owner!.name, "Bob");
});
test("Can assign null to relationships", () {
final u = User();
u.posts = null;
final p = Post();
p.owner = null;
});
test("Can convert object to map", () {
final user = User();
user.id = 1;
user.name = "Bob";
final posts = [
Post()
..text = "A"
..id = 1,
Post()
..text = "B"
..id = 2,
Post()
..text = "C"
..id = 3,
];
user.posts = ManagedSet<Post>.from(posts);
final m = user.asMap();
expect(m["id"], 1);
expect(m["name"], "Bob");
final mPosts = m["posts"];
expect(mPosts.length, 3);
expect(mPosts.first["id"], 1);
expect(mPosts.first["text"], "A");
expect(mPosts.first["owner"], null);
});
test("Can read from map", () {
final map = {"id": 1, "name": "Bob"};
final postMap = [
{"text": "hey", "id": 1},
{"text": "ho", "id": 2}
];
final user = User();
user.readFromMap(washMap(map));
expect(user.id, 1);
expect(user.name, "Bob");
final posts = postMap.map((e) => Post()..readFromMap(washMap(e))).toList();
expect(posts[0].id, 1);
expect(posts[1].id, 2);
expect(posts[0].text, "hey");
expect(posts[1].text, "ho");
});
test("Reading from map with bad key fails", () {
final map = {"id": 1, "name": "Bob", "bad_key": "value"};
final user = User();
try {
user.readFromMap(washMap(map));
expect(true, false);
} on ValidationException catch (e) {
expectError(e, contains("invalid input key 'bad_key'"));
}
});
test("Reading from map with non-assignable type fails", () {
try {
User().readFromMap(washMap({"id": "foo"}));
expect(true, false);
} on ValidationException catch (e) {
expectError(e, contains("invalid input value for 'id'"));
}
});
test("Handles DateTime conversion", () {
const dateString = "2000-01-01T05:05:05.010Z";
var map = {"id": 1, "name": "Bob", "dateCreated": dateString};
var user = User();
user.readFromMap(washMap(map));
expect(
user.dateCreated!.difference(DateTime.parse(dateString)),
Duration.zero,
);
final remap = user.asMap();
expect(remap["dateCreated"], dateString);
map = {"id": 1, "name": "Bob", "dateCreated": 123};
user = User();
try {
user.readFromMap(washMap(map));
expect(true, false);
} on ValidationException catch (e) {
expectError(e, contains("invalid input value for 'dateCreated'"));
}
});
test(
"Handles input of type num for double precision float properties of the model",
() {
final m = TransientTypeTest()
..readFromMap(
washMap({
"transientDouble": 30,
}),
);
expect(m.transientDouble, 30.0);
});
test("Reads embedded object", () {
final postMap = {
"text": "hey",
"id": 1,
"owner": {"name": "Alex", "id": 18}
};
final post = Post()..readFromMap(washMap(postMap));
expect(post.text, "hey");
expect(post.id, 1);
expect(post.owner!.id, 18);
expect(post.owner!.name, "Alex");
});
test("Trying to read embedded object that isnt an object fails", () {
final postMap = {"text": "hey", "id": 1, "owner": 12};
final post = Post();
var successful = false;
try {
post.readFromMap(washMap(postMap));
successful = true;
} on ValidationException catch (e) {
expectError(e, contains("invalid input type for 'owner'"));
}
expect(successful, false);
});
test("Setting embeded object to null doesn't throw exception", () {
final post = Post()
..id = 4
..owner = (User()
..id = 3
..name = "Alex");
post.owner = null;
expect(post.owner, isNull);
});
test("Setting properties to null is OK", () {
final u = User();
u.name = "Bob";
u.dateCreated = null;
u.name =
null; // I previously set this to a value on purpose and then reset it to null
expect(u.name, isNull);
expect(u.dateCreated, isNull);
});
test("Primary key works", () {
final u = User();
expect(u.entity.primaryKey, "id");
final p = Post();
expect(p.entity.primaryKey, "id");
});
test("Transient properties aren't stored in backing", () {
final t = TransientTest();
t.readFromMap(washMap({"inOut": 2}));
expect(t.inOut, 2);
expect(t["inOut"], isNull);
});
test("mappableInput properties are read in readMap", () {
var t = TransientTest()
..readFromMap(washMap({"id": 1, "defaultedText": "bar foo"}));
expect(t.id, 1);
expect(t.text, "foo");
expect(t.inputInt, isNull);
expect(t.inOut, isNull);
t = TransientTest()..readFromMap(washMap({"inputOnly": "foo"}));
expect(t.text, "foo");
t = TransientTest()..readFromMap(washMap({"inputInt": 2}));
expect(t.inputInt, 2);
t = TransientTest()..readFromMap(washMap({"inOut": 2}));
expect(t.inOut, 2);
t = TransientTest()..readFromMap(washMap({"bothOverQualified": "foo"}));
expect(t.text, "foo");
});
test("mappableOutput properties are emitted in asMap", () {
var t = TransientTest()..text = "foo";
final map = t.asMap();
expect(map["defaultedText"], "Mr. foo");
expect(map["outputOnly"], "foo");
expect(map["bothButOnlyOnOne"], "foo");
expect(map["bothOverQualified"], "foo");
t = TransientTest()..outputInt = 2;
expect(t.asMap()["outputInt"], 2);
t = TransientTest()..inOut = 2;
expect(t.asMap()["inOut"], 2);
});
test("Transient properties are type checked in readMap", () {
try {
TransientTest().readFromMap(washMap({"id": 1, "defaultedText": 2}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
try {
TransientTest().readFromMap(washMap({"id": 1, "inputInt": "foo"}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
});
test("Properties that aren't mappableInput are not read in readMap", () {
try {
TransientTest().readFromMap(washMap({"outputOnly": "foo"}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
try {
TransientTest().readFromMap(washMap({"invalidOutput": "foo"}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
try {
TransientTest().readFromMap(washMap({"invalidInput": "foo"}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
try {
TransientTest().readFromMap(washMap({"bothButOnlyOnOne": "foo"}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
try {
TransientTest().readFromMap(washMap({"outputInt": "foo"}));
throw 'Unreachable';
// ignore: empty_catches
} on ValidationException {}
});
test("mappableOutput properties that are null are not emitted in asMap", () {
final m = (TransientTest()
..id = 1
..text = null)
.asMap();
expect(m.length, 3);
expect(m["id"], 1);
expect(m["text"], null);
expect(m["defaultedText"], "Mr. null");
});
test("Properties that aren't mappableOutput are not emitted in asMap", () {
final m = (TransientTest()
..id = 1
..text = "foo"
..inputInt = 2)
.asMap();
expect(m.length, 6);
expect(m["id"], 1);
expect(m["text"], "foo");
expect(m["defaultedText"], "Mr. foo");
expect(m["outputOnly"], "foo");
expect(m["bothButOnlyOnOne"], "foo");
expect(m["bothOverQualified"], "foo");
});
test("Can remove single property from backing map", () {
final u = (User()
..id = 1
..name = "Bob"
..dateCreated = DateTime(2018, 1, 30))
..removePropertyFromBackingMap("name");
final m = u.asMap();
expect(m.containsKey("id"), true);
expect(m.containsKey("name"), false);
expect(m.containsKey("dateCreated"), true);
});
test("Removing single non-existent property from backing map has no effect",
() {
final u = (User()
..id = 1
..name = "Bob"
..dateCreated = DateTime(2018, 1, 30))
..removePropertyFromBackingMap("dummy");
final m = u.asMap();
expect(m.containsKey("id"), true);
expect(m.containsKey("name"), true);
expect(m.containsKey("dateCreated"), true);
});
test("Can remove multiple properties from backing map", () {
final u = (User()
..id = 1
..name = "Bob"
..dateCreated = DateTime(2018, 1, 30))
..removePropertiesFromBackingMap(["name", "dateCreated"]);
final m = u.asMap();
expect(m.containsKey("id"), true);
expect(m.containsKey("name"), false);
expect(m.containsKey("dateCreated"), false);
});
test("Can remove single property from backing map with multi-property method",
() {
final u = (User()
..id = 1
..name = "Bob"
..dateCreated = DateTime(2018, 1, 30))
..removePropertiesFromBackingMap(["name"]);
final m = u.asMap();
expect(m.containsKey("id"), true);
expect(m.containsKey("name"), false);
expect(m.containsKey("dateCreated"), true);
});
test(
"Removing multiple non-existent properties from backing map has no effect",
() {
final u = (User()
..id = 1
..name = "Bob"
..dateCreated = DateTime(2018, 1, 30))
..removePropertiesFromBackingMap(["dummy1", "dummy2"]);
final m = u.asMap();
expect(m.containsKey("id"), true);
expect(m.containsKey("name"), true);
expect(m.containsKey("dateCreated"), true);
});
test(
"DeepMap Transient Properties of all types can be read and returned",
() {
final m = (TransientTypeTest()
..readFromMap(
washMap({
"deepMap": {
"ok": {"ik1": 1, "ik2": 2}
}
}),
))
.asMap();
expect(m["deepMap"], {
"ok": {"ik1": 1, "ik2": 2}
});
},
skip: "NYI in AOT",
);
test("Transient Properties of all types can be read and returned", () {
const dateString = "2016-10-31T15:40:45+00:00";
final m = (TransientTypeTest()
..readFromMap(
washMap({
"transientInt": 5,
"transientBigInt": 123456789,
"transientString": "lowercase string",
"transientDate": dateString,
"transientBool": true,
"transientDouble": 30.5,
"transientMap": {"key": "value", "anotherKey": "anotherValue"},
"transientList": [1, 2, 3, 4, 5],
"defaultList": [1, "foo"],
"defaultMap": {"key": "value"},
"deepList": [
{"str": "val"},
{"other": "otherval"}
]
}),
))
.asMap();
expect(m["transientInt"], 5);
expect(m["transientBigInt"], 123456789);
expect(m["transientString"], "lowercase string");
expect(
m["transientDate"].difference(DateTime.parse(dateString)),
Duration.zero,
);
expect(m["transientBool"], true);
expect(m["transientDouble"], 30.5);
expect(m["transientList"], [1, 2, 3, 4, 5]);
expect(m["defaultMap"], {"key": "value"});
expect(m["defaultList"], [1, "foo"]);
expect(m["deepList"], [
{"str": "val"},
{"other": "otherval"}
]);
final tm = m["transientMap"];
expect(tm is Map, true);
expect(tm["key"], "value");
expect(tm["anotherKey"], "anotherValue");
});
test(
"If primitive type cannot be parsed into correct type, it fails with validation exception",
() {
try {
TransientTypeTest().readFromMap({"transientInt": "a string"});
fail('unreachable');
// ignore: empty_catches
} on ValidationException {}
});
test(
"If map type cannot be parsed into exact type, it fails with validation exception",
() {
try {
TransientTypeTest().readFromMap({
"deepMap": wash({"str": 1})
});
fail('unreachable');
// ignore: empty_catches
} on ValidationException {}
try {
TransientTypeTest().readFromMap({
"deepMap": wash({
"key": {"str": "val", "int": 2}
})
});
fail('unreachable');
// ignore: empty_catches
} on ValidationException {}
try {
TransientTypeTest().readFromMap({"deepMap": wash("str")});
fail('unreachable');
// ignore: empty_catches
} on ValidationException {}
});
test(
"If complex type cannot be parsed into exact type, it fails with validation exception",
() {
try {
TransientTypeTest().readFromMap({
"deepList": wash(["string"])
});
fail('unreachable');
// ignore: empty_catches
} on ValidationException {}
try {
TransientTypeTest().readFromMap({
"deepList": wash([
{"str": "val"},
"string"
])
});
fail('unreachable');
// ignore: empty_catches
} on ValidationException {}
});
test("Reading hasMany relationship from JSON succeeds", () {
final u = User();
u.readFromMap(
washMap({
"name": "Bob",
"id": 1,
"posts": [
{"text": "Hi", "id": 1}
]
}),
);
expect(u.posts!.length, 1);
expect(u.posts![0].id, 1);
expect(u.posts![0].text, "Hi");
});
test(
"Reading/writing instance property that isn't marked as transient shows up nowhere",
() {
final t = TransientTest();
try {
t.readFromMap(washMap({"notAnAttribute": true}));
expect(true, false);
// ignore: empty_catches
} on ValidationException {}
t.notAnAttribute = "foo";
expect(t.asMap().containsKey("notAnAttribute"), false);
});
test(
"Omit transient properties in asMap when object is a foreign key reference",
() {
final b = TransientBelongsTo()
..id = 1
..owner = (TransientOwner()..id = 1);
expect(b.asMap(), {
"id": 1,
"owner": {"id": 1}
});
});
test("readFromMap correctly invoked for relationships of relationships", () {
final t = Top()
..readFromMap(
washMap({
"id": 1,
"middles": [
{
"id": 2,
"bottom": {"id": 3},
"bottoms": [
{"id": 4},
{"id": 5}
]
}
]
}),
);
expect(t.id, 1);
expect(t.middles.first.id, 2);
expect(t.middles.first.bottom.id, 3);
expect(t.middles.first.bottoms.first.id, 4);
expect(t.middles.first.bottoms.last.id, 5);
});
group("Persistent enum fields", () {
test("Can assign/read enum value to persistent property", () {
final e = EnumObject();
e.enumValues = EnumValues.abcd;
expect(e.enumValues, EnumValues.abcd);
});
test("Enum value in readMap is a matching string", () {
final e = EnumObject()..readFromMap(washMap({"enumValues": "efgh"}));
expect(e.enumValues, EnumValues.efgh);
});
test("Enum value in asMap is a matching string", () {
final e = EnumObject()..enumValues = EnumValues.other18;
expect(e.asMap()["enumValues"], "other18");
});
test(
"Cannot assign value via backingMap or readMap that isn't a valid enum case",
() {
final e = EnumObject();
try {
e.readFromMap(washMap({"enumValues": "foobar"}));
expect(true, false);
} on ValidationException catch (e) {
expectError(e, contains("invalid option for key 'enumValues'"));
}
try {
e["enumValues"] = "foobar";
expect(true, false);
} on ValidationException catch (e) {
expectError(e, contains("invalid input value for 'enumValues'"));
}
});
});
group("Private fields", () {
test("Private fields on entity", () {
final entity = context.dataModel!.entityForType(PrivateField);
expect(entity.attributes["_private"], isNotNull);
});
test("Can get/set value of private field", () {
final p = PrivateField();
p._private = "x";
expect(p._private, "x");
});
test("Can get/set value of private field thru public accessor", () {
final p = PrivateField()..public = "x";
expect(p.public, "x");
expect(p._private, "x");
});
test("Private fields are omitted from asMap()", () {
var p = PrivateField()..public = "x";
expect(p.asMap(), {"public": "x"});
p = PrivateField().._private = "x";
expect(p.asMap(), {"public": "x"});
});
test("Private fields cannot be set in readFromMap()", () {
final p = PrivateField();
try {
p.readFromMap(washMap({"_private": "x"}));
fail('unreachable');
} on ValidationException catch (e) {
expect(e.toString(), contains("invalid input"));
}
expect(p.public, isNull);
expect(p._private, isNull);
});
});
group("Document data type", () {
test("Can read object into document data type from map", () {
final o = DocumentTest();
o.readFromMap(
washMap({
"document": {"key": "value"}
}),
);
expect(o.document.data, {"key": "value"});
});
test("Can read array into document data type from list", () {
final o = DocumentTest();
o.readFromMap(
washMap({
"document": [
{"key": "value"},
1
]
}),
);
expect(o.document.data, [
{"key": "value"},
1
]);
});
test("Can emit object into map from object document data type", () {
final o = DocumentTest()..document = Document({"key": "value"});
expect(o.asMap(), {
"document": {"key": "value"}
});
});
test("Can emit array into map from array document data type", () {
final o = DocumentTest()
..document = Document([
{"key": "value"},
1
]);
expect(o.asMap(), {
"document": [
{"key": "value"},
1
]
});
});
});
test("Can have constructor with only optional args", () {
final dm = ManagedDataModel([DefaultConstructorHasOptionalArgs]);
final _ = ManagedContext(dm, DefaultPersistentStore());
final instance =
dm.entityForType(DefaultConstructorHasOptionalArgs).instanceOf();
expect(instance is DefaultConstructorHasOptionalArgs, true);
});
}
class User extends ManagedObject<_User> implements _User {
@Serialize()
String? value;
}
class _User {
@Column(nullable: true)
String? name;
@Column(primaryKey: true)
int? id;
DateTime? dateCreated;
ManagedSet<Post>? posts;
}
class Post extends ManagedObject<_Post> implements _Post {}
class _Post {
@primaryKey
int? id;
String? text;
@Relate(Symbol('posts'))
User? owner;
}
class TransientTest extends ManagedObject<_TransientTest>
implements _TransientTest {
String? notAnAttribute;
@Serialize(input: false, output: true)
String get defaultedText => "Mr. $text";
@Serialize(input: true, output: false)
set defaultedText(String str) {
text = str.split(" ").last;
}
@Serialize(input: true, output: false)
set inputOnly(String s) {
text = s;
}
@Serialize(input: false, output: true)
String? get outputOnly => text;
set outputOnly(String? s) {
text = s;
}
// This is intentionally invalid
@Serialize(input: true, output: false)
String? get invalidInput => text;
// This is intentionally invalid
@Serialize(input: false, output: true)
set invalidOutput(String s) {
text = s;
}
@Serialize()
String? get bothButOnlyOnOne => text;
set bothButOnlyOnOne(String? s) {
text = s;
}
@Serialize(input: true, output: false)
int? inputInt;
@Serialize(input: false, output: true)
int? outputInt;
@Serialize()
int? inOut;
@Serialize()
String? get bothOverQualified => text;
@Serialize()
set bothOverQualified(String? s) {
text = s;
}
}
class _TransientTest {
@primaryKey
int? id;
String? text;
}
class TransientTypeTest extends ManagedObject<_TransientTypeTest>
implements _TransientTypeTest {
@Serialize(input: false, output: true)
int get transientInt => backingInt + 1;
@Serialize(input: true, output: false)
set transientInt(int i) {
backingInt = i - 1;
}
@Serialize(input: false, output: true)
int get transientBigInt => backingBigInt ~/ 2;
@Serialize(input: true, output: false)
set transientBigInt(int i) {
backingBigInt = i * 2;
}
@Serialize(input: false, output: true)
String get transientString => backingString.toLowerCase();
@Serialize(input: true, output: false)
set transientString(String s) {
backingString = s.toUpperCase();
}
@Serialize(input: false, output: true)
DateTime get transientDate => backingDateTime.add(const Duration(days: 1));
@Serialize(input: true, output: false)
set transientDate(DateTime d) {
backingDateTime = d.subtract(const Duration(days: 1));
}
@Serialize(input: false, output: true)
bool get transientBool => !backingBool;
@Serialize(input: true, output: false)
set transientBool(bool b) {
backingBool = !b;
}
@Serialize(input: false, output: true)
double get transientDouble => backingDouble / 5;
@Serialize(input: true, output: false)
set transientDouble(double d) {
backingDouble = d * 5;
}
@Serialize(input: false, output: true)
Map<String, String> get transientMap {
final List<String> pairs = backingMapString.split(",");
final returnMap = <String, String>{};
for (final pair in pairs) {
final List<String> pairList = pair.split(":");
returnMap[pairList[0]] = pairList[1];
}
return returnMap;
}
@Serialize(input: true, output: false)
set transientMap(Map<String, String> m) {
final pairStrings = m.keys.map((key) {
final String? value = m[key];
return "$key:$value";
});
backingMapString = pairStrings.join(",");
}
@Serialize(input: false, output: true)
List<int> get transientList {
return backingListString.split(",").map(int.parse).toList();
}
@Serialize(input: true, output: false)
set transientList(List<int> l) {
backingListString = l.map((i) => i.toString()).join(",");
}
@Serialize()
List<Map<String, dynamic>>? deepList;
@Serialize()
Map<String, Map<String, int>>? deepMap;
@Serialize()
Map<String, dynamic>? defaultMap;
@Serialize()
List<dynamic>? defaultList;
}
class _TransientTypeTest {
// All of the types - ManagedPropertyType
@primaryKey
int? id;
late int backingInt;
@Column(databaseType: ManagedPropertyType.bigInteger)
late int backingBigInt;
late String backingString;
late DateTime backingDateTime;
late bool backingBool;