Example #1
0
def test_create_user_multiple_entries(context):
    session_id1 = get_random_session_id()
    board1 = chess.Board()
    board1.push_san("Nf3")
    color1 = chess.BLACK

    session_id2 = get_random_session_id()
    board2 = chess.Board()
    board2.push_san("e4")
    board2.push_san("e5")
    color2 = chess.WHITE

    # Inserting one at a time
    create_user(session_id1, board1, color1)

    # Only one entry should be present
    assert UserModel.query.count() == 1
    assert UserModel.query.get(session_id1) is not None

    # Insert second entry
    create_user(session_id2, board2, color2)

    assert UserModel.query.count() == 2
    assert UserModel.query.get(session_id1) is not None
    assert UserModel.query.get(session_id2) is not None
Example #2
0
def test_delete_user_multiple_entries(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.WHITE

    create_user(session_id, board, color)

    # Create another user
    session_id2 = get_random_session_id()
    board2 = chess.Board()
    color2 = chess.BLACK

    board2.push_san("e4")
    board2.push_san("d5")

    create_user(session_id2, board2, color2)

    # Delete first user
    delete_user(session_id)

    assert exists_in_db(session_id) is False

    # Verify that no other changes were made
    assert UserModel.query.count() == 1
    assert get_user(session_id2) == User(board2, color2)
Example #3
0
def test_get_user(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.BLACK

    create_user(session_id, board, color)

    assert get_user(session_id) == User(board, color)
Example #4
0
    def test_undo_users_last_move_on_move_one(self, context, mocker):
        board = chess.Board()
        color = chess.BLACK
        board.push_san("e4")
        fen = board.fen()
        create_user(self.session_id, board, color)

        value = undo_users_last_move(self.session_id)

        assert value == []
        assert get_user(self.session_id).board.fen() == fen
Example #5
0
def test_create_user(context):
    # Creating a random session id of length 36
    session_id = get_random_session_id()

    # Generate arguments for create_user
    board = chess.Board()
    color = chess.WHITE

    create_user(session_id, board, color)

    # Only one user was created
    assert UserModel.query.count() == 1
    assert UserModel.query.get(session_id) is not None
Example #6
0
def test_update_user(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.BLACK

    create_user(session_id, board, color)

    # Now update it with some moves
    board.push_san("Nf3")
    board.push_san("d5")

    update_user(session_id, board)

    assert get_user(session_id) == User(board, color)
Example #7
0
def test_create_user_when_entry_with_key_already_exists(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.WHITE

    create_user(session_id, board, color)

    board.push_san("Nf3")
    board.push_san("c6")

    # Now trying to create another entry with same session_id
    with pytest.raises(
        Exception, match=f"Entry with key {session_id} already exists."
    ):
        create_user(session_id, board, color)
Example #8
0
def test_update_user_entry_does_not_exist(context):
    session_id = get_random_session_id()
    board = chess.Board()

    board.push_san("e4")
    board.push_san("e5")

    # Create another random entry
    session_id2 = get_random_session_id()
    board2 = chess.Board()
    color2 = chess.BLACK

    create_user(session_id2, board2, color2)

    with pytest.raises(Exception, match="Entry not found."):
        update_user(session_id, board)
Example #9
0
def test_delete_user(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.BLACK

    create_user(session_id, board, color)

    # Verify that the user has been created
    assert get_user(session_id) == User(board, color)

    delete_user(session_id)

    # Verify that deletion is successful
    assert exists_in_db(session_id) is False

    # Verify that no other operation has been performed
    assert UserModel.query.count() == 0
Example #10
0
    def test_undo_on_move_one(self, client):
        board = chess.Board()
        color = chess.BLACK
        board.push_san("e4")
        fen = board.fen()
        create_user(self.session_id, board, color)

        expected_text = "Nothing to undo!"

        req_data = get_dummy_webhook_request_for_google(
            session_id=self.session_id, action="undo", intent="undo")
        r = client.post(url_for("webhook_bp.webhook"), json=req_data)
        resp = GoogleWebhookResponse(r.json)

        assert get_user(self.session_id).board.fen() == fen
        assert expected_text in resp.simple_response.text_to_speech
        assert expected_text in resp.simple_response.display_text
Example #11
0
    def test_undo_users_last_move_game_ended_after_users_move(
            self, context, mocker):
        board = chess.Board()
        color = chess.WHITE

        for san in ["e4", "e5", "Qh5", "Nc6", "Bc4", "Nf6"]:
            board.push_san(san)

        fen = board.fen()
        board.push_san("Qxf7#")
        expected_undone = ["Qxf7#"]
        create_user(self.session_id, board, color)

        value = undo_users_last_move(self.session_id)

        assert value == expected_undone
        assert get_user(self.session_id).board.fen() == fen
Example #12
0
    def test_undo_users_last_move(self, context, mocker):
        board = chess.Board()
        color = chess.BLACK

        for san in ["e4", "e5", "Nf3", "Nc6", "Bc4"]:
            board.push_san(san)

        fen = board.fen()
        board.push_san("Bc5")
        board.push_san("b4")
        expected_undone = ["b4", "Bc5"]
        create_user(self.session_id, board, color)

        value = undo_users_last_move(self.session_id)

        assert value == expected_undone
        assert get_user(self.session_id).board.fen() == fen
Example #13
0
def test_get_user_when_multiple_entries_exist(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.WHITE

    # Make some moves
    board.push_san("Nc3")
    board.push_san("d5")
    board.push_san("d4")

    create_user(session_id, board, color)

    # Another entry
    session_id2 = get_random_session_id()
    board2 = chess.Board()
    color2 = chess.WHITE

    create_user(session_id2, board2, color2)

    assert get_user(session_id) == User(board, color)
Example #14
0
def start_game_and_get_response(session_id: str, color: str):
    """Initializes game given session and color"""

    if color == "white":
        create_user(session_id, board=chess.Board(), color=chess.WHITE)

    elif color == "black":
        create_user(session_id, board=chess.Board(), color=chess.BLACK)

    else:
        chosen = random.choice([chess.WHITE, chess.BLACK])
        color = "white" if chosen else "black"
        create_user(session_id, board=chess.Board(), color=chosen)

    output = f"Okay! You are playing with the {color} pieces."

    if color == "white":
        output += " Your turn."

    else:
        # Play engine's move and append that move's speech to output
        speech = mediator.play_engine_move_and_get_speech(
            session_id=session_id)
        output += f" My move is {speech}. {get_prompt_phrase()}"

    return get_response_for_google(textToSpeech=output)
Example #15
0
    def test_undo_game_ended_after_users_move(self, client):
        board = chess.Board()
        color = chess.WHITE

        for san in ["e4", "e5", "Qh5", "Nc6", "Bc4", "Nf6"]:
            board.push_san(san)

        fen = board.fen()
        board.push_san("Qxf7#")
        undone_move = "Qxf7#"
        create_user(self.session_id, board, color)

        expected_text = f"OK! Undid the move {undone_move}"

        req_data = get_dummy_webhook_request_for_google(
            session_id=self.session_id, action="undo", intent="undo")
        r = client.post(url_for("webhook_bp.webhook"), json=req_data)
        resp = GoogleWebhookResponse(r.json)

        assert get_user(self.session_id).board.fen() == fen
        assert expected_text in resp.simple_response.text_to_speech
        assert expected_text in resp.simple_response.display_text
Example #16
0
def test_update_user_multiple_entries(context):
    session_id = get_random_session_id()
    board = chess.Board()
    color = chess.WHITE

    create_user(session_id, board, color)

    # Create second user
    session_id2 = get_random_session_id()
    board2 = chess.Board()
    color2 = chess.BLACK

    board2.push_san("e4")
    board2.push_san("e5")

    create_user(session_id2, board2, color2)

    board.push_san("g4")
    update_user(session_id, board)

    assert get_user(session_id) == User(board, color)
    # Ensure that other entry has not been modified
    assert get_user(session_id2) == User(board2, color2)
Example #17
0
    def test_undo(self, client):
        board = chess.Board()
        color = chess.BLACK

        for san in ["e4", "e5", "Nf3", "Nc6", "Bc4"]:
            board.push_san(san)
        fen = board.fen()
        board.push_san("Bc5")
        board.push_san("b4")
        engine_move, user_move = "b4", "Bc5"
        create_user(self.session_id, board, color)

        expected_text = (
            f"Alright! The moves {user_move} and {engine_move} have been "
            "undone.")

        req_data = get_dummy_webhook_request_for_google(
            session_id=self.session_id, action="undo", intent="undo")
        r = client.post(url_for("webhook_bp.webhook"), json=req_data)
        resp = GoogleWebhookResponse(r.json)

        assert get_user(self.session_id).board.fen() == fen
        assert expected_text in resp.simple_response.text_to_speech
        assert expected_text in resp.simple_response.display_text