Skip to content

Commit

Permalink
test: sum of vote weights must equal 100
Browse files Browse the repository at this point in the history
  • Loading branch information
c0nsol3 committed Jun 30, 2022
1 parent 9fab1d8 commit 7c6c22e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions solar_crypto/transactions/builder/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ def set_votes(


def validate(votes):
if sum(votes.values()) > 100:
raise SolarInvalidTransaction("Total vote weight must not be greater than 100.")

for value in votes.values():
if not valid_precision(value):
raise SolarInvalidTransaction("Only two decimal places are allowed.")

if Decimal(sum(votes.values())) != Decimal("100"):
raise SolarInvalidTransaction("Total vote weight must equal 100.")


def valid_precision(value, max_precision=2):
if isinstance(value, Decimal):
Expand Down
10 changes: 7 additions & 3 deletions tests/transactions/builder/test_vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def test_vote_transaction_input_error():
vote = {"deadlock": 50.3333}
vote = {"fun": 50.67, "deadlock": 50.3333}

transaction = Vote()
with pytest.raises(SolarInvalidTransaction) as e:
Expand Down Expand Up @@ -43,13 +43,17 @@ def test_vote_transaction_multiple_votes(vote):
transaction.verify() # if no exception is raised, it means the transaction is valid


def test_vote_transaction_greater_than_100():
@pytest.mark.parametrize("vote", [
{"fun": 50, "deadlock": 55},
{"fun": 50},
])
def test_vote_transaction_must_equal_100(vote):
vote = {"fun": 50, "deadlock": 55}

transaction = Vote()
with pytest.raises(SolarInvalidTransaction) as e:
transaction.set_votes(vote)
assert str(e.value) == "Total vote weight must not be greater than 100."
assert str(e.value) == "Total vote weight must equal 100."


def test_vote_transaction_using_empty_list():
Expand Down

0 comments on commit 7c6c22e

Please sign in to comment.