Skip to content

Commit

Permalink
Add middleware tests for __eq__ and test for unique key gen.
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Aug 28, 2024
1 parent 02c38ac commit d6f321e
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/core/middleware/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import pytest

from web3 import (
Web3,
)
from web3.exceptions import (
Web3ValueError,
)
from web3.middleware import (
FormattingMiddlewareBuilder,
Web3Middleware,
)


def test_middleware_class_eq_magic_method():
w3_a = Web3()
w3_b = Web3()

class TestMiddleware(Web3Middleware):
def request_processor(self, method, params):
return 1234

class TestMiddleware2(Web3Middleware):
def request_processor(self, method, params):
return 4321

mw1w3_a = TestMiddleware(w3_a)
assert mw1w3_a is not None
assert mw1w3_a != ""

mw1w3_a_equal = TestMiddleware(w3_a)
assert mw1w3_a == mw1w3_a_equal

mw2w3_a = TestMiddleware2(w3_a)
mw1w3_b = TestMiddleware(w3_b)
assert mw1w3_a != mw2w3_a
assert mw1w3_a != mw1w3_b


def test_unnamed_middleware_are_given_unique_keys(w3):
request_formatting_middleware = FormattingMiddlewareBuilder.build(
request_formatters={lambda x: x}
)
request_formatting_middleware2 = FormattingMiddlewareBuilder.build(
request_formatters={lambda x: x if x else None}
)
result_formatting_middleware = FormattingMiddlewareBuilder.build(
result_formatters={lambda x: x}
)
error_formatting_middleware = FormattingMiddlewareBuilder.build(
error_formatters={lambda x: x}
)

# adding different middleware should not cause an error
w3.middleware_onion.add(request_formatting_middleware)
w3.middleware_onion.add(request_formatting_middleware2)
w3.middleware_onion.add(result_formatting_middleware)
w3.middleware_onion.add(error_formatting_middleware)
assert isinstance(w3.eth.block_number, int)

with pytest.raises(Web3ValueError):
# adding the same middleware again should cause an error
w3.middleware_onion.add(request_formatting_middleware)

0 comments on commit d6f321e

Please sign in to comment.