Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions tests/nanocontracts/test_contract_upgrade.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from hathor.nanocontracts import Blueprint, Context, fallback, public
from hathor.nanocontracts.exception import BlueprintDoesNotExist, NCFail, NCInvalidSyscall, NCMethodNotFound
from hathor.nanocontracts.types import BlueprintId, ContractId, NCAction, NCArgs
Expand Down Expand Up @@ -127,7 +129,7 @@ def test_basic(self) -> None:
assert isinstance(proxy_contract, ProxyBlueprint)

self.runner.call_public_method(proxy_id, 'set_contract', ctx, proxy_id)
with self.assertRaises(NCInvalidSyscall, match='cannot call the same blueprint'):
with pytest.raises(NCInvalidSyscall, match='cannot call the same blueprint'):
self.runner.call_public_method(proxy_id, 'inc', ctx)

self.runner.call_public_method(proxy_id, 'set_contract', ctx, code1_id)
Expand All @@ -154,7 +156,7 @@ def test_basic(self) -> None:
assert code2_contract.counter == 0
assert proxy_contract.counter == 1

with self.assertRaises(NCFail):
with pytest.raises(NCFail):
self.runner.call_public_method(proxy_id, 'upgrade', ctx, self.code3_bp_id, 'on_upgrade_fail')
assert proxy_storage.get_blueprint_id() == self.proxy_bp_id
assert proxy_contract.counter == 1
Expand All @@ -168,7 +170,7 @@ def test_basic(self) -> None:
assert proxy_contract.counter == 3

# it should invoke the fallback method which will fail calling `dec()` from code2's blueprint.
with self.assertRaises(NCMethodNotFound, match='method `dec` not found and no fallback is provided'):
with pytest.raises(NCMethodNotFound, match='method `dec` not found and no fallback is provided'):
self.runner.call_public_method(proxy_id, 'dec', ctx)
assert proxy_storage.get_blueprint_id() == self.proxy_bp_id
assert proxy_contract.contract == code2_id
Expand All @@ -177,7 +179,7 @@ def test_basic(self) -> None:
assert proxy_contract.counter == 3

unknown_bp_id = self.gen_random_blueprint_id()
with self.assertRaises(BlueprintDoesNotExist):
with pytest.raises(BlueprintDoesNotExist):
self.runner.call_public_method(proxy_id, 'upgrade_no_cb', ctx, unknown_bp_id)

self.runner.call_public_method(proxy_id, 'upgrade', ctx, self.code3_bp_id, 'on_upgrade_inc')
Expand Down
33 changes: 16 additions & 17 deletions tests/nanocontracts/test_rng.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from math import floor, sqrt

import pytest

from hathor.conf import HathorSettings
from hathor.nanocontracts import Blueprint, Context, public
from hathor.nanocontracts.catalog import NCBlueprintCatalog
Expand Down Expand Up @@ -71,41 +73,38 @@ def test_rng_override(self) -> None:
seed = b'0' * 32
rng = NanoRNG(seed=seed)

with self.assertRaises(AttributeError, match='Cannot assign methods to this object.'):
with pytest.raises(AttributeError, match='Cannot assign methods to this object.'):
rng.random = lambda self: 2 # type: ignore[method-assign, misc, assignment]

with self.assertRaises(AttributeError, match='Cannot assign methods to this object.'):
with pytest.raises(AttributeError, match='Cannot assign methods to this object.'):
setattr(rng, 'random', lambda self: 2)

with self.assertRaises(AttributeError, match='Cannot assign methods to this object.'):
with pytest.raises(AttributeError, match='Cannot assign methods to this object.'):
from types import MethodType
rng.random = MethodType(lambda self: 2, rng) # type: ignore[method-assign]

with self.assertRaises(AttributeError, match='\'NanoRNG\' object attribute \'random\' is read-only'):
with pytest.raises(AttributeError, match='\'NanoRNG\' object attribute \'random\' is read-only'):
object.__setattr__(rng, 'random', lambda self: 2)

with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'):
with pytest.raises(AttributeError, match='Cannot override method `random`'):
NanoRNG.random = lambda self: 2 # type: ignore[method-assign]

with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'):
with pytest.raises(AttributeError, match='Cannot override method `random`'):
setattr(NanoRNG, 'random', lambda self: 2)

with self.assertRaises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'):
with pytest.raises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'):
object.__setattr__(NanoRNG, 'random', lambda self: 2)

with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'):
with pytest.raises(AttributeError, match='Cannot override method `random`'):
rng.__class__.random = lambda self: 2 # type: ignore[method-assign]

with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'):
with pytest.raises(AttributeError, match='Cannot override method `random`'):
setattr(rng.__class__, 'random', lambda self: 2)

with self.assertRaises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'):
with pytest.raises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'):
object.__setattr__(rng.__class__, 'random', lambda self: 2)

# mypy incorrectly infers the type of `rng.random` as `Never` (leading to "Never not callable [misc]")
# due to the override attempts above, which are expected to fail at runtime but confuse static analysis.
# This is a false positive in the test context; use `reveal_type(rng.random)` to inspect the inferred type.
assert rng.random() < 1 # type: ignore[misc]
assert rng.random() < 1

def test_rng_shell_class(self) -> None:
seed = b'0' * 32
Expand All @@ -114,13 +113,13 @@ def test_rng_shell_class(self) -> None:

assert rng1.__class__ != rng2.__class__

with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'):
with pytest.raises(AttributeError, match='Cannot override method `random`'):
rng1.__class__.random = lambda self: 2 # type: ignore[method-assign]

with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'):
with pytest.raises(AttributeError, match='Cannot override method `random`'):
setattr(rng1.__class__, 'random', lambda self: 2)

with self.assertRaises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'):
with pytest.raises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'):
object.__setattr__(rng1.__class__, 'random', lambda self: 2)

def assertGoodnessOfFitTest(self, observed: list[int], expected: list[int]) -> None:
Expand Down
Loading