Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5 Move completely from unittest to pytest. #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions tests/test_chessgame.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from unittest import TestCase, mock

import chess
import chess.engine
import pytest
from flask import current_app
from pytest_mock import mocker

from chess_server.chessgame import Mediator
from chess_server.utils import User
from tests.utils import get_random_session_id


@pytest.mark.usefixtures("context")
class TestMediator(TestCase):
class TestMediator():
def setUp(self):
self.mock_engine_path = "engine_path"
self.mock_engine = mock.MagicMock()
self.mock_engine = mocker.MagicMock()

self.board = chess.Board()
self.color = chess.WHITE
Expand All @@ -23,7 +23,7 @@ def setUp(self):
self.mediator = Mediator()

# Mock the popen_uci method to return our mock engine
self.patcher = mock.patch(
self.patcher = mocker.patch(
"chess.engine.SimpleEngine.popen_uci",
return_value=self.mock_engine,
)
Expand All @@ -38,7 +38,7 @@ def test_activate_engine_with_arg_successful(self):
self.mock_popen_uci.assert_called_with(self.mock_engine_path)
self.assertEqual(self.mediator.engine, self.mock_engine)

@mock.patch("chess_server.chessgame.logger.error")
@mocker.patch("chess_server.chessgame.logger.error")
def test_activate_engine_with_arg_error(self, mock_logger):

# popen_uci method will raise exception when run
Expand All @@ -63,9 +63,9 @@ def test_activate_engine_from_config(self):
self.mock_popen_uci.assert_called_with(self.mock_engine_path)
self.assertEqual(self.mediator.engine, self.mock_engine)

@mock.patch("chess_server.chessgame.lan_to_speech")
@mock.patch("chess_server.chessgame.update_user")
@mock.patch("chess_server.chessgame.get_user")
@mocker.patch("chess_server.chessgame.lan_to_speech")
@mocker.patch("chess_server.chessgame.update_user")
@mocker.patch("chess_server.chessgame.get_user")
def test_play_engine_move_and_get_speech(
self, mock_get_user, mock_update_user, mock_lts
):
Expand All @@ -82,8 +82,8 @@ def test_play_engine_move_and_get_speech(
move=move, ponder=None
)

with mock.patch.object(self.board, "push") as mock_push:
with mock.patch.object(self.board, "lan", return_value=lan):
with mocker.patch.object(self.board, "push") as mock_push:
with mocker.patch.object(self.board, "lan", return_value=lan):
value = self.mediator.play_engine_move_and_get_speech(
session_id
)
Expand All @@ -98,8 +98,8 @@ def test_play_engine_move_and_get_speech(
) # DB was updated
self.assertEqual(value, "test reply") # Correctly reply was given

@mock.patch("chess_server.chessgame.update_user")
@mock.patch("chess_server.chessgame.get_user")
@mocker.patch("chess_server.chessgame.update_user")
@mocker.patch("chess_server.chessgame.get_user")
def test_play_lan_success(self, mock_get_user, mock_update_user):

session_id = get_random_session_id()
Expand All @@ -108,7 +108,7 @@ def test_play_lan_success(self, mock_get_user, mock_update_user):

mock_get_user.return_value = self.user

with mock.patch.object(self.board, "push") as mock_push:
with mocker.patch.object(self.board, "push") as mock_push:
# The board method which finally makes the move is `Board.push`
# even though the method being called directly is `Board.push_san`
value = self.mediator.play_lan(session_id, lan)
Expand All @@ -120,8 +120,8 @@ def test_play_lan_success(self, mock_get_user, mock_update_user):
) # DB updated
self.assertTrue(value) # Correct value returned

@mock.patch("chess_server.chessgame.update_user")
@mock.patch("chess_server.chessgame.get_user")
@mocker.patch("chess_server.chessgame.update_user")
@mocker.patch("chess_server.chessgame.get_user")
def test_play_lan_illegal_move(self, mock_get_user, mock_update_user):

session_id = get_random_session_id()
Expand Down
5 changes: 2 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import random
from unittest import TestCase, mock

import chess
from flask import url_for
Expand All @@ -26,7 +25,7 @@
)


class TestGetResultComment(TestCase):
class TestGetResultComment():
def test_get_result_comment_game_is_not_over_as_white(self):

user = User(board=chess.Board(), color=chess.WHITE)
Expand Down Expand Up @@ -275,7 +274,7 @@ def test_start_game_random(self, mocker):
value = start_game_and_get_response(session_id, color)

mock_create_user.assert_called_with(
session_id, board=chess.Board(), color=mock.ANY
session_id, board=chess.Board(), color=mocker.ANY
)
mock_random.assert_any_call([chess.WHITE, chess.BLACK])
mock_get_response.assert_called_once()
Expand Down