Skip to content

Commit

Permalink
feat: complete the deleteApplication function
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Jul 19, 2024
1 parent adffcb6 commit f6e1b08
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 15 deletions.
4 changes: 4 additions & 0 deletions contracts/ApplicationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ contract ApplicationManager is IApplicationManager {
}

function deleteApplication(uint id) external override {
require(applicationExists(id), "Application does not exist");
addressUsed[applications[id].account] = false;
emit ApplicationDeleted(id, applications[id]);
delete applications[id];
}

function getApplication(uint id) external view override returns (Application memory) {
Expand Down
94 changes: 79 additions & 15 deletions test/ApplicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import hre from "hardhat";
import { type Address, getAddress, parseUnits, } from "viem";
import { generatePrivateKey,privateKeyToAccount } from 'viem/accounts'

const privateKey = generatePrivateKey()


interface Application {
name: string;
Expand Down Expand Up @@ -34,6 +32,12 @@ describe("ApplicationManager", () => {
};
}

function generateRandomAddress(): string {
const randomKey = generatePrivateKey();
const account = privateKeyToAccount(randomKey);
return account.address;
}

describe("Deployment", () => {
it("Should set the nextxApplicationId to 0", async () => {
const { contract } = await loadFixture(deploy);
Expand All @@ -47,7 +51,7 @@ describe("ApplicationManager", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let contract: any;
let app: Application;
let account= privateKeyToAccount(generatePrivateKey()).address
let account= generateRandomAddress() as Address;
beforeEach(async () => {
const fixture = await loadFixture(deploy);
contract = fixture.contract;
Expand All @@ -57,7 +61,7 @@ describe("ApplicationManager", () => {
};
});

it("Should accept Application as inputs", async () => {
it("Should accept Application as input", async () => {
expect(await contract.write.createApplication([app])).to.be.string;
});
it("Should increment nextApplicationId", async () => {
Expand All @@ -74,6 +78,12 @@ describe("ApplicationManager", () => {
app,
);
});
it("Should revert if the account address is already used", async () => {
await contract.write.createApplication([app]);
await expect(contract.write.createApplication([app])).to.be.rejectedWith(
"Address already used for another application"
);
});
it("Should emit the ApplicationCreated event with the new application's details", async () => {
const applicationId = await contract.read.getNextApplicationId();
const txHash = await contract.write.createApplication([app]);
Expand All @@ -88,21 +98,14 @@ describe("ApplicationManager", () => {
expect(event.args.id).to.be.equal(applicationId);
expect(event.args.application).to.contain(app);
});

it("Should revert if the account address is already used", async () => {
await contract.write.createApplication([app]);
await expect(contract.write.createApplication([app])).to.be.rejectedWith(
"Address already used for another application"
);
});
});

describe("updateApplication", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let contract: any;
let app: Application;
let applicationId: string;
let account= privateKeyToAccount(generatePrivateKey()).address
let account= generateRandomAddress() as Address;

beforeEach(async () => {
const fixture = await loadFixture(deploy);
Expand All @@ -117,11 +120,11 @@ describe("ApplicationManager", () => {
await contract.write.createApplication([app]);
});

it("Should accept Application as inputs", async () => {
it("Should accept Application and id as inputs", async () => {
expect(await contract.write.updateApplication([applicationId, app])).to.be
.string;
});
it("Should update the Application struct in the applications mapping with the provided applicationId to have the application data", async () => {
it("Should update the application in the applications mapping with the provided applicationId to have the application data", async () => {
await contract.write.updateApplication([
applicationId,
{ account: app.account, name: "new app name" },
Expand All @@ -135,7 +138,7 @@ describe("ApplicationManager", () => {
const t1 = await contract.read.getNextApplicationId();
expect(t1).to.equal(parseUnits("1", 0));
await contract.write.createApplication([{
account: privateKeyToAccount(generatePrivateKey()).address,
account: generateRandomAddress() as Address,
name: "new app"
}]);

Expand All @@ -148,5 +151,66 @@ describe("ApplicationManager", () => {
"Application does not exist"
);
});

it("Should emit the ApplicationUpdated event with the updated application's details", async () => {
const txHash = await contract.write.updateApplication([applicationId, app]);
const events = await contract.getEvents.ApplicationUpdated();
expect(events.length).to.be.equal(1);

const event = events[0];
expect(event.eventName).to.be.equal("ApplicationUpdated");
expect(event.transactionHash).to.be.equal(txHash);
expect(event.args.id).to.be.equal(applicationId);
expect(event.args.application).to.contain(app);
});
});

describe("deleteApplication", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let contract: any;
let app: Application;
let applicationId: string;
let account= privateKeyToAccount(generatePrivateKey()).address

beforeEach(async () => {
const fixture = await loadFixture(deploy);
contract = fixture.contract;

app = {
name: "app",
account,
};

applicationId = await contract.read.getNextApplicationId();
await contract.write.createApplication([app]);
});

it("Should accept id as input", async () => {
expect(await contract.write.deleteApplication([applicationId])).to.be
.string;
});
it("Should delete the application from the applications mapping with the provided applicationId", async () => {
await contract.write.deleteApplication([applicationId]);
await expect(contract.read.getApplication([applicationId])).to.be.rejectedWith("Application does not exist");
});
it("Should revert if the application does not exist", async () => {
const nonExistentId = parseUnits("999", 0);
await expect(contract.write.deleteApplication([nonExistentId])).to.be.rejectedWith(
"Application does not exist"
);
});

it("Should emit the ApplicationDeleted event with the deleted application's details", async () => {
const txHash = await contract.write.deleteApplication([applicationId]);

const events = await contract.getEvents.ApplicationDeleted();
expect(events.length).to.be.equal(1);

const event = events[0];
expect(event.eventName).to.be.equal("ApplicationDeleted");
expect(event.transactionHash).to.be.equal(txHash);
expect(event.args.id).to.be.equal(applicationId);
expect(event.args.application).to.contain(app);
});
});
});

0 comments on commit f6e1b08

Please sign in to comment.