Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Developer community] Refactor test cases of Vote Contract using TestKit AEDPoS extension #1915

Open
EanCuznaivy opened this issue Jul 22, 2019 · 0 comments

Comments

@EanCuznaivy
Copy link
Contributor

EanCuznaivy commented Jul 22, 2019

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.

@EanCuznaivy EanCuznaivy changed the title [Developer community] Refactor test cases of Vote Contract using test kit AEDPoS extension [Developer community] Refactor test cases of Vote Contract using TestKit AEDPoS extension Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@EanCuznaivy and others