-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_dfbuyer_agent_system.py
101 lines (87 loc) · 3.8 KB
/
test_dfbuyer_agent_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#
# Copyright 2024 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
import sys
from unittest.mock import Mock, patch, MagicMock
from pdr_backend.cli import cli_module
from pdr_backend.contract.predictoor_batcher import PredictoorBatcher
from pdr_backend.ppss.web3_pp import Web3PP
from pdr_backend.subgraph.subgraph_feed import SubgraphFeed
from pdr_backend.util.constants import SAPPHIRE_MAINNET_CHAINID
from pdr_backend.util.web3_config import Web3Config
@patch("pdr_backend.dfbuyer.dfbuyer_agent.wait_until_subgraph_syncs")
@patch("pdr_backend.dfbuyer.dfbuyer_agent.time.sleep")
def test_dfbuyer_agent(mock_wait_until_subgraph_syncs, mock_time_sleep, caplog):
_ = mock_wait_until_subgraph_syncs
mock_web3_pp = MagicMock(spec=Web3PP)
mock_web3_pp.network = "sapphire-mainnet"
mock_web3_pp.subgraph_url = (
"http://localhost:8000/subgraphs/name/oceanprotocol/ocean-subgraph"
)
feeds = {
"0x1": SubgraphFeed(
"Feed: binance | BTC/USDT | 5m",
"0x1",
"BTC",
100,
300,
"0xf",
"BTC/USDT",
"5m",
"binance",
)
}
mock_web3_pp.query_feed_contracts.return_value = feeds
mock_token = MagicMock()
mock_token.balanceOf.return_value = 100 * 1e18
mock_token.allowance.return_value = 1e128
mock_web3_pp.get_address.return_value = "0x1"
mock_web3_pp.OCEAN_Token = mock_token
mock_web3_config = Mock(spec=Web3Config)
mock_web3_config.get_block.return_value = {"timestamp": 100}
mock_web3_config.owner = "0x00000000000000000000000000000000000c0ffe"
mock_web3_config.w3 = Mock()
mock_web3_config.w3.eth.block_number = 100
mock_web3_config.w3.eth.chain_id = SAPPHIRE_MAINNET_CHAINID
mock_web3_config.w3.to_checksum_address.return_value = "0x1"
mock_web3_config.get_current_timestamp.return_value = 100
mock_web3_pp.web3_config = mock_web3_config
mock_get_consume_so_far_per_contract = Mock()
mock_get_consume_so_far_per_contract.return_value = {"0x1": 120}
mock_predictoor_batcher = Mock(spec=PredictoorBatcher)
mock_predictoor_batcher.contract_address = "0xpredictoor_batcher"
mock_predictoor_batcher.consume_multiple.return_value = {
"transactionHash": b"0xbatch_submit_tx",
"status": 1,
}
with patch("pdr_backend.ppss.ppss.Web3PP", return_value=mock_web3_pp), patch(
"pdr_backend.dfbuyer.dfbuyer_agent.PredictoorBatcher",
return_value=mock_predictoor_batcher,
), patch(
"pdr_backend.dfbuyer.dfbuyer_agent.get_consume_so_far_per_contract",
mock_get_consume_so_far_per_contract,
), patch(
"pdr_backend.dfbuyer.dfbuyer_agent.DFBuyerAgent._get_prices",
return_value={"0x1": 100},
):
# Mock sys.argv
sys.argv = ["pdr", "dfbuyer", "ppss.yaml", "development"]
cli_module._do_main()
# Verifying outputs
assert "pdr dfbuyer: Begin" in caplog.text
assert "Arguments:" in caplog.text
assert "PPSS_FILE=ppss.yaml" in caplog.text
assert "NETWORK=development" in caplog.text
assert "Feed: 5m binance BTC/USDT 0x1" in caplog.text
assert "Checking allowance..." in caplog.text
assert "Taking step for timestamp: 100" in caplog.text
assert "Missing consume amounts: %s" % {"0x1": 5165.714285714285} in caplog.text
assert "Missing consume times: %s" % {"0x1": 52} in caplog.text
assert "Processing 3 batches" in caplog.text
assert "Consuming contracts ['0x1'] for [20] times." in caplog.text
# Additional assertions
mock_web3_pp.query_feed_contracts.assert_called()
mock_predictoor_batcher.consume_multiple.assert_called()
mock_time_sleep.assert_called()
mock_get_consume_so_far_per_contract.assert_called()