def test_get_game(self): """ Ensure we can get an existing game. """ # Seed the database with a game game = Game() game.game_type = self.gametype game.description = "You move around the game board (a mansion), as of one of the game's six suspects, collecting clues from which to deduce which suspect murdered the game's perpetual victim: Mr. Boddy (Dr. Black, outside of U.S.), and with which weapon and in what room." game.title = "Monopoly" game.number_of_players = 4 game.gamer_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["title"], "Monopoly") self.assertEqual(json_response["description"], "You move around the game board (a mansion), as of one of the game's six suspects, collecting clues from which to deduce which suspect murdered the game's perpetual victim: Mr. Boddy (Dr. Black, outside of U.S.), and with which weapon and in what room.") self.assertEqual(json_response["number_of_players"], 4) self.assertEqual(json_response["game_type"]["id"], 1) self.assertEqual(json_response["gamer"]["id"], 1)
def test_change_game(self): """ Ensure we can change an existing game. """ game = Game() game.game_type = self.gametype game.description = "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player" game.title = "Sorry" game.number_of_players = 4 game.gamer_id = 1 game.save() # DEFINE NEW PROPERTIES FOR GAME data = { "gameTypeId": 1, "description": "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player", "title": "Sorry", "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["title"], "Sorry") self.assertEqual(json_response["description"], "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player") self.assertEqual(json_response["number_of_players"], 4) self.assertEqual(json_response["game_type"]["id"], 1) self.assertEqual(json_response["gamer"]["id"], 1)
def setUp(self): url = "/register" data = { "username": "******", "password": "******", "email": "*****@*****.**", "first_name": "Steve", "last_name": "Brownlee", "bio": "Love those gamez!!" } response = self.client.post(url, data, format='json') json_response = json.loads(response.content) self.token = json_response["token"] self.assertEqual(response.status_code, status.HTTP_201_CREATED) gametype = GameType() gametype.label = "Board game" gametype.save() # url = "/games" # data = { # "gamer": 1, # "game_type": 1, # "title": "Balderdash", # "number_of_players": 2, # "description": "fun" # } # response = self.client.post(url, data, format='json') # json_response = json.loads(response.content) # self.token = json_response["token"] # self.assertEqual(response.status_code, status.HTTP_201_CREATED) game = Game() game.game_type = gametype game.title = "Balderdash" game.number_of_players = 4 game.gamer_id = 1 game.save()
def test_delete_game(self): """ Ensure we can delete an existing game. """ game = Game() game.game_type = self.gametype game.description = "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player" game.title = "Sorry" game.number_of_players = 4 game.gamer_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 gamer = Gamer.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.title = request.data["title"] game.number_of_players = request.data["numberOfPlayers"] game.description = request.data["description"] game.gamer = gamer # 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 = Game_Type.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}) # specify status code 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 create(self, request): # use token passed in the 'Authorization' header gamer = Gamer.objects.get(user=request.auth.user) # create a new Python instance of the Game class con properties de REQUEST de client game = Game() game.title = request.data["title"] game.game_type_id = request.data["gameTypeId"] game.number_of_players = request.data["numberOfPlayers"] game.description = request.data["description"] game.gamer = gamer # now use the Djanog ORM to fetch the record from the database whose 'id' is what the client passed as gameTypeId game_type = GameType.objects.get(pk=request.data["gameTypeId"]) game.game_type = game_type # try to save the new game to the db, then serialize it to JSON, then send that JSON back to client try: game.save() serializer = GameSerializer(game, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): gamer = Gamer.objects.get(user=request.auth.user) game = Game() game.title = request.data['title'] game.maker = request.data['maker'] game.number_of_players = request.data['numberOfPlayers'] game.skill_level = request.data['skillLevel'] game.gamer = gamer game_type = GameType.objects.get(pk=request.data['gameTypeId']) # select * # from gametype # where id=request.data['game_type_id'] game.game_type = game_type try: game.save() serializer = GameSerializer(game, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED) except ValidationError as ex: return Response({'reason': ex.message}, status=status.HTTP_400_BAD_REQUEST)