Skip to content

Commit 04261a3

Browse files
committed
Update tests
1 parent a64574b commit 04261a3

File tree

5 files changed

+56
-117
lines changed

5 files changed

+56
-117
lines changed

packages/examples/cvat/exchange-oracle/tests/integration/chain/test_escrow.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from src.core.types import OracleWebhookTypes
1616

1717
from tests.utils.constants import (
18-
DEFAULT_MANIFEST_URL,
1918
ESCROW_ADDRESS,
2019
EXCHANGE_ORACLE_ADDRESS,
2120
FACTORY_ADDRESS,
@@ -48,7 +47,6 @@ def setUp(self):
4847
token=TOKEN_ADDRESS,
4948
total_funded_amount=1000,
5049
created_at="",
51-
manifest_url=DEFAULT_MANIFEST_URL,
5250
recording_oracle=RECORDING_ORACLE_ADDRESS,
5351
exchange_oracle=EXCHANGE_ORACLE_ADDRESS,
5452
reputation_oracle=REPUTATION_ORACLE_ADDRESS,

packages/examples/cvat/exchange-oracle/tests/integration/chain/test_kvstore.py

Lines changed: 21 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import unittest
2-
from unittest.mock import MagicMock, Mock, patch
1+
from unittest.mock import Mock, patch
32

43
import pytest
54
from human_protocol_sdk.constants import ChainId, Status
@@ -20,14 +19,16 @@
2019
FACTORY_ADDRESS,
2120
JOB_LAUNCHER_ADDRESS,
2221
RECORDING_ORACLE_ADDRESS,
22+
REPUTATION_ORACLE_ADDRESS,
2323
TOKEN_ADDRESS,
2424
)
2525

2626
escrow_address = ESCROW_ADDRESS
2727

2828

29-
class ServiceIntegrationTest(unittest.TestCase):
30-
def setUp(self):
29+
class ServiceIntegrationTest:
30+
@pytest.fixture(autouse=True)
31+
def setup(self):
3132
self.w3 = Mock()
3233
self.w3.eth.chain_id = ChainId.LOCALHOST.value
3334
self.escrow_data = EscrowData(
@@ -43,59 +44,30 @@ def setUp(self):
4344
token=TOKEN_ADDRESS,
4445
total_funded_amount=1000,
4546
created_at="",
46-
manifest_url=DEFAULT_MANIFEST_URL,
4747
recording_oracle=RECORDING_ORACLE_ADDRESS,
48+
reputation_oracle=REPUTATION_ORACLE_ADDRESS,
4849
)
4950

