forked from DimaKudosh/pydfs-lineup-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_optimizer.py
93 lines (80 loc) · 4.3 KB
/
test_optimizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import absolute_import, division
import unittest
from pydfs_lineup_optimizer import get_optimizer, Player
from pydfs_lineup_optimizer.constants import Site, Sport
from pydfs_lineup_optimizer.exceptions import LineupOptimizerException
from pydfs_lineup_optimizer.sites.yahoo.settings import YahooFootballSettings
from .utils import create_players, load_players
class OptimizerMethodsTestCase(unittest.TestCase):
def setUp(self):
self.players = load_players()
self.lineup_optimizer = get_optimizer(Site.DRAFTKINGS, Sport.BASKETBALL)
self.lineup_optimizer.load_players(self.players)
def test_add_player_to_lineup(self):
self.lineup_optimizer.add_player_to_lineup(self.players[0])
self.assertTrue(self.players[0] in self.lineup_optimizer.locked_players)
def test_same_players_in_lineup(self):
self.lineup_optimizer.add_player_to_lineup(self.players[0])
with self.assertRaises(LineupOptimizerException):
self.lineup_optimizer.add_player_to_lineup(self.players[0])
def test_add_player_with_many_positions(self):
players = create_players(['PG/SG', 'PG', 'PG', 'PG', 'PG/SG/SF', 'SF', 'SF'])
self.lineup_optimizer.extend_players(players)
for player in players[:4]:
self.lineup_optimizer.add_player_to_lineup(player)
lineup = next(self.lineup_optimizer.optimize(1))
self.assertTrue(all([p in lineup.players for p in players[:4]]))
self.lineup_optimizer.add_player_to_lineup(players[4])
lineup = next(self.lineup_optimizer.optimize(1))
self.assertTrue(all([p in lineup.players for p in players[:5]]))
num_of_selected_by_optimizer = len(list(filter(
lambda p: 'C' in p.positions or 'PF' in p.positions, lineup.players
)))
self.assertEqual(num_of_selected_by_optimizer, 2)
def test_exact_number_of_players_for_position(self):
optimizer = get_optimizer(Site.DRAFTKINGS, Sport.BASEBALL)
positions = ['OF', 'OF', '2B', '3B', 'SP', 'OF/SS', 'SP', '1B/OF', 'C', 'RP']
optimizer.load_players(create_players(positions))
with self.assertRaises(LineupOptimizerException):
next(optimizer.optimize(1))
positions.append('1B')
optimizer.load_players(create_players(positions))
next(optimizer.optimize(1))
def test_adding_player_with_salary_bigger_than_budget(self):
player = Player('1', '1', '1', ['PG'], 'DEN', 100000, 2)
with self.assertRaises(LineupOptimizerException):
self.lineup_optimizer.add_player_to_lineup(player)
def test_adding_player_to_formed_position(self):
players = create_players(['PG'] * 4)
for i in range(3):
self.lineup_optimizer.add_player_to_lineup(players[i])
with self.assertRaises(LineupOptimizerException):
self.lineup_optimizer.add_player_to_lineup(players[3])
def test_remove_player_from_lineup(self):
optimizer = self.lineup_optimizer
player = Player('1', 'P', 'P', ['PG'], 'DEN', 10, 2)
optimizer.extend_players([player])
optimizer.add_player_to_lineup(player)
optimizer.remove_player_from_lineup(player)
self.assertNotIn(player, optimizer.locked_players)
with self.assertRaises(LineupOptimizerException):
optimizer.remove_player_from_lineup(player)
def test_lineup_with_max_players(self):
players = create_players(['PG', 'SG', 'SF', 'PF', 'C', 'PG', 'SF', 'C'])
self.lineup_optimizer.extend_players(players)
for player in players:
self.lineup_optimizer.add_player_to_lineup(player)
gen = self.lineup_optimizer.optimize(10)
self.assertEqual(len(list(gen)), 1)
def test_get_optimizer(self):
optimizer = get_optimizer(Site.YAHOO, Sport.FOOTBALL)
self.assertIsInstance(optimizer._settings, YahooFootballSettings)
with self.assertRaises(NotImplementedError):
get_optimizer(Site.DRAFTKINGS, 'Some sport')
def test_get_player_by_id(self):
player = self.lineup_optimizer.get_player_by_id('0000001')
self.assertIsNotNone(player)
self.assertEqual(player.last_name, 'Westbrook')
def test_get_player_by_incorrect_id(self):
player = self.lineup_optimizer.get_player_by_id('incorrect_id')
self.assertIsNone(player)