Skip to content

Commit d1d4ca2

Browse files
authored
Merge pull request #396 from rohansen856/models_test
tests: added tests for the Models folder
2 parents f474487 + 5613823 commit d1d4ca2

16 files changed

+790
-0
lines changed

test/models/chart_test.dart

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:taskwarrior/app/models/chart.dart';
3+
4+
void main() {
5+
group('ChartData', () {
6+
test('should create an instance with correct values', () {
7+
final chartData = ChartData('2024-12-12', 100, 200);
8+
9+
expect(chartData.x, '2024-12-12');
10+
expect(chartData.y1, 100);
11+
expect(chartData.y2, 200);
12+
});
13+
14+
test('should handle null or empty values correctly', () {
15+
final chartData = ChartData('', 0, 0);
16+
17+
expect(chartData.x, '');
18+
expect(chartData.y1, 0);
19+
expect(chartData.y2, 0);
20+
});
21+
22+
test('should handle negative values correctly', () {
23+
final chartData = ChartData('2024-12-12', -100, -200);
24+
25+
expect(chartData.x, '2024-12-12');
26+
expect(chartData.y1, -100);
27+
expect(chartData.y2, -200);
28+
});
29+
});
30+
}

test/models/data_test.dart

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:mockito/mockito.dart';
3+
import 'package:taskwarrior/app/models/data.dart';
4+
import 'package:taskwarrior/app/models/json/task.dart';
5+
import 'package:taskwarrior/app/services/notification_services.dart';
6+
import 'package:flutter/widgets.dart';
7+
import 'dart:io';
8+
9+
class MockNotificationService extends Mock implements NotificationService {}
10+
11+
void main() {
12+
TestWidgetsFlutterBinding.ensureInitialized();
13+
14+
group('Data', () {
15+
late Data data;
16+
late Directory home;
17+
late MockNotificationService mockNotificationService;
18+
19+
setUp(() {
20+
WidgetsFlutterBinding.ensureInitialized();
21+
home = Directory.systemTemp.createTempSync();
22+
data = Data(home);
23+
mockNotificationService = MockNotificationService();
24+
25+
when(mockNotificationService.initiliazeNotification())
26+
.thenAnswer((_) async {});
27+
});
28+
29+
test('should update tasks with status "waiting" or "until" correctly',
30+
() async {
31+
final task1 = Task((b) => b
32+
..uuid = '1'
33+
..status = 'pending'
34+
..wait = DateTime.now().toUtc().subtract(const Duration(days: 1))
35+
..description = 'Test Task'
36+
..entry = DateTime.now().toUtc());
37+
final task2 = Task((b) => b
38+
..uuid = '2'
39+
..status = 'deleted'
40+
..until = DateTime.now().toUtc().subtract(const Duration(days: 1))
41+
..description = 'Test Task'
42+
..entry = DateTime.now().toUtc());
43+
44+
final updatedTasks = data.pendingData();
45+
expect(
46+
updatedTasks.any(
47+
(task) => task.uuid == task1.uuid && task.status == task1.status),
48+
isFalse);
49+
expect(
50+
updatedTasks.any(
51+
(task) => task.uuid == task2.uuid && task.status == task2.status),
52+
isFalse);
53+
});
54+
55+
test('should correctly return pending data', () {
56+
final task = Task((b) => b
57+
..uuid = '1'
58+
..status = 'pending'
59+
..description = 'Test Task'
60+
..entry = DateTime.now().toUtc());
61+
62+
data.updateWaitOrUntil([task]);
63+
final tasks = data.pendingData();
64+
expect(tasks.any((t) => t.uuid == '1' && t.description == 'Test Task'),
65+
isFalse);
66+
});
67+
68+
test('should correctly return completed data', () {
69+
final task = Task((b) => b
70+
..uuid = '1'
71+
..status = 'completed'
72+
..description = 'Test Task'
73+
..entry = DateTime.now().toUtc());
74+
75+
data.updateWaitOrUntil([task]);
76+
final tasks = data.completedData();
77+
expect(tasks.any((t) => t.uuid == '1' && t.description == 'Test Task'),
78+
isFalse);
79+
});
80+
81+
test('should correctly return waiting data', () {
82+
final task = Task((b) => b
83+
..uuid = '1'
84+
..status = 'waiting'
85+
..description = 'Test Task'
86+
..entry = DateTime.now().toUtc());
87+
88+
data.updateWaitOrUntil([task]);
89+
final tasks = data.waitingData();
90+
expect(tasks.any((t) => t.uuid == '1' && t.description == 'Test Task'),
91+
isFalse);
92+
});
93+
94+
test('should correctly export data', () {
95+
final exportedData = data.export();
96+
expect(exportedData, isNotNull);
97+
});
98+
99+
tearDown(() {
100+
home.deleteSync(recursive: true);
101+
});
102+
});
103+
}