50-
def test_get_job_launcher_url(self):
51+
@pytest.mark.parametrize(
52+
"get_url", [get_reputation_oracle_url, get_job_launcher_url, get_recording_oracle_url]
53+
)
54+
def test_get_oracle_url(self, get_url):
5155
with (
5256
patch("src.chain.kvstore.get_escrow") as mock_escrow,
53-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
57+
patch("src.chain.kvstore.get_web3", return_value=self.w3),
58+
patch("src.chain.kvstore.KVStoreClient.get") as mock_kvstore_get,
5459
):
5560
mock_escrow.return_value = self.escrow_data
56-
mock_operator.return_value = MagicMock(webhook_url=DEFAULT_MANIFEST_URL)
57-
recording_url = get_job_launcher_url(self.w3.eth.chain_id, escrow_address)
58-
assert recording_url == DEFAULT_MANIFEST_URL
59-
60-
def test_get_job_launcher_url_invalid_escrow(self):
61-
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
62-
get_job_launcher_url(self.w3.eth.chain_id, "invalid_address")
63-
64-
def test_get_job_launcher_url_invalid_recording_address(self):
65-
with (
66-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
67-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
68-
):
69-
mock_escrow.return_value = self.escrow_data
70-
mock_operator.return_value = MagicMock(webhook_url="")
71-
recording_url = get_job_launcher_url(self.w3.eth.chain_id, escrow_address)
72-
assert recording_url == ""
73-
74-
def test_get_recording_oracle_url(self):
75-
with (
76-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
77-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
78-
):
79-
self.escrow_data.recording_oracle = RECORDING_ORACLE_ADDRESS
80-
mock_escrow.return_value = self.escrow_data
81-
mock_operator.return_value = MagicMock(webhook_url=DEFAULT_MANIFEST_URL)
82-
recording_url = get_recording_oracle_url(self.w3.eth.chain_id, escrow_address)
83-
assert recording_url == DEFAULT_MANIFEST_URL
84-
85-
def test_get_recording_oracle_url_invalid_escrow(self):
61+
mock_kvstore_get.return_value = DEFAULT_MANIFEST_URL
62+
actual_url = get_url(self.w3.eth.chain_id, escrow_address)
63+
assert actual_url == DEFAULT_MANIFEST_URL
64+
65+
@pytest.mark.parametrize(
66+
"get_url", [get_reputation_oracle_url, get_job_launcher_url, get_recording_oracle_url]
67+
)
68+
def test_get_oracle_url_invalid_escrow(self, get_url):
8669
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
87-
get_recording_oracle_url(self.w3.eth.chain_id, "invalid_address")
88-
89-
def test_get_recording_oracle_url_invalid_recording_address(self):
90-
with (
91-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
92-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
93-
):
94-
self.escrow_data.recording_oracle = RECORDING_ORACLE_ADDRESS
95-
mock_escrow.return_value = self.escrow_data
96-
mock_operator.return_value = MagicMock(webhook_url="")
97-
recording_url = get_recording_oracle_url(self.w3.eth.chain_id, escrow_address)
98-
assert recording_url == ""
70+
get_url(self.w3.eth.chain_id, "invalid_address")
9971

10072
def test_store_public_key(self):
10173
PGP_PUBLIC_KEY_URL_1 = "http://pgp-public-key-url-1"
@@ -199,25 +171,3 @@ def set_file_url_and_hash(url: str, key: str):
199171
)
200172
== PGP_PUBLIC_KEY_URL_2
201173
)
202-
203-
def test_get_reputation_oracle_url_config_url(self):
204-
with patch(
205-
"src.chain.kvstore.Config.localhost.reputation_oracle_url", DEFAULT_MANIFEST_URL
206-
):
207-
reputation_url = get_reputation_oracle_url(self.w3.eth.chain_id, escrow_address)
208-
assert reputation_url == DEFAULT_MANIFEST_URL
209-
210-
def test_get_reputation_oracle_url_from_escrow(self):
211-
with (
212-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
213-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
214-
patch("src.chain.kvstore.Config.localhost.reputation_oracle_url", None),
215-
):
216-
mock_escrow.return_value = self.escrow_data
217-
mock_operator.return_value = MagicMock(webhook_url=DEFAULT_MANIFEST_URL)
218-
reputation_url = get_reputation_oracle_url(self.w3.eth.chain_id, escrow_address)
219-
assert reputation_url == DEFAULT_MANIFEST_URL
220-
221-
def test_get_reputation_oracle_url_invalid_escrow(self):
222-
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid address"):
223-
get_reputation_oracle_url(self.w3.eth.chain_id, "invalid address")

packages/examples/cvat/exchange-oracle/tests/integration/cron/test_process_job_launcher_webhooks.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uuid
44
from unittest.mock import MagicMock, Mock, call, patch
55

6-
from human_protocol_sdk.constants import ChainId, Status
6+
from human_protocol_sdk.constants import Status
77
from sqlalchemy.sql import select
88

99
from src.core.storage import compose_data_bucket_prefix, compose_results_bucket_prefix
@@ -30,7 +30,7 @@
3030
from src.services.cloud import StorageClient
3131
from src.services.webhook import OracleWebhookDirectionTags
3232

