Skip to content

Commit 49bb285

Browse files
authored
Merge pull request #389 from rohansen856/unit_tests
Unit tests for root files
2 parents fc9b8af + fb2c3af commit 49bb285

File tree

5 files changed

+615
-12
lines changed

5 files changed

+615
-12
lines changed

lib/api_service.dart

+17-12
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,23 @@ String baseUrl = 'http://YOUR_IP:8000';
7171
String origin = 'http://localhost:8080';
7272

7373
Future<List<Tasks>> fetchTasks(String uuid, String encryptionSecret) async {
74-
String url =
75-
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
76-
77-
var response = await http.get(Uri.parse(url), headers: {
78-
"Content-Type": "application/json",
79-
}).timeout(const Duration(seconds: 10000));
80-
if (response.statusCode == 200) {
81-
List<dynamic> allTasks = jsonDecode(response.body);
82-
debugPrint(allTasks.toString());
83-
return allTasks.map((task) => Tasks.fromJson(task)).toList();
84-
} else {
85-
throw Exception('Failed to load tasks');
74+
try {
75+
String url =
76+
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
77+
78+
var response = await http.get(Uri.parse(url), headers: {
79+
"Content-Type": "application/json",
80+
}).timeout(const Duration(seconds: 10000));
81+
if (response.statusCode == 200) {
82+
List<dynamic> allTasks = jsonDecode(response.body);
83+
debugPrint(allTasks.toString());
84+
return allTasks.map((task) => Tasks.fromJson(task)).toList();
85+
} else {
86+
throw Exception('Failed to load tasks');
87+
}
88+
} catch (e) {
89+
debugPrint('Error fetching tasks: $e');
90+
return [];
8691
}
8792
}
8893

pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ dev_dependencies:
6969
build_runner: null
7070
flutter_gen_runner: null
7171
flutter_lints: 4.0.0
72+
http_mock_adapter: ^0.3.0
73+
sqflite_common_ffi: ^2.0.0
7274

7375
flutter_gen:
7476
output: lib/app/utils/gen/

test/api_service_test.dart

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

0 commit comments

Comments
 (0)