class TestAPI(unittest.TestCase): # Initializes local data set fDat = _football_API() fDat.load_data('gameData.csv') # Ensures that the data was loaded correctly def test_load_data(self): self.assertEqual(self.fDat.final_scores[1], '20-19') self.assertEqual(self.fDat.teams[246], 'New York Giants vs. Washington Redskins') self.assertEqual(self.fDat.total_scores[69], '51') self.assertEqual(self.fDat.dates[655], 'January 3 2010') # Ensures that games can be converted to dictionaries def test_game_toDict(self): testDict1 = self.fDat.game_toDict(300) testDict2 = self.fDat.game_toDict(301) self.assertEqual(self.fDat.final_scores[300], testDict1['finalScore']) self.assertEqual(self.fDat.point_diffs[301], testDict2['pointDiff']) # Tests the change_score() function def test_change_score(self): score = ['2-1', '2', '1'] self.assertEqual(self.fDat.final_scores[4], '31-17') self.fDat.change_score(4, score) self.assertEqual(self.fDat.final_scores[4], '2-1') # Tests the get_games() function def test_get_games(self): self.assertEqual(self.fDat.get_games(), len(self.fDat.final_scores)) # Tests the set_game() function def test_set_game(self): game = ['9-6', '9', '6', '15', '3', 'Team A vs. Team B', 'June 9 2021'] self.assertEqual(self.fDat.final_scores[2], '26-10') self.fDat.set_game(2, game) self.assertEqual(self.fDat.final_scores[2], game[0]) self.assertEqual(self.fDat.teams[2], game[5]) self.assertEqual(self.fDat.dates[2], game[6]) # Tests the delete_game() function def test_delete_game(self): self.fDat.delete_game(3) found = False if '3' in self.fDat.final_scores: found = True self.assertFalse(found)
def PUT_KEY(self, fid): output = {'result':'success'} fid = int(fid) try: fDatTmp = _football_API() fDatTmp.load_data('gameData.csv') self.fDat.final_scores[fid] = fDatTmp.final_scores[fid] self.fDat.winning_scores[fid] = fDatTmp.winning_scores[fid] self.fDat.losing_scores[fid] = fDatTmp.losing_scores[fid] self.fDat.point_diffs[fid] = fDatTmp.point_diffs[fid] self.fDat.teams[fid] = fDatTmp.teams[fid] self.fDat.dates[fid] = fDatTmp.dates[fid] except Exception as ex: output['result'] = 'error' output['message'] = str(ex) return json.dumps(output)
def __init__(self, fDat=None): if fDat is None: self.fDat = _football_API() else: self.fDat = fDat self.fDat.load_data('gameData.csv')
def start_service(): # Initialization dispatcher = cherrypy.dispatch.RoutesDispatcher() fDat = _football_API() # Establish Controllers footballController = FootballController(fDat=fDat) resetController = ResetController(fDat=fDat) optionsController = OptionsController() # Dispatcher Connections dispatcher.connect('score_get', '/scores/:fid', controller=footballController, action='GET_KEY', conditions=dict(method=['GET'])) dispatcher.connect('data_get', '/scores/', controller=footballController, action='GET_INDEX', conditions=dict(method=['GET'])) dispatcher.connect('delete_game', '/scores/:fid', controller=footballController, action='DELETE_KEY', conditions=dict(method=['DELETE'])) dispatcher.connect('delete_data', '/scores/', controller=footballController, action='DELETE_INDEX', conditions=dict(method=['DELETE'])) dispatcher.connect('reset_game', '/reset/:fid', controller=resetController, action='PUT_KEY', conditions=dict(method=['PUT'])) dispatcher.connect('reset_data', '/reset/', controller=resetController, action='PUT_INDEX', conditions=dict(method=['PUT'])) dispatcher.connect('put_game', '/scores/:fid', controller=footballController, action='PUT_KEY', conditions=dict(method=['PUT'])) dispatcher.connect('post_index', '/scores/', controller=footballController, action='POST_INDEX', conditions=dict(method=['POST'])) dispatcher.connect('score_key_options', '/scores/:fid', controller=optionsController, action='OPTIONS', conditions=dict(method=['OPTIONS'])) dispatcher.connect('score_options', '/scores/', controller=optionsController, action='OPTIONS', conditions=dict(method=['OPTIONS'])) dispatcher.connect('reset_key_options', '/reset/:fid', controller=optionsController, action='OPTIONS', conditions=dict(method=['OPTIONS'])) dispatcher.connect('reset_options', '/reset/', controller=optionsController, action='OPTIONS', conditions=dict(method=['OPTIONS'])) # Server Configuration conf = { 'global': { 'server.thread_pool': 5, 'server.socket_host': 'student04.cse.nd.edu', 'server.socket_port': 51087 }, '/': { 'request.dispatch': dispatcher, 'tools.CORS.on': True } } # Launch Server cherrypy.config.update(conf) app = cherrypy.tree.mount(None, config=conf) cherrypy.quickstart(app)
def __init__(self, fDat=None): if fDat is None: self.fDat = _football_API() else: self.fDat = fDat