Add BackfillEnqueueWorker tests (0→100% coverage)
This commit is contained in:
parent
62c9600f67
commit
c98aabfc15
1 changed files with 100 additions and 0 deletions
100
test/microwaveprop/workers/backfill_enqueue_worker_test.exs
Normal file
100
test/microwaveprop/workers/backfill_enqueue_worker_test.exs
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
defmodule Microwaveprop.Workers.BackfillEnqueueWorkerTest do
|
||||||
|
use Microwaveprop.DataCase, async: true
|
||||||
|
|
||||||
|
alias Microwaveprop.Radio.Contact
|
||||||
|
alias Microwaveprop.Terrain.ElevationClient
|
||||||
|
alias Microwaveprop.Weather.HrrrClient
|
||||||
|
alias Microwaveprop.Weather.IemClient
|
||||||
|
alias Microwaveprop.Workers.BackfillEnqueueWorker
|
||||||
|
|
||||||
|
defp create_contact(attrs \\ %{}) do
|
||||||
|
default = %{
|
||||||
|
station1: "W5XD",
|
||||||
|
station2: "K5TR",
|
||||||
|
qso_timestamp: ~U[2026-03-28 18:00:00Z],
|
||||||
|
mode: "CW",
|
||||||
|
band: Decimal.new("1296"),
|
||||||
|
grid1: "EM12",
|
||||||
|
grid2: "EM00",
|
||||||
|
pos1: %{"lat" => 32.9, "lon" => -97.0},
|
||||||
|
pos2: %{"lat" => 30.3, "lon" => -97.7},
|
||||||
|
distance_km: Decimal.new("295")
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, contact} =
|
||||||
|
%Contact{}
|
||||||
|
|> Contact.changeset(Map.merge(default, attrs))
|
||||||
|
|> Repo.insert()
|
||||||
|
|
||||||
|
contact
|
||||||
|
end
|
||||||
|
|
||||||
|
setup do
|
||||||
|
Req.Test.stub(IemClient, fn conn ->
|
||||||
|
case conn.request_path do
|
||||||
|
"/cgi-bin/request/asos.py" ->
|
||||||
|
Req.Test.text(conn, "#DEBUG,\nstation,valid,tmpf\n")
|
||||||
|
|
||||||
|
"/iemre/" <> _ ->
|
||||||
|
Req.Test.json(conn, %{"data" => [%{"utc_hour" => 0, "p01m_mm" => 0.0}]})
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
Req.Test.json(conn, %{"profiles" => []})
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
Req.Test.stub(HrrrClient, fn conn ->
|
||||||
|
Plug.Conn.send_resp(conn, 404, "not found")
|
||||||
|
end)
|
||||||
|
|
||||||
|
Req.Test.stub(ElevationClient, fn conn ->
|
||||||
|
params = Plug.Conn.fetch_query_params(conn).query_params
|
||||||
|
lat_count = params["latitude"] |> String.split(",") |> length()
|
||||||
|
Req.Test.json(conn, %{"elevation" => List.duplicate(200.0, lat_count)})
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "perform/1" do
|
||||||
|
test "enqueues enrichment jobs for pending contacts" do
|
||||||
|
create_contact()
|
||||||
|
|
||||||
|
assert :ok = BackfillEnqueueWorker.perform(%Oban.Job{args: %{"limit" => 10}})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "does not enqueue for already-complete contacts" do
|
||||||
|
contact = create_contact()
|
||||||
|
|
||||||
|
contact
|
||||||
|
|> Ecto.Changeset.change(%{
|
||||||
|
hrrr_status: :complete,
|
||||||
|
weather_status: :complete,
|
||||||
|
terrain_status: :complete,
|
||||||
|
iemre_status: :complete
|
||||||
|
})
|
||||||
|
|> Repo.update!()
|
||||||
|
|
||||||
|
assert :ok = BackfillEnqueueWorker.perform(%Oban.Job{args: %{"limit" => 10}})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "respects limit parameter" do
|
||||||
|
for i <- 1..5 do
|
||||||
|
ts = DateTime.add(~U[2026-01-01 00:00:00Z], i * 3600, :second)
|
||||||
|
create_contact(%{qso_timestamp: ts})
|
||||||
|
end
|
||||||
|
|
||||||
|
assert :ok = BackfillEnqueueWorker.perform(%Oban.Job{args: %{"limit" => 2}})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "broadcasts enqueue_complete with count" do
|
||||||
|
Phoenix.PubSub.subscribe(Microwaveprop.PubSub, "backfill:enqueue_complete")
|
||||||
|
|
||||||
|
create_contact()
|
||||||
|
|
||||||
|
assert :ok = BackfillEnqueueWorker.perform(%Oban.Job{args: %{"limit" => 10}})
|
||||||
|
|
||||||
|
assert_receive {:enqueue_complete, 1}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Reference in a new issue