|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +import pytest |
| 7 | +from databento_dbn import Compression |
| 8 | +from databento_dbn import Schema |
| 9 | + |
| 10 | +from databento.common.publishers import Dataset |
| 11 | +from databento.live.protocol import DatabentoLiveProtocol |
| 12 | +from tests.mockliveserver.fixture import MockLiveServerInterface |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("compression", [Compression.NONE, Compression.ZSTD]) |
| 16 | +async def test_protocol_connection_with_compression( |
| 17 | + mock_live_server: MockLiveServerInterface, |
| 18 | + test_live_api_key: str, |
| 19 | + compression: Compression, |
| 20 | +) -> None: |
| 21 | + """ |
| 22 | + Test protocol connection with different compression settings. |
| 23 | + """ |
| 24 | + # Arrange |
| 25 | + transport, protocol = await asyncio.get_event_loop().create_connection( |
| 26 | + protocol_factory=lambda: DatabentoLiveProtocol( |
| 27 | + api_key=test_live_api_key, |
| 28 | + dataset=Dataset.GLBX_MDP3, |
| 29 | + compression=compression, |
| 30 | + ), |
| 31 | + host=mock_live_server.host, |
| 32 | + port=mock_live_server.port, |
| 33 | + ) |
| 34 | + |
| 35 | + # Act, Assert |
| 36 | + await asyncio.wait_for(protocol.authenticated, timeout=1) |
| 37 | + transport.close() |
| 38 | + await asyncio.wait_for(protocol.disconnected, timeout=1) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("compression", [Compression.NONE, Compression.ZSTD]) |
| 42 | +async def test_protocol_streaming_with_compression( |
| 43 | + monkeypatch: pytest.MonkeyPatch, |
| 44 | + mock_live_server: MockLiveServerInterface, |
| 45 | + test_live_api_key: str, |
| 46 | + compression: Compression, |
| 47 | +) -> None: |
| 48 | + """ |
| 49 | + Test streaming records with different compression settings. |
| 50 | + """ |
| 51 | + # Arrange |
| 52 | + monkeypatch.setattr( |
| 53 | + DatabentoLiveProtocol, |
| 54 | + "received_metadata", |
| 55 | + metadata_mock := MagicMock(), |
| 56 | + ) |
| 57 | + monkeypatch.setattr( |
| 58 | + DatabentoLiveProtocol, |
| 59 | + "received_record", |
| 60 | + record_mock := MagicMock(), |
| 61 | + ) |
| 62 | + |
| 63 | + _, protocol = await asyncio.get_event_loop().create_connection( |
| 64 | + protocol_factory=lambda: DatabentoLiveProtocol( |
| 65 | + api_key=test_live_api_key, |
| 66 | + dataset=Dataset.GLBX_MDP3, |
| 67 | + compression=compression, |
| 68 | + ), |
| 69 | + host=mock_live_server.host, |
| 70 | + port=mock_live_server.port, |
| 71 | + ) |
| 72 | + |
| 73 | + await asyncio.wait_for(protocol.authenticated, timeout=1) |
| 74 | + |
| 75 | + # Act |
| 76 | + protocol.subscribe( |
| 77 | + schema=Schema.MBO, |
| 78 | + symbols="ESM4", |
| 79 | + ) |
| 80 | + protocol.start() |
| 81 | + |
| 82 | + # Assert |
| 83 | + await asyncio.wait_for(protocol.disconnected, timeout=5) |
| 84 | + metadata_mock.assert_called() |
| 85 | + record_mock.assert_called() |
0 commit comments