|
| 1 | +from datetime import datetime |
| 2 | +from zoneinfo import ZoneInfo |
| 3 | + |
| 4 | +import polars as pl |
| 5 | +import polars.testing as plt |
| 6 | +import pytest |
| 7 | + |
| 8 | +import cfa.stf.forecasttools as ft |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def tabular_data(): |
| 13 | + return pl.DataFrame( |
| 14 | + { |
| 15 | + "location": ["US", "01"], |
| 16 | + "value": [1.5, 2.5], |
| 17 | + } |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("file_format", ["csv", "tsv", "parquet"]) |
| 22 | +def test_read_tabular_reads_supported_formats(tmp_path, tabular_data, file_format): |
| 23 | + path = tmp_path / f"data.{file_format}" |
| 24 | + if file_format == "csv": |
| 25 | + tabular_data.write_csv(path) |
| 26 | + elif file_format == "tsv": |
| 27 | + tabular_data.write_csv(path, separator="\t") |
| 28 | + else: |
| 29 | + tabular_data.write_parquet(path) |
| 30 | + |
| 31 | + result = ft.read_tabular(path) |
| 32 | + |
| 33 | + plt.assert_frame_equal(result, tabular_data) |
| 34 | + |
| 35 | + |
| 36 | +def test_read_tabular_matches_extension_case_insensitively(tmp_path, tabular_data): |
| 37 | + path = tmp_path / "data.CSV" |
| 38 | + tabular_data.write_csv(path) |
| 39 | + |
| 40 | + result = ft.read_tabular(path) |
| 41 | + |
| 42 | + plt.assert_frame_equal(result, tabular_data) |
| 43 | + |
| 44 | + |
| 45 | +def test_read_tabular_forwards_reader_options(tmp_path): |
| 46 | + path = tmp_path / "data.csv" |
| 47 | + path.write_text("value\n1\n2\n3\n") |
| 48 | + |
| 49 | + result = ft.read_tabular(path, n_rows=2) |
| 50 | + |
| 51 | + assert result.get_column("value").to_list() == [1, 2] |
| 52 | + |
| 53 | + |
| 54 | +def test_read_tabular_corrects_timezone_naive_parquet_timestamps(tmp_path): |
| 55 | + path = tmp_path / "timestamps.parquet" |
| 56 | + tokyo = ZoneInfo("Asia/Tokyo") |
| 57 | + data = pl.DataFrame( |
| 58 | + { |
| 59 | + "timestamp_without_timezone": [datetime(2026, 1, 15, 12, 30)], |
| 60 | + "timestamp_with_timezone": [datetime(2026, 1, 15, 12, 30, tzinfo=tokyo)], |
| 61 | + }, |
| 62 | + schema={ |
| 63 | + "timestamp_without_timezone": pl.Datetime("us"), |
| 64 | + "timestamp_with_timezone": pl.Datetime("us", "Asia/Tokyo"), |
| 65 | + }, |
| 66 | + ) |
| 67 | + data.write_parquet(path) |
| 68 | + |
| 69 | + result = ft.read_tabular(path) |
| 70 | + |
| 71 | + assert result.schema == pl.Schema( |
| 72 | + { |
| 73 | + "timestamp_without_timezone": pl.Datetime("us", "UTC"), |
| 74 | + "timestamp_with_timezone": pl.Datetime("us", "Asia/Tokyo"), |
| 75 | + } |
| 76 | + ) |
| 77 | + assert result.select(pl.all().to_physical()).row(0) == data.select( |
| 78 | + pl.all().to_physical() |
| 79 | + ).row(0) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize("filename", ["data.json", "data"]) |
| 83 | +def test_read_tabular_rejects_unsupported_extensions(tmp_path, filename): |
| 84 | + path = tmp_path / filename |
| 85 | + |
| 86 | + with pytest.raises(ValueError, match="Unsupported file extension"): |
| 87 | + ft.read_tabular(path) |
0 commit comments