-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_topup_system.py
73 lines (60 loc) · 2.62 KB
/
test_topup_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
#
# 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.token import NativeToken, Token
from pdr_backend.ppss.topup_ss import TopupSS
from pdr_backend.ppss.web3_pp import Web3PP
from pdr_backend.util.web3_config import Web3Config
from pdr_backend.util.currency_types import Wei, Eth
def test_topup(caplog):
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"
)
mock_web3_config = Mock(spec=Web3Config)
mock_web3_config.w3 = Mock()
mock_web3_pp.web3_config = mock_web3_config
mock_web3_pp.web3_config.owner = "0xowner"
mock_token = MagicMock(spec=Token)
balances_arr = [Wei(int(5000 * 1e18))] + [Wei(int(5 * 1e18))] * 100
mock_token.balanceOf.side_effect = balances_arr
mock_token.transfer.return_value = True
mock_token.name = "OCEAN"
mock_token_rose = MagicMock(spec=NativeToken)
balances_arr = [Wei(int(5000 * 1e18))] + [Wei(int(5 * 1e18))] * 100
mock_token_rose.balanceOf.side_effect = balances_arr
mock_token_rose.transfer.return_value = True
mock_token_rose.name = "ROSE"
mock_web3_pp.OCEAN_Token = mock_token
mock_web3_pp.NativeToken = mock_token_rose
opf_addresses = {
"predictoor1": "0x1",
"predictoor2": "0x2",
}
topup_ss = MagicMock(spec=TopupSS)
topup_ss.all_topup_addresses.return_value = opf_addresses
topup_ss.get_min_bal.side_effect = [Eth(20), Eth(30), Eth(20), Eth(30)]
topup_ss.get_topup_bal.side_effect = [Eth(20), Eth(30), Eth(20), Eth(30)]
with patch("pdr_backend.ppss.ppss.Web3PP", return_value=mock_web3_pp), patch(
"pdr_backend.ppss.ppss.TopupSS", return_value=topup_ss
), patch("sys.exit"):
# Mock sys.argv
sys.argv = ["pdr", "topup", "ppss.yaml", "sapphire-testnet"]
cli_module._do_main()
addresses = opf_addresses
# Verifying outputs
for key, value in addresses.items():
assert f"{key}: 5.00 OCEAN, 5.00 ROSE" in caplog.text
if key.startswith("pred"):
assert f"Transferring 20 OCEAN to {value}..." in caplog.text
assert f"Transferring 30 ROSE to {value}..." in caplog.text
if key.startswith("dfbuyer"):
assert f"Transferring 250 ROSE to {value}..." in caplog.text
# Additional assertions
mock_token.transfer.assert_called()
mock_token.balanceOf.assert_called()