// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute // SPDX-License-Identifier: MIT-0 // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //! Tests for pezpallet-example-basic. use crate::*; use pezframe_support::{ assert_ok, derive_impl, dispatch::{DispatchInfo, GetDispatchInfo}, traits::{ConstU64, OnInitialize}, }; use pezsp_core::H256; // The testing primitives are very useful for avoiding having to work with signatures // or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. use pezsp_runtime::{ traits::{BlakeTwo256, DispatchTransaction, IdentityLookup}, transaction_validity::TransactionSource::External, BuildStorage, }; // Reexport crate as its pezpallet name for construct_runtime. use crate as pezpallet_example_basic; type Block = pezframe_system::mocking::MockBlock; // For testing the pezpallet, we construct a mock runtime. pezframe_support::construct_runtime!( pub enum Test { System: pezframe_system, Balances: pezpallet_balances, Example: pezpallet_example_basic, } ); #[derive_impl(pezframe_system::config_preludes::TestDefaultConfig)] impl pezframe_system::Config for Test { type BaseCallFilter = pezframe_support::traits::Everything; type BlockWeights = (); type BlockLength = (); type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; type Nonce = u64; type Hash = H256; type RuntimeCall = RuntimeCall; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Block = Block; type RuntimeEvent = RuntimeEvent; type Version = (); type PalletInfo = PalletInfo; type AccountData = pezpallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); type OnSetCode = (); type MaxConsumers = pezframe_support::traits::ConstU32<16>; } #[derive_impl(pezpallet_balances::config_preludes::TestDefaultConfig)] impl pezpallet_balances::Config for Test { type AccountStore = System; } impl Config for Test { type MagicNumber = ConstU64<1_000_000_000>; type WeightInfo = (); } // This function basically just builds a genesis storage key/value store according to // our desired mockup. pub fn new_test_ext() -> pezsp_io::TestExternalities { let t = RuntimeGenesisConfig { // We use default for brevity, but you can configure as desired if needed. system: Default::default(), balances: Default::default(), example: pezpallet_example_basic::GenesisConfig { dummy: 42, // we configure the map with (key, value) pairs. bar: alloc::vec![(1, 2), (2, 3)], foo: 24, }, } .build_storage() .unwrap(); t.into() } #[test] fn it_works_for_optional_value() { new_test_ext().execute_with(|| { // Check that GenesisBuilder works properly. let val1 = 42; let val2 = 27; assert_eq!(Dummy::::get(), Some(val1)); // Check that accumulate works when we have Some value in Dummy already. assert_ok!(Example::accumulate_dummy(RuntimeOrigin::signed(1), val2)); assert_eq!(Dummy::::get(), Some(val1 + val2)); // Check that accumulate works when we Dummy has None in it. >::on_initialize(2); assert_ok!(Example::accumulate_dummy(RuntimeOrigin::signed(1), val1)); assert_eq!(Dummy::::get(), Some(val1 + val2 + val1)); }); } #[test] fn it_works_for_default_value() { new_test_ext().execute_with(|| { assert_eq!(Foo::::get(), 24); assert_ok!(Example::accumulate_foo(RuntimeOrigin::signed(1), 1)); assert_eq!(Foo::::get(), 25); }); } #[test] fn set_dummy_works() { new_test_ext().execute_with(|| { let test_val = 133; assert_ok!(Example::set_dummy(RuntimeOrigin::root(), test_val.into())); assert_eq!(Dummy::::get(), Some(test_val)); }); } #[test] fn signed_ext_watch_dummy_works() { new_test_ext().execute_with(|| { let call = pezpallet_example_basic::Call::set_dummy { new_value: 10 }.into(); let info = DispatchInfo::default(); assert_eq!( WatchDummy::(PhantomData) .validate_only(Some(1).into(), &call, &info, 150, External, 0) .unwrap() .0 .priority, u64::MAX, ); assert_eq!( WatchDummy::(PhantomData) .validate_only(Some(1).into(), &call, &info, 250, External, 0) .unwrap_err(), InvalidTransaction::ExhaustsResources.into(), ); }) } #[test] fn counted_map_works() { new_test_ext().execute_with(|| { assert_eq!(CountedMap::::count(), 0); CountedMap::::insert(3, 3); assert_eq!(CountedMap::::count(), 1); }) } #[test] fn weights_work() { // must have a defined weight. let default_call = pezpallet_example_basic::Call::::accumulate_dummy { increase_by: 10 }; let info1 = default_call.get_dispatch_info(); // aka. `let info = as GetDispatchInfo>::get_dispatch_info(&default_call);` // TODO: account for proof size weight assert!(info1.call_weight.ref_time() > 0); assert_eq!(info1.call_weight, ::WeightInfo::accumulate_dummy()); // `set_dummy` is simpler than `accumulate_dummy`, and the weight // should be less. let custom_call = pezpallet_example_basic::Call::::set_dummy { new_value: 20 }; let info2 = custom_call.get_dispatch_info(); // TODO: account for proof size weight assert!(info1.call_weight.ref_time() > info2.call_weight.ref_time()); }