From cb99b8f65e368899653d1117cab366739744b6ec Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Wed, 10 Jan 2024 16:50:23 +0100 Subject: [PATCH] distance pallet unit test stub --- pallets/distance/src/lib.rs | 2 ++ pallets/distance/src/mock.rs | 13 ++++++++++- pallets/distance/src/tests.rs | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pallets/distance/src/tests.rs diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index b99b5fadb..8f0e7df82 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 64bb309b9..45059bd5d 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 000000000..b1be3c04a --- /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); + }); +} -- GitLab