const People = artifacts.require('People');
const truffleAssert = require('truffle-assertions');
contract('People', async function (accounts) {
let instance;
beforeEach(async function () {
instance = await People.deployed();
});
it('should not create a person with age over 150 years', async function () {
await truffleAssert.fails(
instance.createPerson('Bob', 200, 190, {
value: web3.utils.toWei('1', 'ether'),
}),
truffleAssert.ErrorType.REVERT
);
});
it('should not create a person without payment', async function () {
await truffleAssert.fails(
instance.createPerson('Bob', 70, 190, {
value: 1000,
}),
truffleAssert.ErrorType.REVERT
);
});
it('should set senior status correctly', async function () {
await instance.createPerson('Bob', 70, 190, {
value: web3.utils.toWei('1', 'ether'),
});
let result = await instance.getPerson();
assert(result.senior === true, 'Senior status not set');
});
it('should set age correctly', async function () {
let result = await instance.getPerson();
assert(result.age.toNumber() === 70, 'Age not set');
});
it('should not delete person without sending from contract owner address', async function () {
await instance.createPerson('John', 40, 185, {
from: accounts[5],
value: web3.utils.toWei('1', 'ether'),
});
await truffleAssert.fails(
instance.deletePerson(accounts[5], { from: accounts[5] }),
truffleAssert.ErrorType.REVERT
);
});
it('should delete person sending from contract owner address', async function () {
await truffleAssert.passes(
instance.deletePerson(accounts[5], { from: accounts[0] })
);
});
it('should have state and blockchain balances increase by the correct amount', async function () {
let instance = await People.new();
await instance.createPerson('Bob', 30, 190, {
from: accounts[0],
value: web3.utils.toWei('1', 'ether'),
});
let stateBalance = await instance.balance();
let blockchainBalance = await web3.eth.getBalance(instance.address);
assert(
web3.utils.fromWei(stateBalance.toString()) === '1' &&
web3.utils.fromWei(blockchainBalance) === '1'
);
});
it('should not withdraw from non owner address', async function () {
await truffleAssert.fails(
instance.withdrawAll({ from: accounts[5] }),
truffleAssert.ErrorType.REVERT
);
});
it('should withdraw from owner address', async function () {
await truffleAssert.passes(instance.withdrawAll({ from: accounts[0] }));
});
it('should reduce state and blockchain balances by proper amount when withdraw', async function () {
let instance = await People.new();
await instance.createPerson('Bob', 30, 190, {
from: accounts[5],
value: web3.utils.toWei('1', 'ether'),
});
await instance.withdrawAll({ from: accounts[0] });
let stateBalance = await instance.balance();
let blockchainBalance = await web3.eth.getBalance(instance.address);
assert(stateBalance.toString() === '0' && blockchainBalance === '0');
});
it('should credit the owners address with proper amount when withdrawing', async function () {
let instance = await People.new();
await instance.createPerson('Bob', 30, 190, {
from: accounts[6],
value: web3.utils.toWei('1', 'ether'),
});
let ownerBalance = await web3.eth.getBalance(accounts[0]);
await instance.withdrawAll();
let newBalance = await web3.eth.getBalance(accounts[0]);
assert(
parseFloat(ownerBalance) < parseFloat(newBalance),
'New balance has not increased after withdraw'
);
});
});