Skip to content

Commit a84af10

Browse files
authored
Merge pull request #1011 from opentensor/chore/move-unit-tests
Move unit tests to the right place
2 parents 88fe02d + 27a3194 commit a84af10

27 files changed

+50938
-50954
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use sp_runtime::{traits::Member, RuntimeAppPublic};
99

1010
mod benchmarking;
1111

12+
#[cfg(test)]
13+
mod tests;
14+
1215
#[deny(missing_docs)]
1316
#[frame_support::pallet]
1417
pub mod pallet {

pallets/admin-utils/tests/mock.rs renamed to pallets/admin-utils/src/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ frame_support::construct_runtime!(
2525
{
2626
System: frame_system = 1,
2727
Balances: pallet_balances = 2,
28-
AdminUtils: pallet_admin_utils = 3,
28+
AdminUtils: crate = 3,
2929
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
3030
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
3131
}
@@ -243,7 +243,7 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
243243
}
244244
}
245245

246-
impl pallet_admin_utils::Config for Test {
246+
impl crate::Config for Test {
247247
type RuntimeEvent = RuntimeEvent;
248248
type AuthorityId = AuraId;
249249
type MaxAuthorities = ConstU32<32>;

pallets/admin-utils/tests/tests.rs renamed to pallets/admin-utils/src/tests/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use frame_support::{
44
dispatch::{DispatchClass, GetDispatchInfo, Pays},
55
};
66
use frame_system::Config;
7-
use pallet_admin_utils::Error;
87
use pallet_subtensor::Error as SubtensorError;
98
use pallet_subtensor::{migrations, Event};
109
use sp_core::U256;
1110

12-
mod mock;
11+
use crate::Error;
1312
use mock::*;
1413

14+
mod mock;
15+
1516
#[test]
1617
fn test_sudo_set_default_take() {
1718
new_test_ext().execute_with(|| {
@@ -1183,7 +1184,7 @@ fn test_set_alpha_values_dispatch_info_ok() {
11831184
let netuid: u16 = 1;
11841185
let alpha_low: u16 = 12_u16;
11851186
let alpha_high: u16 = u16::MAX - 10;
1186-
let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::sudo_set_alpha_values {
1187+
let call = RuntimeCall::AdminUtils(crate::Call::sudo_set_alpha_values {
11871188
netuid,
11881189
alpha_low,
11891190
alpha_high,

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub mod utils;
4848
use crate::utils::rate_limiting::TransactionType;
4949
use macros::{config, dispatches, errors, events, genesis, hooks};
5050

51+
#[cfg(test)]
52+
mod tests;
53+
5154
// apparently this is stabilized since rust 1.36
5255
extern crate alloc;
5356

pallets/subtensor/tests/batch_tx.rs renamed to pallets/subtensor/src/tests/batch_tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use frame_support::{assert_ok, traits::Currency};
22
use frame_system::Config;
33
use sp_core::U256;
4-
mod mock;
5-
use mock::*;
4+
5+
use super::mock::*;
66

77
#[test]
88
fn test_batch_txs() {

pallets/subtensor/tests/children.rs renamed to pallets/subtensor/src/tests/children.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![allow(clippy::indexing_slicing)]
2-
use crate::mock::*;
2+
use super::mock::*;
33
use frame_support::{assert_err, assert_noop, assert_ok};
4-
mod mock;
5-
use pallet_subtensor::{utils::rate_limiting::TransactionType, *};
4+
5+
use crate::{utils::rate_limiting::TransactionType, *};
66
use sp_core::U256;
77

88
// 1: Successful setting of a single child
@@ -3461,7 +3461,7 @@ fn test_childkey_take_drain() {
34613461
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
34623462
SubtensorModule::set_max_allowed_validators(netuid, 2);
34633463
step_block(subnet_tempo);
3464-
pallet_subtensor::SubnetOwnerCut::<Test>::set(0);
3464+
crate::SubnetOwnerCut::<Test>::set(0);
34653465

34663466
// Set 20% childkey take
34673467
let max_take: u16 = 0xFFFF / 5;
@@ -3509,15 +3509,15 @@ fn test_childkey_take_drain() {
35093509
vec![(proportion, child)]
35103510
));
35113511
// Make all stakes viable
3512-
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
3513-
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
3512+
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
3513+
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
35143514

35153515
// Setup YUMA so that it creates emissions:
35163516
// Parent and child both set weights
35173517
// Parent and child register on root and
35183518
// Set root weights
3519-
pallet_subtensor::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3520-
pallet_subtensor::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3519+
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3520+
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
35213521
assert_ok!(SubtensorModule::do_root_register(
35223522
RuntimeOrigin::signed(coldkey),
35233523
parent,
@@ -3526,17 +3526,17 @@ fn test_childkey_take_drain() {
35263526
RuntimeOrigin::signed(coldkey),
35273527
child,
35283528
));
3529-
pallet_subtensor::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3530-
pallet_subtensor::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3529+
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3530+
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
35313531

35323532
// Run run_coinbase until PendingHotkeyEmission are populated
3533-
while pallet_subtensor::PendingdHotkeyEmission::<Test>::get(child) == 0 {
3533+
while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
35343534
step_block(1);
35353535
}
35363536

35373537
// Prevent further subnet epochs
3538-
pallet_subtensor::Tempo::<Test>::set(netuid, u16::MAX);
3539-
pallet_subtensor::Tempo::<Test>::set(root_id, u16::MAX);
3538+
crate::Tempo::<Test>::set(netuid, u16::MAX);
3539+
crate::Tempo::<Test>::set(root_id, u16::MAX);
35403540

35413541
// Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
35423542
step_block((hotkey_tempo * 2) as u16);
@@ -3545,9 +3545,9 @@ fn test_childkey_take_drain() {
35453545
// - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
35463546
// - Parent stake increased by 40% of total emission
35473547
// - Nominator stake increased by 50% of total emission
3548-
let child_emission = pallet_subtensor::Stake::<Test>::get(child, coldkey);
3549-
let parent_emission = pallet_subtensor::Stake::<Test>::get(parent, coldkey) - stake;
3550-
let nominator_emission = pallet_subtensor::Stake::<Test>::get(child, nominator) - stake;
3548+
let child_emission = crate::Stake::<Test>::get(child, coldkey);
3549+
let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
3550+
let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
35513551
let total_emission = child_emission + parent_emission + nominator_emission;
35523552

35533553
assert!(is_within_tolerance(
@@ -3600,7 +3600,7 @@ fn test_childkey_take_drain_validator_take() {
36003600
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
36013601
SubtensorModule::set_max_allowed_validators(netuid, 2);
36023602
step_block(subnet_tempo);
3603-
pallet_subtensor::SubnetOwnerCut::<Test>::set(0);
3603+
crate::SubnetOwnerCut::<Test>::set(0);
36043604

36053605
// Set 20% childkey take
36063606
let max_take: u16 = 0xFFFF / 5;
@@ -3648,15 +3648,15 @@ fn test_childkey_take_drain_validator_take() {
36483648
vec![(proportion, child)]
36493649
));
36503650
// Make all stakes viable
3651-
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
3652-
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
3651+
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
3652+
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
36533653

36543654
// Setup YUMA so that it creates emissions:
36553655
// Parent and child both set weights
36563656
// Parent and child register on root and
36573657
// Set root weights
3658-
pallet_subtensor::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3659-
pallet_subtensor::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3658+
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3659+
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
36603660
assert_ok!(SubtensorModule::do_root_register(
36613661
RuntimeOrigin::signed(coldkey),
36623662
parent,
@@ -3665,17 +3665,17 @@ fn test_childkey_take_drain_validator_take() {
36653665
RuntimeOrigin::signed(coldkey),
36663666
child,
36673667
));
3668-
pallet_subtensor::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3669-
pallet_subtensor::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3668+
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3669+
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
36703670

36713671
// Run run_coinbase until PendingHotkeyEmission are populated
3672-
while pallet_subtensor::PendingdHotkeyEmission::<Test>::get(child) == 0 {
3672+
while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
36733673
step_block(1);
36743674
}
36753675

36763676
// Prevent further subnet epochs
3677-
pallet_subtensor::Tempo::<Test>::set(netuid, u16::MAX);
3678-
pallet_subtensor::Tempo::<Test>::set(root_id, u16::MAX);
3677+
crate::Tempo::<Test>::set(netuid, u16::MAX);
3678+
crate::Tempo::<Test>::set(root_id, u16::MAX);
36793679

36803680
// Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
36813681
step_block((hotkey_tempo * 2) as u16);
@@ -3684,9 +3684,9 @@ fn test_childkey_take_drain_validator_take() {
36843684
// - Child stake increased by its child key take (20% * 50% = 10% of total emission) plus childkey's delegate take (10%)
36853685
// - Parent stake increased by 40% of total emission
36863686
// - Nominator stake increased by 40% of total emission
3687-
let child_emission = pallet_subtensor::Stake::<Test>::get(child, coldkey);
3688-
let parent_emission = pallet_subtensor::Stake::<Test>::get(parent, coldkey) - stake;
3689-
let nominator_emission = pallet_subtensor::Stake::<Test>::get(child, nominator) - stake;
3687+
let child_emission = crate::Stake::<Test>::get(child, coldkey);
3688+
let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
3689+
let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
36903690
let total_emission = child_emission + parent_emission + nominator_emission;
36913691

36923692
assert!(is_within_tolerance(child_emission, total_emission / 5, 500));

pallets/subtensor/tests/coinbase.rs renamed to pallets/subtensor/src/tests/coinbase.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
2-
use crate::mock::*;
3-
mod mock;
2+
use super::mock::*;
3+
44
use frame_support::assert_ok;
55
use sp_core::U256;
66
use substrate_fixed::types::I64F64;
77

8-
use pallet_subtensor::TargetStakesPerInterval;
8+
use crate::TargetStakesPerInterval;
99

1010
// Test the ability to hash all sorts of hotkeys.
1111
#[test]

pallets/subtensor/tests/difficulty.rs renamed to pallets/subtensor/src/tests/difficulty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![allow(clippy::unwrap_used)]
22

3-
use crate::mock::*;
4-
mod mock;
53
use sp_core::U256;
64

5+
use super::mock::*;
6+
77
#[test]
88
fn test_registration_difficulty_adjustment() {
99
new_test_ext(1).execute_with(|| {

pallets/subtensor/tests/epoch.rs renamed to pallets/subtensor/src/tests/epoch.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,17 @@
44
clippy::unwrap_used
55
)]
66

7-
use crate::mock::*;
7+
use super::mock::*;
8+
use crate::epoch::math::safe_exp;
9+
use crate::*;
810
use frame_support::{assert_err, assert_ok};
911
use frame_system::Config;
10-
use pallet_subtensor::epoch::math::safe_exp;
11-
use pallet_subtensor::*;
1212
use rand::{distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng, Rng, SeedableRng};
1313
use sp_core::U256;
1414
use sp_runtime::DispatchError;
1515
use std::time::Instant;
1616
use substrate_fixed::types::I32F32;
1717

18-
mod mock;
19-
20-
pub fn fixed(val: f32) -> I32F32 {
21-
I32F32::from_num(val)
22-
}
23-
24-
pub fn fixed_to_u16(x: I32F32) -> u16 {
25-
x.to_num::<u16>()
26-
}
27-
28-
pub fn fixed_proportion_to_u16(x: I32F32) -> u16 {
29-
fixed_to_u16(x * I32F32::from_num(u16::MAX))
30-
}
31-
3218
// Normalizes (sum to 1 except 0) the input vector directly in-place.
3319
#[allow(dead_code)]
3420
pub fn inplace_normalize(x: &mut [I32F32]) {

pallets/subtensor/tests/math.rs renamed to pallets/subtensor/src/tests/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
)]
66
use substrate_fixed::types::{I32F32, I64F64};
77

8-
use pallet_subtensor::epoch::math::*;
8+
use crate::epoch::math::*;
99
use rand::{seq::SliceRandom, thread_rng, Rng};
1010
use substrate_fixed::{
1111
transcendental::exp,

0 commit comments

Comments
 (0)