Skip to content

Commit

Permalink
added tests to the project (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uttkarsh-raj authored Jun 20, 2024
1 parent 2a89c4b commit 50671fb
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 40 deletions.
3 changes: 3 additions & 0 deletions lib/src/pages/home/profile.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:blt/src/pages/home/home_imports.dart';
import 'package:http/http.dart' as http;

/// Page that displays the stats of a user registered on BLT,
/// shows dummy data for Guest login.
Expand Down Expand Up @@ -27,8 +28,10 @@ class _UserProfileState extends ConsumerState<UserProfile> {

Future<List<Issue>?> getAnonymousUserIssueList() async {
List<Issue>? issueList = null;
final client = http.Client();
try {
final IssueData? issueData = await IssueApiClient.getAllIssues(
client,
IssueEndPoints.issues,
);
issueList = issueData!.issueList;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/pages/leaderboards/global_leaderboard.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:blt/src/pages/leaderboards/leaderboards_imports.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;

/// Page showing the all time top contributing users.
class GlobalLeaderBoardPage extends StatefulWidget {
Expand All @@ -16,7 +17,7 @@ class _GlobalLeaderBoardPageState extends State<GlobalLeaderBoardPage> {
@override
void initState() {
var paginatedUrl = LeaderboardEndpoints.globalLeaderboard;
_getObj = LeaderboardApiClient.getLeaderData(paginatedUrl);
_getObj = LeaderboardApiClient.getLeaderData(http.Client(), paginatedUrl);
super.initState();
}

Expand Down
7 changes: 5 additions & 2 deletions lib/src/providers/authstate_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:blt/src/providers/providers_imports.dart';
import 'package:http/http.dart' as http;

/// The provider which exposes the state management for user authentication.
final authStateNotifier =
Expand All @@ -16,6 +17,8 @@ class AuthNotifier extends StateNotifier<AsyncValue<AuthState>> {
authstate ?? const AsyncValue.data(AuthState.loggedOut),
);

final client = http.Client();

/// Do a guest type authentication.
Future<void> guestLogin() async {
await storage.write(
Expand Down Expand Up @@ -75,7 +78,7 @@ class AuthNotifier extends StateNotifier<AsyncValue<AuthState>> {
username: username,
token: accessToken,
);
UserApiClient.getUserInfo(currentUser!);
UserApiClient.getUserInfo(client, currentUser!);

state = AsyncValue.data(AuthState.loggedIn);
read(loginProvider.notifier).setUserLogin();
Expand Down Expand Up @@ -124,7 +127,7 @@ class AuthNotifier extends StateNotifier<AsyncValue<AuthState>> {
if (rememberMe) {
rememberUser(currentUser!.username!, currentUser!.token!);
}
await UserApiClient.getUserInfo(currentUser!);
await UserApiClient.getUserInfo(client, currentUser!);

state = AsyncValue.data(AuthState.loggedIn);
read(loginProvider.notifier).setUserLogin();
Expand Down
4 changes: 3 additions & 1 deletion lib/src/providers/companies/company_list_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:blt/src/components/components_import.dart';
import 'package:blt/src/models/company_model.dart';
import 'package:blt/src/util/api/company_api.dart';
import 'package:http/http.dart' as http;

final companiesListProvider =
StateNotifierProvider<CompanyListNotifier, AsyncValue<List<Company>?>?>(
Expand All @@ -19,9 +20,10 @@ class CompanyListNotifier extends StateNotifier<AsyncValue<List<Company>?>?> {
}

Future<void> _retrieveCompaniesList() async {
final client = http.Client();
try {
final List<Company>? companyData =
await CompanyApiClient.getListOfCompanies("/company/");
await CompanyApiClient.getListOfCompanies(client, "/company/");
state = AsyncValue.data(companyData);
} catch (e) {
AsyncValue.error(e);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/providers/issuelist_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:blt/src/providers/providers_imports.dart';
import 'package:http/http.dart' as http;

/// The provider which exposes the state management
/// for issues in the issue list.
Expand All @@ -16,12 +17,14 @@ class IssueListProvider extends StateNotifier<AsyncValue<List<Issue>?>?> {
: super(issueList ?? const AsyncValue.loading()) {
_retrieveIssueList();
}
final client = http.Client();

/// Default call for getting first page of issues
/// when the provider is initialized.
Future<void> _retrieveIssueList() async {
try {
final IssueData? issueData = await IssueApiClient.getAllIssues(
client,
IssueEndPoints.issues,
);
nxtUrl = issueData!.nextQuery;
Expand All @@ -36,6 +39,7 @@ class IssueListProvider extends StateNotifier<AsyncValue<List<Issue>?>?> {
_cacheState();
try {
final IssueData? issueData = await IssueApiClient.getAllIssues(
client,
nextUrl,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:blt/src/providers/providers_imports.dart';
import 'package:http/http.dart' as http;

/// The provider which exposes the state management
/// for companies in the company scoreboard list.
Expand All @@ -24,6 +25,7 @@ class CompanyScoreboardNotifier
try {
final List<Company>? companyData =
await LeaderboardApiClient.getScoreBoardData(
http.Client(),
LeaderboardEndpoints.companyScoreboard,
);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/providers/leaderboards/globalleaderboard_povider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:blt/src/providers/providers_imports.dart';
import 'package:http/http.dart' as http;

/// The provider which exposes the state management
/// for users in the global leaderboard list.
Expand All @@ -23,6 +24,7 @@ class GlobalLeaderBoardNotifier
try {
final List<Leaders>? leaderData =
await LeaderboardApiClient.getLeaderData(
http.Client(),
LeaderboardEndpoints.globalLeaderboard,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:blt/src/models/leaderdata_model.dart';
import 'package:blt/src/providers/providers_imports.dart';
import 'package:http/http.dart' as http;

final monthlyLeaderBoardProvider = StateNotifierProvider<
MonthlyLeaderBoardNotifier, AsyncValue<List<Leaders>?>?>((ref) {
Expand All @@ -21,6 +22,7 @@ class MonthlyLeaderBoardNotifier
try {
final LeaderData? monthlyLeaderData =
await LeaderboardApiClient.getMonthlyLeaderData(
http.Client(),
LeaderboardEndpoints.monthly_leaderboard,
year,
month,
Expand Down
27 changes: 11 additions & 16 deletions lib/src/util/api/company_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import 'package:http/http.dart' as http;
class CompanyApiClient {
CompanyApiClient._();

static Future<List<Company>> getListOfCompanies(String endpoint) async {
static Future<List<Company>> getListOfCompanies(
http.Client client, String endpoint) async {
String searchUrl = GeneralEndPoints.apiBaseUrl + endpoint;
List<Company> companiesList = [];
try {
var response = await http.get(Uri.parse(searchUrl));
var response = await client.get(Uri.parse(searchUrl));
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes));
companiesList = Company.fromSnapshot(decodedResponse["results"]);
} catch (e) {
Expand All @@ -21,24 +22,18 @@ class CompanyApiClient {

/// Search a company by keyword,
/// returns the first matching result.
static Future<void> getCompanyByKeyWord(
Company company,
String keyword,
) async {
http.Response? response;
String searchUrl = CompanyEndpoints.domain + "?search=$keyword";
static Future<Company?> getCompanyByKeyWord(
http.Client client, String paginated_url, String keyword) async {
String searchUrl = paginated_url + "?search=$keyword";
// print(searchUrl);
try {
response = await http.get(Uri.parse(searchUrl));
var response = await client.get(Uri.parse(searchUrl));
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes));
var companyJson = decodedResponse["results"][0];
company.setMoreInfo(
companyJson["id"],
companyJson["email"],
companyJson["url"],
companyJson["color"],
);
var companyJson = Company.fromJson(decodedResponse["results"][0]);
return companyJson;
} catch (e) {
print(e);
}
return null;
}
}
5 changes: 3 additions & 2 deletions lib/src/util/api/issues_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class IssueApiClient {
IssueApiClient._();

/// Get all the issues relating to the [paginatedUrl].
static Future<IssueData?> getAllIssues(String paginatedUrl) async {
static Future<IssueData?> getAllIssues(
http.Client client, String paginatedUrl) async {
http.Response? response;
IssueData? issueData;
List<Issue>? issueList;
Expand All @@ -18,7 +19,7 @@ class IssueApiClient {
}
: null;
try {
response = await http.get(
response = await client.get(
Uri.parse(paginatedUrl),
headers: headers,
);
Expand Down
22 changes: 12 additions & 10 deletions lib/src/util/api/leaderboard_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import 'package:blt/src/util/util_import.dart';
class LeaderboardApiClient {
LeaderboardApiClient._();

static Future<List<Leaders>> getLeaderData(String paginatedUrl) async {
return http
static Future<List<Leaders>> getLeaderData(
http.Client client, String paginatedUrl) async {
return client
.get(
Uri.parse(paginatedUrl),
)
.then((http.Response response) {
.then((var response) {
List<Leaders> leaders =
(json.decode(utf8.decode(response.bodyBytes)) as List)
.map((data) => Leaders.fromJson(data))
Expand All @@ -20,18 +21,18 @@ class LeaderboardApiClient {
}

static Future<LeaderData> getMonthlyLeaderData(
String paginatedUrl, int? year, int? month) async {
http.Client client, String paginatedUrl, int? year, int? month) async {
final queryParams = {
"filter": '1',
"year": year.toString(),
"month": month.toString(),
};
print(Uri.parse(paginatedUrl).replace(queryParameters: queryParams));
return http
// print(Uri.parse(paginatedUrl).replace(queryParameters: queryParams));
return client
.get(
Uri.parse(paginatedUrl).replace(queryParameters: queryParams),
)
.then((http.Response response) {
.then((var response) {
List<Leaders> leaderList = [];
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes));
decodedResponse["results"].forEach((element) {
Expand All @@ -52,7 +53,7 @@ class LeaderboardApiClient {
.get(
Uri.parse(nextUrl!),
)
.then((http.Response response) {
.then((var response) {
List<Leaders> leaderList = [];
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes));
decodedResponse["results"].forEach((element) {
Expand All @@ -68,8 +69,9 @@ class LeaderboardApiClient {
});
}

static Future<List<Company>> getScoreBoardData(String? paginatedUrl) async {
var req = await http.get(
static Future<List<Company>> getScoreBoardData(
http.Client client, String? paginatedUrl) async {
var req = await client.get(
Uri.parse(paginatedUrl!),
);
var response = jsonDecode(req.body);
Expand Down
18 changes: 10 additions & 8 deletions lib/src/util/api/user_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class UserApiClient {

/// Get a user's details from username and token,
/// used for currentUser.
static Future<void> getUserDetails(User user) async {
static Future<void> getUserDetails(http.Client client, User user) async {
http.Response? response;
try {
response = await http.get(
Expand All @@ -23,30 +23,32 @@ class UserApiClient {

/// Get a user's details, queried
/// against a [user]'s username.
static Future<void> getUserInfo(User user) async {
http.Response? response;
static Future<User?> getUserInfo(http.Client client, User user) async {
try {
String searchUrl = UserEndPoints.userInfo + "?search=${user.username}";
response = await http.get(
var response = await client.get(
Uri.parse(searchUrl),
headers: {
"Authorization": "Token ${user.token}",
},
);
var decodedResponse =
jsonDecode(utf8.decode(response.bodyBytes))["results"][0];
user.id = decodedResponse["user"]["id"];
// user.id = decodedResponse["user"]["id"];
user.pfpLink = decodedResponse["user_avatar"];
user.title = decodedResponse["title"];
user.email = decodedResponse["email"];
user.winning = decodedResponse["winnings"];
user.description = decodedResponse["description"];
user.following = decodedResponse["follows"].cast<int>();
user.likedIssueId = decodedResponse["issue_upvoted"].cast<int>();
user.savedIssueId = decodedResponse["issue_saved"].cast<int>();
user.following = decodedResponse["follows"] as List<int>? ?? [];
user.likedIssueId = decodedResponse["issue_upvoted"] as List<int>? ?? [];
user.savedIssueId = decodedResponse["issue_saved"] as List<int>? ?? [];
user.totalScore = decodedResponse["total_score"];
return user;
} catch (e) {
print(e);
}
return null;
}

static Future<void> updatePfp(XFile image, User user) async {
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
flutter_localizations:
sdk: flutter
primer_progress_bar: ^0.4.2
testing: ^0.0.11

dev_dependencies:
flutter_launcher_icons: ">=0.9.0 <0.13.0"
Expand Down
Loading

0 comments on commit 50671fb

Please sign in to comment.