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