def test_cant_start_match_if_teams_have_different_number_of_players(self): referee = factories.RefereeFactory() match = factories.MatchFactory( first_team__players__count=3, second_team__players__count=5) with self.shouldRaiseException(MatchErrors.CANT_START_NOT_EVEN_TEAMS): logic.can_start_match(referee, match)
def test_draw_divides_reward_between_players(self): referee = factories.RefereeFactory() reward = 1000 match = factories.MatchFactory( first_team__goals=3, second_team__goals=3, status=models.Match.STARTED, reward=reward) logic.finish_match(referee, match) self.assertEqual(match.status, match.FINISHED) self.assertEqual(referee.cash, 0.5 * reward) for player in match.first_team.players: self.assertEqual(player.cash, reward / 2) for player in match.second_team.players: self.assertEqual(player.cash, reward / 2)
def test_finish_match_gives_reward_to_winning_team(self): referee = factories.RefereeFactory() reward = 1000 match = factories.MatchFactory( first_team__goals=1, second_team__goals=0, status=models.Match.STARTED, reward=reward) logic.finish_match(referee, match) self.assertEqual(match.status, match.FINISHED) # referee takes 50% of winning players reward as salary self.assertEqual(referee.cash, 0.5 * reward) for player in match.first_team.players: self.assertEqual(player.cash, reward) for player in match.second_team.players: self.assertEqual(player.cash, 0)
def main(): """ This example shows how easily business logic can be used. To understand this sample properly, check `logic.py`, `errors.py` and `tests.py` """ # lets create out models, two teams 2 players each, and match with 500$ as reward first_team = factories.TeamFactory(players__count=2) second_team = factories.TeamFactory(players__count=2) match = factories.MatchFactory(first_team=first_team, second_team=second_team, status=models.Match.BEFORE_START, reward=500) print(u"Welcome to the football match between '{}' and '{}'!".format( first_team.name, second_team.name)) print(u'\n--------\n') # normal person, not player, who is really enthusiastic about match observer = factories.PersonFactory() referee = factories.RefereeFactory() start_match(match, observer, referee) print(u'\n--------\n') # we add observer to shooters, he want to shoot goals, but our logic won't allow him to do so potential_shooters = first_team.players + second_team.players + [observer] simulate_match(match, potential_shooters) print(u'\n--------\n') # only referee can finish match, it's validated by default # logic.finish_match(observer, match) will raise exception logic.finish_match(referee, match) print(u'\nMatch finished!') if match.winner: print(u'Winner: "{}"!'.format(match.winner.name)) else: print(u'Draw, no winner!') # our logic disallows finishing match second time, but let's check that print(u"\nCan't finish match, reason: {}".format( logic.can_finish_match(referee, match, raise_exception=False)))
def test_cant_finish_match_if_already_finished(self): referee = factories.RefereeFactory() match = factories.MatchFactory(status=models.Match.FINISHED) with self.shouldRaiseException(MatchErrors.CANT_FINISH_NOT_STARTED): logic.can_finish_match(referee, match)
def test_start_match(self): referee = factories.RefereeFactory() match = factories.MatchFactory(status=models.Match.BEFORE_START) logic.start_match(referee, match) self.assertEqual(match.status, match.STARTED)
def test_cant_start_match_if_already_started(self): referee = factories.RefereeFactory() match = factories.MatchFactory(status=models.Match.STARTED) with self.shouldRaiseException(MatchErrors.CANT_START_ALREADY_STARTED): logic.can_start_match(referee, match)
def test_referee_factory(self): referee = factories.RefereeFactory() self.assertIsNotNone(referee.name) self.assertIsInstance(referee, models.Referee)
def test_referee_cant_shoot_goal(self): referee = factories.RefereeFactory() match = factories.MatchFactory(status=models.Match.STARTED) with self.shouldRaiseException(MatchErrors.CANT_SHOOT_GOAL_NOT_PLAYER): logic.can_shoot_goal(referee, match)