|
10 | 10 | import gdax |
11 | 11 | import gdax.orderbook |
12 | 12 |
|
13 | | - |
14 | | -class AsyncContextManagerMock(MagicMock): |
15 | | - async def __aenter__(self): |
16 | | - return self.aenter |
17 | | - |
18 | | - async def __aexit__(self, *args): |
19 | | - pass |
| 13 | +from tests.helpers import AsyncContextManagerMock |
20 | 14 |
|
21 | 15 |
|
22 | 16 | def generate_id(): |
@@ -231,3 +225,65 @@ async def test_authentication(self, mock_book, mock_connect, mocker): |
231 | 225 | } |
232 | 226 | assert orderbook._authenticated |
233 | 227 | mock_connect.return_value.aenter.send_json.assert_called_with(msg) |
| 228 | + |
| 229 | + @patch('gdax.trader.Trader.get_product_order_book') |
| 230 | + async def test_basic_message(self, mock_book, mock_connect): |
| 231 | + mock_connect.return_value.aenter.receive_str = CoroutineMock() |
| 232 | + mock_connect.return_value.aenter.send_json = CoroutineMock() |
| 233 | + mock_book.return_value = {'bids': [], 'asks': [], 'sequence': 1} |
| 234 | + message_expected = { |
| 235 | + "type": "done", |
| 236 | + "side": "sell", |
| 237 | + "order_id": "4eef1226-4b38-422c-a5b1-56def7107f9a", |
| 238 | + "reason": "canceled", |
| 239 | + "product_id": "BTC-USD", |
| 240 | + "price": "2601.76000000", |
| 241 | + "remaining_size": "3.09000000", |
| 242 | + "sequence": 2, |
| 243 | + "time": "2017-06-25T11:23:14.775000Z" |
| 244 | + } |
| 245 | + mock_connect.return_value.aenter.receive_str.side_effect = [ |
| 246 | + json.dumps(message_expected) |
| 247 | + ] |
| 248 | + async with gdax.orderbook.OrderBook('BTC-USD') as orderbook: |
| 249 | + assert orderbook.product_ids == ['BTC-USD'] |
| 250 | + message = await orderbook.handle_message() |
| 251 | + assert message == message_expected |
| 252 | + |
| 253 | + @patch('gdax.trader.Trader.get_product_order_book') |
| 254 | + async def test_logfile(self, mock_book, mock_connect): |
| 255 | + mock_connect.return_value.aenter.receive_str = CoroutineMock() |
| 256 | + mock_connect.return_value.aenter.send_json = CoroutineMock() |
| 257 | + message_expected = { |
| 258 | + "type": "done", |
| 259 | + "side": "sell", |
| 260 | + "order_id": "4eef1226-4b38-422c-a5b1-56def7107f9a", |
| 261 | + "reason": "canceled", |
| 262 | + "product_id": "ETH-USD", |
| 263 | + "price": "2601.76000000", |
| 264 | + "remaining_size": "3.09000000", |
| 265 | + "sequence": 2, |
| 266 | + "time": "2017-06-25T11:23:14.775000Z" |
| 267 | + } |
| 268 | + mock_connect.return_value.aenter.receive_str.side_effect = [ |
| 269 | + json.dumps(message_expected) |
| 270 | + ] |
| 271 | + |
| 272 | + product_id = 'ETH-USD' |
| 273 | + book = {'bids': [], 'asks': [], 'sequence': 1} |
| 274 | + mock_book.return_value = book |
| 275 | + calls = [call(f'B {product_id} {json.dumps(book)}\n')] |
| 276 | + with patch('aiofiles.open', |
| 277 | + new_callable=AsyncContextManagerMock) as mock_open: |
| 278 | + mock_open.return_value.aenter.write = CoroutineMock() |
| 279 | + # mock_open.return_value.aexit = AsyncContextManagerMock() |
| 280 | + mock_write = mock_open.return_value.aenter.write |
| 281 | + async with gdax.orderbook.OrderBook( |
| 282 | + [product_id], |
| 283 | + trade_log_file_path='trades.txt') as orderbook: |
| 284 | + |
| 285 | + mock_write.assert_has_calls(calls) |
| 286 | + |
| 287 | + message = await orderbook.handle_message() |
| 288 | + calls.append(call(f'W {json.dumps(message_expected)}\n')) |
| 289 | + mock_write.assert_has_calls(calls) |
0 commit comments