Skip to content

Commit

Permalink
Merge pull request #389 from rohansen856/unit_tests
Browse files Browse the repository at this point in the history
Unit tests for root files
  • Loading branch information
Pavel401 authored Jan 11, 2025
2 parents fc9b8af + fb2c3af commit 49bb285
Show file tree
Hide file tree
Showing 5 changed files with 615 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ String baseUrl = 'http://YOUR_IP:8000';
String origin = 'http://localhost:8080';

Future<List<Tasks>> fetchTasks(String uuid, String encryptionSecret) async {
String url =
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';

var response = await http.get(Uri.parse(url), headers: {
"Content-Type": "application/json",
}).timeout(const Duration(seconds: 10000));
if (response.statusCode == 200) {
List<dynamic> allTasks = jsonDecode(response.body);
debugPrint(allTasks.toString());
return allTasks.map((task) => Tasks.fromJson(task)).toList();
} else {
throw Exception('Failed to load tasks');
try {
String url =
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';

var response = await http.get(Uri.parse(url), headers: {
"Content-Type": "application/json",
}).timeout(const Duration(seconds: 10000));
if (response.statusCode == 200) {
List<dynamic> allTasks = jsonDecode(response.body);
debugPrint(allTasks.toString());
return allTasks.map((task) => Tasks.fromJson(task)).toList();
} else {
throw Exception('Failed to load tasks');
}
} catch (e) {
debugPrint('Error fetching tasks: $e');
return [];
}
}

Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ dev_dependencies:
build_runner: null
flutter_gen_runner: null
flutter_lints: 4.0.0
http_mock_adapter: ^0.3.0
sqflite_common_ffi: ^2.0.0

flutter_gen:
output: lib/app/utils/gen/
Expand Down
164 changes: 164 additions & 0 deletions test/api_service_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:taskwarrior/api_service.dart';
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';

import 'api_service_test.mocks.dart';

class MockCredentialsStorage extends Mock implements CredentialsStorage {}

class MockMethodChannel extends Mock implements MethodChannel {}

@GenerateMocks([MockMethodChannel, http.Client])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

databaseFactory = databaseFactoryFfi;
MockClient mockClient = MockClient();

setUpAll(() {
sqfliteFfiInit();
});

group('Tasks model', () {
test('fromJson creates Tasks object', () {
final json = {
'id': 1,
'description': 'Task 1',
'project': 'Project 1',
'status': 'pending',
'uuid': '123',
'urgency': 5.0,
'priority': 'H',
'due': '2024-12-31',
'end': null,
'entry': '2024-01-01',
'modified': '2024-11-01',
};

final task = Tasks.fromJson(json);

expect(task.id, 1);
expect(task.description, 'Task 1');
expect(task.project, 'Project 1');
expect(task.status, 'pending');
expect(task.uuid, '123');
expect(task.urgency, 5.0);
expect(task.priority, 'H');
expect(task.due, '2024-12-31');
expect(task.entry, '2024-01-01');
expect(task.modified, '2024-11-01');
});

test('toJson converts Tasks object to JSON', () {
final task = Tasks(
id: 1,
description: 'Task 1',
project: 'Project 1',
status: 'pending',
uuid: '123',
urgency: 5.0,
priority: 'H',
due: '2024-12-31',
end: null,
entry: '2024-01-01',
modified: '2024-11-01',
);

final json = task.toJson();

expect(json['id'], 1);
expect(json['description'], 'Task 1');
expect(json['project'], 'Project 1');
expect(json['status'], 'pending');
expect(json['uuid'], '123');
expect(json['urgency'], 5.0);
expect(json['priority'], 'H');
expect(json['due'], '2024-12-31');
});
});

group('fetchTasks', () {
test('Fetch data successfully', () async {
final responseJson = jsonEncode({'data': 'Mock data'});
when(mockClient.get(
Uri.parse(
'$baseUrl/tasks?email=email&origin=$origin&UUID=123&encryptionSecret=secret'),
headers: {
"Content-Type": "application/json",
})).thenAnswer((_) async => http.Response(responseJson, 200));

final result = await fetchTasks('123', 'secret');

expect(result, isA<List<Tasks>>());
});

test('fetchTasks returns empty array', () async {
const uuid = '123';
const encryptionSecret = 'secret';

expect(await fetchTasks(uuid, encryptionSecret), isEmpty);
});
});

group('TaskDatabase', () {
late TaskDatabase taskDatabase;

setUp(() async {
taskDatabase = TaskDatabase();
await taskDatabase.open();
});

test('insertTask adds a task to the database', () async {
final task = Tasks(
id: 1,
description: 'Task 1',
project: 'Project 1',
status: 'pending',
uuid: '123',
urgency: 5.0,
priority: 'H',
due: '2024-12-31',
end: null,
entry: '2024-01-01',
modified: '2024-11-01',
);

await taskDatabase.insertTask(task);

final tasks = await taskDatabase.fetchTasksFromDatabase();

expect(tasks.length, 1);
expect(tasks[0].description, 'Task 1');
});

test('deleteAllTasksInDB removes all tasks', () async {
final task = Tasks(
id: 1,
description: 'Task 1',
project: 'Project 1',
status: 'pending',
uuid: '123',
urgency: 5.0,
priority: 'H',
due: '2024-12-31',
end: null,
entry: '2024-01-01',
modified: '2024-11-01',
);

await taskDatabase.insertTask(task);
await taskDatabase.deleteAllTasksInDB();

final tasks = await taskDatabase.fetchTasksFromDatabase();

expect(tasks.length, 0);
});
});
}
Loading

0 comments on commit 49bb285

Please sign in to comment.