test/models/filters_test.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:taskwarrior/app/models/filters.dart';
3+
import 'package:taskwarrior/app/services/tag_filter.dart';
4+
5+
void main() {
6+
group('Filters', () {
7+
late Filters filters;
8+
late bool pendingFilter;
9+
late bool waitingFilter;
10+
late String projectFilter;
11+
late bool tagUnion;
12+
late Map<String, TagFilterMetadata> tags;
13+
14+
setUp(() {
15+
pendingFilter = false;
16+
waitingFilter = false;
17+
projectFilter = 'TestProject';
18+
tagUnion = false;
19+
tags = {
20+
'tag1': const TagFilterMetadata(display: 'Tag 1', selected: false),
21+
'tag2': const TagFilterMetadata(display: 'Tag 2', selected: true),
22+
};
23+
24+
filters = Filters(
25+
pendingFilter: pendingFilter,
26+
waitingFilter: waitingFilter,
27+
togglePendingFilter: () {
28+
pendingFilter = !pendingFilter;
29+
},
30+
toggleWaitingFilter: () {
31+
waitingFilter = !waitingFilter;
32+
},
33+
tagFilters: TagFilters(
34+
tagUnion: tagUnion,
35+
toggleTagUnion: () {
36+
tagUnion = !tagUnion;
37+
},
38+
tags: tags,
39+
toggleTagFilter: (String tag) {
40+
tags[tag] = TagFilterMetadata(
41+
display: tags[tag]!.display,
42+
selected: !tags[tag]!.selected,
43+
);
44+
},
45+
),
46+
projects: [],
47+
projectFilter: projectFilter,
48+
toggleProjectFilter: (String project) {
49+
projectFilter = project;
50+
},
51+
);
52+
});
53+
54+
test('should correctly initialize with given parameters', () {
55+
expect(filters.pendingFilter, pendingFilter);
56+
expect(filters.waitingFilter, waitingFilter);
57+
expect(filters.projectFilter, projectFilter);
58+
expect(filters.tagFilters.tagUnion, tagUnion);
59+
expect(filters.tagFilters.tags, tags);
60+
});
61+
62+
test('should correctly toggle pending filter', () {
63+
filters.togglePendingFilter();
64+
expect(pendingFilter, isTrue);
65+
66+
filters.togglePendingFilter();
67+
expect(pendingFilter, isFalse);
68+
});
69+
70+
test('should correctly toggle waiting filter', () {
71+
filters.toggleWaitingFilter();
72+
expect(waitingFilter, isTrue);
73+
74+
filters.toggleWaitingFilter();
75+
expect(waitingFilter, isFalse);
76+
});
77+
78+
test('should correctly toggle project filter', () {
79+
const newProject = 'NewProject';
80+
filters.toggleProjectFilter(newProject);
81+
expect(projectFilter, newProject);
82+
83+
const anotherProject = 'AnotherProject';
84+
filters.toggleProjectFilter(anotherProject);
85+
expect(projectFilter, anotherProject);
86+
});
87+
88+
test('should correctly toggle tag union', () {
89+
filters.tagFilters.toggleTagUnion();
90+
expect(tagUnion, isTrue);
91+
92+
filters.tagFilters.toggleTagUnion();
93+
expect(tagUnion, isFalse);
94+
});
95+
96+
test('should correctly toggle tag filter', () {
97+
filters.tagFilters.toggleTagFilter('tag1');
98+
expect(tags['tag1']!.selected, isTrue);
99+
100+
filters.tagFilters.toggleTagFilter('tag1');
101+
expect(tags['tag1']!.selected, isFalse);
102+
});
103+
});
104+
}

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+
}

0 commit comments

Comments
 (0)