Skip to content

Commit d6f321e

Browse files
committed
Add middleware tests for __eq__ and test for unique key gen.
1 parent 02c38ac commit d6f321e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
3+
from web3 import (
4+
Web3,
5+
)
6+
from web3.exceptions import (
7+
Web3ValueError,
8+
)
9+
from web3.middleware import (
10+
FormattingMiddlewareBuilder,
11+
Web3Middleware,
12+
)
13+
14+
15+
def test_middleware_class_eq_magic_method():
16+
w3_a = Web3()
17+
w3_b = Web3()
18+
19+
class TestMiddleware(Web3Middleware):
20+
def request_processor(self, method, params):
21+
return 1234
22+
23+
class TestMiddleware2(Web3Middleware):
24+
def request_processor(self, method, params):
25+
return 4321
26+
27+
mw1w3_a = TestMiddleware(w3_a)
28+
assert mw1w3_a is not None
29+
assert mw1w3_a != ""
30+
31+
mw1w3_a_equal = TestMiddleware(w3_a)
32+
assert mw1w3_a == mw1w3_a_equal
33+
34+
mw2w3_a = TestMiddleware2(w3_a)
35+
mw1w3_b = TestMiddleware(w3_b)
36+
assert mw1w3_a != mw2w3_a
37+
assert mw1w3_a != mw1w3_b
38+
39+
40+
def test_unnamed_middleware_are_given_unique_keys(w3):
41+
request_formatting_middleware = FormattingMiddlewareBuilder.build(
42+
request_formatters={lambda x: x}
43+
)
44+
request_formatting_middleware2 = FormattingMiddlewareBuilder.build(
45+
request_formatters={lambda x: x if x else None}
46+
)
47+
result_formatting_middleware = FormattingMiddlewareBuilder.build(
48+
result_formatters={lambda x: x}
49+
)
50+
error_formatting_middleware = FormattingMiddlewareBuilder.build(
51+
error_formatters={lambda x: x}
52+
)
53+
54+
# adding different middleware should not cause an error
55+
w3.middleware_onion.add(request_formatting_middleware)
56+
w3.middleware_onion.add(request_formatting_middleware2)
57+
w3.middleware_onion.add(result_formatting_middleware)
58+
w3.middleware_onion.add(error_formatting_middleware)
59+
assert isinstance(w3.eth.block_number, int)
60+
61+
with pytest.raises(Web3ValueError):
62+
# adding the same middleware again should cause an error
63+
w3.middleware_onion.add(request_formatting_middleware)

0 commit comments

Comments
 (0)