Lines
100 %
Functions
Branches
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.
// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
//! Unit testing
use {
crate::{
mock::{AccountId, ExtBuilder, LazyMigrations, RuntimeOrigin, Test},
Error,
},
frame_support::{assert_noop, assert_ok},
sp_core::{H160, H256},
sp_io::hashing::keccak_256,
};
fn address_build(seed: u8) -> H160 {
let address = H160::from(H256::from(keccak_256(&[seed; 32])));
address
}
fn create_dummy_contract_without_metadata(seed: u8) -> H160 {
let address = address_build(seed);
let dummy_code = vec![1, 2, 3];
pallet_evm::AccountCodes::<Test>::insert(address, dummy_code);
#[test]
fn test_create_contract_metadata_contract_not_exist() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(
LazyMigrations::create_contract_metadata(
RuntimeOrigin::signed(AccountId::from([45; 20])),
address_build(1),
),
Error::<Test>::ContractNotExist
);
});
fn test_create_contract_metadata_success_path() {
// Setup: create a dummy contract
let address = create_dummy_contract_without_metadata(1);
assert_ok!(LazyMigrations::create_contract_metadata(
address,
));
assert!(pallet_evm::AccountCodesMetadata::<Test>::get(address).is_some());
// Should not be able to set metadata again
Error::<Test>::ContractMetadataAlreadySet