-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection.js
88 lines (82 loc) · 4.11 KB
/
election.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
var Election = artifacts.require("./Election.sol");
contract("Election", function (accounts) {
var electionInstance;
it("initializes with two candidates", function () {
return Election.deployed().then(function (instance) {
return instance.candidatesCount();
}).then(function (count) {
assert.equal(count, 2);
});
});
it("initializes the candidates with the correct values", function () {
return Election.deployed().then(function (instance) {
electionInstance = instance;
return electionInstance.candidates(1);
}).then(function (candidate) {
assert.equal(candidate.id, 1, "contains the correct id");
assert.equal(candidate.name, "Candidate 1", "contains the correct name");
assert.equal(candidate.voteCount, 0, "contains the correct votes count");
return electionInstance.candidates(2);
}).then(function (candidate) {
assert.equal(candidate.id, 2, "contains the correct id");
assert.equal(candidate.name, "Candidate 2", "contains the correct name");
assert.equal(candidate.voteCount, 0, "contains the correct votes count");
});
});
it("allows a voter to cast a vote", function () {
return Election.deployed().then(function (instance) {
electionInstance = instance;
candidateId = 1;
return electionInstance.vote(candidateId, { from: accounts[0] });
}).then(function (receipt) {
assert.equal(receipt.logs.length, 1, "an event was triggered");
assert.equal(receipt.logs[0].event, "votedEvent", "the event type is correct");
assert.equal(receipt.logs[0].args._candidateId.toNumber(), candidateId, "the candidate id is correct");
return electionInstance.voters(accounts[0]);
}).then(function (voted) {
assert(voted, "the voter was marked as voted");
return electionInstance.candidates(candidateId);
}).then(function (candidate) {
var voteCount = candidate[2];
assert.equal(voteCount, 1, "increments the candidate's vote count");
});
});
it("throws an exception for invalid candiates", function () {
return Election.deployed().then(function (instance) {
electionInstance = instance;
return electionInstance.vote(99, { from: accounts[1] })
}).then(assert.fail).catch(function (error) {
assert(error.message.indexOf('revert') >= 0, "error message must contain revert");
return electionInstance.candidates(1);
}).then(function (candidate1) {
var voteCount = candidate1[2];
assert.equal(voteCount, 1, "candidate 1 did not receive any votes");
return electionInstance.candidates(2);
}).then(function (candidate2) {
var voteCount = candidate2[2];
assert.equal(voteCount, 0, "candidate 2 did not receive any votes");
});
});
it("throws an exception for double voting", function () {
return Election.deployed().then(function (instance) {
electionInstance = instance;
candidateId = 2;
electionInstance.vote(candidateId, { from: accounts[1] });
return electionInstance.candidates(candidateId);
}).then(function (candidate) {
var voteCount = candidate[2];
assert.equal(voteCount, 1, "accepts first vote");
return electionInstance.vote(candidateId, { from: accounts[1] });
}).then(assert.fail).catch(function (error) {
assert(error.message.indexOf('revert') >= 0, "error message must contain revert");
return electionInstance.candidates(1);
}).then(function (candidate1) {
var voteCount = candidate1[2];
assert.equal(voteCount, 1, "candidate 1 did not receive any votes");
return electionInstance.candidates(2);
}).then(function (candidate2) {
var voteCount = candidate2[2];
assert.equal(voteCount, 1, "candidate 2 did not receive any votes");
});
});
});