BksRhalid / Alyra-VotingSystem_including_UnitTest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MIT License LinkedIn


Alyra Voting System Project

This project was perform for Alya Solidity training. It demonstrates how to implement a javascript unit test for that contract.
Explore the docs Β»

Report Bug Β· Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Contributing
  5. License
  6. Acknowledgments

About The Project

The objective of the exercise is to provide the unit tests of a smart contract including everts and revert.

the contract to be tested is based on a previous project. See below the repo link :

Voting.sol

In this context, we have created three test files: :

  • Ownable.test.js : to test if Ownable from openzepplin is correctly implemented

       Units tests on Ownable access functions
      πŸ”Ž Test Ownable Revert if the owner is not the caller
        βœ” should NOT start Proposal session registering if caller is not the owner (58ms)
        βœ” should NOT end Proposal session registering if caller is not the owner (46ms)
        βœ” should NOT start voting session if caller is not the owner (50ms)
        βœ” should NOT end voting session  if caller is not the owner (60ms)
        βœ” should NOT start tallyVotes if caller is not the owner (56ms)
  • Voting.test.js : to test the each function of Voting smart contract separately

      Units tests of each function of voting smart contract
      πŸ”Ž Deployment hardhat deploy testing
        βœ” should deploy the smart contract
      πŸ”Ž Get add new voters function unit test
        βœ” should emit event VoterRegistered
        βœ” should be able to set owner as a voter
        βœ” should add a new voter_1 and change bool isRegistered from false to true (40ms)
        βœ” should NOT add a new voter and revert caller is not the owner
      πŸ”Ž Get voters function unit test
        βœ” should return true for _owner and false for voter_1 not yet Registered
        βœ” should revert as voter is not registered
      πŸ”Ž AddVoter failed as voter registration is not open yet
        βœ” should NOT addVoter and revert with Voters registration is not open yet (95ms)
        βœ” should NOT addVoter if not the owner
      πŸ”Ž Get add new proposal function unit test
        βœ” should add a new proposal
        βœ” should emit event add proposal
      πŸ”Ž  AddProposals failed as Proposals are not allowed yet
        βœ” should NOT addProposals and revert with Proposals are not allowed yet (109ms)
        βœ” should NOT addProposal if caller is not a Voter
      πŸ”Ž getOneProposal function unit test
        βœ” should NOT give proposal if not Voter (48ms)
      πŸ”Ž Get add new vote function unit test
        βœ” should add a new vote and increment voteCount
      πŸ”Ž Test revert setVote function unit test
        βœ” should revert setVote as the voting session not started (92ms)
        βœ” should revert as caller is not a voter (60ms)
        βœ” should revert with Voting session havent started yet (73ms)
        βœ” should revert with Proposal not found (47ms)
      πŸ”Ž Get end voting session function unit test
        βœ” should end the voting session (81ms)
      πŸ”Ž TallyVotes function unit test
        βœ” should return the winning proposal
        βœ” should change the status
      πŸ”Ž TallyVotes revert function unit test
        βœ” should NOT work if not votingSessionEnded
        βœ” should NOT work if not Owner (49ms)
  • VotingWorkflow.test.js : to test the overall workflow of the Voting contract

        Units tests for overall workflow of voting smart contract
      πŸ”Ž Control workflow status not allow to change if previous status is NOT correct
        βœ” should NOT start Proposal registering if the workflow status is not the expected one (67ms)
        βœ” should NOT end Proposal registering if the workflow status is not the expected one (63ms)
        βœ” should NOT start voting session in incorrect WF (62ms)
        βœ” should NOT end voting session if the workflow status is not the expected one (58ms)
        βœ” should NOT launch tallyVotes (65ms)
      πŸ”Ž  Check event of each workflow status change
        βœ” should start Proposal registering if the owner
        βœ” should end Proposal registering if the owner
        βœ” should start voting session if the owner
        βœ” should end voting session if the owner
        βœ” should launch tallyVotes if the owner (45ms)
      ```

Below the coverage report of the test :

-------------|----------|----------|----------|----------|----------------|
File         |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
-------------|----------|----------|----------|----------|----------------|
 contracts/  |      100 |    93.75 |      100 |      100 |                |
  Voting.sol |      100 |    93.75 |      100 |      100 |                |
-------------|----------|----------|----------|----------|----------------|
All files    |      100 |    93.75 |      100 |      100 |                |
-------------|----------|----------|----------|----------|----------------|

(back to top)

Getting Started

Below the instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Prerequisites

Here the list things you need to use the software and how to install them.

  • For this project we are going to use yarn
  1. installation
npm install --global yarn
  1. check installation
 yarn --version
  • solidity

Installation

_Below instruction installing and setting up the project.

  1. Clone the repo

    git clone https://github.com/BksRhalid/Alyra-VotingSystem_including_UnitTest.git
  2. Install packages

    yarn install
  3. Enter your Wallet Private Key and API in env

    const PK = "YOUR WALLET PRIVATE KEY";
    const INFURA = "ENTER YOUR API";
    const ETHERSCAN = "ENTER YOUR API";
  4. Check dependencies used like Openzepplin contracts / hardhat-deploy

    yarn add --dev @openzeppelin/contracts
    yarn add --dev hardhat-deploy

In case of error: _TypeError: ethers.getContract is not a function

see reference : https://ethereum.stackexchange.com/questions/130125/solvedpatrickcollins-solidity-course-2022-lesson-7-112038-typeerror-e

yarn add --dev hardhat @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

(back to top)

Usage

Try running some of the following tasks:

REPORT_GAS=true npx hardhat test

(back to top)

License

Distributed under the MIT License.

(back to top)


About


Languages

Language:JavaScript 82.8%Language:Solidity 17.2%