Exemple #1
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
Exemple #2
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
Exemple #3
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()
Exemple #4
0
    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
Exemple #5
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
Exemple #6
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
Exemple #7
0
    def testCreatePlayerWithSameNameThrowsError(self):
        player_1 = Player(name="Alice")
        player_2 = Player(name="Alice")

        db.session.add(player_1)
        db.session.add(player_2)

        with self.assertRaises(exc.IntegrityError):
            db.session.commit()
Exemple #8
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()
Exemple #9
0
    def testDuplicateIdThrowsError(self):
        player_1 = Player(name="Alice")

        db.session.add(player_1)
        db.session.commit()

        player_2 = Player(id=player_1.id, name="Bob")

        db.session.add(player_2)

        with self.assertRaises(orm.exc.FlushError):
            db.session.commit()
Exemple #10
0
    def testGetPlayerCollectionWithItems(self):
        '''
        Test getting player collection when there are players in database
        Expects HTTP code 200 (OK) as and body with list of players in database as response
        '''
        db.session.add(Player(name="Player A"))
        db.session.add(Player(name="Player B"))
        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'])
Exemple #11
0
    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'])
Exemple #12
0
    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
Exemple #13
0
    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
Exemple #14
0
    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
Exemple #15
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
Exemple #16
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
Exemple #17
0
    def testCreatePlayerWithoutNameThrowsError(self):
        player = Player()

        db.session.add(player)

        with self.assertRaises(exc.IntegrityError):
            db.session.commit()
Exemple #18
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
Exemple #19
0
    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
Exemple #20
0
    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
Exemple #21
0
    def testCreateGameWithoutGameTypeAllowed(self):
        host = Player(name="Test player")
        game = Game(host=host, game_token="12345")

        db.session.add(host)
        db.session.add(game)
        db.session.commit()

        assert Game.query.count() == 1
    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
Exemple #23
0
    def testPutPlayerExistingName(self):
        '''
        Test renaming player to a name that is taken by another player
        Expects HTTP code 409 (Conflict) as reponse, and that player name is not changed
        '''
        db.session.add(Player(name="Player A"))
        db.session.add(Player(name="Player B"))
        db.session.commit()

        edit_url = ITEM_URL.replace('<player_name>', 'Player A')
        response = self.client.put(edit_url,
                                   data=json.dumps({"name": "Player B"}),
                                   content_type=('application/json'))
        assert response.status_code == 409, response.status_code
        assert Player.query.filter_by(
            name="Player A").count() == 1, Player.query.filter_by(
                name="Player A").count()
        assert Player.query.filter_by(
            name="Player B").count() == 1, Player.query.filter_by(
                name="Player B").count()
Exemple #24
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()
Exemple #25
0
    def testCreateAndDeleteValidPlayer(self):
        player = Player(name="Alice")

        db.session.add(player)
        db.session.commit()

        db_player = Player.query.first()
        assert db_player.name == "Alice"

        db.session.delete(db_player)
        db.session.commit()

        assert Player.query.first() == None
Exemple #26
0
    def testPutPlayerMissingContenttype(self):
        '''
        Test updating player with no content type header not set
        Expects HTTP code 415 (Unsupported Media Type) as response
        '''
        player = Player(name="Testaaja")
        db.session.add(player)
        db.session.commit()

        edit_url = ITEM_URL.replace('<player_name>', 'Testaaja')
        response = self.client.put(edit_url,
                                   data=json.dumps({"name": "Testaaja ABC"}))
        assert response.status_code == 415, response.status_code
Exemple #27
0
    def testPostPlayerDuplicateName(self):
        '''
        Test creating player with name that is already in use by another player
        Expects HTTP code 409 (Conflict) as response
        '''
        db.session.add(Player(name="Testaaja"))
        db.session.commit()

        response = self.client.post(COLLECTION_URL,
                                    data=json.dumps({"name": "Testaaja"}),
                                    content_type='application/json')

        assert response.status_code == 409, response.status_code
Exemple #28
0
    def testDeleteExistingPlayer(self):
        '''
        Test deleting a player that exists in database
        Expects HTTP code 204 (No Content) as response
        '''
        db.session.add(Player(name="Testaaja"))
        db.session.commit()

        delete_url = ITEM_URL.replace('<player_name>', 'Testaaja')
        response = self.client.delete(delete_url)

        assert response.status_code == 204, response.status_code
        assert Player.query.count() == 0, Player.query.count()
Exemple #29
0
    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
Exemple #30
0
    def testDeleteScoreNonExistingGame(self):
        '''
        Test deleting score for non-existing game
        Expects HTTP code 404 (Not Found) as response
        '''
        player = Player(name="Jamppa")
        db.session.add(player)
        db.session.commit()

        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