From 5737b830c2610117b21bbcf29ae5fb079bc09a17 Mon Sep 17 00:00:00 2001 From: trizin <25263018+trizin@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:33:36 +0300 Subject: [PATCH 1/3] Use a mock for pred submitter manager --- .../predictoor/test/test_predictoor_agent.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pdr_backend/predictoor/test/test_predictoor_agent.py b/pdr_backend/predictoor/test/test_predictoor_agent.py index 343b54e92..23d2c3e8e 100644 --- a/pdr_backend/predictoor/test/test_predictoor_agent.py +++ b/pdr_backend/predictoor/test/test_predictoor_agent.py @@ -8,7 +8,6 @@ from enforce_typing import enforce_types from numpy.testing import assert_array_equal -from pdr_backend.conftest_ganache import * # pylint: disable=wildcard-import from pdr_backend.ppss.ppss import PPSS from pdr_backend.ppss.predictoor_ss import PredictoorSS from pdr_backend.ppss.web3_pp import Web3PP @@ -30,22 +29,22 @@ @enforce_types -def test_predictoor_agent_main1(tmpdir, monkeypatch, pred_submitter_mgr): - _test_predictoor_agent_main(1, str(tmpdir), monkeypatch, pred_submitter_mgr) +def test_predictoor_agent_main1(tmpdir, monkeypatch): + _test_predictoor_agent_main(1, str(tmpdir), monkeypatch) @enforce_types -def test_predictoor_agent_main2(tmpdir, monkeypatch, pred_submitter_mgr): - _test_predictoor_agent_main(2, str(tmpdir), monkeypatch, pred_submitter_mgr) +def test_predictoor_agent_main2(tmpdir, monkeypatch): + _test_predictoor_agent_main(2, str(tmpdir), monkeypatch) -def test_predictoor_agent_main3(tmpdir, monkeypatch, pred_submitter_mgr): - _test_predictoor_agent_main(3, str(tmpdir), monkeypatch, pred_submitter_mgr) +def test_predictoor_agent_main3(tmpdir, monkeypatch): + _test_predictoor_agent_main(3, str(tmpdir), monkeypatch) @enforce_types def _test_predictoor_agent_main( - approach: int, tmpdir: str, monkeypatch, pred_submitter_mgr + approach: int, tmpdir: str, monkeypatch ): """ @description @@ -57,10 +56,11 @@ def _test_predictoor_agent_main( # mock tokens mock_token = Mock() mock_token.balanceOf.return_value = Eth(1000).to_wei() + pred_submitter_mgr = Mock() with patch("pdr_backend.ppss.web3_pp.Token", return_value=mock_token), patch( "pdr_backend.ppss.web3_pp.NativeToken", return_value=mock_token - ): + ), patch("pdr_backend.predictoor.predictoor_agent.PredSubmitterMgr", return_value=pred_submitter_mgr): _, ppss, _mock_pdr_contract = mock_ppss_1feed( approach, tmpdir, From fb936fa68604817750e91523927a3b0818e5a988 Mon Sep 17 00:00:00 2001 From: trizin <25263018+trizin@users.noreply.github.com> Date: Mon, 22 Apr 2024 11:50:01 +0000 Subject: [PATCH 2/3] Remove ganache dependency from predictoor tests --- .../predictoor/test/test_predictoor_agent.py | 57 ++++++++++++++----- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/pdr_backend/predictoor/test/test_predictoor_agent.py b/pdr_backend/predictoor/test/test_predictoor_agent.py index 23d2c3e8e..b718a93d4 100644 --- a/pdr_backend/predictoor/test/test_predictoor_agent.py +++ b/pdr_backend/predictoor/test/test_predictoor_agent.py @@ -29,22 +29,28 @@ @enforce_types -def test_predictoor_agent_main1(tmpdir, monkeypatch): - _test_predictoor_agent_main(1, str(tmpdir), monkeypatch) +def test_predictoor_agent_main1(tmpdir, monkeypatch, pred_submitter_mgr): + _test_predictoor_agent_main(1, str(tmpdir), monkeypatch, pred_submitter_mgr) @enforce_types -def test_predictoor_agent_main2(tmpdir, monkeypatch): - _test_predictoor_agent_main(2, str(tmpdir), monkeypatch) +def test_predictoor_agent_main2(tmpdir, monkeypatch, pred_submitter_mgr): + _test_predictoor_agent_main(2, str(tmpdir), monkeypatch, pred_submitter_mgr) -def test_predictoor_agent_main3(tmpdir, monkeypatch): - _test_predictoor_agent_main(3, str(tmpdir), monkeypatch) +def test_predictoor_agent_main3(tmpdir, monkeypatch, pred_submitter_mgr): + _test_predictoor_agent_main(3, str(tmpdir), monkeypatch, pred_submitter_mgr) +@pytest.fixture() +def pred_submitter_mgr(): + with patch("pdr_backend.predictoor.predictoor_agent.PredSubmitterMgr") as mock: + mock.submit_prediction.return_value = {"transactionHash": b"hello", "status": 1} + mock.contract_address = "0x123" + yield mock @enforce_types def _test_predictoor_agent_main( - approach: int, tmpdir: str, monkeypatch + approach: int, tmpdir: str, monkeypatch, pred_submitter_mgr ): """ @description @@ -56,25 +62,36 @@ def _test_predictoor_agent_main( # mock tokens mock_token = Mock() mock_token.balanceOf.return_value = Eth(1000).to_wei() - pred_submitter_mgr = Mock() with patch("pdr_backend.ppss.web3_pp.Token", return_value=mock_token), patch( "pdr_backend.ppss.web3_pp.NativeToken", return_value=mock_token - ), patch("pdr_backend.predictoor.predictoor_agent.PredSubmitterMgr", return_value=pred_submitter_mgr): + ): _, ppss, _mock_pdr_contract = mock_ppss_1feed( approach, tmpdir, monkeypatch, - pred_submitter_mgr=pred_submitter_mgr.contract_address, ) assert ppss.predictoor_ss.approach == approach ppss.predictoor_ss.d["pred_submitter_mgr"] = pred_submitter_mgr.contract_address + feed_contracts = ppss.web3_pp.query_feed_contracts() + web3_config = ppss.web3_pp.web3_config + w3 = ppss.web3_pp.w3 + mock_token = Mock() + mock_token.balanceOf.return_value = Eth(1000).to_wei() + ppss.web3_pp = MagicMock(spec=Web3PP) + ppss.web3_pp.OCEAN_Token = mock_token + ppss.web3_pp.NativeToken = mock_token + ppss.web3_pp.get_single_contract.return_value = _mock_pdr_contract + ppss.web3_pp.query_feed_contracts.return_value = feed_contracts + ppss.web3_pp.web3_config = web3_config + ppss.web3_pp.w3 = w3 # now we're done the mocking, time for the real work!! # real work: main iterations agent = PredictoorAgent(ppss) for _ in range(500): agent.take_step() + # log some final results for debubbing / inspection mock_w3 = ppss.web3_pp.web3_config.w3 @@ -202,6 +219,11 @@ def mock_build(*args, **kwargs): # pylint: disable=unused-argument # do prediction mock_model.aimodel_ss = aimodel_ss + + feed_contracts = ppss.web3_pp.query_feed_contracts() + ppss.web3_pp = Mock(spec=Web3PP) + ppss.web3_pp.query_feed_contracts.return_value = feed_contracts + agent = PredictoorAgent(ppss) feed = ppss.predictoor_ss.predict_train_feedsets[0] agent.calc_stakes2(feed) @@ -239,6 +261,9 @@ def mock_build(*args, **kwargs): # pylint: disable=unused-argument 2, str(tmpdir), monkeypatch, pred_submitter_mgr.contract_address ) assert ppss.predictoor_ss.approach == 2 + feed_contracts = ppss.web3_pp.query_feed_contracts() + ppss.web3_pp = Mock(spec=Web3PP) + ppss.web3_pp.query_feed_contracts.return_value = feed_contracts assert len(feeds) == 2 aimodel_ss = ppss.predictoor_ss.aimodel_ss @@ -298,15 +323,17 @@ def test_balance_check(tmpdir, monkeypatch, OCEAN, ROSE, expected, pred_submitte aimodel_ss = ppss.predictoor_ss.aimodel_ss mock_model.aimodel_ss = aimodel_ss - agent = PredictoorAgent(ppss) - + + feed_contracts = ppss.web3_pp.query_feed_contracts() mock_OCEAN = Mock() mock_OCEAN.balanceOf.return_value = OCEAN mock_ROSE = Mock() mock_ROSE.balanceOf.return_value = ROSE + ppss.web3_pp = Mock(spec=Web3PP) + ppss.web3_pp.OCEAN_Token = mock_OCEAN + ppss.web3_pp.NativeToken = mock_ROSE + ppss.web3_pp.query_feed_contracts.return_value = feed_contracts - agent.ppss.web3_pp = Mock(spec=Web3PP) - agent.ppss.web3_pp.OCEAN_Token = mock_OCEAN - agent.ppss.web3_pp.NativeToken = mock_ROSE + agent = PredictoorAgent(ppss) assert agent.check_balances(Eth(100)) == expected From 37449370a72ed96867b034582c64e00b376db6c2 Mon Sep 17 00:00:00 2001 From: trizin <25263018+trizin@users.noreply.github.com> Date: Mon, 22 Apr 2024 11:59:41 +0000 Subject: [PATCH 3/3] Formatting --- pdr_backend/predictoor/test/test_predictoor_agent.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pdr_backend/predictoor/test/test_predictoor_agent.py b/pdr_backend/predictoor/test/test_predictoor_agent.py index b718a93d4..9cd01aa5e 100644 --- a/pdr_backend/predictoor/test/test_predictoor_agent.py +++ b/pdr_backend/predictoor/test/test_predictoor_agent.py @@ -41,6 +41,7 @@ def test_predictoor_agent_main2(tmpdir, monkeypatch, pred_submitter_mgr): def test_predictoor_agent_main3(tmpdir, monkeypatch, pred_submitter_mgr): _test_predictoor_agent_main(3, str(tmpdir), monkeypatch, pred_submitter_mgr) + @pytest.fixture() def pred_submitter_mgr(): with patch("pdr_backend.predictoor.predictoor_agent.PredSubmitterMgr") as mock: @@ -48,6 +49,7 @@ def pred_submitter_mgr(): mock.contract_address = "0x123" yield mock + @enforce_types def _test_predictoor_agent_main( approach: int, tmpdir: str, monkeypatch, pred_submitter_mgr @@ -91,7 +93,6 @@ def _test_predictoor_agent_main( agent = PredictoorAgent(ppss) for _ in range(500): agent.take_step() - # log some final results for debubbing / inspection mock_w3 = ppss.web3_pp.web3_config.w3 @@ -323,7 +324,7 @@ def test_balance_check(tmpdir, monkeypatch, OCEAN, ROSE, expected, pred_submitte aimodel_ss = ppss.predictoor_ss.aimodel_ss mock_model.aimodel_ss = aimodel_ss - + feed_contracts = ppss.web3_pp.query_feed_contracts() mock_OCEAN = Mock() mock_OCEAN.balanceOf.return_value = OCEAN