Skip to content

Commit

Permalink
[Exchange Oracle] Use pytest.raises with match
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobronium committed Aug 17, 2024
1 parent ed28576 commit e2bfac3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,26 @@ def test_validate_escrow(self):
assert validation is None

def test_validate_escrow_invalid_address(self):
with pytest.raises(EscrowClientError) as error:
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
validate_escrow(chain_id, "invalid_address")

assert str(error.exception) == "Invalid escrow address: invalid_address"

def test_validate_escrow_invalid_status(self):
with patch("src.chain.escrow.EscrowUtils.get_escrow") as mock_function:
self.escrow_data.status = Status.Launched.name
mock_function.return_value = self.escrow_data
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(
ValueError,
match=rf"Escrow is not in any of the accepted states \(Pending\). "
f"Current state: {self.escrow_data.status}",
):
validate_escrow(chain_id, escrow_address)
assert (
f"Escrow is not in any of the accepted states (Pending). Current state: {self.escrow_data.status}"
== str(error.exception)
)

def test_validate_escrow_without_funds(self):
with patch("src.chain.escrow.EscrowUtils.get_escrow") as mock_function:
self.escrow_data.balance = "0"
mock_function.return_value = self.escrow_data
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(ValueError, match="Escrow doesn't have funds"):
validate_escrow(chain_id, escrow_address)
assert str(error.exception) == "Escrow doesn't have funds"

