コード例 #1
0
def test_initialization_errors():
    name = 'BadElection'
    contests = {}
    for i in range(10):
        contests[str(i)] = util.generate_contest(100)
    single_contest = util.generate_contest(100)

    # name TypeError
    with pytest.raises(TypeError):
        Election(123, 100, contests)
    with pytest.raises(TypeError):
        Election(False, 100, contests)
    # total_ballots TypeError
    with pytest.raises(TypeError):
        Election(name, 12.5, contests)
    with pytest.raises(TypeError):
        Election(name, False, contests)
    with pytest.raises(TypeError):
        Election(name, '1200', contests)
    # total_ballots ValueError
    with pytest.raises(ValueError):
        Election(name, 0, contests)
    # contests TypeError
    with pytest.raises(TypeError):
        Election(name, 100, 12)
    with pytest.raises(TypeError):
        Election(name, 100, True)
    with pytest.raises(TypeError):
        Election(name, 100, single_contest)
    with pytest.raises(TypeError):
        Election(name, 100, None)
    contests['test1'] = '120'
    with pytest.raises(TypeError):
        Election(name, 100, contests)
コード例 #2
0
def test_repr():
    contests = {}
    for i in range(10):
        contests[str(i)] = util.generate_contest(100)
    simple_election1 = Election('Example Election', 100, contests)
    simple_election2 = Election('Example Election', 100, contests)
    assert repr(simple_election1) == repr(simple_election2)
コード例 #3
0
def test_simple_election():
    contests = {}
    for i in range(10):
        contests[str(i)] = util.generate_contest(100)
    simple_election = Election('test_simple_election', 1000, contests)
    assert simple_election.name == 'test_simple_election'
    assert simple_election.total_ballots == 1000
    assert simple_election.contests is contests
コード例 #4
0
def test_str():
    contests = {}
    for i in range(10):
        contests[str(i)] = util.generate_contest(100)
    simple_election = Election('Example Election', 100, contests)
    election_str = 'Election\n--------\nName: Example Election\n'
    election_str += 'Total Ballots: 100\nList of Contests:\n'
    for name, contest in contests.items():
        election_str += '\n' + name + '\n'
        election_str += str(contest)
    assert str(simple_election) == election_str
コード例 #5
0
import json
import math

import pytest
from click.testing import CliRunner

from r2b2.cli import cli
from r2b2.contest import Contest
from r2b2.contest import ContestType
from r2b2.minerva import Minerva
from r2b2.tests import util as util

default_contest = util.generate_contest(10000)
tol = 0.000001


def test_simple_minerva():
    simple_minerva = Minerva(.1, .1, default_contest)
    assert simple_minerva.alpha == .1
    assert simple_minerva.beta == 0.0
    assert simple_minerva.max_fraction_to_draw == .1
    assert len(simple_minerva.rounds) == 0
    assert len(simple_minerva.sub_audits) == 1
    assert simple_minerva.get_risk_level() is None
    simple_minerva.rounds.append(10)
    simple_minerva.stopped = True
    assert simple_minerva.next_sample_size() == 10
    assert simple_minerva.next_sample_size(verbose=True) == (10, 0, 1)


def test_min_sample_size():