Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Oct 3, 2024
1 parent 1b438ca commit 154bd03
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
14 changes: 8 additions & 6 deletions runtime/common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ impl<const L: u128, const U: u128> BoundedU128<L, U> {
Ok(Self(value))
}

pub fn new_or_min(value: u128) -> Self {
if value < L || value > U {
pub fn safe_new(value: u128) -> Self {
if value < L {
Self(L)
} else if value > U {
Self(U)
} else {
Self(value)
}
Expand Down Expand Up @@ -87,14 +89,14 @@ mod tests {
let bounded = BoundedU128::<1, 10>::new(11);
assert_eq!(bounded, Err("Value out of bounds"));

let bounded = BoundedU128::<1, 10>::new_or_min(0);
let bounded = BoundedU128::<1, 10>::safe_new(0);
assert_eq!(bounded.value(), 1);

let bounded = BoundedU128::<1, 10>::new_or_min(5);
let bounded = BoundedU128::<1, 10>::safe_new(5);
assert_eq!(bounded.value(), 5);

let bounded = BoundedU128::<1, 10>::new_or_min(11);
assert_eq!(bounded.value(), 1);
let bounded = BoundedU128::<1, 10>::safe_new(11);
assert_eq!(bounded.value(), 10);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion runtime/moonbase/src/runtime_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod dynamic_params {
pub static Deposit: BoundedU128<
{ 1 * currency::UNIT * currency::SUPPLY_FACTOR },
{ 1_000 * currency::UNIT * currency::SUPPLY_FACTOR },
> = BoundedU128::new_or_min(1 * currency::UNIT * currency::SUPPLY_FACTOR);
> = BoundedU128::safe_new(1 * currency::UNIT * currency::SUPPLY_FACTOR);
}
}

Expand Down
76 changes: 74 additions & 2 deletions test/suites/dev/moonbase/test-parameters/test-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describeSuite, DevModeContext, expect } from "@moonwall/cli";
import "@moonbeam-network/api-augment";
import { alith } from "@moonwall/util";

import { fail } from "assert";
const UNIT = 1_000_000_000_000_000_000n;

const RUNTIME = "MoonbaseRuntime";
Expand Down Expand Up @@ -83,6 +83,78 @@ describeSuite({
}

testParam("RuntimeConfig", "FeesTreasuryProportion", ["Perbill", 200_000_000]);
testParam("PalletRandomness", "Deposit", ["u128", 1_000_000_000_000_000_000n * 100n]);
testParam("PalletRandomness", "Deposit", ["u128", UNIT * 100n]);

it({
id: `T${testCounter++} - PalletRandomness - Deposit - CustomTests`,
title: "Deposit parameter should only be accepted in bounds",
test: async () => {
const MIN = 1n * UNIT;
const MAX = 1000n * UNIT;

// used as an acceptable value
const AVG = (MIN + MAX) / 2n;


const param1 = parameterType(
context,
"PalletRandomness",
"Deposit",
MIN - 1n,
);
try {
await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param1.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);
fail("An extrinsic should not be created, since the parameter is invalid");
} catch (error) {
expect(error.toString().toLowerCase()).to.contain("value out of bounds");
}

const param2 = parameterType(
context,
"PalletRandomness",
"Deposit",
MAX + 1n,
);
try {
await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param2.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);
fail("An extrinsic should not be created, since the parameter is invalid");
} catch (error) {
expect(error.toString().toLowerCase()).to.contain("value out of bounds");
}


const param3 = parameterType(
context,
"PalletRandomness",
"Deposit",
AVG,
);
const res3 = await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param3.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);
expect(
res3.result?.successful,
"An extrinsic should be created, since the parameter is valid"
).to.be.true;


},
});
},
});

0 comments on commit 154bd03

Please sign in to comment.