Merge pull request #101 from adklempner/test/DayLimit
Create tests for DayLimit
This commit is contained in:
15
contracts/test-helpers/DayLimitMock.sol
Normal file
15
contracts/test-helpers/DayLimitMock.sol
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
pragma solidity ^0.4.4;
|
||||||
|
import "../DayLimit.sol";
|
||||||
|
|
||||||
|
contract DayLimitMock is DayLimit {
|
||||||
|
uint public totalSpending;
|
||||||
|
|
||||||
|
function DayLimitMock(uint _value, address[] _owners, uint _required) DayLimit(_value) Shareable(_owners, _required) {
|
||||||
|
totalSpending = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function attemptSpend(uint _value) external limitedDaily(_value) {
|
||||||
|
totalSpending += _value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
72
test/DayLimit.js
Normal file
72
test/DayLimit.js
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
contract('DayLimit', function(accounts) {
|
||||||
|
|
||||||
|
it('should construct with the passed daily limit', async function() {
|
||||||
|
let initLimit = 10;
|
||||||
|
let dayLimit = await DayLimitMock.new(initLimit, accounts, 2);
|
||||||
|
let dailyLimit = await dayLimit.dailyLimit();
|
||||||
|
assert.equal(initLimit, dailyLimit);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to spend if daily limit is not reached', async function() {
|
||||||
|
let limit = 10;
|
||||||
|
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(8);
|
||||||
|
let spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(2);
|
||||||
|
spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should prevent spending if daily limit is reached', async function() {
|
||||||
|
let limit = 10;
|
||||||
|
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(8);
|
||||||
|
let spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(3);
|
||||||
|
spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should allow spending if daily limit is reached and then set higher', async function() {
|
||||||
|
let limit = 10;
|
||||||
|
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(8);
|
||||||
|
let spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(3);
|
||||||
|
spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
|
||||||
|
await dayLimit.setDailyLimit(15);
|
||||||
|
await dayLimit.attemptSpend(3);
|
||||||
|
spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 11);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should allow spending if daily limit is reached and then amount spent is reset', async function() {
|
||||||
|
let limit = 10;
|
||||||
|
let dayLimit = await DayLimitMock.new(limit, accounts, 1);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(8);
|
||||||
|
let spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
|
||||||
|
await dayLimit.attemptSpend(3);
|
||||||
|
spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 8);
|
||||||
|
|
||||||
|
await dayLimit.resetSpentToday(15);
|
||||||
|
await dayLimit.attemptSpend(3);
|
||||||
|
spentToday = await dayLimit.spentToday();
|
||||||
|
assert.equal(spentToday, 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user