|
| 1 | +import pytest |
| 2 | + |
| 3 | +from web3 import ( |
| 4 | + EthereumTesterProvider, |
| 5 | + Web3, |
| 6 | +) |
| 7 | +from web3.datastructures import ( |
| 8 | + AttributeDict, |
| 9 | +) |
| 10 | +from web3.middleware import ( |
| 11 | + async_attrdict_middleware, |
| 12 | + async_construct_result_generator_middleware, |
| 13 | + attrdict_middleware, |
| 14 | + construct_result_generator_middleware, |
| 15 | +) |
| 16 | +from web3.providers.eth_tester import ( |
| 17 | + AsyncEthereumTesterProvider, |
| 18 | +) |
| 19 | +from web3.types import ( |
| 20 | + RPCEndpoint, |
| 21 | +) |
| 22 | + |
| 23 | +GENERATED_NESTED_DICT_RESULT = { |
| 24 | + "result": { |
| 25 | + "a": 1, |
| 26 | + "b": { |
| 27 | + "b1": 1, |
| 28 | + "b2": {"b2a": 1, "b2b": {"b2b1": 1, "b2b2": {"test": "fin"}}}, |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def _assert_dict_and_not_attrdict(value): |
| 35 | + assert not isinstance(value, AttributeDict) |
| 36 | + assert isinstance(value, dict) |
| 37 | + |
| 38 | + |
| 39 | +def test_attrdict_middleware_default_for_ethereum_tester_provider(): |
| 40 | + w3 = Web3(EthereumTesterProvider()) |
| 41 | + assert w3.middleware_onion.get("attrdict") == attrdict_middleware |
| 42 | + |
| 43 | + |
| 44 | +def test_attrdict_middleware_is_recursive(w3): |
| 45 | + w3.middleware_onion.inject( |
| 46 | + construct_result_generator_middleware( |
| 47 | + {RPCEndpoint("fake_endpoint"): lambda *_: GENERATED_NESTED_DICT_RESULT} |
| 48 | + ), |
| 49 | + "result_gen", |
| 50 | + layer=0, |
| 51 | + ) |
| 52 | + response = w3.manager.request_blocking("fake_endpoint", []) |
| 53 | + |
| 54 | + result = response["result"] |
| 55 | + assert isinstance(result, AttributeDict) |
| 56 | + assert response.result == result |
| 57 | + |
| 58 | + assert isinstance(result["b"], AttributeDict) |
| 59 | + assert result.b == result["b"] |
| 60 | + assert isinstance(result.b["b2"], AttributeDict) |
| 61 | + assert result.b.b2 == result.b["b2"] |
| 62 | + assert isinstance(result.b.b2["b2b"], AttributeDict) |
| 63 | + assert result.b.b2.b2b == result.b.b2["b2b"] |
| 64 | + assert isinstance(result.b.b2.b2b["b2b2"], AttributeDict) |
| 65 | + assert result.b.b2.b2b.b2b2 == result.b.b2.b2b["b2b2"] |
| 66 | + |
| 67 | + # cleanup |
| 68 | + w3.middleware_onion.remove("result_gen") |
| 69 | + |
| 70 | + |
| 71 | +def test_no_attrdict_middleware_does_not_convert_dicts_to_attrdict(): |
| 72 | + w3 = Web3(EthereumTesterProvider()) |
| 73 | + |
| 74 | + w3.middleware_onion.inject( |
| 75 | + construct_result_generator_middleware( |
| 76 | + {RPCEndpoint("fake_endpoint"): lambda *_: GENERATED_NESTED_DICT_RESULT} |
| 77 | + ), |
| 78 | + "result_gen", |
| 79 | + layer=0, |
| 80 | + ) |
| 81 | + |
| 82 | + # remove attrdict middleware |
| 83 | + w3.middleware_onion.remove("attrdict") |
| 84 | + |
| 85 | + response = w3.manager.request_blocking("fake_endpoint", []) |
| 86 | + |
| 87 | + result = response["result"] |
| 88 | + |
| 89 | + _assert_dict_and_not_attrdict(result) |
| 90 | + _assert_dict_and_not_attrdict(result["b"]) |
| 91 | + _assert_dict_and_not_attrdict(result["b"]["b2"]) |
| 92 | + _assert_dict_and_not_attrdict(result["b"]["b2"]["b2b"]) |
| 93 | + _assert_dict_and_not_attrdict(result["b"]["b2"]["b2b"]["b2b2"]) |
| 94 | + |
| 95 | + |
| 96 | +# --- async --- # |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.asyncio |
| 100 | +async def test_async_attrdict_middleware_default_for_async_ethereum_tester_provider(): |
| 101 | + async_w3 = Web3(AsyncEthereumTesterProvider()) |
| 102 | + assert async_w3.middleware_onion.get("attrdict") == async_attrdict_middleware |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_async_attrdict_middleware_is_recursive(async_w3): |
| 107 | + async_w3.middleware_onion.inject( |
| 108 | + await async_construct_result_generator_middleware( |
| 109 | + {RPCEndpoint("fake_endpoint"): lambda *_: GENERATED_NESTED_DICT_RESULT} |
| 110 | + ), |
| 111 | + "result_gen", |
| 112 | + layer=0, |
| 113 | + ) |
| 114 | + response = await async_w3.manager.coro_request("fake_endpoint", []) |
| 115 | + |
| 116 | + result = response["result"] |
| 117 | + assert isinstance(result, AttributeDict) |
| 118 | + assert response.result == result |
| 119 | + |
| 120 | + assert isinstance(result["b"], AttributeDict) |
| 121 | + assert result.b == result["b"] |
| 122 | + assert isinstance(result.b["b2"], AttributeDict) |
| 123 | + assert result.b.b2 == result.b["b2"] |
| 124 | + assert isinstance(result.b.b2["b2b"], AttributeDict) |
| 125 | + assert result.b.b2.b2b == result.b.b2["b2b"] |
| 126 | + assert isinstance(result.b.b2.b2b["b2b2"], AttributeDict) |
| 127 | + assert result.b.b2.b2b.b2b2 == result.b.b2.b2b["b2b2"] |
| 128 | + |
| 129 | + # cleanup |
| 130 | + async_w3.middleware_onion.remove("result_gen") |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.asyncio |
| 134 | +async def test_no_async_attrdict_middleware_does_not_convert_dicts_to_attrdict(): |
| 135 | + async_w3 = Web3(AsyncEthereumTesterProvider()) |
| 136 | + |
| 137 | + async_w3.middleware_onion.inject( |
| 138 | + await async_construct_result_generator_middleware( |
| 139 | + {RPCEndpoint("fake_endpoint"): lambda *_: GENERATED_NESTED_DICT_RESULT} |
| 140 | + ), |
| 141 | + "result_gen", |
| 142 | + layer=0, |
| 143 | + ) |
| 144 | + |
| 145 | + # remove attrdict middleware |
| 146 | + async_w3.middleware_onion.remove("attrdict") |
| 147 | + |
| 148 | + response = await async_w3.manager.coro_request("fake_endpoint", []) |
| 149 | + |
| 150 | + result = response["result"] |
| 151 | + |
| 152 | + _assert_dict_and_not_attrdict(result) |
| 153 | + _assert_dict_and_not_attrdict(result["b"]) |
| 154 | + _assert_dict_and_not_attrdict(result["b"]["b2"]) |
| 155 | + _assert_dict_and_not_attrdict(result["b"]["b2"]["b2b"]) |
| 156 | + _assert_dict_and_not_attrdict(result["b"]["b2"]["b2b"]["b2b2"]) |
0 commit comments