Skip to content

Commit

Permalink
department tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Aug 9, 2024
1 parent 48de1b5 commit c5aa09c
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions custom-pallets/departments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,154 @@ fn add_member_to_department_fails_if_already_member() {
);
});
}

#[test]
fn remove_member_from_department_works() {
new_test_ext().execute_with(|| {
// Go past genesis block so events get deposited
System::set_block_number(1);
let admin_account_id = 1;
let member_account_id = 2;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);

// Create a department
assert_ok!(Departments::create_department(
RuntimeOrigin::signed(admin_account_id),
content.clone()
));

// Get the department ID
let department_id = Departments::next_department_id() - 1;

// Add a member to the department
assert_ok!(Departments::add_member_to_department(
RuntimeOrigin::signed(admin_account_id),
department_id,
member_account_id
));

// Remove the member from the department
assert_ok!(Departments::remove_member_from_department(
RuntimeOrigin::signed(admin_account_id),
department_id,
member_account_id
));

// Check that the member was removed correctly
let stored_accounts = Departments::department_accounts(department_id).unwrap();
assert!(!stored_accounts.contains(&member_account_id));

// Verify that the correct event was emitted
System::assert_last_event(
Event::MemberRemoved { remove_member: member_account_id, department_id }.into(),
);
});
}

#[test]
fn remove_member_from_department_fails_if_not_admin() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let non_admin_account_id = 2;
let member_account_id = 3;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);

// Create a department with admin_account_id = 1
assert_ok!(Departments::create_department(RuntimeOrigin::signed(1), content.clone()));

// Get the department ID
let department_id = Departments::next_department_id() - 1;

// Attempt to remove a member with a non-admin account, should fail
assert_noop!(
Departments::remove_member_from_department(
RuntimeOrigin::signed(non_admin_account_id),
department_id,
member_account_id
),
Error::<Test>::NotAdmin
);
});
}

#[test]
fn remove_member_from_department_fails_if_not_a_member() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let admin_account_id = 1;
let member_account_id = 2;
let non_member_account_id = 3;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);

// Create a department
assert_ok!(Departments::create_department(
RuntimeOrigin::signed(admin_account_id),
content.clone()
));

// Get the department ID
let department_id = Departments::next_department_id() - 1;

// Add a member to the department
assert_ok!(Departments::add_member_to_department(
RuntimeOrigin::signed(admin_account_id),
department_id,
member_account_id
));

// Attempt to remove a non-member, should fail
assert_noop!(
Departments::remove_member_from_department(
RuntimeOrigin::signed(admin_account_id),
department_id,
non_member_account_id
),
Error::<Test>::NotMember
);
});
}

#[test]
fn remove_member_from_department_fails_if_no_accounts() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let admin_account_id = 1;
let member_account_id = 2;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);

// Create a department
assert_ok!(Departments::create_department(
RuntimeOrigin::signed(admin_account_id),
content.clone()
));

// Get the department ID
let department_id = Departments::next_department_id() - 1;

// Attempt to remove a member when there are no accounts, should fail
assert_noop!(
Departments::remove_member_from_department(
RuntimeOrigin::signed(admin_account_id),
department_id,
member_account_id
),
Error::<Test>::NoAccounts
);
});
}

0 comments on commit c5aa09c

Please sign in to comment.