defmodule Microwaveprop.Workers.Era5FetchWorkerTest do use Microwaveprop.DataCase use Oban.Testing, repo: Microwaveprop.Repo alias Microwaveprop.Weather.Era5Profile alias Microwaveprop.Workers.Era5FetchWorker alias Microwaveprop.Workers.Era5MonthBatchWorker describe "perform/1" do test "enqueues a month-tile batch worker when no profile is cached" do args = %{"lat" => 32.75, "lon" => -97.25, "valid_time" => "2014-01-15T12:00:00Z"} Oban.Testing.with_testing_mode(:manual, fn -> assert :ok = Era5FetchWorker.perform(%Oban.Job{args: args}) jobs = all_enqueued(worker: Era5MonthBatchWorker) assert [job] = jobs assert job.args["year"] == 2014 assert job.args["month"] == 1 # 32.75 → tile 32 (floor to even degree), -97.25 → tile -98 assert job.args["tile_lat"] == 32 assert job.args["tile_lon"] == -98 end) end test "is a no-op when the profile is already cached" do valid_time = ~U[2014-01-15 12:00:00Z] %Era5Profile{} |> Era5Profile.changeset(%{ lat: 32.75, lon: -97.25, valid_time: valid_time, profile: [] }) |> Repo.insert!() args = %{"lat" => 32.75, "lon" => -97.25, "valid_time" => DateTime.to_iso8601(valid_time)} Oban.Testing.with_testing_mode(:manual, fn -> assert :ok = Era5FetchWorker.perform(%Oban.Job{args: args}) assert [] = all_enqueued(worker: Era5MonthBatchWorker) end) end end describe "unique constraint" do test "deduplicates identical (lat, lon, valid_time) jobs across inserts" do args = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"} Oban.Testing.with_testing_mode(:manual, fn -> {:ok, job1} = Oban.insert(Era5FetchWorker.new(args)) {:ok, job2} = Oban.insert(Era5FetchWorker.new(args)) # Second insert should hit the unique constraint and return the # existing job (same id) instead of creating a new one. assert job1.id == job2.id count = Repo.aggregate(Oban.Job, :count, :id) assert count == 1 end) end test "allows distinct hours on the same date" do args_00 = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"} args_06 = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T06:00:00Z"} Oban.Testing.with_testing_mode(:manual, fn -> {:ok, job1} = Oban.insert(Era5FetchWorker.new(args_00)) {:ok, job2} = Oban.insert(Era5FetchWorker.new(args_06)) assert job1.id != job2.id assert Repo.aggregate(Oban.Job, :count, :id) == 2 end) end test "allows distinct grid points for the same valid_time" do args_a = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"} args_b = %{"lat" => 33.00, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"} Oban.Testing.with_testing_mode(:manual, fn -> {:ok, job1} = Oban.insert(Era5FetchWorker.new(args_a)) {:ok, job2} = Oban.insert(Era5FetchWorker.new(args_b)) assert job1.id != job2.id assert Repo.aggregate(Oban.Job, :count, :id) == 2 end) end test "deduplicates across repeated Oban.insert calls" do args = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"} Oban.Testing.with_testing_mode(:manual, fn -> Enum.each(1..5, fn _ -> Oban.insert(Era5FetchWorker.new(args)) end) assert Repo.aggregate(Oban.Job, :count, :id) == 1 end) end end end