def test_log_write(): l = Log('test.log') msg = 'Hello' l.message(msg) # Re-open file in read mode l.file = open('test.log', 'r') # Test for message with a newline appended assert l.file.readline() == msg + '\n'
def test_game_modelV2(): log = Log('test.log') model = 'v2' homedata = {} awaydata = {} g = Game() homedata['Points'] = 1 homedata['GP'] = 1 awaydata['Points'] = 1 awaydata['GP'] = 1 threshold = g.calculateThreshold(model, homedata, awaydata) assert threshold['home'] == 58.0 / 117.0 assert threshold['draw'] == 84.0 / 117.0 homedata['Points'] = 3 homedata['GP'] = 1 awaydata['Points'] = 1 awaydata['GP'] = 1 threshold = g.calculateThreshold(model, homedata, awaydata) assert threshold['home'] == 481.0 / 881.0 assert threshold['draw'] == 710.0 / 881.0 homedata['Points'] = 1 homedata['GP'] = 1 awaydata['Points'] = 3 awaydata['GP'] = 1 threshold = g.calculateThreshold(model, homedata, awaydata) assert threshold['home'] == 433.0 / 957.0 assert threshold['draw'] == 711.0 / 957.0
def test_league_updateStandings(): log = Log('test.log') l = League() l.connectDB() l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log) l.initSeason() # Initial assert l.standings['SJ']['GP'] == 0 assert l.standings['SJ']['Points'] == 0 assert l.standings['DC']['Points'] == 0 # Home win l.updateStandings('SJ', 'DC', 'home') assert l.standings['SJ']['GP'] == 1 assert l.standings['DC']['GP'] == 1 assert l.standings['SJ']['Points'] == 3 assert l.standings['DC']['Points'] == 0 # Away win l.initSeason() l.updateStandings('SJ', 'DC', 'away') assert l.standings['SJ']['GP'] == 1 assert l.standings['DC']['GP'] == 1 assert l.standings['SJ']['Points'] == 0 assert l.standings['DC']['Points'] == 3 # Draw l.initSeason() l.updateStandings('SJ', 'DC', 'draw') assert l.standings['SJ']['GP'] == 1 assert l.standings['DC']['GP'] == 1 assert l.standings['SJ']['Points'] == 1 assert l.standings['DC']['Points'] == 1
def test_league_outputLine(): log = Log('test.log') l = League() l.connectDB() l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log) line = l.outputLine('Abbv', l.teams) assert line == 'CLB,COL,DAL,DC,KC,LA,NE,NY,SJ,TB,'
def test_game_modelV0(): log = Log('test.log') model = 'v0' homedata = {} awaydata = {} g = Game() threshold = g.modelV0(homedata, awaydata) assert threshold['home'] == 1.0 assert threshold['draw'] == 1.0 assert threshold['away'] == 1.0
def test_game_lookupGamesBySeason(): log = Log('test.log') g = Game() g.connectDB() g.lookupGamesBySeason(1900, 'mls', '1900-01-01', log) assert len(g.games) == 0 g.lookupGamesBySeason(1996, 'foo', '1996-01-01', log) assert len(g.games) == 0 g.lookupGamesBySeason(1996, 'mls', '1996-01-01', log) assert len(g.games) == 160
def test_league_lookupTeamsBySeason(): log = Log('test.log') l = League() l.connectDB() l.lookupTeamsBySeason(1900, 'mls', '1900-01-01', log) assert len(l.teams) == 0 l.lookupTeamsBySeason(1996, 'foo', '1996-01-01', log) assert len(l.teams) == 0 l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log) assert len(l.teams) == 10
def test_league_printStandings(): log = Log('test.log') l = League() l.connectDB() l.lookupTeamsBySeason(1900, 'foo', '1900-01-01', log) output = l.printStandings() assert output == 'Team Pts GP\n' l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log) output = l.printStandings() assert len(output) > 0
def test_game_modelV1(): log = Log('test.log') model = 'v1' homedata = {} awaydata = {} g = Game() threshold = g.modelV1(homedata, awaydata) assert threshold['home'] == 972.0 assert threshold['draw'] == 533.0 assert threshold['away'] == 450.0
def test_league_initSeason(): log = Log('test.log') l = League() l.connectDB() l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log) assert l.teams['CLB']['GP'] == 0 assert l.teams['CLB']['W'] == 0 l.lookupTeamsBySeason(1996, 'mls', '1996-06-01', log) assert l.teams['CLB']['GP'] == 9 assert l.teams['CLB']['W'] == 3
def test_league_simulateSeason(): log = Log('test.log') model = 'v0' g = Game() g.connectDB() g.lookupGamesBySeason(1996, 'mls', '1996-01-01', log) l = League() l.connectDB() l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log) l.simulateSeason(g, model, log) assert l.standings['CLB']['GP'] == 32
def test_game_calculateThreshold(): log = Log('test.log') model = 'v0' homedata = {} awaydata = {} g = Game() # This tests the ability to get back different models based on a passed # parameter. threshold = g.calculateThreshold(model, homedata, awaydata) assert threshold['home'] == 1.0 / 3.0 model = 'v1' threshold = g.calculateThreshold(model, homedata, awaydata) assert threshold['home'] == 972.0 / 1955.0 model = 'v2' homedata['Points'] = 1 homedata['GP'] = 1 awaydata['Points'] = 1 awaydata['GP'] = 1 threshold = g.calculateThreshold(model, homedata, awaydata) assert threshold['home'] == 58.0 / 117.0
def test_game_simulateResult(): log = Log('test.log') model = 'v0' g = Game() context = {} homedata = {} awaydata = {} # This is hacky, but should generally protect against unexpected results # being returned. I'm not so concerned here about testing randomness, or # specific distributions of results. That is numpy's problem. assert g.simulateResult(context, homedata, awaydata, model) in ['home', 'draw', 'away'] assert g.simulateResult(context, homedata, awaydata, model) in ['home', 'draw', 'away'] assert g.simulateResult(context, homedata, awaydata, model) in ['home', 'draw', 'away'] assert g.simulateResult(context, homedata, awaydata, model) in ['home', 'draw', 'away'] assert g.simulateResult(context, homedata, awaydata, model) in ['home', 'draw', 'away'] assert g.simulateResult(context, homedata, awaydata, model) in ['home', 'draw', 'away']
def test_log_init(): l = Log('test.log') assert isinstance(l, Log)
def test_log_end(): l = Log('test.log') l.end() assert l.file is None
def main(mode, competition, model, batch, season, start): settings = Settings(mode, competition, model, batch, season, start) # Reflect back the configuration being used click.echo(settings.output()) # Initialize tooling click.echo('Initializing tooling...') filename = date.today().strftime("%y%m%d") + '-' + mode + '-' + model log = Log('logs/' + filename + '.log') log.message('Started') output = Log('output/' + filename + '.csv') db = Database() db.connect() log.message('Database connected') # Initialize data click.echo('Initializing data...') league = League() league.connectDB() league.lookupTeamsBySeason(settings.values['season'], settings.values['competition'], settings.values['start'], log) game = Game() game.connectDB() game.lookupGamesBySeason(settings.values['season'], settings.values['competition'], settings.values['start'], log) # Output the starting point for simulation, including the standings. click.echo(league.printStandings()) log.message(league.printStandings()) # Write top lines of output/labels once output.message(league.outputLine('Conference', league.teams)) output.message(league.outputLine('Abbv', league.teams)) # Iterate over games for i in range(settings.values['batch']): log.message("Season " + str(i)) league.simulateSeason(game, settings.values['model'], log) output.message(league.outputLine('Points', league.standings)) # Teardown click.echo('Finishing...') db.disconnect() log.message('Database disconnected') log.end()
def test_log_filename(): l = Log('test.log') assert l.name == 'test.log'