Skip to content

Commit

Permalink
Payroll: Polish payday test file
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Mar 27, 2019
1 parent 991a214 commit f0bb35c
Show file tree
Hide file tree
Showing 2 changed files with 692 additions and 282 deletions.
95 changes: 80 additions & 15 deletions future-apps/payroll/test/payroll_allowed_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ contract('Payroll, allowed tokens,', function(accounts) {
acl = ACL.at(await dao.acl())

priceFeed = await PriceFeed.new()
priceFeed.mockSetTimestamp(NOW)

payrollBase = await Payroll.new()
usdToken = await deployErc20TokenAndDeposit(owner, finance, vault, 'USD', USD_DECIMALS)

usdToken = await deployErc20TokenAndDeposit(owner, finance, vault, 'USD', USD_DECIMALS)
await redistributeEth(accounts, finance)
})

beforeEach('create payroll instance and initialize', async () => {
const receipt = await dao.newAppInstance('0x4321', payrollBase.address, '0x', false, { from: owner })
payroll = Payroll.at(getEventArgument(receipt, 'NewAppProxy', 'proxy'))
await payroll.initialize(finance.address, usdToken.address, priceFeed.address, RATE_EXPIRATION_TIME, { from: owner })
})

beforeEach('set timestamps', async () => {
await payroll.mockSetTimestamp(NOW)
await priceFeed.mockSetTimestamp(NOW)
})

beforeEach('grant permissions', async () => {
Expand All @@ -54,7 +56,7 @@ contract('Payroll, allowed tokens,', function(accounts) {
})

describe('determineAllocation', () => {
const tokenAddresses = [], allocations = [10, 20, 70]
const tokenAddresses = []

before('deploy some tokens', async () => {
const token1 = await deployErc20TokenAndDeposit(owner, finance, vault, 'Token 1', 14)
Expand All @@ -80,20 +82,82 @@ contract('Payroll, allowed tokens,', function(accounts) {
context('when the amount of tokens and allocations match', () => {
context('when the given list is not empty', () => {
context('when all the given tokens are allowed', () => {
it('persists requested allocation', async () => {
const receipt = await payroll.determineAllocation(tokenAddresses, allocations, { from })
context('when the allocations add up to 100', () => {

const itDeterminesAllocationsProperly = allocations => {
context('when there was no previous allocation', () => {
it('persists requested allocation', async () => {
const receipt = await payroll.determineAllocation(tokenAddresses, allocations, { from })

const events = getEvents(receipt, 'DetermineAllocation')
assert.equal(events.length, 1, 'number of emitted DetermineAllocation events does not match')
assert.equal(events[0].args.employee, employee, 'employee address should match')
assert.equal(events[0].args.employeeId.toString(), employeeId, 'employee id should match')

for (const tokenAddress of tokenAddresses) {
const expectedAllocation = allocations[tokenAddresses.indexOf(tokenAddress)]
assert.equal(await payroll.getAllocation(employeeId, tokenAddress), expectedAllocation, 'token allocation does not match')
}

assert.equal(await payroll.getAllocation(employeeId, anyone), 0, 'token allocation should be zero')
})
})

context('when there was a previous allocation', () => {
let token

beforeEach('submit previous allocation', async () => {
token = await deployErc20TokenAndDeposit(owner, finance, vault, 'Previous Token', 18)
await payroll.addAllowedToken(token.address, { from: owner })

await payroll.determineAllocation([token.address], [100], { from })
assert.equal(await payroll.getAllocation(employeeId, token.address), 100)

const events = getEvents(receipt, 'DetermineAllocation')
assert.equal(events.length, 1, 'number of emitted DetermineAllocation events does not match')
assert.equal(events[0].args.employee, employee, 'employee address should match')
assert.equal(events[0].args.employeeId.toString(), employeeId, 'employee id should match')
for (const tokenAddress of tokenAddresses) {
assert.equal(await payroll.getAllocation(employeeId, tokenAddress), 0, 'token allocation does not match')
}
})

for (const tokenAddress of tokenAddresses) {
const expectedAllocation = allocations[tokenAddresses.indexOf(tokenAddress)]
assert.equal(await payroll.getAllocation(employeeId, tokenAddress), expectedAllocation, 'token allocation does not match')
it('replaces previous allocation for the requested one', async () => {
await payroll.determineAllocation(tokenAddresses, allocations, { from })

for (const tokenAddress of tokenAddresses) {
const expectedAllocation = allocations[tokenAddresses.indexOf(tokenAddress)]
assert.equal(await payroll.getAllocation(employeeId, tokenAddress), expectedAllocation, 'token allocation does not match')
}

assert.equal(await payroll.getAllocation(employeeId, token.address), 0)
})
})
}

assert.equal(await payroll.getAllocation(employeeId, anyone), 0, 'token allocation should be zero')
context('when the allocation list does not include zero values', () => {
const allocations = [10, 20, 70]

itDeterminesAllocationsProperly(allocations)
})

context('when the allocation list includes zero values', () => {
const allocations = [90, 10, 0]

itDeterminesAllocationsProperly(allocations)
})
})

context('when the allocations add up less than 100', () => {
const allocations = [10, 20, 69]

it('reverts', async () => {
await assertRevert(payroll.determineAllocation(tokenAddresses, allocations, { from }), 'PAYROLL_DISTRIBUTION_NO_COMPLETE')
})
})

context('when the allocations add up more than 100', () => {
const allocations = [10, 20, 71]

it('reverts', async () => {
await assertRevert(payroll.determineAllocation(tokenAddresses, allocations, { from }), 'PAYROLL_DISTRIBUTION_NO_COMPLETE')
})
})
})

Expand Down Expand Up @@ -124,6 +188,7 @@ contract('Payroll, allowed tokens,', function(accounts) {

context('when the amount of tokens and allocations do not match', () => {
it('reverts', async () => {
const allocations = [100]
const addresses = [...tokenAddresses, anyone]

await assertRevert(payroll.determineAllocation(addresses, allocations, { from }), 'PAYROLL_TOKEN_ALLOCATION_MISMATCH')
Expand All @@ -149,7 +214,7 @@ contract('Payroll, allowed tokens,', function(accounts) {
const from = anyone

it('reverts', async () => {
await assertRevert(payroll.determineAllocation(tokenAddresses, allocations, { from }), 'PAYROLL_EMPLOYEE_DOES_NOT_MATCH')
await assertRevert(payroll.determineAllocation(tokenAddresses, [100, 0, 0], { from }), 'PAYROLL_EMPLOYEE_DOES_NOT_MATCH')
})
})
})
Expand Down
Loading

0 comments on commit f0bb35c

Please sign in to comment.