def test_eq(self): ballot1 = JsonCaseBallot(choices=(1, 2), weight=3) ballot2 = JsonCaseBallot(choices=(1, 2), weight=3) self.assertEqual(ballot1, ballot2) with self.changeAttr(ballot2, "choices", (1, 3)): self.assertNotEqual(ballot1, ballot2) with self.changeAttr(ballot2, "weight", 100): self.assertNotEqual(ballot1, ballot2) self.assertEqual(ballot1, ballot2) # sanity check
def test_repr_info(self): cases = [ (3, (1, 2), "weight=3 choices=(1, 2)"), (None, None, "weight=None choices=None"), ] for weight, choices, expected in cases: with self.subTest(weight=weight, choices=choices, expected=expected): ballot = JsonCaseBallot() ballot.choices = choices ballot.weight = weight self.assertEqual(ballot.repr_info(), expected)
def test_init(self): ballots = [ JsonCaseBallot(choices=[1, 2]), JsonCaseBallot(choices=[2], weight=3) ] contest = self.make_contest(ballots=ballots) cases = [ ("candidate_count", 2), ("ballots", ballots), ("notes", "foo"), ] for attr, expected in cases: with self.subTest(attr=attr, expected=expected): actual = getattr(contest, attr) self.assertEqual(actual, expected)
def test_to_jsobj(self): cases = [ ({ 'weight': 3, 'choices': (1, 2) }, '3 1 2'), # Test an undervote. ({ 'weight': 3 }, '3'), ] for kwargs, expected in cases: with self.subTest(kwargs=kwargs, expected=expected): jc_ballot = JsonCaseBallot(**kwargs) self.assertEqual(jc_ballot.to_jsobj(), expected)
def test_to_jsobj(self): jc_ballots = [ JsonCaseBallot(choices=(1, 2), weight=3), ] jc_contest = JsonCaseContestInput(index=2, ballots=jc_ballots) expected = {'_meta': {'index': 2}, 'ballots': ['3 1 2']} self.assertEqual(jc_contest.to_jsobj(), expected)
def test_from_jsobj(self): cases = [ ('3 1 2', { 'weight': 3, 'choices': (1, 2) }), # Test an undervote. ('3', { 'weight': 3 }), ] # kwargs is the kwargs for the "expected" JsonCaseBallot. for jsobj, kwargs in cases: with self.subTest(jsobj=jsobj, kwargs=kwargs): jc_ballot = JsonCaseBallot.from_jsobj(jsobj) expected = JsonCaseBallot(**kwargs) jc_ballot.assert_equal(expected)
def test_from_model(self): contest = ContestInput() contest.candidates = ['Ann', 'Bob'] ballots = [(2, (3, 1))] contest.ballots_resource = ListResource(ballots) jc_contest = JsonCaseContestInput.from_model(contest) expected = JsonCaseContestInput(candidate_count=2) expected.ballots = [JsonCaseBallot(weight=2, choices=(3, 1))] jc_contest.assert_equal(expected)
def test_from_model(self): ballot = (2, (3, 1)) jc_ballot = JsonCaseBallot.from_model(ballot) expected = JsonCaseBallot(choices=(3, 1), weight=2) jc_ballot.assert_equal(expected)
def test_repr(self): ballot = JsonCaseBallot(choices=(1, 2), weight=3) expected = "<JsonCaseBallot: [weight=3 choices=(1, 2)] %s>" % hex( id(ballot)) self.assertEqual(repr(ballot), expected)
def test_init__tuple(self): """Check that choices are converted to tuples.""" ballot = JsonCaseBallot(choices=[]) self.assertEqual(ballot.choices, ()) ballot = JsonCaseBallot(choices=[1, 2]) self.assertEqual(ballot.choices, (1, 2))
def test_init__defaults(self): ballot = JsonCaseBallot() self.assertEqual(ballot.choices, ()) self.assertEqual(ballot.weight, 1)
def test_init(self): ballot = JsonCaseBallot(choices=(1, 2), weight=3) self.assertEqual(ballot.choices, (1, 2)) self.assertEqual(ballot.weight, 3)
def make_jc_ballot(weight, choices): return JsonCaseBallot(weight=weight, choices=choices)
def test_from_jsobj__bad_format(self): """Check a string that does not parse.""" with self.assertRaises(JsonDeserializeError): jc_ballot = JsonCaseBallot.from_jsobj("2 a 4")
def test_to_object(self): jc_ballot = JsonCaseBallot(choices=(1, 2), weight=3) ballot = jc_ballot.to_model() self.assertEqual(ballot, (3, (1, 2)))