def test_get_escrow_manifest(self):
with (
Expand Down Expand Up @@ -118,9 +115,8 @@ def test_get_encrypted_escrow_manifest(self):
assert downloaded_manifest_content == original_manifest_content

def test_get_escrow_manifest_invalid_address(self):
with pytest.raises(EscrowClientError) as error:
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
get_escrow_manifest(chain_id, "invalid_address")
assert str(error.exception) == "Invalid escrow address: invalid_address"

def test_get_job_launcher_address(self):
with patch("src.chain.escrow.EscrowUtils.get_escrow") as mock_function:
Expand All @@ -130,21 +126,18 @@ def test_get_job_launcher_address(self):
assert job_launcher_address == JOB_LAUNCHER_ADDRESS

def test_get_job_launcher_address_invalid_address(self):
with pytest.raises(EscrowClientError) as error:
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
get_job_launcher_address(chain_id, "invalid_address")
assert str(error.exception) == "Invalid escrow address: invalid_address"

def test_get_job_launcher_address_invalid_chain_id(self):
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(ValueError, match="123 is not a valid ChainId"):
get_job_launcher_address(123, escrow_address)
assert str(error.exception) == "123 is not a valid ChainId"

def test_get_job_launcher_address_empty_escrow(self):
with patch("src.chain.escrow.EscrowUtils.get_escrow") as mock_function:
mock_function.return_value = None
with pytest.raises(Exception) as error: # noqa: PT011
with pytest.raises(Exception, match=f"Can't find escrow {ESCROW_ADDRESS}"):
get_job_launcher_address(chain_id, escrow_address)
assert f"Can't find escrow {ESCROW_ADDRESS}" == str(error.exception)

def test_get_recording_oracle_address(self):
with patch("src.chain.escrow.EscrowUtils.get_escrow") as mock_function:
Expand All @@ -155,18 +148,15 @@ def test_get_recording_oracle_address(self):
assert recording_oracle_address == RECORDING_ORACLE_ADDRESS

def test_get_recording_oracle_address_invalid_address(self):
with pytest.raises(EscrowClientError) as error:
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
get_recording_oracle_address(chain_id, "invalid_address")
assert str(error.exception) == "Invalid escrow address: invalid_address"

def test_get_recording_oracle_address_invalid_chain_id(self):
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(ValueError, match="123 is not a valid ChainId"):
get_recording_oracle_address(123, escrow_address)
assert str(error.exception) == "123 is not a valid ChainId"

def test_get_recording_oracle_address_empty_escrow(self):
with patch("src.chain.escrow.EscrowUtils.get_escrow") as mock_function:
mock_function.return_value = None
with pytest.raises(Exception) as error: # noqa: PT011
with pytest.raises(Exception, match=f"Can't find escrow {ESCROW_ADDRESS}"):
get_recording_oracle_address(chain_id, escrow_address)
assert f"Can't find escrow {ESCROW_ADDRESS}" == str(error.exception)
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ def test_get_job_launcher_url(self):
assert recording_url == DEFAULT_MANIFEST_URL

def test_get_job_launcher_url_invalid_escrow(self):
with pytest.raises(EscrowClientError) as error:
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
get_job_launcher_url(self.w3.eth.chain_id, "invalid_address")
assert str(error.exception) == "Invalid escrow address: invalid_address"

def test_get_job_launcher_url_invalid_recording_address(self):
with (
Expand All @@ -79,9 +78,8 @@ def test_get_recording_oracle_url(self):
assert recording_url == DEFAULT_MANIFEST_URL

def test_get_recording_oracle_url_invalid_escrow(self):
with pytest.raises(EscrowClientError) as error:
with pytest.raises(EscrowClientError, match="Invalid escrow address: invalid_address"):
get_recording_oracle_url(self.w3.eth.chain_id, "invalid_address")
assert str(error.exception) == "Invalid escrow address: invalid_address"

def test_get_recording_oracle_url_invalid_recording_address(self):
with (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def test_get_web3_localhost(self):
assert w3.manager._provider.endpoint_uri == LocalhostConfig.rpc_api

def test_get_web3_invalid_chain_id(self):
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(ValueError, match="1234 is not in available list of networks."):
get_web3(1234)
assert str(error.exception) == "1234 is not in available list of networks."

def test_sign_message_polygon(self):
with patch("src.chain.web3.get_web3") as mock_function:
Expand All @@ -83,9 +82,8 @@ def test_sign_message_amoy(self):
assert serialized_message == json.dumps("message")

def test_sign_message_invalid_chain_id(self):
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(ValueError, match="1234 is not in available list of networks."):
sign_message(1234, "message")
assert str(error.exception) == "1234 is not in available list of networks."

def test_recover_signer(self):
with patch("src.chain.web3.get_web3") as mock_function:
Expand All @@ -104,6 +102,5 @@ def test_validate_address(self):
assert address == DEFAULT_GAS_PAYER

def test_validate_address_invalid_address(self):
with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(ValueError, match="invalid_address is not a correct Web3 address"):
validate_address("invalid_address")
assert str(error.exception) == "invalid_address is not a correct Web3 address"
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,32 @@ def test_create_incoming_webhook_none_chain_id(self):
def test_create_incoming_webhook_none_event_type(self):
escrow_address = "0x1234567890123456789012345678901234567890"
signature = "signature"
with pytest.raises(AssertionError) as error:
with pytest.raises(
AssertionError,
match="'event' and 'event_type' cannot be used together. Please use only one of the fields",
):
webhook_service.inbox.create_webhook(
self.session,
escrow_address=escrow_address,
chain_id=None,
signature=signature,
type=OracleWebhookTypes.job_launcher,
)
assert (
str(error.exception)
== "'event' and 'event_type' cannot be used together. Please use only one of the fields"
)

def test_create_incoming_webhook_none_signature(self):
escrow_address = "0x1234567890123456789012345678901234567890"
chain_id = Networks.localhost.value

with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(
ValueError, match="Webhook signature must be specified for incoming events"
):
webhook_service.inbox.create_webhook(
self.session,
escrow_address=escrow_address,
chain_id=chain_id,
type=OracleWebhookTypes.job_launcher,
event_type=JobLauncherEventTypes.escrow_created.value,
)
assert str(error.exception) == "Webhook signature must be specified for incoming events"

def test_create_outgoing_webhook(self):
escrow_address = "0x1234567890123456789012345678901234567890"
Expand Down Expand Up @@ -156,24 +156,25 @@ def test_create_outgoing_webhook_none_chain_id(self):

def test_create_outgoing_webhook_none_event_type(self):
escrow_address = "0x1234567890123456789012345678901234567890"
with pytest.raises(AssertionError) as error:
with pytest.raises(
AssertionError,
match="'event' and 'event_type' cannot be used together. Please use only one of the fields",
):
webhook_service.outbox.create_webhook(
self.session,
escrow_address=escrow_address,
chain_id=None,
type=OracleWebhookTypes.exchange_oracle,
)
assert (
str(error.exception)
== "'event' and 'event_type' cannot be used together. Please use only one of the fields"
)

def test_create_outgoing_webhook_with_signature(self):
escrow_address = "0x1234567890123456789012345678901234567890"
chain_id = Networks.localhost.value
signature = "signature"

with pytest.raises(ValueError) as error: # noqa: PT011
with pytest.raises(
ValueError, match="Webhook signature must not be specified for outgoing events"
):
webhook_service.outbox.create_webhook(
self.session,
escrow_address=escrow_address,
Expand All @@ -182,7 +183,6 @@ def test_create_outgoing_webhook_with_signature(self):
event=ExchangeOracleEvent_TaskFinished(),
signature=signature,
)
assert str(error.exception) == "Webhook signature must not be specified for outgoing events"

def test_get_pending_webhooks(self):
chain_id = Networks.localhost.value
Expand Down

0 comments on commit e2bfac3

Please sign in to comment.