def test_get_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.description = "Test" event.date = "2021-02-14" event.time = "12:30:00" event.organizer = Gamer.objects.get(pk=1) event.game = Game.objects.get(pk=1) event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.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["description"], "Test") self.assertEqual(json_response["date"], "2021-02-14") self.assertEqual(json_response["time"], "12:30:00") self.assertEqual(json_response["organizer"]["id"], 1) self.assertEqual(json_response["game"]["id"], 1)
def test_get_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.event_time = "2020-08-28T14:51:39.989Z" event.location = "Shelbyville" event.game_id = 1 event.gamer_id = 1 event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.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["event_time"], "2020-08-28T14:51:39.989Z") self.assertEqual(json_response["game"]["id"], 1) self.assertEqual(json_response["gamer"]["id"], 1) self.assertEqual(json_response["location"], "Shelbyville")
def test_get_event(self): """ Ensure we can get an existing event. """ # Seed the database with a event event = Event() event.time = "19:30:00" event.date = "2021-08-14" event.game_id = 1 event.description = "We all live in a yellow submarine" event.organizer_id = 1 event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.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["time"], "19:30:00") self.assertEqual(json_response["date"], "2021-08-14") self.assertEqual(json_response["description"], "We all live in a yellow submarine")
def test_change_event(self): """ Ensure we can change an existing event """ event = Event() event.event_time = "2020-08-29T13:24:27.172Z" event.game_id = 1 event.location = "Nashville, TN" event.scheduler_id = 1 event.save() # Define new properties for game data = { "time": "2020-08-29T13:24:27.172000Z", "gameId": 1, "location": "Columbus, GA", "scheduler": 1 } self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.id}", data, format="json") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET Event again to verify changes response = self.client.get(f"/events/{event.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["event_time"], "2020-08-29T13:24:27.172000Z") self.assertEqual(json_response["game"]["id"], 1) self.assertEqual(json_response["location"], "Columbus, GA") self.assertEqual(json_response["scheduler"]["user"]["id"], 1)
def create(self, request): """Handle POST operations for events Returns: Response -- JSON serialized event instance """ gamer = Gamer.objects.get(user=request.auth.user) event = Event() event.event_time = request.data["event_time"] # is this how you get information from multiple tables? event.game.title = title # same as this one event.gamer.user.first_name = gamer game = Game.objects.get(pk=request.data["gameId"]) event.game = game try: event.save() serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """Handle POST operations for events Returns: Response -- JSON serialized event instance """ gamer = Gamer.objects.get(user=request.auth.user) event = Event() event.time = request.data["time"] event.date = request.data["date"] event.name = request.data["name"] event.description = request.data["description"] event.host = gamer game = Game.objects.get(pk=request.data["game_id"]) event.game = game try: event.save() serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_get_event(self): """ Ensure we can get an existing event. """ # Seed the database with an event event = Event() event.event_time = "2020-08-29T13:24:27.172Z" event.game_id = 1 event.location = "Nashville, TN" event.scheduler_id = 1 event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.id}") # Parse the JSON in the response body json_response = json.loads(response.content) # Assert that the event was retrieved self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the values are correct self.assertEqual(json_response["event_time"], "2020-08-29T13:24:27.172000Z") self.assertEqual(json_response["game"]["id"], 1) self.assertEqual(json_response["location"], "Nashville, TN") self.assertEqual(json_response["scheduler"]["user"]["id"], 1)
def test_change_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.event_time = "2020-08-28T14:51:39.989Z" event.location = "Shelbyville" event.game_id = 1 event.gamer_id = 1 event.save() data = { "event_time": "2020-08-28T14:51:39.989Z", "gameId": 1, "gamerId": 1, "location": "Shelbyville" } self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.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"/events/{event.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["event_time"], "2020-08-28T14:51:39.989Z") self.assertEqual(json_response["game"]["id"], 1) self.assertEqual(json_response["gamer"]["id"], 1) self.assertEqual(json_response["location"], "Shelbyville")
def test_change_event(self): """ Ensure we can change an existing game. """ event = Event() event.event_time = "2021-02-12 12:47:00" event.location = "Sorry" event.scheduler_id = 1 event.game_id = 1 event.save() # DEFINE NEW PROPERTIES FOR GAME data = { "event_time": "2021-03-12 12:47:00", "location": "your moms house", "gameId": 1, "gamerId": 1 } self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.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"/events/{event.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["location"], "your moms house") self.assertEqual(json_response["event_time"], "2021-03-12T12:47:00Z") self.assertEqual(json_response["game"]["id"], 1) self.assertEqual(json_response["scheduler"]["id"], 1)
def test_get_game(self): """ Ensure we can get an existing game. """ event = Event() event.event_day = "1820-10-25" event.event_time = "14:39" event.game_id = 1 event.location = "Tom's" event.gamer_id = 1 event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.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["event_day"], "1820-10-25") self.assertEqual(json_response["event_time"], "14:39:00") self.assertEqual(json_response["location"], "Tom's")
def test_get_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.game_id = 1 event.organizer_id = 1 event.time = "11:45:00" event.date = "2021-05-19" event.description = "budding software developer" event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.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["organizer"]["id"], "Monopoly") self.assertEqual(json_response["description"], "budding software developer") # self.assertEqual(json_response["game"]["id"], 5) self.assertEqual(json_response["time"], "11:45:00") self.assertEqual(json_response["date"], "2021-05-19")
def test_change_event(self): """ Ensure we can change an existing game. """ event = Event() event.game_id = 1 event.organizer_id = 1 event.time = "12:25:00" event.date = "2000-03-29" event.description = "junior software developer" event.save() # DEFINE NEW PROPERTIES FOR GAME data = { "gameId": 1, "description": "Hasbro", "time": "12:25:00", "date": "2000-03-29" } self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.id}", data, format="json") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET EVENT AGAIN TO VERIFY CHANGES response = self.client.get(f"/events/{event.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["gameId"], 1) self.assertEqual(json_response["description"], "Hasbro") self.assertEqual(json_response["time"], "12:25:00") self.assertEqual(json_response["date"], "2000-03-29")
def test_get_event(self): event = Event() event.game_id = 1 event.scheduler_id = 1 event.location = "The moon" event.event_time = "2021-02-05 17:00:00" event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.get(f"/events/{event.id}") json_response = json.loads(response.content) self.assertEqual(json_response["location"], "The moon")
def create(self, request): event = Event() scheduler = Gamer.objects.get(user=request.auth.user) game = Game.objects.get(pk=request.data["gameId"]) event.event_time = request.data["eventTime"] event.game = game event.scheduler = scheduler event.location = ["location"] try: event.save() serializer = EventSerializer(event, 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) event = Event() event.time = request.data["time"] event.date = request.data["date"] event.description = request.data["description"] event.organizer = gamer game = Game.objects.get(pk=request.data["gameId"]) event.game = game try: event.save() serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_delete_game(self): """ Ensure we can delete an existing game. """ event = Event() event.event_time = "2020-08-29T13:24:27.172Z" event.game_id = 1 event.location = "Nashville, TN" event.scheduler_id = 1 event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET again to verify 404 response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_delete_event(self): """ Ensure we can delete an existing event. """ event = Event() event.event_time = "2021-03-02 13:46:38.058841" event.location = "My house" event.game_id = 1 event.scheduler_id = 1 event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET event AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_delete_event(self): """ Ensure we can delete an existing game. """ event = Event() event.event_time = "2020-08-28T14:51:39.989Z" event.location = "Shelbyville" event.game_id = 1 event.gamer_id = 1 event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET event AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def update(self, request, pk=None): """Handle PUT requests for an event Returns: Response -- Empty body with 204 status code """ event = Event() event.event_time = request.data["event_time"] event.location = request.data["location"] game = Game.objects.get(pk=request.data["gameId"]) event.game = game gamer = Gamer.objects.get(pk=request.data["gamerId"]) event.gamer = gamer event.save() return Response({}, status=status.HTTP_204_NO_CONTENT)
def test_delete_event(self): """ Ensure we can delete an existing game. """ event = Event() event.game_id = 1 event.organizer_id = 1 event.time = "11:25:00" event.date = "2000-03-17" event.description = "senior software developer" event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET event AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_change_event(self): """ Ensure we can change an existing event. """ event = Event() event.time = "13:00" event.date = "11-04-2020" event.description = "Party Night" gamer = Gamer.objects.get(pk=1) event.organizer = gamer game = Game.objects.get(pk=1) event.game = game event.save() # DEFINE NEW PROPERTIES FOR GAME data = { "time": "15:00", "date": "12-25-2020", "description": "Xmas Party Night", "organizer": 1, "gameId": 1 } # print('GAME ID ' + str(game.id)) self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.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"/events/{event.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["time"], "15:00") self.assertEqual(json_response["date"], "12-25-2020") self.assertEqual(json_response["description"], "Xmas Party Night")
def create(self, request): """Handle POST operations for events Returns: Response -- JSON serialized event instance """ gamer = Gamer.objects.get(user=request.auth.user) game = Game.objects.get(pk=request.data["gameId"]) event = Event() event.event_time = request.data["time"] event.location = request.data["location"] event.scheduler = gamer event.game = game try: event.save() serializer = EventSerializer(event, 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 test_delete_event(self): """ Ensure we can delete an existing event. """ event = Event() event.time = "13:00" event.date = "11-04-2020" event.description = "Party Night" gamer = Gamer.objects.get(pk=1) event.organizer = gamer game = Game.objects.get(pk=1) event.game = game event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET GAME AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)