|
| 1 | +""" |
| 2 | +Unit Test - Mutations Module |
| 3 | +
|
| 4 | +Tests for the mutations utility functions. |
| 5 | +
|
| 6 | +The code is licensed under the MIT license. |
| 7 | +""" |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | +from meteostat.utilities.mutations import calculate_dwpt |
| 11 | + |
| 12 | + |
| 13 | +def test_calculate_dwpt_with_missing_columns(): |
| 14 | + """ |
| 15 | + Test: calculate_dwpt() with missing required columns |
| 16 | +
|
| 17 | + When the dataframe doesn't have the required 'temp' or 'rhum' columns, |
| 18 | + the function should return the dataframe unchanged without raising a KeyError. |
| 19 | + """ |
| 20 | + |
| 21 | + # Create an empty dataframe |
| 22 | + df = pd.DataFrame() |
| 23 | + result = calculate_dwpt(df, "dwpt") |
| 24 | + assert result.equals(df) |
| 25 | + |
| 26 | + # Create a dataframe with only temp column |
| 27 | + df_temp_only = pd.DataFrame({"temp": [20.0, 21.0, 22.0]}) |
| 28 | + result = calculate_dwpt(df_temp_only, "dwpt") |
| 29 | + assert result.equals(df_temp_only) |
| 30 | + assert "dwpt" not in result.columns |
| 31 | + |
| 32 | + # Create a dataframe with only rhum column |
| 33 | + df_rhum_only = pd.DataFrame({"rhum": [80.0, 85.0, 90.0]}) |
| 34 | + result = calculate_dwpt(df_rhum_only, "dwpt") |
| 35 | + assert result.equals(df_rhum_only) |
| 36 | + assert "dwpt" not in result.columns |
| 37 | + |
| 38 | + |
| 39 | +def test_calculate_dwpt_with_valid_columns(): |
| 40 | + """ |
| 41 | + Test: calculate_dwpt() with valid required columns |
| 42 | +
|
| 43 | + When the dataframe has both 'temp' and 'rhum' columns, |
| 44 | + the function should calculate the dew point temperature. |
| 45 | + """ |
| 46 | + |
| 47 | + # Create a dataframe with both required columns |
| 48 | + df = pd.DataFrame({ |
| 49 | + "temp": [20.0, 15.0, 10.0], |
| 50 | + "rhum": [80.0, 70.0, 60.0], |
| 51 | + "temp_flag": ["A", "A", "B"], |
| 52 | + "rhum_flag": ["A", "B", "A"] |
| 53 | + }) |
| 54 | + |
| 55 | + result = calculate_dwpt(df, "dwpt") |
| 56 | + |
| 57 | + # Check that dwpt column was added |
| 58 | + assert "dwpt" in result.columns |
| 59 | + assert "dwpt_flag" in result.columns |
| 60 | + |
| 61 | + # Check that dwpt values are numeric and rounded to 1 decimal |
| 62 | + assert result["dwpt"].dtype == "float64" |
| 63 | + assert all(result["dwpt"].round(1) == result["dwpt"]) |
| 64 | + |
| 65 | + # Check that dwpt_flag was calculated from temp_flag and rhum_flag |
| 66 | + assert result["dwpt_flag"].tolist() == ["A", "B", "B"] |
0 commit comments