diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index b99b5fadb7a496632e1338eadd075cbe2f96edfa..8f0e7df822660fe2fbee8287addddcf96b06e91e 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -26,6 +26,8 @@ pub mod benchmarking; #[cfg(test)] mod mock; +#[cfg(test)] +mod tests; pub use pallet::*; pub use traits::*; diff --git a/pallets/distance/src/mock.rs b/pallets/distance/src/mock.rs index 64bb309b93a0463355fa1d1b0cd1e3a6bc741112..45059bd5d5d83b13f715a723c842b06e4391b7a9 100644 --- a/pallets/distance/src/mock.rs +++ b/pallets/distance/src/mock.rs @@ -19,7 +19,7 @@ use crate::{self as pallet_distance}; use core::marker::PhantomData; use frame_support::{ parameter_types, - traits::{Everything, GenesisBuild}, + traits::{Everything, GenesisBuild, OnFinalize, OnInitialize}, }; use frame_system as system; use pallet_balances::AccountData; @@ -291,3 +291,14 @@ pub fn new_test_ext() -> sp_io::TestExternalities { sp_io::TestExternalities::new(t) } + +pub fn run_to_block(n: u64) { + while System::block_number() < n { + Session::on_finalize(System::block_number()); + System::on_finalize(System::block_number()); + System::reset_events(); + System::set_block_number(System::block_number() + 1); + System::on_initialize(System::block_number()); + Session::on_initialize(System::block_number()); + } +} diff --git a/pallets/distance/src/tests.rs b/pallets/distance/src/tests.rs new file mode 100644 index 0000000000000000000000000000000000000000..b1be3c04a488d49496992b8166235d9229554261 --- /dev/null +++ b/pallets/distance/src/tests.rs @@ -0,0 +1,41 @@ +// Copyright 2023 Axiom-Team +// +// This file is part of Duniter-v2S. +// +// Duniter-v2S is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// Duniter-v2S 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>. + +use crate::mock::*; +use crate::*; +use frame_support::assert_ok; +use frame_support::traits::Currency; + +#[test] +fn test_request_distance_evaluation() { + new_test_ext().execute_with(|| { + run_to_block(1); + // give enough for reserve + Balances::make_free_balance_be(&1, 10_000); + + // call request + assert_ok!(Distance::request_distance_evaluation( + RuntimeOrigin::signed(1) + )); + System::assert_has_event(RuntimeEvent::Distance(Event::EvaluationRequested { + idty_index: 1, + who: 1, + })); + + // currency was reserved + assert_eq!(Balances::reserved_balance(1), 1000); + }); +}