def lookupPlayerID(self, event): self.log.message('Looking up PlayerID for event:\n' + str(event)) # Swap team and opponent IDs for own goals event = self.swapTeamIDs(event) p = Player() p.connectDB() PlayerID = p.lookupIDbyGoal(event, self.log) # Swap team and opponent IDs back for own goals event = self.swapTeamIDs(event) if (len(PlayerID) != 1): # First step is to ask the user to disambiguate newID = self.disambiguatePlayers(event, PlayerID) PlayerID = [] PlayerID.append(newID) if (PlayerID[0] == 0): # If PlayerID is still zero, mark it as missing self.processMissingRecord(event['playername'], len(PlayerID)) return False event['PlayerID'] = PlayerID[0] return event
def test_player_lookupIDbyGoal(): # Setup log = Log('test.log') p = Player() p.connectDB() # Format error with pytest.raises(RuntimeError) as excinfo: needle = 'Wil' p.lookupIDbyGoal(needle, log) assert 'lookupID requires a dictionary' in str(excinfo.value) # Missing fields error with pytest.raises(RuntimeError) as excinfo: needle = { 'FirstName': 'Wil', 'LastName': 'Trapp' } p.lookupIDbyGoal(needle, log) assert 'Submitted data is missing the following fields' in str(excinfo.value) # TODO: Need to actually look something up... needle = { 'playername': 'Man', 'TeamID': 2, 'GameID': 1, } assert p.lookupIDbyGoal(needle, log) == [3]
def test_player_lookupID(): # Setup log = Log('test.log') p = Player() p.connectDB() # Format error with pytest.raises(RuntimeError) as excinfo: needle = 'Wil' p.lookupID(needle, log) assert 'lookupID requires a dictionary' in str(excinfo.value) # Missing fields error with pytest.raises(RuntimeError) as excinfo: needle = { 'FirstName': 'Wil', 'LastName': 'Trapp' } p.lookupID(needle, log) assert 'Submitted data is missing the following fields' in str(excinfo.value) # Need a test of successful lookups needle = { 'FirstName': 'Sample', 'LastName': 'Player', 'Position': 'Midfielder', 'DOB': (1980, 1, 1, 0, 0, 0, 0, 0, 0,), 'Hometown': 'Oneonta, NY' } assert p.lookupID(needle, log) == [15]
def test_player_lookupIDbyName(): # Setup log = Log('test.log') p = Player() p.connectDB() # Format error with pytest.raises(RuntimeError) as excinfo: needle = 'Wil' p.lookupIDbyName(needle, log) assert 'lookupID requires a dictionary' in str(excinfo.value) # Missing fields error with pytest.raises(RuntimeError) as excinfo: needle = { 'FirstName': 'Wil', 'LastName': 'Trapp' } p.lookupIDbyName(needle, log) assert 'Submitted data is missing the following fields' in str(excinfo.value) # Look up a player we know doesn't exist. needle = { 'PlayerName': 'asdf', } assert p.lookupIDbyName(needle, log) == [] # Look up a known player in the test dataset. needle = { 'PlayerName': 'Sample Player', } assert p.lookupIDbyName(needle, log) == [15]
def importRecord(self, record): record['PlayerName'] = ( record['FirstName'] + " " + record['LastName'] ).strip() self.log.message('Importing player ' + str(record)) p = Player() p.connectDB() # Does the record exist? found = p.lookupIDbyName(record, self.log) if (len(found) == 0): # Nothing found, so we import p.saveDict(record, self.log) self.imported += 1 elif (len(found) == 1): # Found one record, so we update record['PlayerID'] = found[0] p.saveDict(record, self.log) self.updated += 1 else: # Something(s) found, so we skip self.processMissingRecords(found, len(found)) self.log.message('') return True
def test_player_load(): p = Player() p.connectDB() # Format error with pytest.raises(RuntimeError) as excinfo: needle = 'Foo' p.loadByID(needle) assert 'loadByID requires an integer' in str(excinfo.value) # Actual lookup needle = 1 p.loadByID(needle) assert isinstance(p.data, dict) assert p.data['FirstName'] == 'Harvey' assert p.data['LastName'] == 'the Rabbit' assert isinstance(p.data['DOB'], datetime.date)
def parsePlayer(self, starter, gameID, teamID, duration): result = [] # Set default timeoff to game duration timeoff = duration # Split the player string into a list result = self.parsePlayerSplit(starter) augmented = [] # parse each member of the list for string in result: # Split time from player name timeon = self.parsePlayerTimeOn(string) player = self.parsePlayerRemoveTime(string).strip() # Look up playerID playerID = [0] if (player != 'sent off' and player != 'ejected'): p = Player() p.connectDB() needle = { 'PlayerName': player, } playerID = p.lookupIDbyName(needle, self.log) if (len(playerID) == 1): playerID = playerID[0] augmented.append({ 'PlayerID': playerID, 'PlayerName': player, 'TimeOn': timeon, 'TimeOff': timeoff, 'Ejected': False, 'GameID': gameID, 'TeamID': teamID }) else: self.processMissingRecord(player, len(playerID)) # Transfer timeon values to previous player's timeoff result = self.adjustTimeOff(augmented, timeoff) return result
def test_player_disconnect(): p = Player() p.connectDB() assert hasattr(p, 'db') p.disconnectDB() assert hasattr(p, 'db') is False