def setUp(self):
        super(ScoreCategoryTests, self).setUp()

        self.injector.inject(self.tourn_1)
        self.db.session.flush()
        self.db.session.commit()

        # Some default categories
        self.cat_1 = cat('painting', 10, False, 1, 20)
        self.cat_2 = cat('cat_battle', 80, True, 1, 20)
        self.cat_3 = cat('cat_sports', 10, True, 1, 5)

        self.tournament = Tournament(self.tourn_1)
    def test_broken_categories(self):
        # cat should perform input validation only
        fifty_one = cat('painting', 51, False, 1, 20)
        neg_pct = cat('painting', -1, False, 1, 20)
        zero_pct = cat('painting', 0, False, 1, 20)
        lge_pct = cat('painting', 101, False, 1, 20)
        char_pct = cat('painting', 'a', False, 1, 20)
        no_name = cat('', 10, False, 1, 20)
        none_name = cat(None, 10, False, 1, 20)

        func = self.tournament.update
        self.assertRaises(ValueError, func, {'score_categories': [neg_pct]})
        self.assertRaises(ValueError, func, {'score_categories': [zero_pct]})
        self.assertRaises(ValueError, func, {'score_categories': [lge_pct]})
        self.assertRaises(ValueError, func, {'score_categories': [char_pct]})
        self.assertRaises(ValueError, func, {'score_categories': [no_name]})
        self.assertRaises(ValueError, func, {'score_categories': [none_name]})
        self.assertRaises(ValueError, func,
                          {'score_categories': [fifty_one, fifty_one]})
    def setUp(self):
        super(EnterScore, self).setUp()
        self.injector.inject(self.tourn_1, num_players=5)
        tourn = Tournament(self.tourn_1)
        tourn.update({
            'rounds': 2,
            'missions': ['foo_mission_1', 'foo_mission_2']
        })
        self.injector.add_player(self.tourn_1, self.player)
        score_args = cat('per_tournament', 50, True, 0, 100)

        # per tournament category
        self.cat_1 = ScoreCategory(tournament_id=self.tourn_1, **score_args)
        self.db.session.add(self.cat_1)

        # per round category
        score_args['name'] = 'per_round'
        score_args['per_tournament'] = False
        self.category_2 = ScoreCategory(tournament_id=self.tourn_1,
                                        **score_args)
        self.db.session.add(self.category_2)
        self.db.session.commit()
    def test_score_entered(self):
        tourn = Tournament(self.tourn_1)

        score_args = cat('per_round', 50, False, 0, 100)
        cat_1 = ScoreCategory(tournament_id=self.tourn_1, **score_args)
        self.db.session.add(cat_1)
        self.db.session.flush()

        entry_2_id = TournamentEntry.query.filter_by(
            player_id='{}_player_{}'.format(self.tourn_1, 2),
            tournament_id=self.tourn_1).first().id
        entry_3_id = TournamentEntry.query.filter_by(
            player_id='{}_player_{}'.format(self.tourn_1, 3),
            tournament_id=self.tourn_1).first().id
        entry_4_id = TournamentEntry.query.filter_by(
            player_id='{}_player_{}'.format(self.tourn_1, 4),
            tournament_id=self.tourn_1).first().id
        entry_5_id = TournamentEntry.query.filter_by(
            player_id='{}_player_{}'.format(self.tourn_1, 5),
            tournament_id=self.tourn_1).first().id

        # A completed game
        game = self.get_game_by_round(entry_4_id, 1)
        Score(category=cat_1.name, game_id=game.id, tournament=tourn,
              entry_id=entry_2_id, score=2).write()
        Score(category=cat_1.name, game_id=game.id, tournament=tourn,
              entry_id=entry_4_id, score=4).write()
        entrants = [x.entrant_id for x in game.entrants.all()]
        self.assertTrue(entry_2_id in entrants)
        self.assertTrue(entry_4_id in entrants)
        self.assertTrue(Score.is_score_entered(game))

        # A BYE will only have one entrant
        game = self.get_game_by_round(entry_3_id, 1)
        Score(category=cat_1.name, game_id=game.id, tournament=tourn,
              entry_id=entry_3_id, score=3).write()
        entrants = [x.entrant_id for x in game.entrants.all()]
        compare(len(entrants), 1)
        self.assertTrue(entry_3_id in entrants)
        self.assertTrue(Score.is_score_entered(game))

        # Ensure the rd2 game entry_4 vs. entry_5 is listed as not scored. This
        # will force a full check. entry_5's score hasn't been entered.
        game = self.get_game_by_round(entry_4_id, 2)
        game.score_entered = False
        self.db.session.add(game)
        self.db.session.flush()

        game = self.get_game_by_round(entry_4_id, 2)
        entrants = [x.entrant_id for x in game.entrants.all()]
        Score(category=cat_1.name, game_id=game.id, tournament=tourn,
              entry_id=entry_4_id, score=4).write()
        self.assertTrue(entry_4_id in entrants)
        self.assertTrue(entry_5_id in entrants)
        self.assertFalse(Score.is_score_entered(game))

        # Enter the final score for entry_5
        tourn = Tournament(self.tourn_1)
        Score(category=cat_1.name, game_id=game.id, tournament=tourn,
              entry_id=entry_5_id, score=5).write()
        self.assertTrue(Score.is_score_entered(game))
    def test_broken_min_max(self):
        neg_min = cat('painting', 10, False, -1, 20)
        neg_max = cat('painting', 10, False, 1, -1)
        zero_max = cat('painting', 10, False, 0, 0)
        min_high = cat('painting', 10, False, 10, 9)
        zero_min = cat('painting', 10, False, 0, 20)
        equal = cat('painting', 10, False, 1, 1)
        no_min = cat('painting', '10', False, '', 20)
        no_max = cat('painting', '10', False, 1, '')
        none_min = cat('painting', '1', False, None, 1)
        none_max = cat('painting', '1', False, 1, None)
        char_min = cat('painting', '1', False, 'a', 1)
        char_max = cat('painting', '1', False, 1, 'a')

        func = self.tournament.update
        self.assertRaises(ValueError, func, {'score_categories': [neg_min]})
        self.assertRaises(ValueError, func, {'score_categories': [neg_max]})
        self.assertRaises(ValueError, func, {'score_categories': [zero_max]})
        self.assertRaises(ValueError, func, {'score_categories': [min_high]})
        self.assertRaises(ValueError, func, {'score_categories': [no_min]})
        self.assertRaises(ValueError, func, {'score_categories': [no_max]})
        self.assertRaises(ValueError, func, {'score_categories': [none_min]})
        self.assertRaises(ValueError, func, {'score_categories': [none_max]})
        self.assertRaises(ValueError, func, {'score_categories': [char_min]})
        self.assertRaises(ValueError, func, {'score_categories': [char_max]})