From b955ef7797b31d5b830690c8e77b9966f423658f Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 31 Jan 2018 17:36:39 +0200 Subject: [PATCH] Tests for WaterVouchers and Governing --- contracts/WaterGoverning.sol | 1 - contracts/WaterVouchers.sol | 4 +- test/WaterVoucher.js | 109 +++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 test/WaterVoucher.js diff --git a/contracts/WaterGoverning.sol b/contracts/WaterGoverning.sol index 07722df..31212da 100644 --- a/contracts/WaterGoverning.sol +++ b/contracts/WaterGoverning.sol @@ -1,6 +1,5 @@ pragma solidity ^0.4.18; -// TODO Write TESTS contract WaterGoverning { address public voucherContractAddress; mapping(address => uint) public meterLiters; diff --git a/contracts/WaterVouchers.sol b/contracts/WaterVouchers.sol index fc57f70..9958395 100644 --- a/contracts/WaterVouchers.sol +++ b/contracts/WaterVouchers.sol @@ -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; @@ -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]; diff --git a/test/WaterVoucher.js b/test/WaterVoucher.js new file mode 100644 index 0000000..6a7a228 --- /dev/null +++ b/test/WaterVoucher.js @@ -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 +}); \ No newline at end of file