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

Added tests for Invalid Asset Type. Fixes #374 #548

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

ritankarsaha
Copy link
Contributor

@ritankarsaha ritankarsaha commented Dec 25, 2024

Fixes #374

This PR introduces a new unit test, asset_create_should_fail_with_invalid_asset_type, to verify that the Asset::create function fails as expected when provided with an invalid asset type.
The test attempts to create the asset using the invalid entry and checks that the Asset::create function returns the expected Error::<Test>::InvalidAssetType. This ensures the system rejects invalid asset types correctly.

Screenshot 2024-12-26 at 1 28 19 AM

The test has passed as well, as shown in the screenshot.

@JiyaGupta-cs
Copy link
Contributor

@ritankarsaha This approach doesn't solve the problem as you have created a new type INVALID and in your tests you generate an error by creating INVALID type and then checking for the same

@ritankarsaha
Copy link
Contributor Author

@JiyaGupta-cs i am trying to create invalid entry by having invalid type However, the invalid type seems to be automatically converted into AssetTypeOf::MF. This results in the assertion for assert_err not matching as expected.

This is the output I am receiving:-

---- tests::asset_create_should_fail_with_invalid_asset_type stdout ----
Invalid type being tested: AssetTypeOf::MF
thread 'tests::asset_create_should_fail_with_invalid_asset_type' panicked at pallets/asset/src/tests.rs:2887:9:
assertion `left == right` failed
  left: Ok(())
 right: Err(Module(ModuleError { index: 2, error: [7, 0, 0, 0], message: Some("InvalidAssetType") }))

My initial approach to the code was:-

new_test_ext().execute_with(|| {
		assert_ok!(Space::create(
			DoubleOrigin(author.clone(), creator.clone()).into(),
			space_digest
		));

		assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, capacity));

		
		let invalid_entry = AssetInputEntryOf::<Test> {
			asset_desc: BoundedVec::try_from([72u8; 10].to_vec()).unwrap(),
			asset_tag: BoundedVec::try_from([72u8; 10].to_vec()).unwrap(),
			asset_meta: BoundedVec::try_from([72u8; 10].to_vec()).unwrap(),
			asset_qty: 10,
			asset_value: 10,
			asset_type: unsafe { std::mem::transmute(3u8) },
		};
		println!("Invalid type being tested: {:?}", invalid_entry.asset_type);

		let invalid_digest = <Test as frame_system::Config>::Hashing::hash(
			&[
				&invalid_entry.encode()[..],
				&[0u8; 32].to_vec()[..], 
			]
			.concat()[..],
		);

		assert_err!(
			Asset::create(
				DoubleOrigin(author, creator).into(),
				invalid_entry,
				invalid_digest,
				authorization_id
			),
			Error::<Test>::InvalidAssetType
		);
	});

@JiyaGupta-cs @vatsa287 any suggestions for this?

@ritankarsaha
Copy link
Contributor Author

@vatsa287 is this implementation correct? Or do i need to make certain changes in this PR?

@ritankarsaha
Copy link
Contributor Author

@vatsa287 does this PR require any more changes?

@vatsa287
Copy link
Member

@ritankarsaha This might be a better approach IMO. Please refer below and add a different new commit.
#374 (comment)

@ritankarsaha
Copy link
Contributor Author

@ritankarsaha This might be a better approach IMO. Please refer below and add a different new commit.

#374 (comment)

Sure @vatsa287

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C4GT] Asset: Add tests for InvalidAssetType
3 participants