Skip to content

Commit

Permalink
Add tests and fix bug (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
literalEval authored Jan 28, 2023
1 parent f47a3db commit 0c9dbc2
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/models/user/user_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ class User extends HiveObject {

update(User details) {
this.firstName = details.firstName;
this.lastName = details.firstName;
this.email = details.firstName;
this.image = details.firstName;
this.lastName = details.lastName;
this.email = details.email;
this.image = details.image;
this.authToken = details.authToken;
this.refreshToken = details.refreshToken;
this.joinedOrganizations = details.joinedOrganizations;
Expand Down
208 changes: 208 additions & 0 deletions test/model_tests/user/user_info_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:talawa/models/organization/org_info.dart';
import 'package:talawa/models/user/user_info.dart';

final encodedOrg = {
"name": 'test_org',
"image": 'https://testimg.com',
"isPublic": false,
};

final updatedOrg = OrgInfo.fromJson({
"name": 'test_org_updated',
"image": 'https://testimg_updated.com',
"isPublic": true,
});

final membershipRequestOrg = {
"organization": encodedOrg,
};

final updatedMembershipRequestOrg = {
"organization": updatedOrg,
};

final testOrgList = [
encodedOrg,
encodedOrg,
];

final membershipRequestOrgList = [
membershipRequestOrg,
membershipRequestOrg,
];

final testDataFromOrg = <String, dynamic>{
"firstName": "ravidi",
"lastName": "sheikh",
"email": "[email protected]",
"image": "https://testimg.com",
"accessToken": "randomAccessToken",
"authToken": "randomAuthToken",
"refreshToken": "randomRefreshToken",
"adminFor": testOrgList,
'createdOrganizations': testOrgList,
'joinedOrganizations': testOrgList,
'membershipRequests': membershipRequestOrgList,
};

final testDataNotFromOrg = {
"user": {
"firstName": "ravidi",
"lastName": "sheikh",
"email": "[email protected]",
"image": "https://testimg.com",
"accessToken": "randomAccessToken",
"authToken": "randomAuthToken",
"refreshToken": "randomRefreshToken",
"adminFor": testOrgList,
'createdOrganizations': testOrgList,
'joinedOrganizations': testOrgList,
'membershipRequests': membershipRequestOrgList,
}
};

void main() {
group("Tests for UserInfo.dart", () {
test('Check if UserInfo.fromJson works with fromOrg', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

expect(userInfo.firstName, "ravidi");
expect(userInfo.lastName, "sheikh");
expect(userInfo.email, "[email protected]");
expect(userInfo.image, "https://testimg.com");
expect(userInfo.authToken, " ");
});

test('Check if UserInfo.fromJson works without fromOrg', () async {
final userInfo = User.fromJson(testDataNotFromOrg);

expect(userInfo.firstName, "ravidi");
expect(userInfo.lastName, "sheikh");
expect(userInfo.email, "[email protected]");
expect(userInfo.image, "https://testimg.com");
expect(userInfo.authToken, null);
});

test('Check if the method "update" works', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

expect(userInfo.firstName, "ravidi");
expect(userInfo.lastName, "sheikh");
expect(userInfo.email, "[email protected]");
expect(userInfo.image, "https://testimg.com");
expect(userInfo.authToken, " ");

userInfo.update(User(
firstName: "ravidi_updated",
lastName: "sheikh_updated",
email: "[email protected]",
image: "https://testimgupdated.com",
authToken: "randomAuthToken_updated",
));

expect(userInfo.firstName, "ravidi_updated");
expect(userInfo.lastName, "sheikh_updated");
expect(userInfo.email, "[email protected]");
expect(userInfo.image, "https://testimgupdated.com");
expect(userInfo.authToken, "randomAuthToken_updated");
});

test('Check if print method works', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

// No way to test this. Calling here to increase
userInfo.print();
});

test('Check if the method updateJoinedOrg works', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

expect(userInfo.joinedOrganizations!.length, 2);
expect(userInfo.joinedOrganizations![0].name, 'test_org');
expect(userInfo.joinedOrganizations![0].image, 'https://testimg.com');
expect(userInfo.joinedOrganizations![0].isPublic, false);

userInfo.updateJoinedOrg([updatedOrg, updatedOrg, updatedOrg]);

expect(userInfo.joinedOrganizations!.length, 3);
expect(userInfo.joinedOrganizations![0].name, 'test_org_updated');
expect(userInfo.joinedOrganizations![0].image,
'https://testimg_updated.com');
expect(userInfo.joinedOrganizations![0].isPublic, true);
});

test('Check if the method updateCreatedOrg works', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

expect(userInfo.createdOrganizations!.length, 2);
expect(userInfo.createdOrganizations![0].name, 'test_org');
expect(userInfo.createdOrganizations![0].image, 'https://testimg.com');
expect(userInfo.createdOrganizations![0].isPublic, false);

userInfo.updateCreatedOrg([updatedOrg, updatedOrg, updatedOrg]);

expect(userInfo.createdOrganizations!.length, 3);
expect(userInfo.createdOrganizations![0].name, 'test_org_updated');
expect(userInfo.createdOrganizations![0].image,
'https://testimg_updated.com');
expect(userInfo.createdOrganizations![0].isPublic, true);
});

test('Check if the method updateMemberRequestOrg works', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

expect(userInfo.membershipRequests!.length, 2);
expect(userInfo.membershipRequests![0].name, 'test_org');
expect(userInfo.membershipRequests![0].image, 'https://testimg.com');
expect(userInfo.membershipRequests![0].isPublic, false);

userInfo.updateMemberRequestOrg([updatedOrg, updatedOrg, updatedOrg]);

expect(userInfo.membershipRequests!.length, 5);
expect(userInfo.membershipRequests![3].name, 'test_org_updated');
expect(
userInfo.membershipRequests![3].image, 'https://testimg_updated.com');
expect(userInfo.membershipRequests![3].isPublic, true);
});

test('Check if the method updateAdminFor works', () async {
final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);

expect(userInfo.adminFor!.length, 2);
expect(userInfo.adminFor![0].name, 'test_org');
expect(userInfo.adminFor![0].image, 'https://testimg.com');
expect(userInfo.adminFor![0].isPublic, false);

userInfo.updateAdminFor([updatedOrg, updatedOrg, updatedOrg]);

expect(userInfo.adminFor!.length, 3);
expect(userInfo.adminFor![0].name, 'test_org_updated');
expect(userInfo.adminFor![0].image, 'https://testimg_updated.com');
expect(userInfo.adminFor![0].isPublic, true);
});

test('Check if Hive storage works', () async {
Hive
..init("./data")
..registerAdapter(UserAdapter())
..registerAdapter(OrgInfoAdapter());

final userBox = await Hive.openBox('userInfo');
expect(userBox.isOpen, true);

final userInfo = User.fromJson(testDataFromOrg, fromOrg: true);
userBox.put('user', userInfo);

final newUserBox = await Hive.openBox('userInfo');
final loadedUserInfo = newUserBox.get('user');

expect(loadedUserInfo.firstName, "ravidi");
expect(loadedUserInfo.lastName, "sheikh");
expect(loadedUserInfo.email, "[email protected]");
expect(loadedUserInfo.image, "https://testimg.com");
expect(loadedUserInfo.authToken, " ");
});
});
}

0 comments on commit 0c9dbc2

Please sign in to comment.