def test_update_updates_game(self): update_url = "/games/{}".format(self.game.get_id()) game_number = self.game.get_id() * 100 name = get_incremental_game_name(game_number) description = get_incremental_game_description(game_number) match_size = self.game.get_match_size() game_data = {"name": name, "description": description, "is_active": False} response = self.put(update_url, data=game_data) self.assertEqual(200, response.status_code) game = json.loads(response.data) self.assertIsNotNone(game.get("id")) self.assertIsNotNone(game.get("date_created")) self.assertIsNotNone(game.get("date_modified")) self.assertEqual(name, game.get("name")) self.assertEqual(description, game.get("description")) self.assertEqual(match_size, game.get("match_size")) self.assertEqual(False, game.get("is_active")) # Make sure the game was actually updated in the database saved_game = GamesService.get_instance().get(int(game.get("id"))) self.assertEqual(saved_game.get_id(), game.get("id")) self.assertEqual(Game.dump_datetime(saved_game.get_date_created()), game.get("date_created")) self.assertEqual(Game.dump_datetime(saved_game.get_date_modified()), game.get("date_modified")) self.assertEqual(saved_game.get_name(), game.get("name")) self.assertEqual(saved_game.get_description(), game.get("description")) self.assertEqual(saved_game.get_match_size(), game.get("match_size")) self.assertEqual(saved_game.get_is_active(), game.get("is_active"))
def test_create_returns_created_status(self): game_id = self.NUM_GAMES + 1 create_url = "/games" name = get_incremental_game_name(game_id) description = get_incremental_game_description(game_id) game_data = {"name": name, "description": description, "match_size": 2} response = self.post(create_url, data=game_data) self.assertEqual(201, response.status_code) game = json.loads(response.data) self.assertIsNotNone(game.get("id")) self.assertIsNotNone(game.get("date_created")) self.assertIsNotNone(game.get("date_modified")) self.assertEqual(name, game.get("name")) self.assertEqual(description, game.get("description")) self.assertEqual(2, game.get("match_size")) self.assertEqual(True, game.get("is_active")) # Make sure the game was actually saved to the database saved_game = GamesService.get_instance().get(int(game.get("id"))) self.assertEqual(saved_game.get_id(), game.get("id")) self.assertEqual(Game.dump_datetime(saved_game.get_date_created()), game.get("date_created")) self.assertEqual(Game.dump_datetime(saved_game.get_date_modified()), game.get("date_modified")) self.assertEqual(saved_game.get_name(), game.get("name")) self.assertEqual(saved_game.get_description(), game.get("description")) self.assertEqual(saved_game.get_match_size(), game.get("match_size")) self.assertEqual(saved_game.get_is_active(), game.get("is_active"))
def index(): """ Request: { "offset": "offset", "limit": "limit" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "offset": "value passed in. empty string if missing", "limit": "value passed in. empty string if missing" } } Response [200] (success): [ { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" }, { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" }, ... ] """ # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): games = GamesService.get_instance().get_list(inputs.limit.data, inputs.offset.data) return render_view('games/index', 200, games={game.get_id(): game.serialized for game in games}) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def create(): # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the match creation inputs if inputs.validate_on_submit(): # Ensure we have a valid game game = GamesService.get_instance().get(inputs.game_id.data) if game: # If an opponent is specified, match with that opponent if inputs.opponent_id.data: # Ensure that the opponent is a valid user opponent = PlayersService.get_instance().get(inputs.opponent_id.data) if not opponent: return render_view('422', 422, errors=OPPONENT_NOT_FOUND_ERROR, inputs=inputs.serialized()) # First attempt to find a match already requested by the desired opponent match = MatchesService.get_instance().get_opponent_match( game.get_id(), get_current_user(), opponent.get_id() ) # If the match is found, add the player to it and start the match if it's full if match: match.add_player(get_current_user()) # If no match is found, create one that is assigned to the desired opponent, # but leave it in the waiting state else: match = Match(game, get_current_user()) match.add_player(opponent, should_start=False) # Otherwise, match with a random opponent else: # First, attempt to find a match that is looking for an opponent match = MatchesService.get_instance().get_random_match(game.get_id(), get_current_user()) # If the match is found, add the player to it and start the match if it's full if match: match.add_player(get_current_user()) # If no match is found, create one that is waiting for an opponent else: match = Match(game, get_current_user()) try: match.save() return render_view('matches/show', 201, match=match.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=GAME_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def delete(game_id): """ Request: {} Response [422] (game with game_id doesn't exist): { "errors": { "GameNotFound": [ "Unable to find Game" ] }, "inputs": { "id": "game_id" } } Response [422] (save failure - unable to set game as inactive): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "id": "game_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the game game = GamesService.get_instance().get(game_id) # Verify the game creation inputs if game: try: game.update(**{'is_active': False}) return render_view('games/show', 200, game=game.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs={'id': game_id}) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': game_id})
def test_delete_deletes_game(self): delete_url = "/games/{}".format(self.game.get_id()) self.assertEqual(True, self.game.get_is_active()) response = self.delete(delete_url) self.assertEqual(200, response.status_code) game = json.loads(response.data) self.assertIsNotNone(game.get("id")) self.assertIsNotNone(game.get("date_created")) self.assertIsNotNone(game.get("date_modified")) self.assertIsNotNone(game.get("name")) self.assertIsNotNone(game.get("description")) self.assertIsNotNone(game.get("match_size")) self.assertEqual(False, game.get("is_active")) # Make sure the game was actually updated in the database saved_game = GamesService.get_instance().get(int(game.get("id"))) self.assertEqual(saved_game.get_id(), game.get("id")) self.assertEqual(Game.dump_datetime(saved_game.get_date_created()), game.get("date_created")) self.assertEqual(Game.dump_datetime(saved_game.get_date_modified()), game.get("date_modified")) self.assertEqual(saved_game.get_name(), game.get("name")) self.assertEqual(saved_game.get_description(), game.get("description")) self.assertEqual(saved_game.get_match_size(), game.get("match_size")) self.assertEqual(saved_game.get_is_active(), game.get("is_active"))
def show(game_id): """ Request: {} Response [422] (game with game_id doesn't exist): { "errors": { "GameNotFound": [ "Unable to find Game" ] }, "inputs": { "id": "game_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the game game = GamesService.get_instance().get(game_id) if game: return render_view('games/show', 200, game=game.serialized) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': game_id})
def update(game_id): """ Request: { "name": "name", "description": "description", "is_active": "is_active" } Response [422] (game with game_id doesn't exist): { "errors": { "GameNotFound": [ "Unable to find Game" ] }, "inputs": { "id": "game_id" } } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "id": "game_id", "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed, such as name uniqueness" ] }, "inputs": { "id": "game_id", "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (illegal update request - protected attribute, like match_size, passed in as input): { "errors": { "AttributeError": [ "Update to Game is not supported with changes to match_size" ] }, "inputs": { "id": "game_id", "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the game game = GamesService.get_instance().get(game_id) # Verify the game creation inputs if game: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.serialized().items() + {'id': game_id}.items()) if inputs.validate_on_submit(): try: game.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view('games/show', 200, game=game.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': game_id})