Beispiel #1
0
    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()
Beispiel #2
0
    def testAssignPlayerScoresToGame(self):
        self.create_basic_game()
        db_game = Game.query.first()

        host = db_game.host

        player_a = Player(name="Test player A")
        player_b = Player(name="Test player B")

        host_score = PlayerScore(player=host, game=db_game)
        player_score_a = PlayerScore(player=player_a, game=db_game)
        player_score_b = PlayerScore(player=player_b, game=db_game)

        db_game.scores.append(host_score)
        db_game.scores.append(player_score_a)
        db_game.scores.append(player_score_b)

        db.session.add(db_game)
        db.session.commit()

        db_game = Game.query.first()
        assert len(db_game.scores) == 3

        db_game.scores.remove(player_score_a)

        db.session.delete(player_score_a)
        db.session.add(db_game)
        db.session.commit()
        assert len(Game.query.first().scores) == 2
Beispiel #3
0
    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
Beispiel #4
0
    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
Beispiel #5
0
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()
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
0
    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
Beispiel #9
0
    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
Beispiel #10
0
    def testCreatePlayerScoreWithoutGameThrowsError(self):
        player = self.create_player()
        player_score = PlayerScore(player=player)

        db.session.add(player_score)

        with self.assertRaises(exc.IntegrityError):
            db.session.commit()
Beispiel #11
0
    def testCreatePlayerScoreWithoutPlayerThrowsError(self):
        game = self.create_basic_game()
        player_score = PlayerScore(game=game)

        db.session.add(player_score)

        with self.assertRaises(exc.IntegrityError):
            db.session.commit()
Beispiel #12
0
    def testDuplicateIdThrowsError(self):
        game = self.create_basic_game()

        player_1 = self.create_player("Player 1")
        player_score_1 = PlayerScore(player=player_1, game=game)
        db.session.add(player_1)
        db.session.add(player_score_1)
        db.session.commit()

        player_2 = self.create_player("Player 2")
        player_score_2 = PlayerScore(id=player_score_1.id,
                                     player=player_2,
                                     game=game)
        db.session.add(player_2)
        db.session.add(player_score_2)

        with self.assertRaises(orm.exc.FlushError):
            db.session.commit()
Beispiel #13
0
    def post(self, game_name):
        if not request.json:
            return create_error_response(415, "Unsupported Media Type",
                                         "use JSON")
        try:
            validate(request.json, ScoreBuilder.scoreSchema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        score = PlayerScore(score=request.json["score"])
        db_game = Game.query.filter_by(game_token=game_name).first()
        if db_game is None:
            return create_error_response(
                404, "Game not found",
                "Game with this name doesn't exist " + str(game_name))
        score.game_id = db_game.id
        db_player = Player.query.filter_by(name=request.json["player"]).first()
        if db_player is None:
            return create_error_response(
                404, "Player not found",
                "Player with this name doesn't exist " +
                str(request.json["player"]))
        score.player_id = db_player.id

        #Check if player already has score in this game
        prevscores = PlayerScore.query.filter_by(game_id=db_game.id).filter_by(
            player_id=db_player.id).first()
        if prevscores is not None:
            return create_error_response(
                409, "Already exists",
                "Playerscore for this player already exists in game " +
                game_name)

        db.session.add(score)
        db.session.commit()

        return Response(status=201,
                        headers={
                            "Location":
                            url_for("playerscoreresource",
                                    game_name=game_name,
                                    player_name=db_player.name)
                        })
Beispiel #14
0
def first_example():

    #GAME 1 - A chess game with 3 players

    #Players
    player_1 = Player(name="Alice")
    player_2 = Player(name="Bob")
    player_3 = Player(name="Charles")

    #Example game type
    game_type = GameType(name="chess", max_players=3)

    #Create a chess tournament (created_at defaults to func.now() but is here as an example)
    tournament = Tournament(name="Chess Tournament 1", status=1, created_at=func.now())

    #Initialize a game and add the players (created_at defaults to func.now() but is here as an example)
    chess_game = Game(game_token="testgame1" ,status=1, game_type=game_type, host=player_1, tournament=tournament, created_at=func.now())

    #Initial player scores, the points are optional
    player_1_score = PlayerScore(player=player_1, game=chess_game)
    player_2_score = PlayerScore(player=player_2, game=chess_game)
    player_3_score = PlayerScore(player=player_3, game=chess_game)

    #Connect the player scores to the game
    chess_game.scores.append(player_1_score)
    chess_game.scores.append(player_2_score)
    chess_game.scores.append(player_3_score)

    #Add the created things to the database
    db.session.add(player_1)
    db.session.add(player_2)
    db.session.add(player_3)

    db.session.add(game_type)
    db.session.add(tournament)
    db.session.add(chess_game)
    db.session.add(player_1_score)
    db.session.add(player_2_score)
    db.session.add(player_3_score)

    db.session.commit()
Beispiel #15
0
    def testCreateAndDeleteValidPlayerScore(self):
        player = self.create_player()
        game = self.create_basic_game()
        player_score = PlayerScore(game=game, player=player)

        db.session.add(player_score)
        db.session.commit()

        assert PlayerScore.query.count() == 1

        db.session.delete(player_score)
        db.session.commit()

        assert PlayerScore.query.count() == 0
Beispiel #16
0
    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
Beispiel #17
0
    def testGameDeletionDeletesPlayerScore(self):
        game = self.create_basic_game()

        player_1 = self.create_player("Player 1")
        player_score_1 = PlayerScore(player=player_1, game=game)
        db.session.add(player_1)
        db.session.add(player_score_1)
        db.session.commit()

        assert Game.query.count() == 1
        assert PlayerScore.query.count() == 1

        db.session.delete(game)
        db.session.commit()

        assert Game.query.count() == 0
        assert PlayerScore.query.count() == 0
Beispiel #18
0
    def testDeleteExistingPlayerScore(self):
        '''
        Test deleting score that exists in database
        Expects HTTP code 204 (No Content) 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)

        url = SCORE_URL.replace("<game_token>", "test12345")
        url = url.replace("<player_name>", "Jamppa")
        response = self.client.delete(url)

        assert response.status_code == 204, response.status_code
Beispiel #19
0
    def testDeleteScorePlayerDeletesScore(self):
        '''
        Test that deleting player of a score also deletes the score of that player
        Expects HTTP code 204 (No Content as response)
        Expects that the score will be deleted from the database
        '''
        game_type = GameType(name="Uno")
        game = Game(game_token="test12345", game_type=game_type)
        player = Player(name="Testman")
        score = PlayerScore(player=player, game=game)

        db.session.add(game_type)
        db.session.add(game)
        db.session.add(player)
        db.session.add(score)

        player_url = PLAYER_URL.replace("<player_name>", "Testman")
        response = self.client.delete(player_url)

        assert response.status_code == 204
        assert PlayerScore.query.count() == 0, PlayerScore.query.count()