From 2d7c77c3fe3a47e981f297aff1b355d400593b6a Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Thu, 16 Nov 2023 11:55:37 +0100 Subject: [PATCH] move balance transfer tests to separate file --- runtime/gdev/tests/balance_tests.rs | 129 ++++++++++++++++++++++++ runtime/gdev/tests/integration_tests.rs | 83 --------------- 2 files changed, 129 insertions(+), 83 deletions(-) create mode 100644 runtime/gdev/tests/balance_tests.rs diff --git a/runtime/gdev/tests/balance_tests.rs b/runtime/gdev/tests/balance_tests.rs new file mode 100644 index 000000000..8db1efb15 --- /dev/null +++ b/runtime/gdev/tests/balance_tests.rs @@ -0,0 +1,129 @@ +// Copyright 2021 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/>. + +// this file is for balance transfer tests (split from integration_tests) + +mod common; + +use common::*; +use frame_support::{assert_noop, assert_ok}; +use gdev_runtime::*; +use sp_keyring::AccountKeyring; + +/// test currency transfer +/// (does not take fees into account because it's only calls, not extrinsics) +#[test] +fn test_transfer() { + ExtBuilder::new(1, 3, 4) + .with_initial_balances(vec![ + (AccountKeyring::Alice.to_account_id(), 10_000), + (AccountKeyring::Eve.to_account_id(), 10_000), + ]) + .build() + .execute_with(|| { + // Alice gives 500 to Eve + assert_ok!(Balances::transfer_allow_death( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + AccountKeyring::Eve.to_account_id().into(), + 500 + )); + // check amounts + assert_eq!( + Balances::free_balance(AccountKeyring::Alice.to_account_id()), + 10_000 - 500 + ); + assert_eq!( + Balances::free_balance(AccountKeyring::Eve.to_account_id()), + 10_000 + 500 + ); + }) +} + +/// test balance transfer +#[test] +fn test_transfer() { + ExtBuilder::new(1, 3, 4) + .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 600)]) + .build() + .execute_with(|| { + run_to_block(1); + assert_ok!(Balances::transfer_allow_death( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + AccountKeyring::Dave.to_account_id().into(), + 500 + )); + }) +} + +/// test balance transfer without enough balance (would prefer TokenError::BelowMinimum or NotExpendable) +#[test] +fn test_transfer_not_enough() { + ExtBuilder::new(1, 3, 4) + .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 599)]) + .build() + .execute_with(|| { + run_to_block(1); + assert_noop!( + Balances::transfer_allow_death( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + AccountKeyring::Dave.to_account_id().into(), + 500 + ), + sp_runtime::TokenError::Frozen + ); + }) +} + +/// test balance transfer without enough balance (would prefer TokenError::FundsUnavailable) +#[test] +fn test_transfer_underflow() { + ExtBuilder::new(1, 3, 4) + .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 499)]) + .build() + .execute_with(|| { + run_to_block(1); + assert_noop!( + Balances::transfer_allow_death( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + AccountKeyring::Dave.to_account_id().into(), + 500 + ), + sp_runtime::ArithmeticError::Underflow + ); + }) +} + +/// test balance transfer without enough balance +#[test] +fn test_transfer_funds_unavailable() { + ExtBuilder::new(1, 3, 4) + .with_initial_balances(vec![ + (AccountKeyring::Alice.to_account_id(), 499), + (AccountKeyring::Bob.to_account_id(), 200), + ]) + .build() + .execute_with(|| { + run_to_block(1); + assert_noop!( + Balances::transfer_allow_death( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + AccountKeyring::Dave.to_account_id().into(), + 500 + ), + sp_runtime::TokenError::FundsUnavailable + ); + }) +} diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs index a44640ad4..acb895ca9 100644 --- a/runtime/gdev/tests/integration_tests.rs +++ b/runtime/gdev/tests/integration_tests.rs @@ -184,60 +184,6 @@ fn test_total_issuance_vs_monetary_mass() { }); } -/// test balance transfer -#[test] -fn test_simple_transfer() { - ExtBuilder::new(1, 3, 4) - .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 600)]) - .build() - .execute_with(|| { - run_to_block(1); - assert_ok!(Balances::transfer_allow_death( - frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), - AccountKeyring::Dave.to_account_id().into(), - 500 - )); - }) -} - -/// test balance transfer without enough balance (would prefer TokenError::BelowMinimum or NotExpendable) -#[test] -fn test_simple_transfer_not_enough() { - ExtBuilder::new(1, 3, 4) - .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 599)]) - .build() - .execute_with(|| { - run_to_block(1); - assert_noop!( - Balances::transfer_allow_death( - frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), - AccountKeyring::Dave.to_account_id().into(), - 500 - ), - sp_runtime::TokenError::Frozen - ); - }) -} - -/// test balance transfer without enough balance (would prefer TokenError::FundsUnavailable) -#[test] -fn test_simple_transfer_underflow() { - ExtBuilder::new(1, 3, 4) - .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 499)]) - .build() - .execute_with(|| { - run_to_block(1); - assert_noop!( - Balances::transfer_allow_death( - frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), - AccountKeyring::Dave.to_account_id().into(), - 500 - ), - sp_runtime::ArithmeticError::Underflow - ); - }) -} - /// test identity go below ED #[test] fn test_identity_below_ed() { @@ -1179,35 +1125,6 @@ fn test_oneshot_accounts() { }); } -/// test currency transfer -/// (does not take fees into account because it's only calls, not extrinsics) -#[test] -fn test_transfer() { - ExtBuilder::new(1, 3, 4) - .with_initial_balances(vec![ - (AccountKeyring::Alice.to_account_id(), 10_000), - (AccountKeyring::Eve.to_account_id(), 10_000), - ]) - .build() - .execute_with(|| { - // Alice gives 500 to Eve - assert_ok!(Balances::transfer_allow_death( - frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), - AccountKeyring::Eve.to_account_id().into(), - 500 - )); - // check amounts - assert_eq!( - Balances::free_balance(AccountKeyring::Alice.to_account_id()), - 10_000 - 500 - ); - assert_eq!( - Balances::free_balance(AccountKeyring::Eve.to_account_id()), - 10_000 + 500 - ); - }) -} - /// test linking account to identity #[test] fn test_link_account() { -- GitLab