def test_state(self): """ methods tested: save_state load_state """ _LOGGER.info('===BEGIN TEST_STATE===') ## save player_1_id = 1 player_2_id = 2 game = Game(creator_id=1, name='game creation test', level=3, private=True, password='******', num_players=3, extensions={1: 'rare_technologies', 11: 'alliances'}, init_state=True) players_ids = [player_1_id, player_2_id] status, game = self.db.create_game(game, players_ids) self.assertEqual(status, DB_STATUS.OK) status, state_id = self.db.save_state(game) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(game.cur_state_id(), state_id) self.assertTrue(state_id in game.states_ids) ## load status, state_loaded = self.db.load_state(state_id) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(game.cur_state.__dict__, state_loaded.__dict__) not_a_state_id = 666 status, dummy_state = self.db.load_state(not_a_state_id) self.assertEqual(status, DB_STATUS.NO_ROWS) self.assertEqual(dummy_state, None) # test DB_ERROR self.db.set_unittest_to_fail(True) status, dummy = self.db.save_state(game) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.load_state(state_id) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) self.db.set_unittest_to_fail(False) _LOGGER.info('===END TEST_STATE===')
def load_game(self, game_id): """ load a game from the db args: game_id (int) return: db_status, incomplete Game object do not load players ids, extensions ids, states ids, cur state """ sql = ('SELECT * ' 'FROM games ' 'WHERE id = ?;') try: db = self._connect() # to have access to returned row as a dict db.row_factory = sqlite3.Row cursor = db.cursor() cursor.execute(sql, (game_id, )) except sqlite3.DatabaseError: msg = 'Error while loading game with id {}'.format(game_id) self._logger.exception(msg) return (DB_STATUS.ERROR, None) else: game_params = cursor.fetchone() if game_params is None: msg = 'Game {} not found in database'.format(game_id) self._logger.warning(msg) return (DB_STATUS.NO_ROWS, None) game = Game.from_db(**game_params) msg = 'Game {} successfully loaded from database'.format(game.id_) self._logger.info(msg) return (DB_STATUS.OK, game) finally: if 'cursor' in locals(): cursor.close() if 'db' in locals(): db.close()
def test_game(self): """ methods tested: get_game_players_ids get_game_ext get_game_states_ids get_pub_priv_games_ids get_my_games_ids create_game save_game load_game """ _LOGGER.info('===BEGIN TEST_GAME===') # the games ids in the test db not_started_gid = 1 in_progress_gid = 2 # ended_gid = 3 private_gid = 4 not_a_gid = 666 ## get the players who joined the test game 1 # in the 'not started' test game, players are : [1, 2] status, players_ids = self.db.get_game_players_ids(not_started_gid) self.assertEqual(status, DB_STATUS.OK) # there's no 'order by' in the sql query, so the order is not guaranteed players_ids.sort() self.assertEqual([1, 2], players_ids) ## get the extensions associated with the test game 1 # in the 'not started' test game, extensions are: # {2: 'developments', 4: 'secret_world', 6: 'ancient_hives'} status, game_exts = self.db.get_game_ext(not_started_gid) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(game_exts, {2: 'developments', 4: 'secret_world', 6: 'ancient_hives'}) ## get the states played in the game 1 # default state for now status, states_ids = self.db.get_game_states_ids(not_started_gid) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(states_ids, [1]) ## get the not started test games # the 'not started' test game is public status, games_ids = self.db.get_pub_priv_games_ids() self.assertEqual(status, DB_STATUS.OK) self.assertEqual(games_ids, ([not_started_gid], [private_gid])) ## list not ended games joined by player 1 player_1_id = 1 player_2_id = 2 # the test player has join all the test games : not started, # in progress, ended and the private one status, games_ids = self.db.get_my_games_ids(player_1_id) self.assertEqual(status, DB_STATUS.OK) games_ids.sort() self.assertEqual(games_ids, [1, 2, 4]) ## test loading games # game loading status, game = self.db.load_game(not_a_gid) self.assertEqual(status, DB_STATUS.NO_ROWS) self.assertEqual(game, None) status, game = self.db.load_game(in_progress_gid) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(game.id_, 2) self.assertEqual(game.name, 'my in-progress test game') self.assertEqual(game.started, True) self.assertEqual(game.ended, False) self.assertEqual(game.level, 2) self.assertEqual(game.cur_state_id(), -1) self.assertEqual(game.private, False) self.assertEqual(game.password, '') start_date = datetime.strptime('2006-06-06 06:06:06.666666', '%Y-%m-%d %H:%M:%S.%f') self.assertEqual(game.start_date, start_date) last_play = datetime.strptime('2006-06-16 06:06:06.666666', '%Y-%m-%d %H:%M:%S.%f') self.assertEqual(game.last_play, last_play) self.assertEqual(game.num_players, 2) self.assertEqual(game.creator_id, 2) # loaded by the gm, not db, so must be empty self.assertEqual(game.players_ids, []) self.assertEqual(game.extensions, {}) self.assertEqual(game.states_ids, []) self.assertEqual(game.last_valid_state_id, None) ## test updating a game # update game game.last_play = datetime.now() status, _ = self.db.save_game(game) self.assertEqual(status, DB_STATUS.OK) ## then reloading the saved game status, game_mod = self.db.load_game(in_progress_gid) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(game.last_play, game_mod.last_play) self.assertEqual(game.cur_state_id(), game_mod.cur_state_id()) # updating a game with wrong id game_mod.id_ = 666 status, _ = self.db.save_game(game_mod) self.assertEqual(status, DB_STATUS.NO_ROWS) ## test creating a new game new_game = Game(creator_id=1, name='game creation test', level=3, private=True, password='******', num_players=3, extensions={1: 'rare_technologies', 11: 'alliances'}, init_state=True) players_ids = [player_1_id, player_2_id] status, new_game = self.db.create_game(new_game, players_ids) self.assertEqual(status, DB_STATUS.OK) ## then loading it status, new_game_reload = self.db.load_game(new_game.id_) self.assertEqual(status, DB_STATUS.OK) self.assertEqual(new_game.id_, new_game_reload.id_) self.assertEqual(new_game.name, new_game_reload.name) self.assertEqual(new_game.started, new_game_reload.started) self.assertEqual(new_game.ended, new_game_reload.ended) self.assertEqual(new_game.level, new_game_reload.level) self.assertEqual(new_game.cur_state_id(), new_game_reload.cur_state_id()) self.assertEqual(new_game.private, new_game_reload.private) self.assertEqual(new_game.password, new_game_reload.password) self.assertEqual(new_game.start_date, new_game_reload.start_date) self.assertEqual(new_game.last_play, new_game_reload.last_play) self.assertEqual(new_game.num_players, new_game_reload.num_players) self.assertEqual(new_game.creator_id, new_game_reload.creator_id) # load_game returns an incomplete game self.assertNotEqual(new_game.players_ids, new_game_reload.players_ids) self.assertNotEqual(new_game.extensions, new_game_reload.extensions) # states are handled from game_mangager self.assertEqual(new_game.states_ids, new_game_reload.states_ids) self.assertEqual(new_game.last_valid_state_id, new_game_reload.last_valid_state_id) #self.assertEqual(new_game.cur_state.__dict__, # new_game_reload.cur_state.__dict__) # test DB_ERROR self.db.set_unittest_to_fail(True) status, dummy = self.db.create_game(new_game, players_ids) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.save_game(game) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.load_game(in_progress_gid) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.get_game_players_ids(not_started_gid) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.get_game_ext(not_started_gid) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.get_game_states_ids(not_started_gid) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.get_pub_priv_games_ids() self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) status, dummy = self.db.get_my_games_ids(player_1_id) self.assertEqual(status, DB_STATUS.ERROR) self.assertEqual(dummy, None) self.db.set_unittest_to_fail(False) _LOGGER.info('===END TEST_GAME===')