- Add precip_1h_in and wx_codes fields to ASOS surface observations - Add IEMRE reanalysis schema for radar-derived hourly precipitation - Add IemreFetchWorker with exponential backoff and idempotency - Integrate IEMRE enqueue into cron weather backfill pipeline - All existing QSOs marked iemre_queued=false for automatic backfill
121 lines
3.2 KiB
Elixir
121 lines
3.2 KiB
Elixir
defmodule Microwaveprop.Workers.IemreFetchWorkerTest do
|
|
use Microwaveprop.DataCase, async: true
|
|
|
|
alias Microwaveprop.Weather
|
|
alias Microwaveprop.Weather.IemClient
|
|
alias Microwaveprop.Weather.IemreObservation
|
|
alias Microwaveprop.Workers.IemreFetchWorker
|
|
|
|
@sample_iemre_data [
|
|
%{"utc_hour" => 0, "p01m_mm" => 0.0, "skyc_pct" => 25.0},
|
|
%{"utc_hour" => 1, "p01m_mm" => 0.5, "skyc_pct" => 50.0}
|
|
]
|
|
|
|
describe "perform/1" do
|
|
test "skips if IEMRE observation already exists" do
|
|
Weather.upsert_iemre_observation(%{
|
|
lat: 32.875,
|
|
lon: -97.0,
|
|
date: ~D[2026-03-28],
|
|
hourly: @sample_iemre_data
|
|
})
|
|
|
|
# Stub should NOT be called
|
|
Req.Test.stub(IemClient, fn conn ->
|
|
Plug.Conn.send_resp(conn, 500, "Should not be called")
|
|
end)
|
|
|
|
job = %Oban.Job{
|
|
args: %{
|
|
"lat" => 32.875,
|
|
"lon" => -97.0,
|
|
"date" => "2026-03-28"
|
|
}
|
|
}
|
|
|
|
assert :ok = IemreFetchWorker.perform(job)
|
|
assert Repo.aggregate(IemreObservation, :count) == 1
|
|
end
|
|
|
|
test "fetches and stores IEMRE data on success" do
|
|
Req.Test.stub(IemClient, fn conn ->
|
|
Req.Test.json(conn, %{"data" => @sample_iemre_data})
|
|
end)
|
|
|
|
job = %Oban.Job{
|
|
args: %{
|
|
"lat" => 32.875,
|
|
"lon" => -97.0,
|
|
"date" => "2026-03-28"
|
|
}
|
|
}
|
|
|
|
assert :ok = IemreFetchWorker.perform(job)
|
|
assert Repo.aggregate(IemreObservation, :count) == 1
|
|
|
|
obs = Repo.one(IemreObservation)
|
|
assert obs.lat == 32.875
|
|
assert obs.lon == -97.0
|
|
assert obs.date == ~D[2026-03-28]
|
|
assert length(obs.hourly) == 2
|
|
end
|
|
|
|
test "returns :ok on empty data" do
|
|
Req.Test.stub(IemClient, fn conn ->
|
|
Req.Test.json(conn, %{"data" => []})
|
|
end)
|
|
|
|
job = %Oban.Job{
|
|
args: %{
|
|
"lat" => 32.875,
|
|
"lon" => -97.0,
|
|
"date" => "2026-03-28"
|
|
}
|
|
}
|
|
|
|
assert :ok = IemreFetchWorker.perform(job)
|
|
assert Repo.aggregate(IemreObservation, :count) == 0
|
|
end
|
|
|
|
test "retries on transient server error" do
|
|
Req.Test.stub(IemClient, fn conn ->
|
|
Plug.Conn.send_resp(conn, 503, "Service Unavailable")
|
|
end)
|
|
|
|
job = %Oban.Job{
|
|
args: %{
|
|
"lat" => 32.875,
|
|
"lon" => -97.0,
|
|
"date" => "2026-03-28"
|
|
}
|
|
}
|
|
|
|
assert {:error, "IEM IEMRE HTTP 503"} = IemreFetchWorker.perform(job)
|
|
end
|
|
|
|
test "cancels on permanent failure (404)" do
|
|
Req.Test.stub(IemClient, fn conn ->
|
|
Plug.Conn.send_resp(conn, 404, "not found")
|
|
end)
|
|
|
|
job = %Oban.Job{
|
|
args: %{
|
|
"lat" => 32.875,
|
|
"lon" => -97.0,
|
|
"date" => "2026-03-28"
|
|
}
|
|
}
|
|
|
|
assert {:cancel, "IEM IEMRE HTTP 404"} = IemreFetchWorker.perform(job)
|
|
end
|
|
end
|
|
|
|
describe "backoff/1" do
|
|
test "uses exponential backoff capped at 6 hours" do
|
|
assert IemreFetchWorker.backoff(%Oban.Job{attempt: 1}) == 120
|
|
assert IemreFetchWorker.backoff(%Oban.Job{attempt: 2}) == 240
|
|
assert IemreFetchWorker.backoff(%Oban.Job{attempt: 10}) == 21_600
|
|
assert IemreFetchWorker.backoff(%Oban.Job{attempt: 20}) == 21_600
|
|
end
|
|
end
|
|
end
|