Skip to content

Commit

Permalink
add deploy and test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
neila committed Jan 22, 2024
1 parent d46ca30 commit 847c566
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 0 deletions.
15 changes: 15 additions & 0 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ethers } from "hardhat"

async function main() {
const submission = await ethers.getContractFactory("SocialNetworkExample")
const contract = await submission.deploy()

await contract.deployed()

console.log(`Contract deployed to ${contract.address}`)
}

main().catch((error) => {
console.error(error)
process.exitCode = 1
})
115 changes: 115 additions & 0 deletions test/Government.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers"
import { expect } from "chai"
import { ethers } from "hardhat"

import { IGovernment } from "../typechain-types/interfaces"

describe("Government", function () {
async function deployContract() {
const accounts = await ethers.getSigners()

const governmentFactory = await ethers.getContractFactory("Government")
const government = (await governmentFactory.deploy()) as IGovernment

return {
government,
deployAccount: accounts[0],
userAccounts: accounts.slice(1, accounts.length),
}
}

describe("Run for", function () {
it("run for & check the address is registered as candidate.", async function () {
const { government, userAccounts } = await loadFixture(deployContract)
const candidate1 = userAccounts[0]
await government.connect(candidate1).runForCandidate()

expect(await government.isCandidate(candidate1.address)).to.equal(true)
})

it("check a address is not registered as a candidate before running for.", async function () {
const { government, userAccounts } = await loadFixture(deployContract)
const someone = userAccounts[0]

expect(await government.isCandidate(someone.address)).to.equal(false)
})
})

describe("Vote", function () {
it("someone run for & check the number of vote is initially zero.", async function () {
const { government, userAccounts } = await loadFixture(deployContract)
const candidate1 = userAccounts[0]
await government.connect(candidate1).runForCandidate()

expect(await government.numberOfVotes(candidate1.address)).to.equal(0)
})

it("Revert transaction if there is a double vote.", async function () {
const { government, userAccounts } = await loadFixture(deployContract)

const candidate1 = userAccounts[0]
const voter1 = userAccounts[1]

await government.connect(candidate1).runForCandidate()

// first time
await government.connect(voter1).vote(candidate1.address)
// second time -> revert
await expect(government.connect(voter1).vote(candidate1.address)).to.be
.reverted
})

it("Revert transaction if there is a vote after abstain.", async function () {
const { government, userAccounts } = await loadFixture(deployContract)

const candidate1 = userAccounts[0]
const voter1 = userAccounts[1]

await government.connect(candidate1).runForCandidate()

// abstain.
await government.connect(voter1).abstain()
// vote -> revert
await expect(government.connect(voter1).vote(candidate1.address)).to.be
.reverted
})
})

describe("Determine winner", function () {
it("Candidates run for & voters vote & determine president & check president.", async function () {
const { government, userAccounts } = await loadFixture(deployContract)

const threeWeeksInSeconds = 60 * 60 * 24 * 7 * 3
const numOfCandidates = 3
const indexOfPresident = 1
const candidates = userAccounts.slice(0, numOfCandidates)
const voters = userAccounts.slice(numOfCandidates, userAccounts.length)

// Run for.
for (let index = 0; index < candidates.length; index++) {
await government.connect(candidates[index]).runForCandidate()
}

// Vote.
for (let voterIndex = 0; voterIndex < voters.length; voterIndex++) {
let candidateIndexToVote: number = voterIndex % numOfCandidates
if (candidateIndexToVote === 0 || candidateIndexToVote === 1) {
candidateIndexToVote = indexOfPresident
}
await government
.connect(voters[voterIndex])
.vote(candidates[candidateIndexToVote].address)
}

// Adjust the end time.
await time.increase(threeWeeksInSeconds)

// Determine president.
await government.determinePresident()

expect(await government.getPresident()).to.equal(
candidates[indexOfPresident].address,
)
})
})
})
125 changes: 125 additions & 0 deletions test/Medical.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"
import { expect } from "chai"
import { ethers } from "hardhat"

import { IMedical } from "../typechain-types/interfaces"

describe("Medical", function () {
async function deploy() {
const accounts = await ethers.getSigners()
const deployAccount = accounts[0]
const userAccounts = accounts.slice(1, accounts.length)

// Deploy.
const medicalFactory = await ethers.getContractFactory("Medical")
const medical = (await medicalFactory.deploy()) as IMedical

return {
medical,
deployAccount,
userAccounts,
}
}

async function deployAndRegister() {
const { medical, deployAccount, userAccounts } = await loadFixture(deploy)

const patient = userAccounts[0]
const otherAccounts = userAccounts.slice(1, userAccounts.length)

const defaultName = "Default patient"
const defaultBloodType = "A"
await medical.connect(patient).register(defaultName, defaultBloodType)
return {
medical,
deployAccount,
patient,
otherAccounts,
defaultName,
defaultBloodType,
}
}

describe("Register and get data.", function () {
it("Verify that the correct name is registered.", async function () {
const { medical, patient, defaultName } = await deployAndRegister()

const { name } = await medical
.connect(patient)
.getMedicalData(patient.address)

expect(name).to.equal(defaultName)
})

it("Verify that the correct blood type is registered.", async function () {
const { medical, patient, defaultBloodType } = await deployAndRegister()

const { bloodType } = await medical
.connect(patient)
.getMedicalData(patient.address)

expect(bloodType).to.equal(defaultBloodType)
})

// Skip the test of time of last update because it's implementation-dependent.
})

describe("Edit medical data", function () {
it("Patients can update their name.", async function () {
const { medical, patient } = await deployAndRegister()

const newName = "new name"
await medical.connect(patient).updateName(newName)

const { name } = await medical
.connect(patient)
.getMedicalData(patient.address)

expect(name).to.equal(newName)
})

it("Patients can update their blood type.", async function () {
const { medical, patient } = await deployAndRegister()

const newBloodType = "B"
await medical.connect(patient).updateBloodType(newBloodType)

const { bloodType } = await medical
.connect(patient)
.getMedicalData(patient.address)

expect(bloodType).to.equal(newBloodType)
})
})

describe("Viewing access", function () {
it("No viewing access by default.", async function () {
const { medical, patient, otherAccounts } = await deployAndRegister()

const medicalProvider = otherAccounts[0]

expect(
await medical.isViewingAccessGranted(
patient.address,
medicalProvider.address,
),
).to.equal(false)
})

it("Check viewing access after provided.", async function () {
const { medical, patient, otherAccounts } = await deployAndRegister()

const medicalProvider = otherAccounts[0]
await medical
.connect(patient)
.provideViewingAccess(medicalProvider.address)

expect(
await medical.isViewingAccessGranted(
patient.address,
medicalProvider.address,
),
).to.equal(true)
})
})
})
Loading

0 comments on commit 847c566

Please sign in to comment.