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

introduction to testing #45

Open
wants to merge 1 commit into
base: intro-to-testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/IStudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.19;
import "./Student.sol";
interface IStudentRegistry {

Expand Down
2 changes: 1 addition & 1 deletion contracts/Student.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.19;
struct Student {
address studentAddr;
string name;
Expand Down
2 changes: 1 addition & 1 deletion contracts/StudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.19;
import "./Ownable.sol";
import "./Student.sol";

Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.24",
solidity: "0.8.19",
};
28 changes: 7 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"hardhat": "^2.22.6"
},
"dependencies": {
"ethers": "^6.13.2"
}
}
}
30 changes: 29 additions & 1 deletion test/Counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
time,
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");
// const { ethers } = require("hardhat");

Expand Down Expand Up @@ -101,5 +101,33 @@ const {
});
});
});

describe("Events", () => {
it("should emit an event when count is increased", async () => {
const deployedCounter = await loadFixture(deployUtil);

const count1 = await deployedCounter.count();
console.log("count1 ___", count1);
expect(count1).to.eq(0);

await expect(deployedCounter.increaseByOne())
.to.emit(deployedCounter, "CountIncreased")
.withArgs(count1 + BigInt(1), anyValue)
})

it("should emit an event when count is decreased", async () => {
const deployedCounter = await loadFixture(deployUtil);

await deployedCounter.increaseByOne();

const count1 = await deployedCounter.count();
console.log("count1 ____", count1);
expect(count1).to.eq(1);

await expect(deployedCounter.decreaseByOne())
.to.emit(deployedCounter, "CountDecreased")
.withArgs(count1 - BigInt(1), anyValue)
})
});
});

145 changes: 145 additions & 0 deletions test/StudentRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const {
time,
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { ethers } = require("hardhat");
const { expect } = require("chai");

const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

describe("Student Registry Test Suite", () => {
// deployUtil function
const deployUtil = async () => {
const [owner, newOwner, otherAccount] = await ethers.getSigners();
const StudentRegistry = await ethers.getContractFactory("StudentRegistry");
const deployedRegistry = await StudentRegistry.deploy();
return {deployedRegistry, owner, newOwner, otherAccount};
}

describe("Student Mapping", () => {
it("should return correct details from mapping", async () => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await expect(
deployedRegistry.studentsMapping(otherAccount)
).to.not.be.reverted;
})
})

describe("Add Students", () => {
it("should add a student successfully", async () => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(otherAccount).addStudent(otherAccount.address, "Made", 19)
).to.not.be.reverted;
})

it("should be reverted when name is empty", async () => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(otherAccount).addStudent(otherAccount.address, "", 19)
).to.be.revertedWithCustomError(deployedRegistry, "NameIsEmpty");
})

it("should be reverted when age is less than 18", async () => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(otherAccount).addStudent(otherAccount.address, "John", 12)
).to.be.revertedWithCustomError(deployedRegistry, "UnderAge").withArgs(12, 18);
})
})

describe("Delete Students", () => {
it("should delete a student successfully", async () => {
const { deployedRegistry, owner, otherAccount } = await loadFixture(deployUtil);

await deployedRegistry.addStudent(otherAccount.address, "John", 19);

await expect(
deployedRegistry.connect(owner).deleteStudent(otherAccount.address))
.to.not.be.reverted;
})

it("should be reverted when another account tries to delete", async () => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(otherAccount).deleteStudent(otherAccount.address))
.to.be.revertedWith("Caller not owner");
})

it("should be reverted when trying to delete a student that has not been added", async () => {
const { deployedRegistry, owner, otherAccount } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(owner).deleteStudent(otherAccount.address)
).to.be.revertedWith("Student does not exist");
})
})

describe("Get Students", () => {
it("should get student successfully", async() => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await deployedRegistry.addStudent(otherAccount.address, "John", 19);

await expect(
deployedRegistry.getStudent(1)
).to.not.be.reverted;
})

it("should revert when an invalid ID is used to get student", async() => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await deployedRegistry.addStudent(otherAccount.address, "John", 19);

await expect(
deployedRegistry.getStudent(0)
).to.be.reverted;
})
})

describe("Get Student From Mapping", () => {
it("should successfully get student from mapping", async () => {
const { deployedRegistry, otherAccount } = await loadFixture(deployUtil);

await deployedRegistry.addStudent(otherAccount.address, "John", 19);

await expect(
deployedRegistry.connect(otherAccount).getStudentFromMapping(otherAccount.address)
).to.not.be.reverted;
})

})

describe("Modify Owner", () => {
it("should successfully modify owner", async () => {
const { deployedRegistry, owner, newOwner } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(owner).modifyOwner(newOwner.address)
).to.not.be.reverted;
})

it("should be reverted when other account tries to change owner", async () => {
const { deployedRegistry, otherAccount, newOwner } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(otherAccount).modifyOwner(newOwner.address)
).to.be.revertedWith("Caller not owner");
})

it("should revert when trying to set new owner as address zero", async () => {
const { deployedRegistry, owner } = await loadFixture(deployUtil);

await expect(
deployedRegistry.connect(owner).modifyOwner(ZERO_ADDRESS)
).to.be.revertedWith("Owner can not be address zero");
})
})

});