const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract ("People", async function(accounts){
let instance;
//used if new istance is needed for each test
beforeEach(async function(){
instance = await People.deployed();
//used for instance that are resusinf info
// before(async function(){
// instance = await People.deployed();
//these two work the same just after the test however
//after()
//afterEach()
});
it("shouldn't create a person with age over 150 years", async function(){
//beforeEcach is run prior to each test
let instance = await People.deployed();
await truffleAssert.fails(instance.createPerson("Bob", 200, 190, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
});
it("shouldn't create a person without payment", async function(){
let instance = await People.deployed();
await truffleAssert.fails(instance.createPerson("Bob", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
});
it("should set senior status correctly", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
let result = await instance.getPerson();
assert(result.senior === true, "Senior level not set");
});
it("should set age correctly", async function(){
let instance = await People.deployed();
let result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly");
});
it("should only allow owner to delete person", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 15, 187, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(accounts[1], {from: accounts[1]}), truffleAssert.ErrorType.REVERT);
});
it("should only be deleted by contract owner", async function(){
let instance = await People.new();
await instance.createPerson("Bob", 15, 187, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
});
//web3.eth.getBalance(address)
it("should add 1 eth to balance after createPerson call", async function(){
let instance = await People.new();
await instance.createPerson("Steve", 44, 187, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
let balance = await instance.balance();
let floatBalance = parseFloat(balance);
let realBalance = await web3.eth.getBalance(instance.address);
assert(floatBalance == web3.utils.toWei("1", "ether") && floatBalance == realBalance)
})
it("should allow owner to withdraw balance", async function(){
let instance = await People.new();
await instance.createPerson("Amber", 25, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.withDrawAll({from: accounts[0]}));
});
it("should only allow owner to withdraw balance", async function(){
let instance = await People.new();
await instance.createPerson("Amber", 25, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.withDrawAll({from: accounts[2]}), truffleAssert.ErrorType.REVERT);
});
it("should have increased owner's balance after withdrawl", async function(){
let instance = await People.new();
await instance.createPerson("Tom", 45, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
let balanceBefore = parseFloat(await web3.eth.getBalance(accounts[0]));
await instance.withDrawAll();
let balanceAfter = parseFloat(await web3.eth.getBalance(accounts[0]));
assert(balanceBefore < balanceAfter, "Owner's balance was not increased after withdrawl");
});
it("should have balance of 0 after withdrawl", async function(){
let instance = await People.new();
await instance.createPerson("Tom", 45, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await instance.withDrawAll();
let balance = await instance.balance();
let floatBalance = parseFloat(balance);
let realBalance = await web3.eth.getBalance(instance.address);
assert(floatBalance == web3.utils.toWei("0", "ether") && floatBalance == realBalance, "Contrat balance was not at 0 after withdrawl")
})
});