From e88c98a695d12481eedd399647bc54da5ec96721 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Thu, 23 Feb 2017 21:39:38 +0100 Subject: [PATCH] test_count_votes --- bigchaindb/voting.py | 2 +- tests/test_voting.py | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/bigchaindb/voting.py b/bigchaindb/voting.py index 7962eec4..c5482e92 100644 --- a/bigchaindb/voting.py +++ b/bigchaindb/voting.py @@ -94,8 +94,8 @@ class Voting: n_invalid += 1 continue - prev_blocks[vote['vote']['previous_block']] += 1 if vote['vote']['is_block_valid']: + prev_blocks[vote['vote']['previous_block']] += 1 n_valid += 1 else: n_invalid += 1 diff --git a/tests/test_voting.py b/tests/test_voting.py index 5f1a1069..e70addf1 100644 --- a/tests/test_voting.py +++ b/tests/test_voting.py @@ -31,16 +31,27 @@ def test_partition_eligible_votes(): # Test vote counting -@patch('bigchaindb.voting.Voting.verify_vote_schema') -def test_count_votes(_): - nodes = list(map(Bigchain, 'abc')) +def test_count_votes(): + class TestVoting(Voting): + @classmethod + def verify_vote_schema(cls, vote): + return vote['node_pubkey'] != 'malformed' - votes = [n.vote('block', 'a', True) for n in nodes] + voters = ['cheat', 'cheat', 'says invalid', 'malformed'] + voters += ['kosher' + str(i) for i in range(10)] - assert Voting.count_votes(votes)['counts'] == { - 'n_valid': 3, - 'n_invalid': 0, - 'n_agree_prev_block': 3 + votes = [Bigchain(v).vote('block', 'a', True) for v in voters] + votes[2]['vote']['is_block_valid'] = False + votes[-1]['vote']['previous_block'] = 'z' + + assert TestVoting.count_votes(votes) == { + 'counts': { + 'n_valid': 10, + 'n_invalid': 3, + 'n_agree_prev_block': 9 + }, + 'cheat': [votes[:2]], + 'malformed': [votes[3]], }