Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: add
  • Loading branch information
wischli committed Mar 15, 2023
commit 47443ec7f97505657c777cc23180c96ed45d1db0
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frame/treasury-oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ frame-system = { version = "4.0.0-dev", default-features = false, path = "../sys
sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../balances" }
sp-core = { version = "7.0.0", path = "../../primitives/core" }
sp-io = { version = "7.0.0", path = "../../primitives/io" }

Expand All @@ -33,6 +34,7 @@ std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"scale-info/std",
"sp-runtime/std",
]
Expand Down
18 changes: 7 additions & 11 deletions frame/treasury-oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use sp_runtime::{traits::Zero, FixedPointNumber, FixedPointOperand, FixedU128};

pub use pallet::*;

// #[cfg(test)]
// mod mock;
#[cfg(test)]
mod mock;

// #[cfg(test)]
// mod tests;
#[cfg(test)]
mod tests;

// #[cfg(feature = "runtime-benchmarks")]
// mod benchmarking;
Expand Down Expand Up @@ -94,7 +94,7 @@ pub mod pallet {
// Some `asset_id` conversion rate was created.
Created { asset_id: T::AssetId, rate: FixedU128 },
// Some `asset_id` conversion rate was removed.
Removed { asset_id: T::AssetId, rate: FixedU128 },
Removed { asset_id: T::AssetId },
// Some existing `asset_id` conversion rate was updated from `old` to `new`.
Updated { asset_id: T::AssetId, old: FixedU128, new: FixedU128 },
}
Expand Down Expand Up @@ -146,17 +146,13 @@ pub mod pallet {
}

#[pallet::call_index(2)]
pub fn remove(
origin: OriginFor<T>,
asset_id: T::AssetId,
rate: FixedU128,
) -> DispatchResult {
pub fn remove(origin: OriginFor<T>, asset_id: T::AssetId) -> DispatchResult {
T::RemoveOrigin::ensure_origin(origin)?;

ensure!(ConversionRateToNative::<T>::contains_key(asset_id), Error::<T>::Unknown);
ConversionRateToNative::<T>::remove(asset_id);

Self::deposit_event(Event::Removed { asset_id, rate });
Self::deposit_event(Event::Removed { asset_id });
Ok(())
}
}
Expand Down
96 changes: 96 additions & 0 deletions frame/treasury-oracle/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The crate's mock.

use crate as pallet_treasury_oracle;
use frame_support::traits::{ConstU16, ConstU64};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system,
TreasuryOracle: pallet_treasury_oracle,
Balances: pallet_balances,
}
);

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl pallet_balances::Config for Test {
type Balance = u64;
type DustRemoval = ();
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
}

impl pallet_treasury_oracle::Config for Test {
type WeightInfo = ();
type RuntimeEvent = RuntimeEvent;
type CreateOrigin = frame_system::EnsureRoot<u64>;
type RemoveOrigin = frame_system::EnsureSigned<u64>;
type UpdateOrigin = frame_system::EnsureSigned<u64>;
type Balance = u64;
type Currency = Balances;
type AssetId = u32;
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
128 changes: 128 additions & 0 deletions frame/treasury-oracle/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The crate's tests.

use super::*;
use crate::pallet as pallet_treasury_oracle;
use frame_support::{assert_noop, assert_ok};
use mock::{new_test_ext, RuntimeOrigin, Test, TreasuryOracle};
use sp_runtime::FixedU128;

const ASSET_ID: u32 = 42;

#[test]
fn create_works() {
new_test_ext().execute_with(|| {
assert!(TreasuryOracle::conversion_rate_to_native(ASSET_ID).is_none());
assert_ok!(TreasuryOracle::create(
RuntimeOrigin::root(),
ASSET_ID,
FixedU128::from_float(0.1)
));

assert_eq!(
TreasuryOracle::conversion_rate_to_native(ASSET_ID),
Some(FixedU128::from_float(0.1))
);
});
}

#[test]
fn remove_works() {
new_test_ext().execute_with(|| {
assert_ok!(TreasuryOracle::create(
RuntimeOrigin::root(),
ASSET_ID,
FixedU128::from_float(0.1)
));

assert_ok!(TreasuryOracle::remove(RuntimeOrigin::signed(1), ASSET_ID,));
assert!(TreasuryOracle::conversion_rate_to_native(ASSET_ID).is_none());
});
}

#[test]
fn remove_unknown_throws() {
new_test_ext().execute_with(|| {
assert_noop!(
TreasuryOracle::remove(RuntimeOrigin::signed(1), ASSET_ID,),
Error::<Test>::Unknown
);
});
}

#[test]
fn update_works() {
new_test_ext().execute_with(|| {
assert_ok!(TreasuryOracle::create(
RuntimeOrigin::root(),
ASSET_ID,
FixedU128::from_float(0.1)
));
assert_ok!(TreasuryOracle::update(
RuntimeOrigin::signed(1),
ASSET_ID,
FixedU128::from_float(0.5)
));

assert_eq!(
TreasuryOracle::conversion_rate_to_native(ASSET_ID),
Some(FixedU128::from_float(0.5))
);
});
}

#[test]
fn update_unknown_throws() {
new_test_ext().execute_with(|| {
assert_noop!(
TreasuryOracle::update(RuntimeOrigin::signed(1), ASSET_ID, FixedU128::from_float(0.5)),
Error::<Test>::Unknown
);
});
}

#[test]
fn convert_works() {
new_test_ext().execute_with(|| {
assert_ok!(TreasuryOracle::create(
RuntimeOrigin::root(),
ASSET_ID,
FixedU128::from_float(2.51)
));

let conversion = <TreasuryOracle as BalanceConversion<
BalanceOf<Test>,
<Test as pallet_treasury_oracle::Config>::AssetId,
BalanceOf<Test>,
>>::to_asset_balance(10, ASSET_ID);
assert_eq!(conversion.expect("Conversion rate exists for asset"), 25);
});
}

#[test]
fn convert_unknown_works() {
new_test_ext().execute_with(|| {
let conversion = <TreasuryOracle as BalanceConversion<
BalanceOf<Test>,
<Test as pallet_treasury_oracle::Config>::AssetId,
BalanceOf<Test>,
>>::to_asset_balance(10, ASSET_ID);
assert!(conversion.is_err());
});
}