Example #1
0
 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)
Example #2
0
 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)
Example #3
0
 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)
Example #4
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)))
Example #5
0
 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)
Example #6
0
 def test_not_referee_cant_finish_match(self):
     person = factories.PersonFactory()
     match = factories.MatchFactory(status=models.Match.STARTED)
     with self.shouldRaiseException(MatchErrors.CANT_FINISH_NOT_REFEREE):
         logic.can_finish_match(person, match)
Example #7
0
 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)
Example #8
0
 def test_not_referee_cant_start_match(self):
     person = factories.PersonFactory()
     match = factories.MatchFactory()
     with self.shouldRaiseException(MatchErrors.CANT_START_NOT_REFEREE):
         logic.can_start_match(person, match)
Example #9
0
 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)
Example #10
0
 def test_shoot_goal(self):
     match = factories.MatchFactory(status=models.Match.STARTED)
     player = match.first_team.players[0]
     logic.shoot_goal(player, match)
     self.assertEqual(player.total_goals, 1)
     self.assertEqual(match.first_team.goals, 1)
Example #11
0
 def test_cant_shoot_goal_if_not_in_any_team(self):
     player = factories.PlayerFactory()
     match = factories.MatchFactory(status=models.Match.STARTED)
     with self.shouldRaiseException(MatchErrors.CANT_SHOOT_GOAL_NOT_IN_TEAMS):
         logic.can_shoot_goal(player, match)
Example #12
0
 def test_cant_shoot_goal_if_match_not_in_progress(self):
     match = factories.MatchFactory(status=models.Match.BEFORE_START)
     with self.shouldRaiseException(MatchErrors.CANT_SHOOT_GOAL_MATCH_NOT_STARTED):
         logic.can_shoot_goal(match.first_team.players[0], match)
Example #13
0
 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)
Example #14
0
 def test_normal_person_cant_shoot_goal(self):
     person = factories.PersonFactory()
     match = factories.MatchFactory(status=models.Match.STARTED)
     with self.shouldRaiseException(MatchErrors.CANT_SHOOT_GOAL_NOT_PLAYER):
         logic.can_shoot_goal(person, match)