AElf
AElf copied to clipboard
[Developer community] Refactor test cases of Vote Contract using TestKit AEDPoS extension
This task is based on branch refactor/vote-contract-tests
, so anyone interested in refactoring test cases of Vote Contract need to checkout his branch from dev
to refactor/vote-contract-tests
. (Or create a new branch on your own repo based on this branch.)
Check project test/AElf.Contracts.AEDPoSExtension.Demo.Tests
to know basically how to use TestKit AEDPoS extension.
Test case DemoTest
(shown bellow) shows how a blockchain system works: choose some transactions then package them to a block, every new block is based on previous block.
By using XXStub
, you can act like a specific user calling / sending a transaction to call a method of specific contract in test case. Also, you can use XXStub
for generating transactions.
[Fact]
public async Task DemoTest()
{
// Check round information after initialization.
{
var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty());
round.RoundNumber.ShouldBe(1);
round.TermNumber.ShouldBe(1);
round.RealTimeMinersInformation.Count.ShouldBe(AEDPoSExtensionConstants.InitialKeyPairCount);
}
// We can use this method process testing.
// Basically this will produce one block with no transaction.
await BlockMiningService.MineBlockAsync();
// And this will produce one block with one transaction.
// This transaction will call Create method of Token Contract.
await BlockMiningService.MineBlockAsync(new List<Transaction>
{
TokenStub.Create.GetTransaction(new CreateInput
{
Symbol = "ELF",
Decimals = 8,
TokenName = "Test",
Issuer = Address.FromPublicKey(SampleECKeyPairs.KeyPairs[0].PublicKey),
IsBurnable = true,
TotalSupply = 1_000_000_000_00000000
})
});
// Check whether previous Create transaction successfully executed.
{
var tokenInfo = await TokenStub.GetTokenInfo.CallAsync(new GetTokenInfoInput {Symbol = "ELF"});
tokenInfo.Symbol.ShouldBe("ELF");
}
// Next steps will check whether the AEDPoS process is correct.
// Now 2 miners produced block during first round, so there should be 2 miners' OutValue isn't null.
{
var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty());
round.RealTimeMinersInformation.Values.Count(m => m.OutValue != null).ShouldBe(2);
}
await BlockMiningService.MineBlockAsync(new List<Transaction>());
{
var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty());
round.RealTimeMinersInformation.Values.Count(m => m.OutValue != null).ShouldBe(3);
}
// Currently we have 5 miners, and before this line, 3 miners already produced blocks.
// 3 more blocks will end current round.
for (var i = 0; i < 3; i++)
{
await BlockMiningService.MineBlockAsync(new List<Transaction>());
}
// Check round number.
{
var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty());
round.RoundNumber.ShouldBe(2);
}
// 6 more blocks will end second round.
for (var i = 0; i < 6; i++)
{
await BlockMiningService.MineBlockAsync(new List<Transaction>());
}
// Check round number.
{
var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty());
round.RoundNumber.ShouldBe(3);
}
}
After knowing this, you can go to test/AElf.Contracts.Vote.AEDPoSExtension.Tests
and try to refactor test cases of Vote Contract. For the logic of testing, just copy logic of current test cases in AElf.Contracts.Vote.Tests
should be enough.
Also, refactoring of other system contracts (located in contract/
) is welcomed. README
in test/AElf.Contracts.AEDPoSExtension.Demo.Tests
explains how to create a test project.