def test_fetch_by_id(self): expected_count = 1 generated_data = self._prepopulate_datastore(year=self.year, week=self.week) test_data = generated_data[0] spread = Spread() data = spread.fetch_by_id(year=self.year, week=self.week, game_id=test_data['game_id']) self.assertEqual(data, test_data)
def _delete(self): query = Spread.all() query.filter('week =', str(12)) result = query.fetch(1000) for item in result: key = item.key() matchup = Spread.get(key) matchup.delete()
def get(self, year, week, game): year = int(year) week = int(week) game = int(game) spread = Spread() result = spread.fetch_by_id(year=year, week=week, game_id=game) self._send_response(result)
def test_update_model(self): generated_data = self._generate_data(year=self.year, week=self.week, count=1) test_data = generated_data[0] model = SpreadModel(**test_data) test_data['game_line'] += randint(1, 100) spread = Spread() result = spread._update_model(model, test_data) self.assertEqual(result.to_dict(), test_data)
def test_find_existing_with_all_data(self): test_data = self._prepopulate_datastore(year=self.year, week=self.week, count=self.expected_count) spread = Spread() key = spread._generate_key(year=self.year, week=self.week) data = spread._find_existing_entries(key, test_data) self.assertEqual(len(data), self.expected_count) for game in test_data: self.assertTrue(game['game_id'] in data)
def put(self, year, week, game): year = int(year) week = int(week) game = int(game) spread_data = json.loads(self.request.POST['spread']) spread = Spread() result = spread.save_by_id(year=year, week=week, game_id=game, data=spread_data) self._send_response()
def test_fetch_spread(self): expected_count = 1 test_data = self._prepopulate_datastore(year=self.year, week=self.week, count=expected_count) spread = Spread() data = spread.fetch(year=self.year, week=self.week) self.assertEqual(len(data), expected_count) for index, game in enumerate(data): expected_game = test_data[index] self.assertEqual(game, expected_game)
def test_save_by_id(self): generated_data = self._generate_data(year=self.year, week=self.week) test_data = generated_data[0] game_id = test_data['game_id'] spread = Spread() count = spread.save_by_id(year=self.year, week=self.week, game_id=game_id, data=test_data) self.assertEqual(count, 1) # Check datastore data = SpreadModel.query(SpreadModel.game_id == game_id).fetch(1) self.assertEqual(data[0].to_dict(), test_data)
def get(self, year, week): year = int(year) week = int(week) spread = Spread() spread_data = spread.fetch(year=year, week=week) result = { 'spread': spread_data } self._send_response(result)
class TestSpreadAbstract(unittest.TestCase): def setUp(self): self.spread = Spread() self.week = 1 def test_initial(self): self.assertIsNone( self.spread.next, "Testing initial state") def test_fetch_is_not_implemented(self): with self.assertRaises(NotImplementedError): self.spread.fetch(self.week), def test_protected_fetch_is_unimplemented(self): with self.assertRaises(NotImplementedError): self.spread._fetch_spread(self.week) def test_save_is_not_implemented(self): data = {} with self.assertRaises(NotImplementedError): self.spread.save(self.week, data), def test_protected_save_is_unimplemented(self): data = {} with self.assertRaises(NotImplementedError): self.spread._save_spread(self.week, data)
def put(self, year, week): """ WARNING! Use of this endpoint will create odd side-effects This endpoint does not acknowledge the 'game_id' property of spread data """ year = int(year) week = int(week) param = self.request.POST['spread'] # webapp2 treats POST body the same for PUT spread_data = json.loads(param) spread = Spread() result = spread.save(year=year, week=week, data=spread_data) self.response.set_status(201) self._send_response()
def test_save_by_id_same(self): generated_data = self._generate_data(year=self.year, week=self.week) test_data = generated_data[0] game_id = test_data['game_id'] spread = Spread() spread.save_by_id(year=self.year, week=self.week, game_id=game_id, data=test_data) test_data['game_line'] += randint(7, 14) spread.save_by_id(year=self.year, week=self.week, game_id=game_id, data=test_data) # Check datastore data = SpreadModel.query(SpreadModel.game_id == game_id).fetch(2) self.assertEqual(len(data), 1) self.assertEqual(data[0].to_dict(), test_data)
def test_save_multiple_spread(self): expected_count = randint(2, 16) test_data = self._generate_data(year=self.year, week=self.week, count=expected_count) spread = Spread() count = spread.save(year=self.year, week=self.week, data=test_data) self.assertEqual(count, expected_count) # Check datastore ancestor_key = spread._generate_key(year=self.year, week=self.week) data = SpreadModel().query(ancestor=ancestor_key).order(SpreadModel.game_id).fetch(expected_count+1) self.assertEqual(len(data), expected_count) for index in range(expected_count): expected_game = test_data[index] game = data[index].to_dict() self.assertEqual(game, expected_game)
def test_find_existing_with_some_data(self): test_data = self._generate_data(year=self.year, week=self.week, count=self.expected_count) spread = Spread() key = spread._generate_key(year=self.year, week=self.week) # Preload half preload = [] preload_count = self.expected_count/2 for i in range(preload_count): spread_data = {'parent': key} spread_data.update(test_data[i]) preload.append(SpreadModel(**spread_data)) ndb.put_multi(preload) data = spread._find_existing_entries(key, test_data) self.assertEqual(len(data), preload_count) for game in preload: self.assertTrue(game.game_id in data)
def test_save_same(self): expected_count = 1 generated_data = self._generate_data(year=self.year, week=self.week, count=expected_count) spread = Spread() count = spread.save(year=self.year, week=self.week, data=generated_data) generated_data[0]['game_line'] += 13 count = spread.save(year=self.year, week=self.week, data=generated_data) # Check datastore ancestor_key = spread._generate_key(year=self.year, week=self.week) data = SpreadModel().query(ancestor=ancestor_key).fetch(expected_count+1) self.assertEqual(len(data), expected_count) for index in range(expected_count): expected_game = generated_data[index] game = data[index].to_dict() self.assertEqual(game, expected_game)
def _query_database(self, week): query = {} result = {} query = Spread.all() query.filter('week =', week) query.order('person') result = query.fetch(constants.QUERY_LIMIT) return result
def _save_spread(self, week, spread): query = Spread.all() result = None item = None query.filter('week =', week) result = query.fetch(constants.QUERY_LIMIT) if len(result) == 0: #completely new save for person in spread: for line in spread[person]: item = Spread( year = constants.YEAR, week = week, person = person, team_name = line[constants.SPREAD_TEAM_NAME], over_under = line[constants.SPREAD_OVER_UNDER], total_score = int(line[constants.SPREAD_TOTAL_SCORE]) ) item.put()
def setUp(self): self.spread = Spread() self.week = 1