def testPutScoreInvalidSchema(self): ''' Test updating score with invalid fields Expects HTTP code 400 (Bad Request) as response ''' game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player = Player(name="Jamppa") score = PlayerScore(game=game, player=player) db.session.add(game_type) db.session.add(game) db.session.add(player) db.session.add(score) db.session.commit() put_data = { "color": "green" } url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Jamppa") response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 400, response.status_code
def testPutScoreEditScore(self): """ Make a valid put request to change score value of a PlayerScore Expects HTTP code 204 (No Content) as response Expects score is updated in database """ game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player = Player(name="Jamppa") score = PlayerScore(game=game, player=player) db.session.add(game_type) db.session.add(game) db.session.add(player) db.session.add(score) db.session.commit() put_data = { "player": "Jamppa", "score": 50 } url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Jamppa") response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 204, response.status_code db_score = PlayerScore.query.filter_by(player_id=player.id).first() assert db_score.score == 50, db_score.score
def testPutScoreNonExistingScore(self): ''' Test updating a non-existing score Expects HTTP code 404 (Not Found) as response ''' game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player = Player(name="Jamppa") db.session.add(game_type) db.session.add(game) db.session.add(player) db.session.commit() url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Jamppa") put_data = { "player": "Jamppa", "score": 10 } response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 404, response.status_code
def testDeleteGameDeletesScores(self): """ Test that deleting game of a score also deletes scores for that game Expects HTTP code 204 (No Content) as response Expects that the scores will be deleted from the database """ game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player_a = Player(name="Testman A") player_b = Player(name="Testman B") score_a = PlayerScore(player=player_a, game=game) score_b = PlayerScore(player=player_b, game=game) db.session.add(game_type) db.session.add(game) db.session.add(player_a) db.session.add(player_b) db.session.add(score_a) db.session.add(score_b) db.session.commit() url = GAME_URL.replace("<game_token>", "test12345") response = self.client.delete(url) assert response.status_code == 204 assert PlayerScore.query.count() == 0, PlayerScore.query.count()
def testPutScoreMissingContentType(self): ''' Test updating score with missing ContentType header Expects HTTP code 415 (Unsupported Media Type) as response ''' game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player = Player(name="Jamppa") score = PlayerScore(game=game, player=player) db.session.add(game_type) db.session.add(game) db.session.add(player) db.session.add(score) db.session.commit() put_data = { "player": "Jamppa", "score": 50 } url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Jamppa") response = self.client.put( url, data=json.dumps(put_data) ) assert response.status_code == 415, response.status_code
def testCreateGameTypeWithoutName(self): game_type = GameType() db.session.add(game_type) with self.assertRaises(exc.IntegrityError): db.session.commit()
def testGetGameScoreboard(self): ''' Test getting existing scoreboard Expects response with HTTP code 200 (OK) Expects response body contains scores from database ''' game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player_a = Player(name="Testman A") player_b = Player(name="Testman B") score_a = PlayerScore(player=player_a, game=game, score=8.5) score_b = PlayerScore(player=player_b, game=game) db.session.add(game_type) db.session.add(game) db.session.add(player_a) db.session.add(player_b) db.session.add(score_a) db.session.add(score_b) db.session.commit() url = SCOREBOARD_URL.replace("<game_token>", "test12345") response = self.client.get(url) assert response.status_code == 200 data = json.loads(response.data.decode("utf-8")) assert len(data["items"]) == 2
def testPutGameWithoutType(self): """ Test for editing a game with missing required data (gametype) Error 400 expected """ host = Player(name="Alice") db.session.add(host) game_type = GameType(name="Korona") db.session.add(game_type) game = Game(host=host, game_type=game_type, game_token="test123") db.session.add(game) db.session.commit() game_data = { "host": "Alice" } url = ITEM_URL.replace("<game_token>", "test123") response = self.client.put( url, data=json.dumps(game_data), content_type='application/json' ) assert response.status_code == 400, response.status_code assert Game.query.count() == 1
def testGetGameCollection(self): """ Test for successfully retrieving a collection of games """ host = Player(name="Alice") db.session.add(host) game_type = GameType(name="Korona") db.session.add(game_type) db.session.add(Game( host=host, game_type=game_type, game_token="test123", finished_at=func.now() )) db.session.add(Game(host=host, game_type=game_type, game_token="test1234")) db.session.commit() response = self.client.get(COLLECTION_URL) assert response.status_code == 200, response.status_code assert response.data is not None json_object = json.loads(response.data) assert json_object is not None assert len(json_object['items']) == 2, len(json_object['items'])
def testPutGameFinishedAt(self): ''' Test that updating game status to 1 will set finished_at for the game Expects response with HTTP code 204 ''' host = Player(name="Alice") db.session.add(host) game_type = GameType(name="Korona") db.session.add(game_type) game = Game(host=host, game_type=game_type, game_token="test123") db.session.add(game) db.session.commit() url = ITEM_URL.replace("<game_token>", "test123") put_data = { "host" : host.name, "game_type": game_type.name, "name": "test123", "status": 1 } response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 204, response.status_code game = Game.query.first() assert game.finished_at is None, game.finished_at
def testPutGameRestartGame(self): """ Test seeing if finished_at is removed from game if its status is changed back to active (1) """ host = Player(name="Alice") db.session.add(host) game_type = GameType(name="Korona") db.session.add(game_type) game = Game(host=host, game_type=game_type, game_token="test123", status=0, finished_at=datetime.now()) db.session.add(game) db.session.commit() url = ITEM_URL.replace("<game_token>", "test123") put_data = { "host" : host.name, "game_type": game_type.name, "name": "test123", "status": 1 } response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 204, response.status_code game = Game.query.first() assert game.finished_at is None, game.finished_at
def testPostGameWithSameName(self): """ Test for trying to add a game with already used name """ host = Player(name="Alice") db.session.add(host) host2 = Player(name="James") db.session.add(host2) game_type = GameType(name="Korona") db.session.add(game_type) db.session.commit() game = Game( host=host, game_type=game_type, game_token="test123" ) db.session.add(game) db.session.commit() game_data = { "host": "James", "game_type": "Korona", "name": "test123" } response = self.client.post( COLLECTION_URL, data=json.dumps(game_data), content_type='application/json' ) assert response.status_code == 409, response.status_code assert Game.query.count() == 1
def testPutScoreNewUrl(self): """ Change player of a score so that url changes Expects HTTP code 201 (Created) as response """ game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player_a = Player(name="Player A") player_b = Player(name="Player B") score_a = PlayerScore(game=game, player=player_a) db.session.add(game_type) db.session.add(game) db.session.add(player_a) db.session.add(player_b) db.session.add(score_a) db.session.commit() put_data = { "player": "Player B", "score": 100, "game": "test12345" } url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Player A") response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 201, response.status_code
def testPutScoreDuplicatePlayer(self): """ Change player of a score to one that already has score for the game Expects response code 409 (Conflict) as response """ game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player_a = Player(name="Player A") player_b = Player(name="Player B") score_a = PlayerScore(game=game, player=player_a) score_b = PlayerScore(game=game, player=player_b) db.session.add(game_type) db.session.add(game) db.session.add(player_a) db.session.add(player_b) db.session.add(score_a) db.session.add(score_b) db.session.commit() put_data = { "player": "Player B", "score": 100, "game": "test12345" } url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Player A") response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 409, response.status_code
def testPutGameValid(self): """ Test for successfully renaming a game Expects 201 due to new address """ host = Player(name="Alice") host_alter = Player(name="Bob") db.session.add(host) db.session.add(host_alter) game_type = GameType(name="Korona") game_type_alter = GameType(name="Blind Korona") db.session.add(game_type) db.session.add(game_type_alter) game = Game( host=host, game_type=game_type, game_token="test123" ) db.session.add(game) db.session.commit() url = ITEM_URL.replace("<game_token>", "test123") put_data = { "host" : host_alter.name, "game_type": game_type_alter.name, "name": "test999", "status": 0 } response = self.client.put( url, data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 201, response.status_code game = Game.query.first() assert game.game_token == "test999", game.game_token assert game.host == host_alter, game.host assert game.game_type == game_type_alter, game.game_type assert game.finished_at is not None
def testCreateGameWithoutHostAllowed(self): game_type = GameType(name="chess") game = Game(game_type = game_type, game_token="12345") db.session.add(game_type) db.session.add(game) db.session.commit() assert Game.query.count() == 1
def second_example(): #GAME 2 - Hearts game with 4 players #players player_1 = Player(name="Jessica") player_2 = Player(name="Ken") player_3 = Player(name="Laura") player_4 = Player(name="Maurice") #game type game_type = GameType(name="Hearts", max_players=4) #No tournament this time as it is optional #Initialize a game and add the players, in this example game has already ended hearts_game = Game(game_token="gameofhearts", status=0, game_type=game_type, host=player_1, created_at=datetime.datetime.now()-datetime.timedelta(days=5), finished_at=datetime.datetime.now()-datetime.timedelta(days=2)) #Player scores, this time they have scores player_1_score = PlayerScore(player=player_1, score=22, game=hearts_game) player_2_score = PlayerScore(player=player_2, score=35, game=hearts_game) player_3_score = PlayerScore(player=player_3, score=102, game=hearts_game) player_4_score = PlayerScore(player=player_4, score=24, game=hearts_game) #Connect the players scores to the game hearts_game.scores.append(player_1_score) hearts_game.scores.append(player_2_score) hearts_game.scores.append(player_3_score) hearts_game.scores.append(player_4_score) #Add the created things to database db.session.add(player_1) db.session.add(player_2) db.session.add(player_3) db.session.add(game_type) db.session.add(hearts_game) db.session.add(player_1_score) db.session.add(player_2_score) db.session.add(player_3_score) db.session.commit() #Let's create a leaderboard player_1_leaderboard = Leaderboard(player_id=player_1.id, game_type_id=hearts_game.id, wins=5, losses=3) player_2_leaderboard = Leaderboard(player_id=player_2.id, game_type_id=hearts_game.id, wins=4, losses=6) player_3_leaderboard = Leaderboard(player_id=player_3.id, game_type_id=hearts_game.id, wins=2, losses=5) player_4_leaderboard = Leaderboard(player_id=player_4.id, game_type_id=hearts_game.id, wins=0, losses=3) #Add the leaderboard entries to the db db.session.add(player_1_leaderboard) db.session.add(player_2_leaderboard) db.session.add(player_3_leaderboard) db.session.add(player_4_leaderboard) db.session.commit()
def create_basic_game(self): game_type = GameType(name="chess", max_players=3) host = Player(name="Test player") game = Game(game_type=game_type, host=host, game_token="test") db.session.add(game_type) db.session.add(host) db.session.add(game) db.session.commit() return game
def testPutGametypeDuplicate(self): """ Test for changing a gametype's name to a duplicate 409 error expected """ db.session.add(GameType(name="Chess")) db.session.add(GameType(name="3D Chess")) db.session.commit() put_data = { "name": "3D Chess" } response = self.client.put( ITEM_URL.replace("<gametype_name>", "Chess"), data=json.dumps(put_data), content_type="application/json" ) assert response.status_code == 409, response.status_code assert GameType.query.count() == 2, GameType.query.count()
def testGetGametype(self): """ Test for successful gametype retrieval Expects response with HTTP code 200 (OK) """ db.session.add(GameType(name="Chess", min_players=2, max_players=2)) db.session.commit url = ITEM_URL.replace("<gametype_name>", "Chess") response = self.client.get(url) assert response.status_code == 200, response.status_code
def testDeleteGametype(self): """ Test for successfully deleting a gametype """ db.session.add(GameType(name="Chess")) db.session.commit() url = ITEM_URL.replace("<gametype_name>", "Chess") response = self.client.delete(url) assert response.status_code == 204, response.status_code assert GameType.query.count() == 0, GameType.query.count()
def testUpdateMaxPlayers(self): game_type = GameType(name="Hearts") db.session.add(game_type) db.session.commit() db_game_type = GameType.query.filter_by(name="Hearts").first() assert db_game_type.min_players == None assert db_game_type.max_players == None game_type.min_players = 3 game_type.max_players = 4 db.session.add(game_type) db.session.commit() db_game_type = GameType.query.filter_by(name="Hearts").first() assert db_game_type.min_players == 3 assert db_game_type.max_players == 4
def testDeleteNonExistingGametype(self): """ Test for failing to delete a nonexisting gametype 404 error expected """ db.session.add(GameType(name="Chess")) db.session.commit() url = ITEM_URL.replace("<gametype_name>", "imaginary type") response = self.client.delete(url) assert response.status_code == 404, response.status_code assert GameType.query.count() == 1, GameType.query.count()
def create_game_with_tournament(self): game_type = GameType(name="chess", max_players=3) host = Player(name="Test player") tournament = Tournament(name="test tournament") game = Game(game_token="basicgame", game_type=game_type, host=host, tournament=tournament) db.session.add(game_type) db.session.add(host) db.session.add(tournament) db.session.add(game) db.session.commit() return game
def testPutGametypeInvalidContent(self): """ Test for trying to edit with invalid content 415 error expected """ db.session.add(GameType(name="Chess", max_players=2)) db.session.commit() response = self.client.put( ITEM_URL.replace("<gametype_name>", "Chess"), data="ASDASD" ) assert response.status_code == 415, response.status_code assert GameType.query.count() == 1, GameType.query.count()
def testPutGametypeInvalidSchema(self): """ Test for trying to add an invalid gametype 400 error expected """ db.session.add(GameType(name="Chess", max_players=2)) db.session.commit() response = self.client.put( ITEM_URL.replace("<gametype_name>", "Chess"), data=json.dumps({"shoe_size": 10}), content_type="application/json" ) assert response.status_code == 400, response.status_code assert GameType.query.count() == 1, GameType.query.count()
def testGetNonExistingPlayerPlayerScore(self): """ Error test for retrieving player score for nonexisting player Expects HTTP code 404 (Not Found) as response """ game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) db.session.add(game_type) db.session.add(game) db.session.commit() url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "idontexist") response = self.client.get(url) assert response.status_code == 404, response.status_code
def testPutGametypeRename(self): """ Test for successful PUT gametype """ db.session.add(GameType(name="Chess", max_players=2)) db.session.commit() response = self.client.put( ITEM_URL.replace("<gametype_name>", "Chess"), data=json.dumps({"name": "Chess2", "max_players": 3}), content_type="application/json" ) assert response.status_code == 201, response.status_code game_type = GameType.query.filter_by(name="Chess2").first() assert game_type.max_players == 3
def testGetPlayerScore(self): game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player = Player(name="Jamppa") score = PlayerScore(game=game, player=player) db.session.add(game_type) db.session.add(game) db.session.add(player) db.session.add(score) db.session.commit() url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Jamppa") response = self.client.get(url) assert response.status_code == 200, response.status_code
def testDeleteNonExistingScore(self): ''' Test deleting non-existing score Expects HTTP code 404 (Not Found) as response ''' game_type = GameType(name="Uno") game = Game(game_token="test12345", game_type=game_type) player = Player(name="Jamppa") db.session.add(game_type) db.session.add(game) db.session.add(player) url = SCORE_URL.replace("<game_token>", "test12345") url = url.replace("<player_name>", "Jamppa") response = self.client.delete(url) assert response.status_code == 404, response.status_code