|
| 1 | +""" |
| 2 | +Unit Test - MeteoData with no stations |
| 3 | +
|
| 4 | +Tests the fix for the issue where an AttributeError occurs |
| 5 | +when no weather stations are found near a location. |
| 6 | +
|
| 7 | +The code is licensed under the MIT license. |
| 8 | +""" |
| 9 | + |
| 10 | +from datetime import datetime |
| 11 | +import pandas as pd |
| 12 | +from meteostat import Monthly, Hourly, Daily, Normals |
| 13 | + |
| 14 | + |
| 15 | +def test_monthly_no_stations(): |
| 16 | + """ |
| 17 | + Test: Monthly class with no stations found (simulated via empty station list) |
| 18 | + """ |
| 19 | + # Create an empty DataFrame with station columns |
| 20 | + empty_stations = pd.DataFrame(columns=["id", "latitude", "longitude", "elevation"]) |
| 21 | + empty_stations = empty_stations.set_index("id") |
| 22 | + |
| 23 | + # This should not raise AttributeError: 'Monthly' object has no attribute '_types' |
| 24 | + try: |
| 25 | + data = Monthly(empty_stations, start=datetime(2024, 1, 1), end=datetime(2024, 12, 31)) |
| 26 | + df = data.fetch() |
| 27 | + # Should return an empty DataFrame with the correct columns |
| 28 | + assert isinstance(df, pd.DataFrame) |
| 29 | + assert len(df) == 0 |
| 30 | + except AttributeError as exception: |
| 31 | + if "'Monthly' object has no attribute '_types'" in str(exception): |
| 32 | + raise AssertionError(f"Bug still present: {exception}") from exception |
| 33 | + raise |
| 34 | + |
| 35 | + |
| 36 | +def test_hourly_no_stations(): |
| 37 | + """ |
| 38 | + Test: Hourly class with no stations found |
| 39 | + """ |
| 40 | + empty_stations = pd.DataFrame(columns=["id", "latitude", "longitude", "elevation"]) |
| 41 | + empty_stations = empty_stations.set_index("id") |
| 42 | + |
| 43 | + try: |
| 44 | + data = Hourly(empty_stations, start=datetime(2024, 1, 1, 0), end=datetime(2024, 1, 1, 23)) |
| 45 | + df = data.fetch() |
| 46 | + assert isinstance(df, pd.DataFrame) |
| 47 | + assert len(df) == 0 |
| 48 | + except AttributeError as exception: |
| 49 | + if "'Hourly' object has no attribute '_types'" in str(exception): |
| 50 | + raise AssertionError(f"Bug still present: {exception}") from exception |
| 51 | + raise |
| 52 | + |
| 53 | + |
| 54 | +def test_daily_no_stations(): |
| 55 | + """ |
| 56 | + Test: Daily class with no stations found |
| 57 | + """ |
| 58 | + empty_stations = pd.DataFrame(columns=["id", "latitude", "longitude", "elevation"]) |
| 59 | + empty_stations = empty_stations.set_index("id") |
| 60 | + |
| 61 | + try: |
| 62 | + data = Daily(empty_stations, start=datetime(2024, 1, 1), end=datetime(2024, 1, 31)) |
| 63 | + df = data.fetch() |
| 64 | + assert isinstance(df, pd.DataFrame) |
| 65 | + assert len(df) == 0 |
| 66 | + except AttributeError as exception: |
| 67 | + if "'Daily' object has no attribute '_types'" in str(exception): |
| 68 | + raise AssertionError(f"Bug still present: {exception}") from exception |
| 69 | + raise |
| 70 | + |
| 71 | + |
| 72 | +def test_normals_no_stations(): |
| 73 | + """ |
| 74 | + Test: Normals class with no stations found |
| 75 | + """ |
| 76 | + empty_stations = pd.DataFrame(columns=["id", "latitude", "longitude", "elevation"]) |
| 77 | + empty_stations = empty_stations.set_index("id") |
| 78 | + |
| 79 | + try: |
| 80 | + # The main bug fix: creating Normals with no stations |
| 81 | + # should not raise AttributeError |
| 82 | + data = Normals(empty_stations, start=1991, end=2020) |
| 83 | + # Check that data object was created successfully |
| 84 | + assert isinstance(data, Normals) |
| 85 | + assert len(data._stations) == 0 |
| 86 | + assert data.count() == 0 |
| 87 | + except AttributeError as exception: |
| 88 | + if "'Normals' object has no attribute '_types'" in str(exception): |
| 89 | + raise AssertionError(f"Bug still present: {exception}") from exception |
| 90 | + raise |
0 commit comments