Skip to content

Commit 79b8b53

Browse files
committed
test: added test for files under lib/models/json directory
1 parent e571ee9 commit 79b8b53

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

test/models/json/annotation_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// test/annotation_test.dart
2+
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:intl/intl.dart';
5+
import 'package:taskwarrior/app/models/json/annotation.dart';
6+
7+
void main() {
8+
group('Annotation', () {
9+
late Annotation annotation;
10+
late DateTime entry;
11+
late String description;
12+
13+
setUp(() {
14+
entry = DateTime.now().toUtc(); // Ensure the DateTime is in UTC
15+
description = 'Test description';
16+
17+
annotation = Annotation((b) => b
18+
..entry = entry
19+
..description = description);
20+
});
21+
22+
test('should correctly initialize with given parameters', () {
23+
expect(annotation.entry, entry);
24+
expect(annotation.description, description);
25+
});
26+
27+
test('should correctly convert to JSON', () {
28+
final json = annotation.toJson();
29+
expect(DateFormat("yyyyMMdd'T'HH").format(DateTime.parse(json['entry'])),
30+
DateFormat("yyyyMMdd'T'HH").format(entry));
31+
expect(json['description'], description);
32+
});
33+
34+
test('should correctly create from JSON', () {
35+
final json = {
36+
'entry': entry.toIso8601String(),
37+
'description': description,
38+
};
39+
final newAnnotation = Annotation.fromJson(json);
40+
expect(newAnnotation.entry, entry);
41+
expect(newAnnotation.description, description);
42+
});
43+
});
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:intl/intl.dart';
3+
import 'package:taskwarrior/app/models/json/iso_8601_basic.dart';
4+
import 'package:taskwarrior/app/models/json/serializers.dart';
5+
6+
void main() {
7+
group('Iso8601BasicDateTimeSerializer', () {
8+
late Iso8601BasicDateTimeSerializer serializer;
9+
late DateTime dateTime;
10+
late DateFormat dateFormat;
11+
12+
setUp(() {
13+
serializer = Iso8601BasicDateTimeSerializer();
14+
dateTime = DateTime.utc(2024, 12, 12, 13, 30, 40, 495);
15+
dateFormat = DateFormat('yMMddTHHmmss\'Z\'');
16+
});
17+
18+
test('should throw ArgumentError if dateTime is not in UTC', () {
19+
expect(
20+
() => serializer.serialize(serializers, dateTime.toLocal()),
21+
throwsArgumentError,
22+
);
23+
});
24+
25+
test('should correctly serialize UTC dateTime', () {
26+
final serialized = serializer.serialize(serializers, dateTime);
27+
expect(serialized, dateFormat.format(dateTime));
28+
});
29+
});
30+
}

test/models/json/serializer_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:intl/intl.dart';
3+
import 'package:taskwarrior/app/models/json/serializers.dart';
4+
import 'package:taskwarrior/app/models/json/annotation.dart';
5+
import 'package:taskwarrior/app/models/json/task.dart';
6+
7+
void main() {
8+
group('Serializers', () {
9+
test('should correctly serialize and deserialize Annotation', () {
10+
final annotation = Annotation((b) => b
11+
..entry = DateTime.now().toUtc()
12+
..description = 'Test description');
13+
14+
final serialized =
15+
serializers.serializeWith(Annotation.serializer, annotation);
16+
final deserialized =
17+
serializers.deserializeWith(Annotation.serializer, serialized!);
18+
19+
expect(DateFormat("yyyyMMdd'T'HH").format(deserialized!.entry),
20+
DateFormat("yyyyMMdd'T'HH").format(annotation.entry));
21+
expect(deserialized.description, annotation.description);
22+
});
23+
24+
test('should correctly serialize and deserialize Task', () {
25+
final task = Task((b) => b
26+
..uuid = '1'
27+
..status = 'pending'
28+
..description = 'Test Task'
29+
..entry = DateTime.now().toUtc());
30+
31+
final serialized = serializers.serializeWith(Task.serializer, task);
32+
final deserialized =
33+
serializers.deserializeWith(Task.serializer, serialized!);
34+
35+
expect(DateFormat("yyyyMMdd'T'HH").format(deserialized!.entry),
36+
DateFormat("yyyyMMdd'T'HH").format(task.entry));
37+
expect(deserialized.uuid, task.uuid);
38+
expect(deserialized.status, task.status);
39+
expect(deserialized.description, task.description);
40+
});
41+
});
42+
}

test/models/json/task_test.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:intl/intl.dart';
3+
import 'package:taskwarrior/app/models/json/task.dart';
4+
5+
void main() {
6+
group('Task', () {
7+
late Task task;
8+
late DateTime entry;
9+
late String uuid;
10+
late String description;
11+
12+
setUp(() {
13+
entry = DateTime.now().toUtc();
14+
uuid = '123e4567-e89b-12d3-a456-426614174000';
15+
description = 'Test task';
16+
17+
task = Task((b) => b
18+
..entry = entry
19+
..uuid = uuid
20+
..status = 'pending'
21+
..description = description);
22+
});
23+
24+
test('should correctly initialize with given parameters', () {
25+
expect(task.entry, entry);
26+
expect(task.uuid, uuid);
27+
expect(task.description, description);
28+
});
29+
30+
test('should correctly convert to JSON', () {
31+
final json = task.toJson();
32+
expect(DateFormat("yyyyMMdd'T'HH").format(DateTime.parse(json['entry'])),
33+
DateFormat("yyyyMMdd'T'HH").format(entry));
34+
expect(json['uuid'], uuid);
35+
expect(json['description'], description);
36+
});
37+
38+
test('should correctly create from JSON', () {
39+
final json = {
40+
'entry': entry.toIso8601String(),
41+
'uuid': uuid,
42+
'status': 'pending',
43+
'description': description,
44+
};
45+
final newTask = Task.fromJson(json);
46+
expect(newTask.entry, entry);
47+
expect(newTask.uuid, uuid);
48+
expect(newTask.description, description);
49+
});
50+
});
51+
}

0 commit comments

Comments
 (0)