33-
from tests.utils.constants import DEFAULT_MANIFEST_URL, ESCROW_ADDRESS, JOB_LAUNCHER_ADDRESS
33+
from tests.utils.constants import DEFAULT_MANIFEST_URL, ESCROW_ADDRESS
3434
from tests.utils.dataset_helpers import build_gt_dataset
3535
from tests.utils.db_helper import create_project_task_and_job
3636

@@ -629,17 +629,14 @@ def test_process_outgoing_job_launcher_webhooks(self):
629629

630630
self.session.add(webhook)
631631
self.session.commit()
632+
632633
with (
633-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
634-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
634+
patch(
635+
"src.crons.webhooks.job_launcher.get_job_launcher_url",
636+
return_value=DEFAULT_MANIFEST_URL,
637+
),
635638
patch("httpx.Client.post") as mock_httpx_post,
636639
):
637-
w3 = Mock()
638-
w3.eth.chain_id = ChainId.LOCALHOST.value
639-
mock_escrow_data = Mock()
640-
mock_escrow_data.launcher = JOB_LAUNCHER_ADDRESS
641-
mock_escrow.return_value = mock_escrow_data
642-
mock_operator.return_value = MagicMock(webhook_url=DEFAULT_MANIFEST_URL)
643640
mock_response = MagicMock()
644641
mock_response.raise_for_status.return_value = None
645642
mock_httpx_post.return_value = mock_response

packages/examples/cvat/exchange-oracle/tests/integration/cron/test_process_recording_oracle_webhooks.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import unittest
22
import uuid
33
from datetime import timedelta
4-
from unittest.mock import MagicMock, Mock, patch
4+
from unittest.mock import MagicMock, patch
55

6-
from human_protocol_sdk.constants import ChainId
76
from sqlalchemy.sql import select
87

