|
4 | 4 | AsyncMock,
|
5 | 5 | )
|
6 | 6 |
|
| 7 | +from eth_typing import ( |
| 8 | + HexStr, |
| 9 | +) |
7 | 10 | import pytest_asyncio
|
8 | 11 |
|
9 | 12 | from web3 import (
|
@@ -137,20 +140,51 @@ async def test_unsubscribe_all_clears_all_subscriptions(subscription_manager):
|
137 | 140 |
|
138 | 141 |
|
139 | 142 | @pytest.mark.asyncio
|
140 |
| -async def test_unsubscribe_with_hex_ids(subscription_manager): |
| 143 | +async def test_unsubscribe_with_one_or_multiple(subscription_manager): |
141 | 144 | sub1 = NewHeadsSubscription()
|
142 | 145 | sub2 = PendingTxSubscription()
|
143 | 146 | sub3 = NewHeadsSubscription()
|
144 | 147 | sub4 = LogsSubscription()
|
| 148 | + sub5 = NewHeadsSubscription() |
| 149 | + sub6 = PendingTxSubscription() |
| 150 | + |
| 151 | + ( |
| 152 | + sub_id1, |
| 153 | + sub_id2, |
| 154 | + sub_id3, |
| 155 | + sub_id4, |
| 156 | + sub_id5, |
| 157 | + sub_id6, |
| 158 | + ) = await subscription_manager.subscribe([sub1, sub2, sub3, sub4, sub5, sub6]) |
| 159 | + assert sub_id1 == "0x0" |
| 160 | + assert sub_id2 == "0x1" |
| 161 | + assert sub_id3 == "0x2" |
| 162 | + assert sub_id4 == "0x3" |
| 163 | + assert sub_id5 == "0x4" |
| 164 | + assert sub_id6 == "0x5" |
| 165 | + |
| 166 | + assert subscription_manager.subscriptions == [sub1, sub2, sub3, sub4, sub5, sub6] |
| 167 | + |
| 168 | + # unsubscribe single by hex id |
| 169 | + assert await subscription_manager.unsubscribe(sub_id1) is True |
| 170 | + assert subscription_manager.subscriptions == [sub2, sub3, sub4, sub5, sub6] |
145 | 171 |
|
146 |
| - sub_id1, sub_id2, sub_id3, sub_id4 = await subscription_manager.subscribe( |
147 |
| - [sub1, sub2, sub3, sub4] |
148 |
| - ) |
| 172 | + # unsubscribe many by hex id |
| 173 | + assert await subscription_manager.unsubscribe([sub_id2, sub_id3]) is True |
| 174 | + assert subscription_manager.subscriptions == [sub4, sub5, sub6] |
149 | 175 |
|
150 |
| - assert subscription_manager.subscriptions == [sub1, sub2, sub3, sub4] |
| 176 | + # unsubscribe non-existent hex id |
| 177 | + with pytest.raises(Web3ValueError, match=f"Subscription not found|{0x7}"): |
| 178 | + await subscription_manager.unsubscribe(HexStr("0x7")) |
151 | 179 |
|
152 |
| - assert await subscription_manager.unsubscribe(sub_id1) is True |
153 |
| - assert subscription_manager.subscriptions == [sub2, sub3, sub4] |
| 180 | + # unsubscribe by subscription object |
| 181 | + assert await subscription_manager.unsubscribe(sub4) is True |
| 182 | + assert subscription_manager.subscriptions == [sub5, sub6] |
154 | 183 |
|
155 |
| - assert await subscription_manager.unsubscribe([sub_id2, sub_id3]) is True |
156 |
| - assert subscription_manager.subscriptions == [sub4] |
| 184 | + # unsubscribe many by subscription object |
| 185 | + assert await subscription_manager.unsubscribe([sub5, sub6]) is True |
| 186 | + assert subscription_manager.subscriptions == [] |
| 187 | + |
| 188 | + # unsubscribe non-existent subscription object |
| 189 | + with pytest.raises(Web3ValueError, match=f"Subscription not found|{sub5.id}"): |
| 190 | + await subscription_manager.unsubscribe(sub5) |
0 commit comments