From 503a1947d0270654b2526a8f1a19ec75b9b2ba41 Mon Sep 17 00:00:00 2001 From: Isaac Wanger Date: Thu, 6 Apr 2023 23:37:43 +0100 Subject: [PATCH 1/4] testing for the mapping contract --- test/Maapping.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test/Maapping.js diff --git a/test/Maapping.js b/test/Maapping.js new file mode 100644 index 0000000..4f5f4d3 --- /dev/null +++ b/test/Maapping.js @@ -0,0 +1,22 @@ +const { + time, + loadFixture, +}= require("@nomicfoundation/hardhat-helpers"); +const { anyvalue } =require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { expect } = require("chai"); + +describe("Mapping Test Suite", async () => { + let owner, addr1, addr2, mappingContract + beforeEach(async () => { + [owner, addr1, addr2, mappingContract] = await ethers.getSigners(); + const MappingContract = await ethers.getContractFactory("Mapping") + mappingContract = await MappingContract.deploy(owner.address) + }) + + // Test case for deployment + describe("Deployment", async () => { + it("should set the right owner", async function () { + expect(await mappingContract.owner()).to.equal(owner.address); + }) + }) +}) \ No newline at end of file From be3b6243b304478344080f760ac36466493b0e2c Mon Sep 17 00:00:00 2001 From: Isaac Wanger Date: Fri, 7 Apr 2023 06:52:08 +0100 Subject: [PATCH 2/4] test cases --- test/Maapping.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/Maapping.js b/test/Maapping.js index 4f5f4d3..e62a0dc 100644 --- a/test/Maapping.js +++ b/test/Maapping.js @@ -11,12 +11,29 @@ describe("Mapping Test Suite", async () => { [owner, addr1, addr2, mappingContract] = await ethers.getSigners(); const MappingContract = await ethers.getContractFactory("Mapping") mappingContract = await MappingContract.deploy(owner.address) - }) + }); - // Test case for deployment + + // Before each test, deploy a new Mappings contract and get the owner describe("Deployment", async () => { it("should set the right owner", async function () { expect(await mappingContract.owner()).to.equal(owner.address); }) }) + + // Test adding and retrirving + it("should add and retrieve retrieve multiple students", async function () { + // Add a new student + const id = 1; + const name = "Isaac"; + await mappingContract.addStudent(id, name); + + // Retrieve the student's name + const studentName = await mappingContract.viewSutudent(id); + + // Check that name is correct + expect(studentName).to.equal(name); + }); + + // Test add }) \ No newline at end of file From 798ad2f96b8e7f85793426b138323bff59a56ccc Mon Sep 17 00:00:00 2001 From: Isaac Wanger Date: Fri, 7 Apr 2023 07:13:21 +0100 Subject: [PATCH 3/4] test cases --- test/Maapping.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/Maapping.js b/test/Maapping.js index e62a0dc..b9b6df3 100644 --- a/test/Maapping.js +++ b/test/Maapping.js @@ -35,5 +35,24 @@ describe("Mapping Test Suite", async () => { expect(studentName).to.equal(name); }); - // Test add + // Test adding a student an existing ID + it("should not allow adding a student with an existing ID", async function () { + // Add a new student + const id = 1; + const name = "Isaac"; + await mappingContract.addStudent(id, name); + + // Try to add another with the same ID + const name2 = "Bob"; + await expect(mappingContract.addStudent(id, name2)).to.be.revertedWith("Student ID already exist"); + }); + + // Test retrieving a student that does not exist + it("should not allow retrieving a student thta does not exist", async function () { + // REtrieve a student that hasn't been added + const id = 1; + const studentName = await mappingContract.viewSutudent(id); + + await expect(studentName).to.brrrrrre.revertedWith("studdent ID does not exist"); + }); }) \ No newline at end of file From 3888fca139935aca411e8ff7d67a1c34fc2e187e Mon Sep 17 00:00:00 2001 From: Isaac Wanger Date: Fri, 7 Apr 2023 16:16:01 +0100 Subject: [PATCH 4/4] final test case --- contracts/002_Mapping.sol | 2 +- test/Maapping.js | 58 --------------------------------------- test/Mapping.js | 36 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 59 deletions(-) delete mode 100644 test/Maapping.js create mode 100644 test/Mapping.js diff --git a/contracts/002_Mapping.sol b/contracts/002_Mapping.sol index f63aa9c..fe63f7a 100644 --- a/contracts/002_Mapping.sol +++ b/contracts/002_Mapping.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.17; // Declare the contract -contract Mappings{ +contract Mapping{ // Declare a mapping that associates uint keys with string values. mapping(uint => string) students; diff --git a/test/Maapping.js b/test/Maapping.js deleted file mode 100644 index b9b6df3..0000000 --- a/test/Maapping.js +++ /dev/null @@ -1,58 +0,0 @@ -const { - time, - loadFixture, -}= require("@nomicfoundation/hardhat-helpers"); -const { anyvalue } =require("@nomicfoundation/hardhat-chai-matchers/withArgs"); -const { expect } = require("chai"); - -describe("Mapping Test Suite", async () => { - let owner, addr1, addr2, mappingContract - beforeEach(async () => { - [owner, addr1, addr2, mappingContract] = await ethers.getSigners(); - const MappingContract = await ethers.getContractFactory("Mapping") - mappingContract = await MappingContract.deploy(owner.address) - }); - - - // Before each test, deploy a new Mappings contract and get the owner - describe("Deployment", async () => { - it("should set the right owner", async function () { - expect(await mappingContract.owner()).to.equal(owner.address); - }) - }) - - // Test adding and retrirving - it("should add and retrieve retrieve multiple students", async function () { - // Add a new student - const id = 1; - const name = "Isaac"; - await mappingContract.addStudent(id, name); - - // Retrieve the student's name - const studentName = await mappingContract.viewSutudent(id); - - // Check that name is correct - expect(studentName).to.equal(name); - }); - - // Test adding a student an existing ID - it("should not allow adding a student with an existing ID", async function () { - // Add a new student - const id = 1; - const name = "Isaac"; - await mappingContract.addStudent(id, name); - - // Try to add another with the same ID - const name2 = "Bob"; - await expect(mappingContract.addStudent(id, name2)).to.be.revertedWith("Student ID already exist"); - }); - - // Test retrieving a student that does not exist - it("should not allow retrieving a student thta does not exist", async function () { - // REtrieve a student that hasn't been added - const id = 1; - const studentName = await mappingContract.viewSutudent(id); - - await expect(studentName).to.brrrrrre.revertedWith("studdent ID does not exist"); - }); -}) \ No newline at end of file diff --git a/test/Mapping.js b/test/Mapping.js new file mode 100644 index 0000000..0d59d54 --- /dev/null +++ b/test/Mapping.js @@ -0,0 +1,36 @@ +const { expect } = require("chai"); +const { ethers } = require("hardhat"); + +describe("Mapping Test suite", function () { + let mapping; + beforeEach(async () => { + const Mapping = await ethers.getContractFactory("Mapping"); + mapping = await Mapping.deploy(); + await mapping.deployed(); + }); + + it("Should add and retrieve a student", async function () { + const id = 1; + const name = "Isaac"; + await mapping.addStudent(id, name); + const studentName = await mapping.viewStudent(id); + expect(studentName).to.equal(name); + }); + + it("Should add multiple students and retrieve them", async function () { + const students = [ + { id: 1, name: "Isaac" }, + { id: 2, name: "Pelz" }, + { id: 3, name: "Esther-ego" }, + ]; + + for (const student of students) { + await mapping.addStudent(student.id, student.name); + } + + for (const student of students) { + const studentName = await mapping.viewStudent(student.id); + expect(studentName).to.equal(student.name); + } + }); +}); \ No newline at end of file