Skip to content

Commit

Permalink
fix http requests in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guidopetri committed Sep 1, 2024
1 parent b8f0cf5 commit 566e19a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import lichess.api
import pytest


@pytest.fixture
def mock_task(mocker):
return mocker.MagicMock()


@pytest.fixture
def mocked_cloud_eval(mocker):
mocker.patch('lichess.api.cloud_eval',
side_effect=lichess.api.ApiHttpError(http_status=429,
url='https://link.com',
response_text='error',
)
)
22 changes: 12 additions & 10 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ def test_get_sf_evaluation_cloud(mocker):
assert rating == -0.3


def test_get_sf_evaluation_cloud_mate_in_x():
def test_get_sf_evaluation_cloud_mate_in_x(mocker):
mock_parsed_resp = {'pvs': [{'mate': 1}]}

mocker.patch('lichess.api.cloud_eval', return_value=mock_parsed_resp)

# this specific FEN is already evaluated by lichess
# scholar's mate
fen = 'r1bqkbnr/ppp2ppp/2np4/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 2 4'

Expand All @@ -250,15 +252,15 @@ def test_get_sf_evaluation_cloud_error(mocker):
transforms.get_sf_evaluation('fake fen', '', 1)


def test_get_sf_evaluation_local_returns_error(mocker, monkeypatch):
def test_get_sf_evaluation_local_returns_error(mocker, mocked_cloud_eval):
mocker.patch('stockfish.Stockfish')
mocker.patch('re.search', return_value=None)

with pytest.raises(SubprocessError):
transforms.get_sf_evaluation('', '', 1)


def test_get_sf_evaluation_shallow():
def test_get_sf_evaluation_shallow(mocked_cloud_eval):

fen = 'r1bq1rk1/1pp3b1/3p2np/nP2P1p1/4Pp2/PN3NP1/1B3PBP/R2Q1RK1 b - - 2 0'

Expand All @@ -280,7 +282,7 @@ def test_get_sf_evaluation_shallow():
assert rating == -0.89


def test_get_sf_evaluation_deep():
def test_get_sf_evaluation_deep(mocked_cloud_eval):

fen = 'r1bq1rk1/1pp3b1/3p2np/nP2P1p1/4Pp2/PN3NP1/1B3PBP/R2Q1RK1 b - - 2 0'

Expand All @@ -302,7 +304,7 @@ def test_get_sf_evaluation_deep():
assert rating == -0.89


def test_get_sf_evaluation_checkmate_black():
def test_get_sf_evaluation_checkmate_black(mocked_cloud_eval):

fen = '8/5q1k/7p/4Q2r/P3P3/4R1P1/7p/3R1r1K w - - 3 0'

Expand All @@ -317,7 +319,7 @@ def test_get_sf_evaluation_checkmate_black():
assert rating == -9999


def test_get_sf_evaluation_checkmate_white():
def test_get_sf_evaluation_checkmate_white(mocked_cloud_eval):

fen = '5rk1/4Q1b1/8/pp6/8/7N/1P2R1PK/8 w - - 1 0'

Expand All @@ -332,7 +334,7 @@ def test_get_sf_evaluation_checkmate_white():
assert rating == 9999


def test_get_sf_evaluation_in_stalemate():
def test_get_sf_evaluation_in_stalemate(mocked_cloud_eval):

fen = '3Q4/8/8/8/8/3QK2P/8/4k3 b - - 0 56'

Expand All @@ -347,7 +349,7 @@ def test_get_sf_evaluation_in_stalemate():
assert rating == 0


def test_get_sf_evaluation_in_checkmate():
def test_get_sf_evaluation_in_checkmate(mocked_cloud_eval):

fen = '4Rb1k/7Q/8/1p4N1/p7/8/1P4PK/8 b - - 4 0'

Expand All @@ -362,7 +364,7 @@ def test_get_sf_evaluation_in_checkmate():
assert rating == 9999


def test_get_sf_evaluation_double_checkmate():
def test_get_sf_evaluation_double_checkmate(mocked_cloud_eval):

fen = '6k1/4pppp/6r1/3b4/4r3/8/1Q5P/1R5K w - - 0 0'

Expand Down
9 changes: 3 additions & 6 deletions tests/vendors/test_stockfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ def mock_run_remote_sql_query(mocker):
)


@pytest.fixture
def mock_task(mocker):
return mocker.MagicMock()


def test_get_evals_on_checkmate_position(mock_run_remote_sql_query, mock_task):
def test_get_evals_on_checkmate_position(mock_run_remote_sql_query,
mock_task,
mocked_cloud_eval):
fen = 'rnb1k1nr/pp1p1ppp/4p3/8/8/1P2qN2/PBPKPbPP/RN1Q1B1R w kq - 2 7'

df = pd.DataFrame([[None, None, fen]],
Expand Down

0 comments on commit 566e19a

Please sign in to comment.