Skip to content

Commit

Permalink
Tests for WaterVouchers and Governing
Browse files Browse the repository at this point in the history
  • Loading branch information
thcrnk committed Jan 31, 2018
1 parent f982c5e commit b955ef7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
1 change: 0 additions & 1 deletion contracts/WaterGoverning.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pragma solidity ^0.4.18;

// TODO Write TESTS
contract WaterGoverning {
address public voucherContractAddress;
mapping(address => uint) public meterLiters;
Expand Down
4 changes: 1 addition & 3 deletions contracts/WaterVouchers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import "zeppelin-solidity/contracts/ownership/Ownable.sol";
import "./WaterGoverning.sol";
import "./PriceEstimator/PriceEstimator.sol";

// TODO Write TESTS

contract WaterVouchers is Ownable {
address public waterGoverningContractAddress;
address public priceEstimatorContractAddress;
Expand Down Expand Up @@ -80,7 +78,7 @@ contract WaterVouchers is Ownable {
return (amountResult, priceResult);
}

function getLastVoucherLitersInMonth(address _meter, uint256 _timestampEnd) public constant returns(uint256 liters) {
function getLastVoucherLitersInMonth(address _meter, uint256 _timestampEnd) public view returns(uint256 liters) {
uint256 resultLiters;
bytes32[] storage voucherIdsLoc = meterVouchers[_meter];

Expand Down
109 changes: 109 additions & 0 deletions test/WaterVoucher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const HouseholdMeters = artifacts.require("./HouseholdMeters.sol");
const PriceEstimator = artifacts.require("./PriceEstimator/PriceEstimator.sol");
const IPriceEstimator = artifacts.require("./PriceEstimator/IPriceEstimator.sol");
const PriceEstimatorProxy = artifacts.require("./PriceEstimator/PriceEstimatorProxy.sol");
const IOwnableUpgradeableImplementation = artifacts.require("./Upgradeability/OwnableUpgradeableImplementation/IOwnableUpgradeableImplementation.sol");
const WaterVoucher = artifacts.require("./WaterVouchers.sol");
const WaterGoverning = artifacts.require("./WaterGoverning.sol");
const util = require('./util');
const expectThrow = util.expectThrow;
const web3 = require("web3");

contract('PriceEstimator', function (accounts) {
const _owner = accounts[0];
const _notOwner = accounts[1];

const _householdMembers = 4;
const _householdMembersUpdate = 6;

const _householdMeter = "0x638cdecbdb9af6f7bc9b9415633fb42be0f89ad8";
const _householdMeterUpdate = "0x638cdecbdb9af6f7bc9b9415633fb42be0f89ad9";

const _admin1 = accounts[2];
const _admin2 = accounts[3];

const _liters1 = 4000;
const _liters2 = 10000;
const _liters3 = 30000;
const _liters4 = 60000;

const _price1 = 456;
const _price2 = 1775;
const _price3 = 4153;
const _price4 = 23859;

const _voucherId1 = "1";
const _voucherId2 = "2";

let HouseholdMetersContract;
let PriceEstimatorContract;
let WaterVoucherContract;
let PriceEstimatorContractImpl;
let PriceEstimatorContractProxy;

describe("Purchase voucher", () => {
beforeEach(async function () {
HouseholdMetersContract = await HouseholdMeters.new({
from: _owner
});

WaterVoucherContract = await WaterVoucher.new({
from: _owner
});

WaterGoverningContract = await WaterGoverning.new(WaterVoucherContract.address, {
from: _owner
});

let PriceEstimatorContractImpl = await PriceEstimator.new({
from: _owner
});

let PriceEstimatorContractProxy = await PriceEstimator.new(PriceEstimatorContractImpl.address, {
from: _owner
});

PriceEstimatorContract = await IPriceEstimator.at(PriceEstimatorContractProxy.address);

await PriceEstimatorContract.init({
from: _owner
});
await PriceEstimatorContract.setHouseholdMetersContract(HouseholdMetersContract.address);
await PriceEstimatorContract.setWaterVouchersContract(WaterVoucherContract.address);

await WaterVoucherContract.setPriceEstimatorContractAddress(PriceEstimatorContract.address);
await WaterVoucherContract.setWaterGoverningContractAddress(WaterGoverningContract.address);

await HouseholdMetersContract.addAdmin(_admin1);
await HouseholdMetersContract.addHouseholdMeter(_householdMeter, _householdMembers, {
from: _admin1
});

await WaterVoucherContract.addIntermediary(_admin1);
});

it("should purchase a voucher", async function () {
let result = await WaterVoucherContract.purchaseVoucher(_voucherId1, _householdMeter, _liters1, {
from: _admin1
});

assert.isTrue(Boolean(result.receipt.status), "The voucher was not purchased successfully");
});

it("should increase meter liters", async function () {
let result = await WaterVoucherContract.purchaseVoucher(_voucherId1, _householdMeter, _liters1, {
from: _admin1
});
let meterLiters = await WaterGoverningContract.meterLiters(_householdMeter);
assert(meterLiters.eq(_liters1), "The voucher was not purchased successfully");
});
});

// TODO write more test for
// addIntermediary
// removeIntermediary
// setPriceEstimatorContractAddress
// setWaterGoverningContractAddress
// estimatePrice
// getLastVoucherLitersInMonth
});

0 comments on commit b955ef7

Please sign in to comment.