Beispiel #1
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'])
Beispiel #2
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 #3
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 #4
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 #5
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 #6
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
Beispiel #7
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 #8
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
Beispiel #9
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
Beispiel #10
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
Beispiel #11
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 #12
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
Beispiel #13
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 #14
0
    def testDuplicateIdThrowsError(self):
        game = self.create_basic_game()

        game_duplicate = Game(id=game.id, game_type=game.game_type, host=game.host)

        db.session.add(game_duplicate)
        
        with self.assertRaises(orm.exc.FlushError):
            db.session.commit()
Beispiel #15
0
    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
Beispiel #16
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
Beispiel #17
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 #18
0
    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
Beispiel #19
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
Beispiel #20
0
    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
Beispiel #21
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 #22
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
Beispiel #23
0
    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
Beispiel #24
0
    def testDeleteScoreNonExistingPlayer(self):
        '''
        Test deleting score for non-existing 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.delete(url)

        assert response.status_code == 404, response.status_code
Beispiel #25
0
    def testGetNonExistingPlayerScore(self):
        """
        Error test when player and game exist, but the player score does not
        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")
        response = self.client.get(url)

        assert response.status_code == 404, response.status_code
Beispiel #26
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 #27
0
    def testGetGameEmptyScoreboard(self):
        '''
        Test getting scoreboard for a game with empty scoreboard
        Expects HTTP code 200 (OK) as response
        Expect response body contains empty item array
        '''
        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 = 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"]) == 0
Beispiel #28
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 #29
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()
Beispiel #30
0
    def testDeleteGame(self):
        """
        Test for successful game deletion
        Expects response with HTTP code 204 (No Content)
        Excepts that game is deleted from database
        """
        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")
        response = self.client.delete(url)

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