Skip to content

Commit

Permalink
Future/election factory test (#30)
Browse files Browse the repository at this point in the history
* Created election-factory test script

* Removed peer independencies

* removed peer dependencies

* Changes to merge PR

* Removing ethers as deps

---------

Co-authored-by: mickeymond <[email protected]>
  • Loading branch information
Ratawine7 and mickeymond authored Sep 1, 2024
1 parent fd4dac3 commit 20fe18b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 16 deletions.
3 changes: 2 additions & 1 deletion blockchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ ignition/deployments/chain-31337

# Others
package-lock.json
yarn.lock
yarn.lock
pnpm-lock.yaml
33 changes: 18 additions & 15 deletions blockchain/contracts/ElectionFactory.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./Election.sol";

contract ElectionFactory {
address owner;
//Array to keep track of the elections

// Array to keep track of the elections
address[] elections;

constructor (){
constructor() {
owner = msg.sender;
}

Expand All @@ -18,18 +17,23 @@ contract ElectionFactory {
_;
}

//Event emitted when an election is created
// Event emitted when an election is created
event ElectionCreated(address electionAddress);

//Function to create new election
// Function to return the owner address (added this function)
function getOwner() public view returns (address) {
return owner;
}

// Function to create new election
function createElection(
string memory _title,
string memory _description,
bool _isPublic,
uint _startDate,
uint _endDate
) public {
//Create a new instance of the Election contract
// Create a new instance of the Election contract
Election newElection = new Election(
_title,
_description,
Expand All @@ -38,25 +42,24 @@ contract ElectionFactory {
_endDate
);

//Store the address of the newly created election
// Store the address of the newly created election
elections.push(address(newElection));

//Emit an event for the creation of the new contract
// Emit an event for the creation of the new contract
emit ElectionCreated(address(newElection));
}


//function to get addresses of all elections
function getElections() public view returns (address[] memory){
// Function to get addresses of all elections
function getElections() public view returns (address[] memory) {
return elections;
}

//function to delete an election
// Function to delete an election
function deleteElection(uint _electionID) public onlyOwner {
delete elections[_electionID];
}

//Function to retrieve the total number of elections
// Function to retrieve the total number of elections
function getTotalElections() public view returns (uint) {
return elections.length;
}
Expand Down
103 changes: 103 additions & 0 deletions blockchain/test/ElectionFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import hre from "hardhat";


describe("ElectionFactory Contract", function () {
async function deployElectionFactoryFixture() {
// Get the ContractFactory and Signers here.
const [owner, addr1] = await hre.ethers.getSigners();

const ElectionFactory = await hre.ethers.getContractFactory("ElectionFactory");
const electionFactory = await ElectionFactory.deploy();

// await electionFactory.deployed();

// Return the contract and accounts to be used in the tests
return { electionFactory, owner, addr1 };
}

it("Should deploy the contract and set the owner correctly", async function () {
const { electionFactory, owner } = await loadFixture(deployElectionFactoryFixture);

expect(await electionFactory.getOwner()).to.equal(owner.address);
});

it("Should create a new election", async function () {
const { electionFactory, addr1 } = await loadFixture(deployElectionFactoryFixture);

const newElection = await electionFactory.createElection(
"Election Title",
"Election Description",
true,
1234567890,
1234567899
);

// Wait for the transaction to be mined
await newElection.wait();

const elections = await electionFactory.getElections();
expect(elections.length).to.equal(1);
});

it("Should emit an event when a new election is created", async function () {
const { electionFactory } = await loadFixture(deployElectionFactoryFixture);

await expect(electionFactory.createElection(
"Election Title",
"Election Description",
true,
1234567890,
1234567899
)).to.emit(electionFactory, "ElectionCreated");
});

it("Should only allow the owner to delete an election", async function () {
const { electionFactory, owner, addr1 } = await loadFixture(deployElectionFactoryFixture);

// Create an election first
const newElection = await electionFactory.createElection(
"Election Title",
"Election Description",
true,
1234567890,
1234567899
);

await newElection.wait();

// Attempt to delete the election with a non-owner account
await expect(electionFactory.connect(addr1).deleteElection(0)).to.be.rejectedWith(
"You are not the owner"
);

// Delete the election with the owner account
await electionFactory.connect(owner).deleteElection(0);

const elections = await electionFactory.getElections();
expect(elections[0]).to.equal(ethers.ZeroAddress); // Use ethers.constants.AddressZero to check the zero address
});

it("Should return the correct total number of elections", async function () {
const { electionFactory } = await loadFixture(deployElectionFactoryFixture);

// Check initial total elections count
expect(await electionFactory.getTotalElections()).to.equal(0);

// Create an election
const newElection = await electionFactory.createElection(
"Election Title",
"Election Description",
true,
1234567890,
1234567899
);

await newElection.wait();

// Check total elections count after creating one
expect(await electionFactory.getTotalElections()).to.equal(1);
});
});
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ next-env.d.ts

package-lock.json
yarn.lock
pnpm-lock.yaml

0 comments on commit 20fe18b

Please sign in to comment.