|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | +import xarray as xr |
| 7 | + |
| 8 | +import rompy_binary_datasources |
| 9 | + |
| 10 | +HERE = Path(__file__).parent |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_dataset(): |
| 15 | + """Create a mock xarray Dataset for testing.""" |
| 16 | + # Create a simple dataset with time, lat, lon dimensions |
| 17 | + times = pd.date_range("2023-01-01", periods=3, freq="D") |
| 18 | + lats = np.linspace(-10, 10, 3) |
| 19 | + lons = np.linspace(120, 140, 3) |
| 20 | + |
| 21 | + # Create some test data |
| 22 | + data = np.random.rand(3, 3, 3) # time, lat, lon |
| 23 | + |
| 24 | + # Create the dataset |
| 25 | + ds = xr.Dataset( |
| 26 | + data_vars={"temperature": (["time", "lat", "lon"], data, {"units": "celsius"})}, |
| 27 | + coords={ |
| 28 | + "time": times, |
| 29 | + "lat": lats, |
| 30 | + "lon": lons, |
| 31 | + }, |
| 32 | + ) |
| 33 | + return ds |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def mock_dataframe(): |
| 38 | + """Create a mock pandas DataFrame for testing.""" |
| 39 | + # Create a simple time series dataframe |
| 40 | + times = pd.date_range("2023-01-01", periods=24, freq="H") |
| 41 | + data = { |
| 42 | + "wind_speed": np.random.rand(24) * 10, |
| 43 | + "wind_direction": np.random.rand(24) * 360, |
| 44 | + } |
| 45 | + df = pd.DataFrame(data, index=times) |
| 46 | + df.index.name = "time" |
| 47 | + return df |
| 48 | + |
| 49 | + |
| 50 | +def test_source_dataset(mock_dataset): |
| 51 | + """Test SourceDataset with a mock dataset.""" |
| 52 | + source = rompy_binary_datasources.source.SourceDataset(obj=mock_dataset) |
| 53 | + result = source.open() |
| 54 | + assert isinstance(result, xr.Dataset) |
| 55 | + assert "temperature" in result.data_vars |
| 56 | + assert list(result.dims) == ["time", "lat", "lon"] |
| 57 | + |
| 58 | + |
| 59 | +def test_source_dataframe(mock_dataframe): |
| 60 | + """Test SourceTimeseriesDataFrame with a mock dataframe.""" |
| 61 | + source = rompy_binary_datasources.source.SourceTimeseriesDataFrame( |
| 62 | + obj=mock_dataframe |
| 63 | + ) |
| 64 | + result = source.open() |
| 65 | + assert isinstance(result, xr.Dataset) |
| 66 | + assert list(result.dims) == ["time"] |
| 67 | + assert "wind_speed" in result.data_vars |
| 68 | + assert "wind_direction" in result.data_vars |
0 commit comments