def setUpData(self): self.past_match = PastMatchFactory.create(home_team='Bordo', away_team='Chelsea') self.past_match2 = PastMatchFactory.create(home_team='Bordo', away_team='Ajax') self.past_match3 = PastMatchFactory.create(home_team='Chelsea', away_team='Ajax', home_score=None, away_score=None) self.future_match = FutureMatchFactory.create(home_team='Ajax', away_team='Barcelona', datetime="2047-01-09 05:04+00:00") self.user1 = UserFactory.create(username='******') self.user2 = UserFactory.create(username='******') self.bets = [ [ BetFactory.create(match=self.future_match, user=self.user1, home_score=2, away_score=1), BetFactory.create(match=self.future_match, user=self.user2, home_score=4, away_score=0), ], # future match [ None, BetFactory.create(match=self.past_match, user=self.user2, home_score=2, away_score=0), ], # past match [ BetFactory.create(match=self.past_match2, user=self.user1, home_score=1, away_score=3), BetFactory.create(match=self.past_match2, user=self.user2, home_score=0, away_score=0), ], [ None, BetFactory.create(match=self.past_match3, user=self.user2, home_score=0, away_score=0), ] ] self.past_match.set_score(home_score=2, away_score=0) self.past_match2.set_score(home_score=0, away_score=0)
def setUp(self): super().setUp() self.future_match = FutureMatchFactory.create(home_team='Ajax', away_team='Barcelona') self.future_match2 = FutureMatchFactory.create(home_team='Barcelona', away_team='Ajax') self.past_match = PastMatchFactory.create() self.past_match2 = PastMatchFactory.create() # Ugo is a logged-in user user = self.create_pre_authenticated_session('ugo') self.bets = [ BetFactory.create(match=self.future_match, user=user, home_score=5, away_score=1), BetFactory.create(match=self.past_match, user=user, home_score=1, away_score=1), BetFactory.create(match=self.past_match2, user=user, home_score=2, away_score=1), ]
def test_match_update_bets_calls_set_result_for_every_bet( self, mock_set_result): user1 = UserFactory.create() user2 = UserFactory.create() match = PastMatchFactory(home_score=1, away_score=2) bet1 = BetFactory(match=match, user=user1) BetFactory(match=match, user=user2) match.update_bets() self.assertEqual(mock_set_result.call_count, 2)
def test_set_score_calls_save_model_for_past_match(self, mock_save): match = PastMatchFactory.create() mock_save.reset_mock() match.set_score(home_score=1, away_score=2) self.assertTrue(mock_save.called)
def test_past_match_set_score_calls_calc_result_func_for_all_match_bets( self, mock_calc_bet_result): match = PastMatchFactory.create(home_score=None, away_score=None) user2 = UserFactory.create() BetFactory.create(home_score=4, away_score=3, match=match, user=self.user) BetFactory.create(home_score=2, away_score=1, match=match, user=user2) mock_calc_bet_result.return_value = 12 self.assertFalse(mock_calc_bet_result.called) match.set_score(home_score=2, away_score=1) self.assertEqual(mock_calc_bet_result.call_count, 2) mock_calc_bet_result.assert_has_calls([ call( home_bet=4, away_bet=3, home_score=2, away_score=1, shootout_winner=None, shootout_bet=None, ), call( home_bet=2, away_bet=1, home_score=2, away_score=1, shootout_winner=None, shootout_bet=None, ) ])
def test_past_match_set_score_set_all_match_bets_results(self): match = PastMatchFactory.create(home_score=None, away_score=None) user2 = UserFactory.create() BetFactory.create(home_score=4, away_score=3, match=match, user=self.user) BetFactory.create(home_score=2, away_score=1, match=match, user=user2) match.set_score(home_score=2, away_score=1) self.assertEqual(Bet.objects.all()[0].result, 6) self.assertEqual(Bet.objects.all()[1].result, 12)
def test_for_past_matches_is_not_valid(self): past_match = PastMatchFactory.create() self.client.post(self.url, data={f'match_{past_match.id}': '2 - 0'}) bet = Bet.objects.filter(user=self.user, match=past_match).first() self.assertEqual(bet, None)
def test_match_saves_with_scores_calls_update_bets(self, mock_update_bets): match = PastMatchFactory(home_score=None, away_score=None) match.home_score = 1 match.home_score = 1 match.save() self.assertTrue(mock_update_bets.called)
def test_match_has_result_property_for_no_result(self): match = PastMatchFactory(home_score=None, away_score=None) self.assertEqual(match.result, '')
def test_match_has_result_property(self): match = PastMatchFactory(home_score=0, away_score=1) self.assertEqual(match.result, '0 - 1')
def test_match_without_result_has_empty_string_representation(self): match = PastMatchFactory(home_team='A', away_team='B', home_score=None, away_score=None) self.assertEqual(str(match), 'A - B')
def test_has_string_representation(self): match = PastMatchFactory(home_team='A', away_team='B', home_score=0, away_score=1) self.assertEqual(str(match), 'A 0 - 1 B')
def test_has_result_false_for_past_match_without_result(self): match = PastMatchFactory.create(home_score=None, away_score=None) self.assertFalse(match.has_result)
def test_has_result_true_for_past_match_with_result(self): match = PastMatchFactory.create() self.assertTrue(match.has_result)
def test_has_is_in_future_attr(self): past_match = PastMatchFactory.create() future_match = FutureMatchFactory.create() self.assertTrue(future_match.in_future) self.assertFalse(past_match.in_future)
def setUpTestData(cls): cls.past_match = PastMatchFactory.create() cls.future_match = FutureMatchFactory.create() cls.user = UserFactory.create()