Skip to content

Commit

Permalink
refactor: split test into 2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Nov 12, 2024
1 parent b623e35 commit 28712c6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {describeSuite, expect} from "@moonwall/cli";
import {alith} from "@moonwall/util";
import {fail} from "assert";
import {parameterType, UNIT} from "./test-parameters";

describeSuite({
id: "DTemp02",
title: "Parameters - Pallet Randomness",
foundationMethods: "dev",
testCases: ({it, context, log}) => {
it({
id: `T01 - 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;
},
});
},
});
64 changes: 8 additions & 56 deletions test/suites/dev/moonbase/test-parameters/test-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ 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;
export const UNIT = 1_000_000_000_000_000_000n;

const RUNTIME = "MoonbaseRuntime";
const CRATE = "RuntimeParams";
const ALL_PARAMS = "DynamicParams";

function parameterType(context: DevModeContext, module: string, name: string, value: unknown) {
export function parameterType(context: DevModeContext, module: string, name: string, value: unknown) {
const paramWrapper = context
.polkadotJs()
.createType(`${RUNTIME}${CRATE}${ALL_PARAMS}${module}Parameters`, {
Expand All @@ -22,7 +22,7 @@ function parameterType(context: DevModeContext, module: string, name: string, va
return runtimeParameter;
}

function parameterKey(context: DevModeContext, module: string, name: string) {
export function parameterKey(context: DevModeContext, module: string, name: string) {
const key = context
.polkadotJs()
.createType(`${RUNTIME}${CRATE}${ALL_PARAMS}${module}ParametersKey`, {
Expand Down Expand Up @@ -82,60 +82,12 @@ describeSuite({
});
}

testParam("RuntimeConfig", "FeesTreasuryProportion", ["Perbill", 200_000_000]);
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");
}
// Add all the parameters here to test against 2 test cases:
// 1. Parameters cannot by a normal user.
// 2. Parameters can be set by sudo.

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;
},
});
testParam("RuntimeConfig", "FeesTreasuryProportion", ["Perbill", 200_000_000]);
testParam("PalletRandomness", "Deposit", ["u128", UNIT * 100n]);
},
});

0 comments on commit 28712c6

Please sign in to comment.