Spaces:
Running
Running
File size: 3,614 Bytes
8aa7d2f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import json
from datetime import datetime, timedelta
from unittest.mock import patch, MagicMock
from surf_spot_finder.tools import openmeteo
def test_extract_hourly_data():
data = {
"hourly": {
"time": ["2023-01-01T00:00", "2023-01-01T01:00"],
"wave_height": [1.5, 1.6],
"wave_period": [10, 11],
}
}
expected = [
{"time": "2023-01-01T00:00", "wave_height": 1.5, "wave_period": 10},
{"time": "2023-01-01T01:00", "wave_height": 1.6, "wave_period": 11},
]
assert openmeteo._extract_hourly_data(data) == expected
def test_filter_by_date():
hourly_data = [
{"time": "2023-01-01T00:00", "wave_height": 1.5},
{"time": "2023-01-01T01:00", "wave_height": 1.6},
{"time": "2023-01-01T02:00", "wave_height": 1.7},
{"time": "2023-01-01T03:00", "wave_height": 1.8},
]
date = datetime.fromisoformat("2023-01-01T01:00")
expected = [
{"time": "2023-01-01T00:00", "wave_height": 1.5},
{"time": "2023-01-01T01:00", "wave_height": 1.6},
{"time": "2023-01-01T02:00", "wave_height": 1.7},
]
assert openmeteo._filter_by_date(date, hourly_data) == expected
expected = [
{"time": "2023-01-01T01:00", "wave_height": 1.6},
]
assert openmeteo._filter_by_date(date, hourly_data, timedelta(hours=0)) == expected
def test_get_wave_forecast():
with patch("requests.get") as mock_get:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content.decode.return_value = json.dumps(
{
"hourly": {
"time": ["2023-02-02T00:00", "2023-02-02T01:00"],
"wave_direction": [270, 280],
"wave_height": [1.5, 1.6],
"wave_period": [10, 11],
"sea_level_height_msl": [0.5, 0.6],
}
}
)
mock_get.return_value = mock_response
result = openmeteo.get_wave_forecast(lat=40.0, lon=-3.0)
assert len(result) == 2
assert result[1]["time"] == "2023-02-02T01:00"
assert result[1]["wave_direction"] == 280
assert result[1]["wave_height"] == 1.6
assert result[1]["wave_period"] == 11
assert result[1]["sea_level_height_msl"] == 0.6
result_filtered = openmeteo.get_wave_forecast(
lat=40.0, lon=-3.0, date="2023-02-02T02:00"
)
assert len(result_filtered) == 1
assert result_filtered[0]["time"] == "2023-02-02T01:00"
def test_get_wind_forecast():
with patch("requests.get") as mock_get:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content.decode.return_value = json.dumps(
{
"hourly": {
"time": ["2023-02-02T00:00", "2023-02-02T01:00"],
"winddirection_10m": [270, 280],
"windspeed_10m": [10, 11],
}
}
)
mock_get.return_value = mock_response
result = openmeteo.get_wind_forecast(lat=40.0, lon=-3.0)
assert len(result) == 2
assert result[1]["time"] == "2023-02-02T01:00"
assert result[1]["winddirection_10m"] == 280
assert result[1]["windspeed_10m"] == 11
result_filtered = openmeteo.get_wind_forecast(
lat=40.0, lon=-3.0, date="2023-02-02T02:00"
)
assert len(result_filtered) == 1
assert result_filtered[0]["time"] == "2023-02-02T01:00"
assert result[0]["windspeed_10m"] == 10
|