Skip to content

Commit

Permalink
department test
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Aug 8, 2024
1 parent 25fef8b commit a51d7df
Show file tree
Hide file tree
Showing 4 changed files with 666 additions and 9 deletions.
3 changes: 2 additions & 1 deletion custom-pallets/departments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,10 @@ pub mod pallet {
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn create_department(origin: OriginFor<T>, content: Content) -> DispatchResult {
let who = ensure_signed(origin)?;

let new_department_id = Self::next_department_id();

let who = ensure_signed(origin)?;
let new_department: DepartmentDetails<T> =
DepartmentDetails::new(new_department_id, content, who.clone());

Expand Down
5 changes: 4 additions & 1 deletion custom-pallets/departments/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
CreateDepartmentModule: pallet_template,
Timestamp: pallet_timestamp,
Departments: pallet_template,
}
);

Expand Down Expand Up @@ -63,6 +64,8 @@ impl pallet_template::Config for Test {
type WeightInfo = ();
}



// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
Expand Down
32 changes: 25 additions & 7 deletions custom-pallets/departments/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
use crate::{mock::*, Error, Event, Something};
use frame_support::{assert_noop, assert_ok};
use pallet_support::Content;

#[test]
fn it_works_for_default_value() {
fn create_department_works() {
new_test_ext().execute_with(|| {
// Go past genesis block so events get deposited
// // Go past genesis block so events get deposited
System::set_block_number(1);
});
}
let account_id = 1;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(Departments::create_department(
RuntimeOrigin::signed(account_id),
content.clone()
));

#[test]
fn correct_error_for_none_value() {
new_test_ext().execute_with(|| {});
// Check that the department was stored correctly
let department_id = Departments::next_department_id() - 1;
let stored_department = Departments::departments(department_id).unwrap();
assert_eq!(stored_department.department_id, department_id);
assert_eq!(stored_department.content, content);
assert_eq!(stored_department.department_admin, account_id);

// Verify that the correct event was emitted
System::assert_last_event(
Event::DepartmentCreated { account: account_id, department_id }.into(),
);
});
}
Loading

0 comments on commit a51d7df

Please sign in to comment.