Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for root files #389

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

rohansen856
Copy link
Contributor

Description

Please include a summary of the change and which issue is fixed. List any dependencies that are required for this change.

Fixes #388

Summary:

  • Added 2 new packages: http_mock_adapter and sqflite_common_ffi for mocking local server and database
  • added test coverage for 2 files: lib/main and lib/api_service.dart

test result:

  • there is a common NULL safety error issue. please follow this thread for any confusion: stackoverflow. added this line for resolving it: @GenerateMocks([MockMethodChannel])
  • tests are done in 2 conditions: once with the server running and once with the server down. please update these urls for your specific server port: String baseUrl = 'http://YOUR_IP:8000';, String origin = 'http://localhost:8080';

mocking with server up:

00:10 +54: /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart: TaskDatabase insertTask adds a task to the database                                                       
Deleted all tasks
Created new task table
00:10 +55: /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart: TaskDatabase deleteAllTasksInDB removes all tasks                                                         
Deleted all tasks
Created new task table
Deleted all tasks
Created new task table
00:10 +56: All tests passed!  

mocking with server down:

00:09 +54 -1: /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart: fetchTasks fetchTasks returns list of Tasks on success [E]                                             
  type 'Null' is not a subtype of type 'Future<Response>'
  test/api_service_test.dart 9:7     MockHttpClient.get
  test/api_service_test.dart 108:27  main.<fn>.<fn>
  

To run this test again: /home/rcsen/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart test /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart -p vm --plain-name 'fetchTasks fetchTasks returns list of Tasks on success'
00:09 +54 -2: /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart: fetchTasks fetchTasks throws exception on failure [E]                                                  
  Bad state: Cannot call `when` within a stub response
  package:mockito/src/mock.dart 1207:5  when
  test/api_service_test.dart 123:7      main.<fn>.<fn>
  

To run this test again: /home/rcsen/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart test /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart -p vm --plain-name 'fetchTasks fetchTasks throws exception on failure'
00:09 +54 -2: /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart: TaskDatabase insertTask adds a task to the database                                                    
Deleted all tasks
Created new task table
00:09 +55 -2: /home/rcsen/Documents/open-source/taskwarrior_test/test/api_service_test.dart: TaskDatabase deleteAllTasksInDB removes all tasks                                                      
Deleted all tasks
Created new task table
Deleted all tasks
Created new task table
00:09 +56 -2: Some tests failed. 

Checklist

  • Tests have been added or updated to cover the changes
  • Documentation has been updated to reflect the changes
  • Code follows the established coding style guidelines
  • All tests are passing

@BrawlerXull
Copy link
Collaborator

@rohansen856 Make sure it passes all the checks.
Also good work we need more tests coverage in our application

@rohansen856
Copy link
Contributor Author

@rohansen856 Make sure it passes all the checks. Also good work we need more tests coverage in our application

Thanks for the follow up @BrawlerXull . Currently there seems to be some changes in package mockito (as i mentioned in the PR description) which is giving error for NULL check. I will make sure the tests are passing. Thank you.

@rohansen856 rohansen856 changed the title Unit tests Unit tests for root files Nov 28, 2024
@rohansen856
Copy link
Contributor Author

@BrawlerXull I have updated the test and it's currently passing 100% tests. For going further, I would like your advice on should i open different PRs for different folders in the codebase (as some folder contents are quite large), or should i do all the tets in this PR alone (folder by folder). thank you.

@rohansen856
Copy link
Contributor Author

@BrawlerXull I have updated the test and it's currently passing 100% tests. For going further, I would like your advice on should i open different PRs for different folders in the codebase (as some folder contents are quite large), or should i do all the tets in this PR alone (folder by folder). thank you.

@Pavel401 @BrawlerXull any suggestion?

@BrawlerXull
Copy link
Collaborator

@BrawlerXull I have updated the test and it's currently passing 100% tests. For going further, I would like your advice on should i open different PRs for different folders in the codebase (as some folder contents are quite large), or should i do all the tets in this PR alone (folder by folder). thank you.

Sorry for late reply was busy in some work.
Make different PRs for each folder.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also make sure u add relevant testcases removing testcases to make flutter test is not the solution please add testcases like fetchTasks returns list of Tasks on success and make sure it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely @BrawlerXull . There has been some problems with the mock fetchtasks due to its dependence taskchampion server. I will resolve the issue and add the test shortly. Thanks.

@Pavel401
Copy link
Member

Is there any update on this? @rohansen856 @BrawlerXull

@rohansen856
Copy link
Contributor Author

Is there any update on this? @rohansen856 @BrawlerXull

Extremely Sorry for the late, I will update the tests and try to make PR within a day. thanks!

@rohansen856
Copy link
Contributor Author

@Pavel401 I see that due to current changes in the codebase, there are also some other tests that are failing... I would solve all of them and make a PR so that all tests are up to date and passing.

@rohansen856
Copy link
Contributor Author

rohansen856 commented Dec 28, 2024

@Pavel401 While rewriting tests for the lib/api_services.dart file i saw that this function:

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');
  }
}

throws direct exceptions, which should not happen. I would suggest to encapsulate it inside a try catch block or handle it in other way. ex.:

Future<List<Tasks>> fetchTasks(String uuid, String encryptionSecret) async {
  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('Failed to add tasks: $e');
    return [];
  }
}

any thought about this...?

@rohansen856
Copy link
Contributor Author

@Pavel401 @BrawlerXull it seems that the libsqlite3.so dynamic library is not included in the GitHub Actions, but its required for sqflite_common_ffi package which is needed to mock the sqlite database... Either the tests for that need to be removed or this should be added to the CI: - run: sudo apt-get update && sudo apt-get install -y sqlite3 libsqlite3-dev. Any thoughts on this...?

@rohansen856 rohansen856 mentioned this pull request Jan 2, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

tests: add unit tests for all files
3 participants