|
1 | 1 | import importlib |
2 | 2 | import gzip |
3 | | -from datetime import datetime |
| 3 | +from datetime import datetime, timedelta, timezone |
4 | 4 | from pathlib import Path |
5 | 5 |
|
6 | 6 | import pytest |
@@ -128,6 +128,82 @@ def test_replay_formats_query_date_as_utc_z(): |
128 | 128 | assert _format_replay_query_date(datetime.fromisoformat("2019-05-01T00:00:00+00:00")) == "2019-05-01T00:00:00.000Z" |
129 | 129 |
|
130 | 130 |
|
| 131 | +@pytest.mark.asyncio |
| 132 | +async def test_replay_accepts_naive_datetime_inputs_as_utc(monkeypatch, tmp_path: Path): |
| 133 | + cache_dir = tmp_path / "cache" |
| 134 | + filters = _live_replay_filters() |
| 135 | + captured = {} |
| 136 | + |
| 137 | + slice_path = Path( |
| 138 | + _get_slice_cache_path( |
| 139 | + str(cache_dir), |
| 140 | + LIVE_REPLAY_EXCHANGE, |
| 141 | + datetime(2019, 5, 1, 0, 0, tzinfo=timezone.utc), |
| 142 | + filters, |
| 143 | + ) |
| 144 | + ) |
| 145 | + slice_path.parent.mkdir(parents=True, exist_ok=True) |
| 146 | + |
| 147 | + with gzip.open(slice_path, "wb") as file: |
| 148 | + file.write(b'2019-05-01T00:00:00.0000000Z {"table":"trade","action":"partial","data":[{"symbol":"BTCUSD"}]}\n') |
| 149 | + |
| 150 | + async def fake_fetch_data_to_replay(**kwargs): |
| 151 | + captured.update(kwargs) |
| 152 | + return None |
| 153 | + |
| 154 | + monkeypatch.setattr(replay_module, "_fetch_data_to_replay", fake_fetch_data_to_replay) |
| 155 | + |
| 156 | + results = [] |
| 157 | + async for item in replay( |
| 158 | + exchange=LIVE_REPLAY_EXCHANGE, |
| 159 | + from_date=datetime(2019, 5, 1, 0, 0), |
| 160 | + to_date=datetime(2019, 5, 1, 0, 1), |
| 161 | + filters=filters, |
| 162 | + cache_dir=str(cache_dir), |
| 163 | + ): |
| 164 | + results.append(item) |
| 165 | + |
| 166 | + assert len(results) == 1 |
| 167 | + assert captured["from_date"] == datetime(2019, 5, 1, 0, 0, tzinfo=timezone.utc) |
| 168 | + assert captured["to_date"] == datetime(2019, 5, 1, 0, 1, tzinfo=timezone.utc) |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.asyncio |
| 172 | +async def test_replay_converts_timezone_aware_datetime_inputs_to_utc(monkeypatch, tmp_path: Path): |
| 173 | + cache_dir = tmp_path / "cache" |
| 174 | + filters = _live_replay_filters() |
| 175 | + captured = {} |
| 176 | + |
| 177 | + utc_from_date = datetime(2019, 5, 1, 0, 0, tzinfo=timezone.utc) |
| 178 | + utc_to_date = datetime(2019, 5, 1, 0, 1, tzinfo=timezone.utc) |
| 179 | + |
| 180 | + slice_path = Path(_get_slice_cache_path(str(cache_dir), LIVE_REPLAY_EXCHANGE, utc_from_date, filters)) |
| 181 | + slice_path.parent.mkdir(parents=True, exist_ok=True) |
| 182 | + |
| 183 | + with gzip.open(slice_path, "wb") as file: |
| 184 | + file.write(b'2019-05-01T00:00:00.0000000Z {"table":"trade","action":"partial","data":[{"symbol":"BTCUSD"}]}\n') |
| 185 | + |
| 186 | + async def fake_fetch_data_to_replay(**kwargs): |
| 187 | + captured.update(kwargs) |
| 188 | + return None |
| 189 | + |
| 190 | + monkeypatch.setattr(replay_module, "_fetch_data_to_replay", fake_fetch_data_to_replay) |
| 191 | + |
| 192 | + results = [] |
| 193 | + async for item in replay( |
| 194 | + exchange=LIVE_REPLAY_EXCHANGE, |
| 195 | + from_date=datetime(2019, 5, 1, 2, 0, tzinfo=timezone(timedelta(hours=2))), |
| 196 | + to_date=datetime(2019, 5, 1, 2, 1, tzinfo=timezone(timedelta(hours=2))), |
| 197 | + filters=filters, |
| 198 | + cache_dir=str(cache_dir), |
| 199 | + ): |
| 200 | + results.append(item) |
| 201 | + |
| 202 | + assert len(results) == 1 |
| 203 | + assert captured["from_date"] == utc_from_date |
| 204 | + assert captured["to_date"] == utc_to_date |
| 205 | + |
| 206 | + |
131 | 207 | @pytest.mark.asyncio |
132 | 208 | async def test_replay_raw_mode_returns_bytes(monkeypatch, tmp_path: Path): |
133 | 209 | cache_dir = tmp_path / "cache" |
|
0 commit comments