Skip to content

Commit

Permalink
Merge pull request #75 from mangata-finance/feature/asset-info-tests
Browse files Browse the repository at this point in the history
Feature/asset info tests
  • Loading branch information
gleb-urvanov authored Jun 24, 2021
2 parents 85b2d20 + d32e452 commit 60a243f
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 13 deletions.
30 changes: 21 additions & 9 deletions pallets/assets-info/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2020 Mangata team

use crate::{Module, Trait};
use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types, weights::Weight};
use frame_system as system;
use mangata_primitives::{Amount, Balance, TokenId};
use orml_tokens as assets;
Expand All @@ -12,11 +12,22 @@ use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
Perbill,
};
mod assets_info {
pub use crate::Event;
}

impl_outer_origin! {
pub enum Origin for Test {}
}

impl_outer_event! {
pub enum TestEvent for Test {
frame_system<T>,
assets<T>,
assets_info,
}
}

// Configure a mock runtime to test the pallet.

#[derive(Clone, Eq, PartialEq)]
Expand All @@ -26,12 +37,12 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const MinLengthName: usize = 0;
pub const MaxLengthName: usize = 32;
pub const MinLengthSymbol: usize = 3;
pub const MinLengthName: usize = 1;
pub const MaxLengthName: usize = 8;
pub const MinLengthSymbol: usize = 1;
pub const MaxLengthSymbol: usize = 8;
pub const MinLengthDescription: usize = 0;
pub const MaxLengthDescription: usize = 255;
pub const MinLengthDescription: usize = 1;
pub const MaxLengthDescription: usize = 8;
pub const MaxDecimals: u32 = 10;
}

Expand All @@ -46,7 +57,7 @@ impl system::Trait for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
Expand All @@ -64,7 +75,7 @@ impl system::Trait for Test {
}

impl assets::Trait for Test {
type Event = ();
type Event = TestEvent;
type Balance = Balance;
type Amount = Amount;
type CurrencyId = TokenId;
Expand All @@ -73,7 +84,7 @@ impl assets::Trait for Test {
}

impl Trait for Test {
type Event = ();
type Event = TestEvent;
type MinLengthName = MinLengthName;
type MaxLengthName = MaxLengthName;
type MinLengthSymbol = MinLengthSymbol;
Expand All @@ -84,6 +95,7 @@ impl Trait for Test {
type Currency = MultiTokenCurrencyAdapter<Test>;
}

pub type System = system::Module<Test>;
pub type AssetsInfoModule = Module<Test>;

// Build genesis storage according to the mock runtime.
Expand Down
197 changes: 193 additions & 4 deletions pallets/assets-info/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (C) 2020 Mangata team

use crate::{mock::*, AssetInfo, Error};
use crate::{mock::*, AssetInfo, Error, Event};
use frame_support::{assert_noop, assert_ok};

#[test]
fn set_info_and_retrieve_works_ok() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: Some(b"name".to_vec()),
Expand All @@ -24,9 +25,18 @@ fn set_info_and_retrieve_works_ok() {
));
// Read pallet storage and assert an expected result.
assert_eq!(AssetsInfoModule::get_info(ASSET_ID), info);

let info_stored_event =
TestEvent::assets_info(Event::InfoStored(ASSET_ID, info));

assert!(System::events()
.iter()
.any(|record| record.event == info_stored_event));
});
}



#[test]
fn set_info_optional_and_retrieve_works_ok() {
new_test_ext().execute_with(|| {
Expand All @@ -53,12 +63,191 @@ fn set_info_optional_and_retrieve_works_ok() {
}

#[test]
fn correct_error_for_invalid_symbol_value() {
fn min_length_name_check() {
new_test_ext().execute_with(|| {
// Ensure the expected error is thrown when no value is present.
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: Some(vec![]),
symbol: None,
description: None,
decimals: None,
};
// Dispatch a signed extrinsic.

assert_noop!(
AssetsInfoModule::set_info(Origin::root(), 0, None, Some(vec![]), None, None,),
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::TooShortName
);
// Read pallet storage and assert an expected result.

});
}

#[test]
fn min_length_symbol_check() {
new_test_ext().execute_with(|| {
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: None,
symbol: Some(vec![]),
description: None,
decimals: Some(8),
};
// Dispatch a signed extrinsic.
assert_noop!(
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::TooShortSymbol
);
// Read pallet storage and assert an expected result.

});
}

#[test]
fn min_length_description_check() {
new_test_ext().execute_with(|| {
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: None,
symbol: None,
description: Some(vec![]),
decimals: Some(8),
};
// Dispatch a signed extrinsic.
assert_noop!(
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::TooShortDescription
);
// Read pallet storage and assert an expected result.

});
}

#[test]
fn max_length_name_check() {
new_test_ext().execute_with(|| {
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: Some(b"veryLongName".to_vec()),
symbol: Some(b"SYM".to_vec()),
description: Some(b"desc".to_vec()),
decimals: Some(8),
};
// Dispatch a signed extrinsic.
assert_noop!(
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::TooLongName
);
// Read pallet storage and assert an expected result.

});
}

#[test]
fn max_length_symbol_check() {
new_test_ext().execute_with(|| {
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: Some(b"name".to_vec()),
symbol: Some(b"veryLongSymbol".to_vec()),
description: Some(b"desc".to_vec()),
decimals: Some(8),
};
// Dispatch a signed extrinsic.
assert_noop!(
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::TooLongSymbol
);
// Read pallet storage and assert an expected result.

});
}

#[test]
fn max_length_description_check() {
new_test_ext().execute_with(|| {
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: Some(b"name".to_vec()),
symbol: Some(b"SYM".to_vec()),
description: Some(b"veryLongDescription".to_vec()),
decimals: Some(8),
};
// Dispatch a signed extrinsic.
assert_noop!(
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::TooLongDescription
);
// Read pallet storage and assert an expected result.

});
}

#[test]
fn max_decimals_check() {
new_test_ext().execute_with(|| {
const ASSET_ID: u32 = 0;
let info = AssetInfo {
name: Some(b"name".to_vec()),
symbol: Some(b"SYM".to_vec()),
description: Some(b"desc".to_vec()),
decimals: Some(11),
};
// Dispatch a signed extrinsic.
assert_noop!(
AssetsInfoModule::set_info(
Origin::root(),
ASSET_ID,
info.name.clone(),
info.symbol.clone(),
info.description.clone(),
info.decimals,
),
Error::<Test>::DecimalsOutOfRange
);
// Read pallet storage and assert an expected result.

});
}

0 comments on commit 60a243f

Please sign in to comment.