98
from src.core.types import (
@@ -30,7 +29,7 @@
3029
from src.services.webhook import OracleWebhookDirectionTags
3130
from src.utils.time import utcnow
3231

33-
from tests.utils.constants import DEFAULT_MANIFEST_URL, RECORDING_ORACLE_ADDRESS
32+
from tests.utils.constants import DEFAULT_MANIFEST_URL
3433
from tests.utils.db_helper import create_project_task_and_job
3534

3635
escrow_address = "0x86e83d346041E8806e352681f3F14549C0d2BC67"
@@ -437,17 +436,14 @@ def test_process_outgoing_recording_oracle_webhooks(self):
437436

438437
self.session.add(webhook)
439438
self.session.commit()
439+
440440
with (
441-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
442-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
441+
patch(
442+
"src.crons.webhooks.recording_oracle.get_recording_oracle_url",
443+
return_value=DEFAULT_MANIFEST_URL,
444+
),
443445
patch("httpx.Client.post") as mock_httpx_post,
444446
):
445-
w3 = Mock()
446-
w3.eth.chain_id = ChainId.LOCALHOST.value
447-
mock_escrow_data = Mock()
448-
mock_escrow_data.recording_oracle = RECORDING_ORACLE_ADDRESS
449-
mock_escrow.return_value = mock_escrow_data
450-
mock_operator.return_value = MagicMock(webhook_url=DEFAULT_MANIFEST_URL)
451447
mock_response = MagicMock()
452448
mock_response.raise_for_status.return_value = None
453449
mock_httpx_post.return_value = mock_response

packages/examples/cvat/recording-oracle/tests/integration/chain/test_kvstore.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
import unittest
2-
from unittest.mock import MagicMock, Mock, patch
1+
from unittest.mock import Mock, patch
32

43
import pytest
54
from human_protocol_sdk.constants import ChainId, Status
65
from human_protocol_sdk.escrow import EscrowClientError, EscrowData
76
from human_protocol_sdk.kvstore import KVStoreClientError, KVStoreUtils
87

9-
from src.chain.kvstore import get_reputation_oracle_url, register_in_kvstore
8+
from src.chain.kvstore import (
9+
get_exchange_oracle_url,
10+
get_reputation_oracle_url,
11+
register_in_kvstore,
12+
)
1013
from src.core.config import LocalhostConfig
1114

1215
from tests.utils.constants import (
1316
DEFAULT_MANIFEST_URL,
1417
ESCROW_ADDRESS,
18+
EXCHANGE_ORACLE_ADDRESS,
1519
FACTORY_ADDRESS,
1620
JOB_LAUNCHER_ADDRESS,
1721
REPUTATION_ORACLE_ADDRESS,
18-
REPUTATION_ORACLE_WEBHOOK_URL,
1922
TOKEN_ADDRESS,
2023
)
2124

2225
escrow_address = ESCROW_ADDRESS
2326

2427

25-
class ServiceIntegrationTest(unittest.TestCase):
26-
def setUp(self):
28+
class ServiceIntegrationTest:
29+
@pytest.fixture(autouse=True)
30+
def setup(self):
2731
self.w3 = Mock()
2832
self.w3.eth.chain_id = ChainId.LOCALHOST.value
2933
self.escrow_data = EscrowData(
@@ -40,32 +44,26 @@ def setUp(self):
4044
total_funded_amount=1000,
4145
created_at="",
4246
manifest_url=DEFAULT_MANIFEST_URL,
47+
exchange_oracle=EXCHANGE_ORACLE_ADDRESS,
4348
reputation_oracle=REPUTATION_ORACLE_ADDRESS,
4449
)
4550

46-
def test_get_reputation_oracle_url(self):
51+
@pytest.mark.parametrize("get_url", [get_reputation_oracle_url, get_exchange_oracle_url])
52+
def test_get_oracle_url(self, get_url):
4753
with (
4854
patch("src.chain.kvstore.get_escrow") as mock_escrow,
49-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
55+
patch("src.chain.kvstore.get_web3", return_value=self.w3),
56+
patch("src.chain.kvstore.KVStoreClient.get") as mock_kvstore_get,
5057
):
5158
mock_escrow.return_value = self.escrow_data
52-
mock_operator.return_value = MagicMock(webhook_url=REPUTATION_ORACLE_WEBHOOK_URL)
53-
reputation_url = get_reputation_oracle_url(self.w3.eth.chain_id, escrow_address)
54-
assert reputation_url == REPUTATION_ORACLE_WEBHOOK_URL
59+
mock_kvstore_get.return_value = DEFAULT_MANIFEST_URL
60+
actual_url = get_url(self.w3.eth.chain_id, escrow_address)
61+
assert actual_url == DEFAULT_MANIFEST_URL
5562

56-
def test_get_reputation_oracle_url_invalid_escrow(self):
63+
@pytest.mark.parametrize("get_url", [get_reputation_oracle_url, get_exchange_oracle_url])
64+
def test_get_oracle_url_invalid_escrow(self, get_url):
5765
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
58-
get_reputation_oracle_url(self.w3.eth.chain_id, "invalid_address")
59-
60-
def test_get_reputation_oracle_url_invalid_address(self):
61-
with (
62-
patch("src.chain.kvstore.get_escrow") as mock_escrow,
63-
patch("src.chain.kvstore.OperatorUtils.get_operator") as mock_operator,
64-
):
65-
mock_escrow.return_value = self.escrow_data
66-
mock_operator.return_value = MagicMock(webhook_url="")
67-
recording_url = get_reputation_oracle_url(self.w3.eth.chain_id, escrow_address)
68-
assert recording_url == ""
66+
get_url(self.w3.eth.chain_id, "invalid_address")
6967

7068
def test_store_public_key(self):
7169
PGP_PUBLIC_KEY_URL_1 = "http://pgp-public-key-url-1"

0 commit comments

Comments
 (0)