Skip to content

Commit c9b4b9f

Browse files
committed
ToMany with json_serializable & freezed - generator integration tests
1 parent 18d67ee commit c9b4b9f

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

generator/integration-tests/part-partof/1.dart

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,28 @@ void main() {
1919
group('package:JsonSerializable', () {
2020
setupTestsFor(JsonEntity(id: 0, str: 'foo', date: DateTime.now()));
2121
setupRelTestsFor(JsonBook.fromJson({
22-
'author': {'name': 'Charles'}
22+
'author': {'name': 'Charles'},
23+
'readers': [
24+
{'name': 'Emily'},
25+
{'name': 'Diana'}
26+
]
2327
}));
2428
});
2529

2630
group('package:Freezed', () {
2731
setupTestsFor(FrozenEntity(id: 1, str: 'foo', date: DateTime.now()));
2832
final author = FrozenPerson(id: 1, name: 'Charles');
33+
final readers = [
34+
FrozenPerson(id: 2, name: 'Emily'),
35+
FrozenPerson(id: 3, name: 'Diana')
36+
];
2937
setupRelTestsFor(
30-
FrozenBook(id: 1, author: ToOne<FrozenPerson>(target: author)), author);
38+
FrozenBook(
39+
id: 1,
40+
author: ToOne(target: author),
41+
readers: ToMany(items: readers)),
42+
(Store store) =>
43+
store.box<FrozenPerson>().putMany([author, ...readers]));
3144
});
3245
}
3346

@@ -44,23 +57,23 @@ void setupTestsFor<EntityT>(EntityT newObject) {
4457
});
4558
}
4659

47-
void setupRelTestsFor<BookEntityT, PersonEntityT>(BookEntityT book,
48-
[PersonEntityT? author]) {
60+
void setupRelTestsFor<BookEntityT>(BookEntityT book,
61+
[void Function(Store)? init]) {
4962
group(BookEntityT.toString(), () {
5063
late TestEnv<BookEntityT> env;
5164
setUp(() => env = TestEnv(getObjectBoxModel()));
5265
tearDown(() => env.close());
5366

5467
test('relations', () {
55-
if (author != null) {
56-
env.store.box<PersonEntityT>().put(author);
57-
(book as dynamic).author.target = author;
58-
}
68+
if (init != null) init(env.store);
5969
env.box.put(book);
6070

6171
final bookRead = env.box.get(1)! as dynamic;
6272
expect(bookRead.author.targetId, 1);
6373
expect(bookRead.author.target!.name, 'Charles');
74+
75+
expect(bookRead.readers[0]!.name, 'Emily');
76+
expect(bookRead.readers[1]!.name, 'Diana');
6477
});
6578
});
6679
}

generator/integration-tests/part-partof/lib/frozen.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ class FrozenBook with _$FrozenBook {
2525
@Entity(realClass: FrozenBook)
2626
factory FrozenBook(
2727
{@Id(assignable: true) required int id,
28-
required ToOne<FrozenPerson> author}) = _FrozenBook;
28+
required ToOne<FrozenPerson> author,
29+
required ToMany<FrozenPerson> readers}) = _FrozenBook;
2930
}

generator/integration-tests/part-partof/lib/json.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ class JsonBook {
4040
@_PersonRelToOneConverter()
4141
final ToOne<JsonPerson> author;
4242

43-
JsonBook({required this.author});
43+
@_PersonRelToManyConverter()
44+
final ToMany<JsonPerson> readers;
4445

45-
factory JsonBook.fromJson(Map<String, dynamic> json) => _$JsonBookFromJson(json);
46+
JsonBook({required this.author, required this.readers});
47+
48+
factory JsonBook.fromJson(Map<String, dynamic> json) =>
49+
_$JsonBookFromJson(json);
4650

4751
Map<String, dynamic> toJson() => _$JsonBookToJson(this);
4852
}
@@ -58,3 +62,19 @@ class _PersonRelToOneConverter
5862
@override
5963
Map<String, dynamic>? toJson(ToOne<JsonPerson> rel) => rel.target?.toJson();
6064
}
65+
66+
class _PersonRelToManyConverter
67+
implements JsonConverter<ToMany<JsonPerson>, List<Map<String, dynamic>>?> {
68+
const _PersonRelToManyConverter();
69+
70+
@override
71+
ToMany<JsonPerson> fromJson(List<Map<String, dynamic>>? json) =>
72+
ToMany<JsonPerson>(
73+
items: json == null
74+
? null
75+
: json.map((e) => JsonPerson.fromJson(e)).toList());
76+
77+
@override
78+
List<Map<String, dynamic>>? toJson(ToMany<JsonPerson> rel) =>
79+
rel.map((JsonPerson obj) => obj.toJson()).toList();
80+
}

0 commit comments

Comments
 (0)