def test_game_lookupID(): # Setup log = Log('test.log') g = Game() g.connectDB() # This should raise a format error with pytest.raises(RuntimeError) as excinfo: needle = '1996-04-13' g.lookupID(needle, log) assert 'lookupID requires a dictionary' in str(excinfo.value) # This should raise a missing-fields error with pytest.raises(RuntimeError) as excinfo: needle = { 'MatchTime': '1996-04-13', 'HTeamID': 11 } g.lookupID(needle, log) assert 'Submitted data is missing the following fields' in str(excinfo.value) # This should bring back one record needle = { 'MatchTime': (1980, 1, 1, 0, 0, 0, 0, 0, 0), 'HTeamID': 11, 'ATeamID': 12 } assert len(g.lookupID(needle, log)) >= 1
def test_game_lookupDuration(): g = Game() g.connectDB() log = Log('test_lookupDuration.log') # Standard-duration game is 90 minutes needle = 1 assert g.lookupDuration(needle, log) == 90
def test_game_loadByID(): g = Game() g.connectDB() # This should raise a format error with pytest.raises(RuntimeError) as excinfo: needle = 'Foo' g.loadByID(needle) assert 'loadByID requires an integer' in str(excinfo.value) needle = 1 g.loadByID(needle) assert isinstance(g.data, dict) assert g.data['HTeamID'] == 1 assert g.data['ATeamID'] == 2
def importRecord(self, record): self.log.message('\nImporting lineup ' + str(record)) # If the game hasn't been played yet, move to the next # Doesn't get recorded as a "skip" to keep reporting clean today = datetime.now() gamedate = datetime( record['Date'][0], record['Date'][1], record['Date'][2] ) if (gamedate > today): self.log.message('Game has not been played yet') return True # Start working with game record g = Game() g.connectDB() # Lookup this gameID needle = { 'MatchTime': record['Date'], 'HTeamID': record['homeID'], 'ATeamID': record['awayID'], } game = g.lookupID(needle, self.log) self.log.message('Found games: ' + str(game)) if (len(game) != 1): self.log.message('Found wrong number of games: ' + str(len(game))) self.skipped += 1 # If we didn't find one gameID, then we abort processing this game return False # Need to convert gameID from a list of 1 number to an integer game = game[0] # Lookup game duration duration = g.lookupDuration(game, self.log) # Parse lineup string self.parseLineup(record['Lineup'], game, record['teamID'], duration) # At this point we have self.players - but need to store them [self.importPlayer(player) for player in self.players] return True
def importRecord(self, record): self.log.message('Importing game ' + str(record)) g = Game() g.connectDB() # Does the record exist? found = g.lookupID(record, self.log) if (len(found) == 0): # Nothing found, so we import g.saveDict(record, self.log) self.imported += 1 elif (len(found) == 1): record['MatchID'] = found[0] g.saveDict(record, self.log) self.imported += 1 else: # Something(s) found, so we skip self.processMissingRecord(found, len(found)) return True
def test_game_saveDict(): # Setup log = Log('test.log') g = Game() g.connectDB() # This should raise an error with pytest.raises(RuntimeError) as excinfo: testRecord = "fake player record" g.saveDict(testRecord, log) assert 'saveDict requires a dictionary' in str(excinfo.value) # This should work sample = { 'MatchTime': (1980, 1, 1, 19, 30, 0, 0, 0, 0), 'MatchTypeID': 21, 'HTeamID': 11, 'HScore': 0, 'ATeamID': 12, 'AScore': 0, 'VenueID': 1, } assert g.saveDict(sample, log) is True assert g.db.warnings() is None
def correctValues(self): # This takes in all rows of the imported spreadsheet, and performs # any needed repairs / adjustments # We can re-use the same Game object inside the loop g = Game() g.connectDB() self.log.message('Correcting values...') for record in self.records: # Log the record when we begin self.log.message('\nParsing game record:') # self.log.message(' ' + str(record) + '\n') # NewEvents holds the rebuilt event records - we create it this # early because the next step skips this record when no goals # are scored - and it still needs to be present. record['NewEvents'] = [] # Games with no goals get skipped if (record['Goals'] == ''): continue # 1. TeamID lookup teamID = self.lookupTeamID(record['Team']) # 2. OpponentID lookup opponentID = self.lookupTeamID(record['Opponent']) # 3. Home or Away if (record['H/A'] == 'H'): homeID = teamID awayID = opponentID elif (record['H/A'] == 'A'): homeID = opponentID awayID = teamID # 4. The game date needs to be converted record['Date'] = self.source.recoverDate(record['Date']) # Use home/away ID and game date to look up the game ID needle = { 'MatchTime': record['Date'], 'HTeamID': homeID, 'ATeamID': awayID, } self.log.message(' Looking up needle: ' + str(needle)) game = g.lookupID(needle, self.log) self.log.message(' Found games: ' + str(game) + '\n') if (len(game) != 1): self.log.message('Found wrong number of games: ' + str(len(game))) self.skipped += 1 # If we didn't find one gameID, then we abort processing this # game return False # Need to convert gameID from a list of 1 number to an integer game = game[0] # 5. The goalscorers string needs to be expanded record['Events'] = self.splitGoals(record['Goals']) # record['Events'] is now a list of strings. We now need to parse # each individual string into a dictionary. for item in record['Events']: item = self.parseOneGoal(item, game, teamID, opponentID) for subitem in item: record['NewEvents'].append(self.lookupPlayerID(subitem)) # Log the corrected record for later inspection self.log.message(' Outcome:\n ' + str(record)) return True
def test_game_disconnect(): g = Game() g.connectDB() assert hasattr(g, 'db') g.disconnectDB() assert hasattr(g, 'db') is False