Organize test files following contracts folders structure

This commit is contained in:
Facundo Spagnuolo
2018-01-09 12:34:29 -03:00
parent 7532dab17d
commit b925b2dae6
31 changed files with 55 additions and 56 deletions

View File

@ -0,0 +1,71 @@
var PullPaymentMock = artifacts.require('mocks/PullPaymentMock.sol');
contract('PullPayment', function (accounts) {
let ppce;
let amount = 17 * 1e18;
beforeEach(async function () {
ppce = await PullPaymentMock.new({ value: amount });
});
it('can\'t call asyncSend externally', async function () {
assert.isUndefined(ppce.asyncSend);
});
it('can record an async payment correctly', async function () {
let AMOUNT = 100;
await ppce.callSend(accounts[0], AMOUNT);
let paymentsToAccount0 = await ppce.payments(accounts[0]);
let totalPayments = await ppce.totalPayments();
assert.equal(totalPayments, AMOUNT);
assert.equal(paymentsToAccount0, AMOUNT);
});
it('can add multiple balances on one account', async function () {
await ppce.callSend(accounts[0], 200);
await ppce.callSend(accounts[0], 300);
let paymentsToAccount0 = await ppce.payments(accounts[0]);
let totalPayments = await ppce.totalPayments();
assert.equal(totalPayments, 500);
assert.equal(paymentsToAccount0, 500);
});
it('can add balances on multiple accounts', async function () {
await ppce.callSend(accounts[0], 200);
await ppce.callSend(accounts[1], 300);
let paymentsToAccount0 = await ppce.payments(accounts[0]);
assert.equal(paymentsToAccount0, 200);
let paymentsToAccount1 = await ppce.payments(accounts[1]);
assert.equal(paymentsToAccount1, 300);
let totalPayments = await ppce.totalPayments();
assert.equal(totalPayments, 500);
});
it('can withdraw payment', async function () {
let payee = accounts[1];
let initialBalance = web3.eth.getBalance(payee);
await ppce.callSend(payee, amount);
let payment1 = await ppce.payments(payee);
assert.equal(payment1, amount);
let totalPayments = await ppce.totalPayments();
assert.equal(totalPayments, amount);
await ppce.withdrawPayments({ from: payee });
let payment2 = await ppce.payments(payee);
assert.equal(payment2, 0);
totalPayments = await ppce.totalPayments();
assert.equal(totalPayments, 0);
let balance = web3.eth.getBalance(payee);
assert(Math.abs(balance - initialBalance - amount) < 1e16);
});
});

View File

@ -0,0 +1,78 @@
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-as-promised'))
.use(require('chai-bignumber')(BigNumber))
.should();
const EVMThrow = require('../helpers/EVMThrow.js');
const SplitPayment = artifacts.require('../contracts/payment/SplitPayment.sol');
contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, payer1]) {
const amount = web3.toWei(1.0, 'ether');
beforeEach(async function () {
this.payees = [payee1, payee2, payee3];
this.shares = [20, 10, 70];
this.contract = await SplitPayment.new(this.payees, this.shares);
});
it('should accept payments', async function () {
await web3.eth.sendTransaction({ from: owner, to: this.contract.address, value: amount });
const balance = web3.eth.getBalance(this.contract.address);
balance.should.be.bignumber.equal(amount);
});
it('should store shares if address is payee', async function () {
const shares = await this.contract.shares.call(payee1);
shares.should.be.bignumber.not.equal(0);
});
it('should not store shares if address is not payee', async function () {
const shares = await this.contract.shares.call(nonpayee1);
shares.should.be.bignumber.equal(0);
});
it('should throw if no funds to claim', async function () {
await this.contract.claim({ from: payee1 }).should.be.rejectedWith(EVMThrow);
});
it('should throw if non-payee want to claim', async function () {
await web3.eth.sendTransaction({ from: payer1, to: this.contract.address, value: amount });
await this.contract.claim({ from: nonpayee1 }).should.be.rejectedWith(EVMThrow);
});
it('should distribute funds to payees', async function () {
await web3.eth.sendTransaction({ from: payer1, to: this.contract.address, value: amount });
// receive funds
const initBalance = web3.eth.getBalance(this.contract.address);
initBalance.should.be.bignumber.equal(amount);
// distribute to payees
const initAmount1 = web3.eth.getBalance(payee1);
await this.contract.claim({ from: payee1 });
const profit1 = web3.eth.getBalance(payee1) - initAmount1;
assert(Math.abs(profit1 - web3.toWei(0.20, 'ether')) < 1e16);
const initAmount2 = web3.eth.getBalance(payee2);
await this.contract.claim({ from: payee2 });
const profit2 = web3.eth.getBalance(payee2) - initAmount2;
assert(Math.abs(profit2 - web3.toWei(0.10, 'ether')) < 1e16);
const initAmount3 = web3.eth.getBalance(payee3);
await this.contract.claim({ from: payee3 });
const profit3 = web3.eth.getBalance(payee3) - initAmount3;
assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16);
// end balance should be zero
const endBalance = web3.eth.getBalance(this.contract.address);
endBalance.should.be.bignumber.equal(0);
// check correct funds released accounting
const totalReleased = await this.contract.totalReleased.call();
totalReleased.should.be.bignumber.equal(initBalance);
});
});