-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.js
104 lines (85 loc) · 4.12 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var Registry = artifacts.require("./RealEstateRegistry.sol");
contract('RealEstateRegistry', function(accounts) {
var seller = accounts[0];
var inspector = accounts[1];
var buyer = accounts[2];
var initialBuyerBalance = web3.fromWei(web3.eth.getBalance(buyer)).toNumber();
var initialSellerBalance = web3.fromWei(web3.eth.getBalance(seller)).toNumber();
var initialInspectorBalance = web3.fromWei(web3.eth.getBalance(inspector)).toNumber();
console.log('Initial buyer balance:', initialBuyerBalance);
console.log('Initial seller balance:', initialSellerBalance);
console.log('Initial inspector balance:', initialInspectorBalance);
var appartmentPrice = web3.toWei(15, "Ether");
console.log('Appartment price:', web3.fromWei(appartmentPrice), 'ETH');
var status = {0: 'Owned', 1: 'On Sale', 2: 'Wait Confirmation'};
function printEstate(estate) {
console.log(' - Real Estate with id ' + estate[0] + '\n\tname: ' + estate[1] + '\n\tprice: ' + web3.fromWei(estate[2], 'Ether') +
' eth\n\tstatus: ' + status[estate[3]] + '\n\towner: ' + estate[4], '\n\tpotential owner: ' + estate[5]);
}
it("Should set on sale the 1 real estate", function(done) {
var contract;
Registry.deployed().then(function(instance) {
contract = instance;
return contract.getEstate.call(1);
}).then(function(estate) {
printEstate(estate);
console.log('Sale 1 appartment');
return contract.sell(1, appartmentPrice, { from: seller });
}).then(function(tx) {
return contract.getEstate.call(1);
}).then(function(estate) {
printEstate(estate);
}).then(done);
});
it("Should buy the 1 real estate", function(done) {
var contract;
Registry.deployed().then(function(instance) {
contract = instance;
console.log('Buy 1 real estate');
return contract.buy(1, { from: buyer });
}).then(function(tx) {
return contract.getEstate.call(1);
}).then(function(estate) {
printEstate(estate);
}).then(done);
});
it("Should check that buyer spent required amount of money", function(done) {
var balance = web3.fromWei(web3.eth.getBalance(buyer)).toNumber();
assert.approximately(balance, initialBuyerBalance - web3.fromWei(appartmentPrice), 0.02, 'Buyer didn\'t spend any money on purchase');
done();
});
it("Should confirm the purchase", function(done) {
var contract;
Registry.deployed().then(function(instance) {
contract = instance;
console.log('Confirm purchase of the 1 real estate');
return contract.confirm(1, { from: inspector });
}).then(function(tx) {
return contract.getEstate.call(1);
}).then(function(estate) {
printEstate(estate);
}).then(done);
});
it("Should check that seller received appropriate amount of money", function(done) {
var balance = web3.fromWei(web3.eth.getBalance(seller)).toNumber();
assert.approximately(balance, initialSellerBalance + web3.fromWei(appartmentPrice) * 0.95, 0.02, 'Seller didn\'t received appropriate amount of money');
done();
});
it("Should check that inspector received appropriate amount of money", function(done) {
var balance = web3.fromWei(web3.eth.getBalance(inspector)).toNumber();
assert.approximately(balance, initialInspectorBalance + web3.fromWei(appartmentPrice) * 0.05, 0.02, 'Inspector didn\'t received appropriate amount of money');
done();
});
it("Should add new real estate", function(done) {
var contract;
Registry.deployed().then(function(instance) {
contract = instance;
console.log('Add new real estate to the registry');
return contract.addEstate('New Estate', web3.toWei(2, 'Ether'), { from: seller });
}).then(function(tx) {
return contract.getEstate.call(4);
}).then(function(estate) {
printEstate(estate);
}).then(done);
});
});