def test_get_game(self): """ Ensure we can get an existing game. """ # Seed the database with a game game = Game() game.game_type_id = 1 game.skill_level = 5 game.name = "Monopoly" game.maker = "Milton Bradley" game.number_of_players = 4 game.player_id = 1 game.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/games/{game.id}") # Parse the JSON in the response body json_response = json.loads(response.content) # Assert that the game was retrieved self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the values are correct self.assertEqual(json_response["name"], "Monopoly") self.assertEqual(json_response["maker"], "Milton Bradley") self.assertEqual(json_response["skill_level"], 5) self.assertEqual(json_response["number_of_players"], 4)
def test_delete_game(self): """ Ensure we can delete an existing game. """ game = Game() game.game_type_id = 1 game.skill_level = 5 game.name = "Sorry" game.maker = "Milton Bradley" game.number_of_players = 4 game.player_id = 1 game.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/games/{game.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET GAME AGAIN TO VERIFY 404 response response = self.client.get(f"/games/{game.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def create(self, request): """Handle POST operations Returns: Response -- JSON serialized game instance """ # Uses the token passed in the `Authorization` header player = Player.objects.get(user=request.auth.user) # Create a new Python instance of the Game class # and set its properties from what was sent in the # body of the request from the client. game = Game() game.name = request.data["name"] game.maker = request.data["maker"] game.number_of_players = request.data["numberOfPlayers"] game.skill_level = request.data["skillLevel"] game.player = player # Use the Django ORM to get the record from the database # whose `id` is what the client passed as the # `gameTypeId` in the body of the request. gametype = GameType.objects.get(pk=request.data["gameTypeId"]) game.game_type = gametype # Try to save the new game to the database, then # serialize the game instance as JSON, and send the # JSON as a response to the client request try: game.save() serializer = GameSerializer(game, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED) # If anything went wrong, catch the exception and # send a response with a 400 status code to tell the # client that something was wrong with its request data except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_change_game(self): """ Ensure we can change an existing game. """ game = Game() game.game_type_id = 1 game.skill_level = 5 game.name = "Sorry" game.maker = "Milton Bradley" game.number_of_players = 4 game.player_id = 1 game.save() # DEFINE NEW PROPERTIES FOR GAME data = { "gameTypeId": 1, "skillLevel": 2, "name": "Sorry", "maker": "Hasbro", "numberOfPlayers": 4 } self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/games/{game.id}", data, format="json") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET GAME AGAIN TO VERIFY CHANGES response = self.client.get(f"/games/{game.id}") json_response = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the properties are correct self.assertEqual(json_response["name"], "Sorry") self.assertEqual(json_response["maker"], "Hasbro") self.assertEqual(json_response["skill_level"], 2) self.assertEqual(json_response["number_of_players"], 4)