Skip to content

Commit

Permalink
test: sum of weight must equal > 100, allow int values
Browse files Browse the repository at this point in the history
  • Loading branch information
c0nsol3 committed Jun 30, 2022
1 parent 3d7cc33 commit a3cc3ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion solar_crypto/transactions/builder/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ def validate(votes):
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):
if abs(value.as_tuple().exponent) <= max_precision:
return True
elif isinstance(value, float) or isinstance(value, str):
elif isinstance(value, float) or isinstance(value, str) or isinstance(value, int):
if str(value)[::-1].find(".") <= max_precision:
return True
return False
Expand Down
23 changes: 19 additions & 4 deletions tests/transactions/builder/test_vote.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import OrderedDict
from decimal import Decimal

import pytest

Expand All @@ -12,17 +13,20 @@


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:
transaction.set_votes(vote)
assert str(e.value) == "Only two decimal places are allowed."


def test_vote_transaction_multiple_votes():
vote = {"fun": 35.3, "deadlock": 64.7}

@pytest.mark.parametrize("vote", [
{"fun": 35.3, "deadlock": 64.7},
{"fun": 50, "deadlock": 50},
{"fun": Decimal(50), "deadlock": Decimal(50)},
])
def test_vote_transaction_multiple_votes(vote):
transaction = Vote()
transaction.set_votes(vote)
transaction.set_nonce(1)
Expand All @@ -39,6 +43,17 @@ def test_vote_transaction_multiple_votes():
transaction.verify() # if no exception is raised, it means the transaction is valid


@pytest.mark.parametrize("vote", [
{"fun": 50, "deadlock": 55},
{"fun": 50},
])
def test_vote_transaction_must_equal_100(vote):
transaction = Vote()
with pytest.raises(SolarInvalidTransaction) as e:
transaction.set_votes(vote)
assert str(e.value) == "Total vote weight must equal 100."


def test_vote_transaction_using_empty_list():
transaction = Vote()
transaction.set_votes(["-awesome"])
Expand Down

0 comments on commit a3cc3ee

Please sign in to comment.