Skip to content

Commit 54552a4

Browse files
committed
test(publications): add comprehensive tests for related publications
- Test fromEntity preserves relatedPublications - Test fromJson correctly parses related publications - Test fromJson handles missing related field - Test toJson includes related publications - Test copyWith updates relatedPublications - All 6 tests pass successfully
1 parent fe4ca75 commit 54552a4

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

packages/stadata_flutter_sdk/test/src/features/publications/data/models/publication_model_test.dart

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:stadata_flutter_sdk/src/core/core.dart';
23
import 'package:stadata_flutter_sdk/src/features/features.dart';
34

45
void main() {
@@ -20,5 +21,190 @@ void main() {
2021

2122
expect(expected, isA<PublicationModel>());
2223
});
24+
25+
test(
26+
'fromEntity should preserve relatedPublications from entity',
27+
() {
28+
final relatedPub = RelatedPublication(
29+
id: 'related_id_123',
30+
title: 'Related Publication Title',
31+
releaseDate: DateTime.parse('2023-01-15'),
32+
url: 'https://example.com/publication',
33+
cover: 'https://example.com/cover.jpg',
34+
);
35+
36+
final entity = Publication(
37+
id: '219dajkda3108',
38+
title: 'Mock Publication',
39+
issn: 'issn',
40+
cover: 'cover',
41+
pdf: 'pdf',
42+
size: '2Mb',
43+
abstract: 'abstract',
44+
catalogueNumber: 'catalogue number',
45+
publicationNumber: 'publication number',
46+
relatedPublications: [relatedPub],
47+
);
48+
49+
final model = PublicationModel.fromEntity(entity);
50+
51+
expect(model, isA<PublicationModel>());
52+
expect(model.relatedPublications, hasLength(1));
53+
expect(model.relatedPublications.first.id, equals('related_id_123'));
54+
expect(
55+
model.relatedPublications.first.title,
56+
equals('Related Publication Title'),
57+
);
58+
},
59+
);
60+
61+
test('fromJson should correctly parse related publications', () {
62+
final json = <String, dynamic>{
63+
'pub_id': 'pub_123',
64+
'title': 'Test Publication',
65+
'issn': '1234-5678',
66+
'cover': 'https://example.com/cover.jpg',
67+
'pdf': 'https://example.com/publication.pdf',
68+
'size': '2.5 MB',
69+
'abstract': 'This is a test publication',
70+
'kat_no': 'CAT-001',
71+
'pub_no': 'PUB-001',
72+
'related': [
73+
{
74+
'pub_id': 'related_1',
75+
'title': 'Related Publication 1',
76+
'rl_date': '2023-01-15',
77+
'url': 'https://example.com/related1',
78+
'cover': 'https://example.com/related1_cover.jpg',
79+
},
80+
{
81+
'pub_id': 'related_2',
82+
'title': 'Related Publication 2',
83+
'rl_date': '2023-02-20',
84+
'url': 'https://example.com/related2',
85+
'cover': 'https://example.com/related2_cover.jpg',
86+
},
87+
],
88+
};
89+
90+
final model = PublicationModel.fromJson(json);
91+
92+
expect(model, isA<PublicationModel>());
93+
expect(model.id, equals('pub_123'));
94+
expect(model.title, equals('Test Publication'));
95+
96+
// Verify related publications
97+
expect(model.relatedPublications, hasLength(2));
98+
expect(model.relatedPublications[0].id, equals('related_1'));
99+
expect(
100+
model.relatedPublications[0].title,
101+
equals('Related Publication 1'),
102+
);
103+
expect(model.relatedPublications[1].id, equals('related_2'));
104+
expect(
105+
model.relatedPublications[1].title,
106+
equals('Related Publication 2'),
107+
);
108+
});
109+
110+
test('fromJson should handle missing related field', () {
111+
final json = <String, dynamic>{
112+
'pub_id': 'pub_123',
113+
'title': 'Test Publication',
114+
'issn': '1234-5678',
115+
'cover': 'https://example.com/cover.jpg',
116+
'pdf': 'https://example.com/publication.pdf',
117+
'size': '2.5 MB',
118+
'abstract': 'This is a test publication',
119+
'kat_no': 'CAT-001',
120+
'pub_no': 'PUB-001',
121+
};
122+
123+
final model = PublicationModel.fromJson(json);
124+
125+
expect(model, isA<PublicationModel>());
126+
expect(model.relatedPublications, isEmpty);
127+
});
128+
129+
test('toJson should include related publications', () {
130+
final model = PublicationModel(
131+
id: 'pub_123',
132+
title: 'Test Publication',
133+
issn: '1234-5678',
134+
cover: 'https://example.com/cover.jpg',
135+
pdf: 'https://example.com/publication.pdf',
136+
size: '2.5 MB',
137+
abstract: 'This is a test publication',
138+
catalogueNumber: 'CAT-001',
139+
publicationNumber: 'PUB-001',
140+
relatedPublications: [
141+
RelatedPublication(
142+
id: 'related_1',
143+
title: 'Related Publication 1',
144+
releaseDate: DateTime.parse('2023-01-15'),
145+
url: 'https://example.com/related1',
146+
cover: 'https://example.com/related1_cover.jpg',
147+
),
148+
],
149+
);
150+
151+
final json = model.toJson();
152+
153+
expect(json, isA<JSON>());
154+
expect(json['related'], isNotNull);
155+
expect(json['related'], isA<List<dynamic>>());
156+
expect(json['related'] as List<dynamic>, hasLength(1));
157+
158+
final relatedJson = (json['related'] as List<dynamic>)[0] as JSON;
159+
expect(relatedJson['pub_id'], equals('related_1'));
160+
expect(relatedJson['title'], equals('Related Publication 1'));
161+
expect(relatedJson['rl_date'], equals('2023-01-15T00:00:00.000'));
162+
});
163+
164+
test('copyWith should update relatedPublications', () {
165+
final original = PublicationModel(
166+
id: 'pub_123',
167+
title: 'Test Publication',
168+
issn: '1234-5678',
169+
cover: 'https://example.com/cover.jpg',
170+
pdf: 'https://example.com/publication.pdf',
171+
size: '2.5 MB',
172+
abstract: 'This is a test publication',
173+
catalogueNumber: 'CAT-001',
174+
publicationNumber: 'PUB-001',
175+
relatedPublications: [
176+
RelatedPublication(
177+
id: 'related_1',
178+
title: 'Related Publication 1',
179+
releaseDate: DateTime.parse('2023-01-15'),
180+
url: 'https://example.com/related1',
181+
cover: 'https://example.com/related1_cover.jpg',
182+
),
183+
],
184+
);
185+
186+
final newRelated = RelatedPublication(
187+
id: 'related_2',
188+
title: 'Related Publication 2',
189+
releaseDate: DateTime.parse('2023-02-20'),
190+
url: 'https://example.com/related2',
191+
cover: 'https://example.com/related2_cover.jpg',
192+
);
193+
194+
final updated = original.copyWith(
195+
relatedPublications: [newRelated],
196+
);
197+
198+
expect(updated.relatedPublications, hasLength(1));
199+
expect(updated.relatedPublications.first.id, equals('related_2'));
200+
expect(
201+
updated.relatedPublications.first.title,
202+
equals('Related Publication 2'),
203+
);
204+
205+
// Verify other fields are preserved
206+
expect(updated.id, equals(original.id));
207+
expect(updated.title, equals(original.title));
208+
});
23209
});
24210
}

0 commit comments

Comments
 (0)