Skip to content

Commit

Permalink
Merge pull request #33 from matt-bernhardt/25_importer
Browse files Browse the repository at this point in the history
Implements child Importer objects for multiple data types
  • Loading branch information
matt-bernhardt committed Dec 13, 2015
2 parents 39b6d95 + 4f9065c commit 0ac166d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 43 deletions.
69 changes: 47 additions & 22 deletions tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ def test_game_init():
# Default values


def test_game_connect():
g = Game()
assert hasattr(g, 'db') is False
g.connectDB()
assert hasattr(g, 'db')


def test_game_disconnect():
g = Game()
g.connectDB()
assert hasattr(g, 'db')
g.disconnectDB()
assert hasattr(g, 'db') is False


def test_game_loadByID():
g = Game()

Expand All @@ -23,12 +38,36 @@ def test_game_loadByID():
assert 'loadByID requires an integer' in str(excinfo.value)


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,
}
assert g.saveDict(sample, log) is True
assert g.db.warnings() is None


def test_game_lookupID():
# Setup
log = Log('test.log')
g = Game()
# This is commented out pending my figuring out mock
# g.connectDB()
g.connectDB()

# This should raise a format error
with pytest.raises(RuntimeError) as excinfo:
Expand All @@ -45,24 +84,10 @@ def test_game_lookupID():
g.lookupID(needle, log)
assert 'Submitted data is missing the following fields' in str(excinfo.value)

# This is commented out pending my figuring out mock
# This should bring back one record
# needle = {
# 'MatchTime': '1996-04-13',
# 'HTeamID': 11,
# 'ATeamID': 12
# }
# assert g.lookupID(needle, log) is True
# assert g.data['MatchID'] == 10992


def test_game_saveDict():
# Setup
log = Log('test.log')
g = Game()

# 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)
needle = {
'MatchTime': (1980, 1, 1, 0, 0, 0, 0, 0, 0),
'HTeamID': 11,
'ATeamID': 12
}
assert len(g.lookupID(needle, log)) >= 1
14 changes: 7 additions & 7 deletions tests/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ def test_player_init():

def test_player_connect():
p = Player()
# Assert DB connection not present
# p.connectDB()
# Assert DB connection exists
assert hasattr(p, 'db') is False
p.connectDB()
assert hasattr(p, 'db')


def test_player_disconnect():
p = Player()
# p.connectDB()
# Assert DB connection exists
# p.disconnectDB()
# Assert DB connection not present
p.connectDB()
assert hasattr(p, 'db')
p.disconnectDB()
assert hasattr(p, 'db') is False


def test_player_load():
Expand Down
6 changes: 3 additions & 3 deletions trapp/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

import argparse
from trapp.log import Log
from trapp.importer import Importer
from trapp.importer import Importer, ImporterPlayers, ImporterGames


def importGames(infile):
# Feedback, setup
print('Importing games from ' + str(infile))
log = Log('trapp-import-games.log')
importer = Importer(infile, log)
importer = ImporterGames(infile, log)

# Check for required fields
requiredColumns = ([
Expand All @@ -34,7 +34,7 @@ def importPlayers(infile):
# Feedback, setup
print('Importing players from ' + str(infile))
log = Log('trapp-import-players.log')
importer = Importer(infile, log)
importer = ImporterPlayers(infile, log)

# Check for required fields
requiredColumns = ([
Expand Down
2 changes: 1 addition & 1 deletion trapp/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def connectDB(self):

def disconnectDB(self):
self.db.disconnect()
self.db = None
del self.db

def loadByID(self, gameID):
# Verify that gameID is an integer
Expand Down
66 changes: 61 additions & 5 deletions trapp/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import
from trapp.spreadsheet import Spreadsheet
from trapp.game import Game
from trapp.player import Player


class Importer():
Expand Down Expand Up @@ -41,20 +42,25 @@ def checkData(self):

return True

def correctValues(self):
# This is overwritten in child objects depending on the corrective
# steps needed with the data.
return True

def doImport(self):
# need to prepare records
# Prepare records
self.records = self.source.buildRecords()

# need to correct dates
for record in self.records:
record['MatchTime'] = self.source.recoverDate(record['MatchTime'])
# Correct values
self.correctValues()

# need to iterate over records
# Iterate over records
[self.importRecord(record) for record in self.records]

return True

def importRecord(self, record):
# This is overwritten in child objects
print('Importing record ' + str(record))
g = Game()
g.connectDB()
Expand All @@ -72,3 +78,53 @@ def setLog(self, log):
self.log = log
self.log.message('Log transferred')
return True


class ImporterGames(Importer):

def correctValues(self):
for record in self.records:
record['MatchTime'] = self.source.recoverDate(record['MatchTime'])

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)
else:
# Something(s) found, so we skip
self.log.message('Found ' + str(found) + ' matching games')

return True


class ImporterPlayers(Importer):

def correctValues(self):
for record in self.records:
record['DOB'] = self.source.recoverDate(record['DOB'])

return True

def importRecord(self, record):
self.log.message('Importing player ' + str(record))
p = Player()
p.connectDB()

# Does the record exist?
found = p.lookupID(record, self.log)
if (len(found) == 0):
# Nothing found, so we import
p.saveDict(record, self.log)
else:
# Something(s) found, so we skip
self.log.message('Found ' + str(found) + ' matching players')

return True
16 changes: 11 additions & 5 deletions trapp/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def connectDB(self):

def disconnectDB(self):
self.db.disconnect()
self.db = None
del self.db

def loadByID(self, playerID):
if not (isinstance(playerID, int)):
Expand Down Expand Up @@ -100,10 +100,16 @@ def saveDict(self, newData, log):
else:
# Insert
log.message(' ...Inserting')
sql = ('INSERT INTO tbl_games '
'(%s) '
sql = ('INSERT INTO tbl_players '
'(FirstName, LastName, Position, DOB, Hometown) '
'VALUES '
'(%s)')
rs = self.db.query(sql, (newData.keys(), newData.values(), ))
'(%s, %s, %s, %s, %s)')
rs = self.db.query(sql, (
newData['FirstName'],
newData['LastName'],
newData['Position'],
self.db.convertDate(newData['DOB']),
newData['Hometown'],
))

return True

0 comments on commit 0ac166d

Please sign in to comment.