Skip to content

Commit 1612fdd

Browse files
committed
test: added test for root files
1 parent 0f2b8e4 commit 1612fdd

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed

test/api_service_test.dart

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import 'dart:convert';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:mockito/annotations.dart';
4+
import 'package:mockito/mockito.dart';
5+
import 'package:http/http.dart' as http;
6+
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
7+
import 'package:taskwarrior/api_service.dart';
8+
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';
9+
10+
import 'main_test.dart';
11+
12+
class MockHttpClient extends Mock implements http.Client {}
13+
14+
class MockCredentialsStorage extends Mock implements CredentialsStorage {}
15+
16+
@GenerateMocks([MockMethodChannel])
17+
void main() {
18+
TestWidgetsFlutterBinding.ensureInitialized();
19+
20+
late MockHttpClient mockHttpClient;
21+
databaseFactory = databaseFactoryFfi;
22+
23+
setUpAll(() {
24+
sqfliteFfiInit();
25+
});
26+
27+
setUp(() {
28+
mockHttpClient = MockHttpClient();
29+
});
30+
31+
group('Tasks model', () {
32+
test('fromJson creates Tasks object', () {
33+
final json = {
34+
'id': 1,
35+
'description': 'Task 1',
36+
'project': 'Project 1',
37+
'status': 'pending',
38+
'uuid': '123',
39+
'urgency': 5.0,
40+
'priority': 'H',
41+
'due': '2024-12-31',
42+
'end': null,
43+
'entry': '2024-01-01',
44+
'modified': '2024-11-01',
45+
};
46+
47+
final task = Tasks.fromJson(json);
48+
49+
expect(task.id, 1);
50+
expect(task.description, 'Task 1');
51+
expect(task.project, 'Project 1');
52+
expect(task.status, 'pending');
53+
expect(task.uuid, '123');
54+
expect(task.urgency, 5.0);
55+
expect(task.priority, 'H');
56+
expect(task.due, '2024-12-31');
57+
expect(task.entry, '2024-01-01');
58+
expect(task.modified, '2024-11-01');
59+
});
60+
61+
test('toJson converts Tasks object to JSON', () {
62+
final task = Tasks(
63+
id: 1,
64+
description: 'Task 1',
65+
project: 'Project 1',
66+
status: 'pending',
67+
uuid: '123',
68+
urgency: 5.0,
69+
priority: 'H',
70+
due: '2024-12-31',
71+
end: null,
72+
entry: '2024-01-01',
73+
modified: '2024-11-01',
74+
);
75+
76+
final json = task.toJson();
77+
78+
expect(json['id'], 1);
79+
expect(json['description'], 'Task 1');
80+
expect(json['project'], 'Project 1');
81+
expect(json['status'], 'pending');
82+
expect(json['uuid'], '123');
83+
expect(json['urgency'], 5.0);
84+
expect(json['priority'], 'H');
85+
expect(json['due'], '2024-12-31');
86+
});
87+
});
88+
89+
group('fetchTasks', () {
90+
test('fetchTasks returns list of Tasks on success', () async {
91+
const uuid = '123';
92+
const encryptionSecret = 'secret';
93+
final url =
94+
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
95+
96+
final response = [
97+
{
98+
'id': 1,
99+
'description': 'Task 1',
100+
'project': 'Project 1',
101+
'status': 'pending',
102+
'uuid': '123',
103+
'urgency': 5.0,
104+
'priority': 'H',
105+
'due': '2024-12-31',
106+
'end': null,
107+
'entry': '2024-01-01',
108+
'modified': '2024-11-01',
109+
}
110+
];
111+
112+
when(mockHttpClient.get(Uri.parse(url), headers: anyNamed('headers')))
113+
.thenAnswer((_) async => http.Response(jsonEncode(response), 200));
114+
115+
final tasks = await fetchTasks(uuid, encryptionSecret);
116+
117+
expect(tasks.length, 1);
118+
expect(tasks[0].description, 'Task 1');
119+
});
120+
121+
test('fetchTasks throws exception on failure', () async {
122+
const uuid = '123';
123+
const encryptionSecret = 'secret';
124+
final url =
125+
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
126+
127+
when(mockHttpClient.get(Uri.parse(url), headers: anyNamed('headers')))
128+
.thenAnswer((_) async => http.Response('Error', 500));
129+
130+
expect(() => fetchTasks(uuid, encryptionSecret), throwsException);
131+
});
132+
});
133+
134+
group('TaskDatabase', () {
135+
late TaskDatabase taskDatabase;
136+
137+
setUp(() async {
138+
taskDatabase = TaskDatabase();
139+
await taskDatabase.open();
140+
});
141+
142+
tearDown(() async {
143+
await taskDatabase.deleteAllTasksInDB();
144+
});
145+
146+
test('insertTask adds a task to the database', () async {
147+
final task = Tasks(
148+
id: 1,
149+
description: 'Task 1',
150+
project: 'Project 1',
151+
status: 'pending',
152+
uuid: '123',
153+
urgency: 5.0,
154+
priority: 'H',
155+
due: '2024-12-31',
156+
end: null,
157+
entry: '2024-01-01',
158+
modified: '2024-11-01',
159+
);
160+
161+
await taskDatabase.insertTask(task);
162+
163+
final tasks = await taskDatabase.fetchTasksFromDatabase();
164+
165+
expect(tasks.length, 1);
166+
expect(tasks[0].description, 'Task 1');
167+
});
168+
169+
test('deleteAllTasksInDB removes all tasks', () async {
170+
final task = Tasks(
171+
id: 1,
172+
description: 'Task 1',
173+
project: 'Project 1',
174+
status: 'pending',
175+
uuid: '123',
176+
urgency: 5.0,
177+
priority: 'H',
178+
due: '2024-12-31',
179+
end: null,
180+
entry: '2024-01-01',
181+
modified: '2024-11-01',
182+
);
183+
184+
await taskDatabase.insertTask(task);
185+
await taskDatabase.deleteAllTasksInDB();
186+
187+
final tasks = await taskDatabase.fetchTasksFromDatabase();
188+
189+
expect(tasks.length, 0);
190+
});
191+
});
192+
}

test/main_test.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter/services.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:get/get_navigation/src/root/get_material_app.dart';
5+
import 'package:mockito/annotations.dart';
6+
import 'package:mockito/mockito.dart';
7+
import 'package:taskwarrior/app/utils/app_settings/app_settings.dart';
8+
import 'package:taskwarrior/main.dart' as app;
9+
10+
class MockMethodChannel extends Mock implements MethodChannel {}
11+
12+
class AppSettingsService {
13+
Future<void> init() => AppSettings.init();
14+
}
15+
16+
class MockAppSettingsService extends Mock implements AppSettingsService {}
17+
18+
@GenerateMocks([MockMethodChannel])
19+
void main() {
20+
late MockAppSettingsService mockAppSettingsService;
21+
22+
setUp(() {
23+
mockAppSettingsService = MockAppSettingsService();
24+
});
25+
26+
testWidgets('App initializes and renders correctly',
27+
(WidgetTester tester) async {
28+
when(mockAppSettingsService.init()).thenAnswer((_) async {});
29+
30+
await tester.pumpWidget(
31+
MaterialApp(
32+
home: Builder(
33+
builder: (context) {
34+
app.main();
35+
return Container();
36+
},
37+
),
38+
),
39+
);
40+
41+
await tester.pumpAndSettle();
42+
43+
verify(mockAppSettingsService.init()).called(1);
44+
45+
expect(find.byType(GetMaterialApp), findsOneWidget);
46+
});
47+
}

0 commit comments

Comments
 (0)