def test_confirm_types(self): self.assertTrue(decimal5._confirm_types(28, int, str)) self.assertTrue(decimal5._confirm_types('abc', str)) self.assertTrue(decimal5._confirm_types(D5TOTAL(), D5)) _test_aids.assertRaises_with_message( self, sb1288.decimal5.Decimal5Error, 'Value is not an instance of the required type:', decimal5._confirm_types, (3.4, int, str))
def run_test_spec(test_case, input_json): """ Run a test case using test specs from a JSON text file """ tabulate_args, test_spec = with_json.build_tabulate_args( input_json, 'all-tests-spec.json') if 'maxDiff' in test_spec: maxDiff = test_spec['maxDiff'] if maxDiff is None: test_case.maxDiff = None if type(maxDiff) == int and maxDiff >= 0: test_case.maxDiff = maxDiff try: print_description = test_spec['print_description'] except KeyError: print_description = False if print_description: print('\n """' + test_spec['description'] + '"""') if 'exception' in test_spec: exception_type, exception_message = test_spec['exception'] _test_aids.assertRaises_with_message(test_case, u2s(exception_type), u2s(exception_message), rcv.tabulate, tabulate_args) else: expected_elected = validate.str_tuple(test_spec['elected']) expected_status = _test_aids.build_expected_status( test_spec['status_codes']) expected_tally = { _test_aids.u2s(candidate): [ K.Decimal(vote_total) if test_spec['nbr_seats_to_fill'] > 1 else vote_total for vote_total in votes ] for candidate, votes in test_spec['tally'].items() } elected, status, tally = rcv.Tabulation(*tabulate_args).tabulate() if 'print_results' in test_spec and test_spec['print_results']: print_elected(elected) print_status(status) print_tally(tally) try: description = test_spec['description'] except KeyError: description = None jason_str = with_json.results_to_json(elected, status, tally, description) print(jason_str) status_dict = { candidate: status.as_dict() for candidate, status in status.items() } test_case.assertEqual(tally, expected_tally) test_case.assertEqual(status_dict, expected_status) test_case.assertEqual(set(elected), set(expected_elected))
def test_str_tuple_from_tuple(self): self.assertEqual(validate.str_tuple(tuple()), tuple()) self.assertEqual(validate.str_tuple(('',)), ('',)) self.assertEqual(validate.str_tuple(('#',)), ('#',)) self.assertEqual(validate.str_tuple(('AA',)), ('AA',)) self.assertEqual(validate.str_tuple(('A', 'B')), ('A', 'B')) self.assertEqual(validate.str_tuple(('A', '', 'B')), ('A', '', 'B')) _test_aids.assertRaises_with_message(self, TypeError, 'Item in tuple is not a str:', validate.str_tuple, (('A', 7, 'B'),))
def test_str_tuple_from_list(self): self.assertEqual(validate.str_tuple([]), tuple()) self.assertEqual(validate.str_tuple(['']), ('',)) self.assertEqual(validate.str_tuple(['#']), ('#',)) self.assertEqual(validate.str_tuple(['AA']), ('AA',)) self.assertEqual(validate.str_tuple(['A', 'B']), ('A', 'B')) self.assertEqual(validate.str_tuple(['A', '', 'B']), ('A', '', 'B')) _test_aids.assertRaises_with_message(self, TypeError, 'Item in list is not a str:', validate.str_tuple, (['A', 7, 'B'],))
def test_candidates_invalid(self): _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidates type:', validate.candidates, (17,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name:', validate.candidates, (('A', ''),)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name:', validate.candidates, (('A', '#'),)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name:', validate.candidates, (('A', ':who'),)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Candidate names are not unique.', validate.candidates, (('A', 'B', 'A'),))
def test_decimal5_compare(self): test_compare(self, D5(), D5(), 0) test_compare(self, D5(), D5(0), 0) test_compare(self, D5(-1), D5(-1), 0) test_compare(self, D5(-3), D5(-2), -1) test_compare(self, D5(798, -5), D5(7987, -6), 0) test_compare(self, D5(12), D5(2), 1) test_compare(self, D5(122), D5(29), 1) self.assertFalse(D5() == None) #print() _test_aids.assertRaises_with_message( self, sb1288.decimal5.Decimal5Error, 'Value is not an instance of the required type:', D5().__eq__, (0, )) _test_aids.assertRaises_with_message( self, sb1288.decimal5.Decimal5Error, 'Value is not an instance of the required type:', (lambda x, y: x < y), (D5(1), 7))
def test_decimal5_div(self): self.assertEqual(D5(24).__div__(D5(3))._get_value(), 800000) self.assertEqual(D5(2) / D5(3), D5(66666, -5)) self.assertEqual(D5(-2) / D5(3), D5(-66666, -5)) self.assertEqual(D5(2) / D5(-3), D5(-66666, -5)) self.assertEqual(D5(-2) / D5(-3), D5(66666, -5)) self.assertEqual(D5(2) / 3, D5(66666, -5)) self.assertEqual(D5(2).divide_by(3, round_away=True), D5(66667, -5)) self.assertEqual(D5(2).__div__(D5(3), round_away=True), D5(66667, -5)) self.assertEqual( D5(-2).__div__(D5(3), round_away=True), D5(-66667, -5)) self.assertEqual(D5(-314159, -5) / D5(-314159, -5), D5(1)) self.assertEqual(D5(2).divide_by(D5(-3)), D5(-66666, -5)) self.assertEqual( D5(2).divide_by(D5(-3), round_away=True), D5(-66667, -5)) #print() _test_aids.assertRaises_with_message( self, ZeroDivisionError, ('integer division or modulo by zero', 'long division or modulo by zero'), D5.divide_by, (D5(2), D5()))
def test_nbr_seats_to_fill_invalid(self): _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'nbr_seats_to_fill not an int:', validate.nbr_seats_to_fill, ('two',)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'nbr_seats_to_fill not >= 1:', validate.nbr_seats_to_fill, (0,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'nbr_seats_to_fill not >= 1:', validate.nbr_seats_to_fill, (-1,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'nbr_seats_to_fill not >= 1:', validate.nbr_seats_to_fill, (-2,))
def test_indent_message(self): self.assertEqual(errors.indent_message('Hello'), ' Hello') self.assertEqual(errors.indent_message('Hello\n'), ' Hello\n') self.assertEqual(errors.indent_message('Hello\nagain'), ' Hello\n again') self.assertEqual(errors.indent_message( 'Hello\nagain\n'), ' Hello\n again\n') self.assertEqual(errors.indent_message( 'Hello\n again\n'), ' Hello\n again\n') self.assertEqual(errors.indent_message( 'Hello\n\nagain\n'), ' Hello\n \n again\n') self.assertEqual(errors.indent_message(''), ' ') self.assertEqual(errors.indent_message('Hello', indent_by=4), ' Hello') self.assertEqual(errors.indent_message( 'Hello\n', indent_by=4), ' Hello\n') self.assertEqual(errors.indent_message( 'Hello\nagain', indent_by=4), ' Hello\n again') if False: _test_aids.assertRaises_with_message(self, decimal5.Decimal5Error, 'Value is not a supported type: type=\'<class \'str\'>\', ' + 'str(value)=\'2.8\'', D5, ('2.8',))
def test_decimal5_creation_with_list(self): #print() _test_aids.assertRaises_with_message(self, decimal5.Decimal5Error, 'Value is not a supported type:', D5, ([-3.9], ))
def test_str_tuple_from_other(self): type_type = 'type' if sys.version_info.major == 2 else 'class' _test_aids.assertRaises_with_message(self, TypeError, 'Can not make a str_tuple from a <' + type_type + ' \'set\'>.', validate.str_tuple, (set(('A', 7, 'B')),))
def test_options_invalid(self): _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'options is not a dict:', validate.options, (7,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'An option name is not a str:', validate.options, ({('junk',): True},)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'The option \'stop_at_majority\' must be True or False.', validate.options, ({'stop_at_majority': 1},)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid option value type:', validate.options, ({'alternative_defeats': True},)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid per-round option value:', validate.options, ({'alternative_defeats': ('Z',)},)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid per-round option value:', validate.options, ({'alternative_defeats': ['Z']},)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid per-round option value:', validate.options, ({'alternative_defeats': ' Z'},)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid option name:', validate.options, ({'no_skipped_rankings': True},))
def test_max_ranking_levels_invalid(self): _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'max_ranking_levels not an int:', validate.max_ranking_levels, ('two',)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'max_ranking_levels is less than 3:', validate.max_ranking_levels, (2,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'max_ranking_levels is less than 3:', validate.max_ranking_levels, (1,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'max_ranking_levels is less than 3:', validate.max_ranking_levels, (0,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'max_ranking_levels is less than 3:', validate.max_ranking_levels, (-1,)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'max_ranking_levels is less than 3:', validate.max_ranking_levels, (-2,))
def test_ballots_invalid(self): candidates = validate.str_tuple(' A B C D E F G H I J K L') _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'ballots is not a list or tuple:', validate.ballots, (' A B', candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'A ballot is not a list or tuple:', validate.ballots, ((' A B',), candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'A ballot is not a pair of values:', validate.ballots, ([('A', 'B', 'C')], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'A ballot multiple is not an int:', validate.ballots, ([(0.12,' A B C')], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'A ballot multiple is zero or less:', validate.ballots, ([(-1,' A B C')], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'A ballot multiple is zero or less:', validate.ballots, ([(0,' A B C')], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid ballot rankings type:', validate.ballots, ([(1, 77)], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid ballot rankings type:', validate.ballots, ([(2, ('A', 77))], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Ballot rankings is too long:', validate.ballots, ([(2, ' A B C D')], candidates, 3)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid ballot ranking code:', validate.ballots, ([(2, ('A', 'Z'))], candidates, 3))
def test_tie_breaker_invalid(self): candidates = ('A', 'C') _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid tie_breaker type:', validate.tie_breaker, (17, candidates)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name in tie_breaker:', validate.tie_breaker, (('A', 'B'), candidates)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name in tie_breaker:', validate.tie_breaker, (('A', ''), candidates)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name in tie_breaker:', validate.tie_breaker, (('A', '#'), candidates)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Invalid candidate name in tie_breaker:', validate.tie_breaker, (('A', ':who'), candidates)) _test_aids.assertRaises_with_message(self, errors.RcvValueError, 'Candidate names in tie_breaker are not unique.', validate.tie_breaker, (('A', 'C', 'A'), candidates))