defmodule Microwaveprop.Workers.Era5FetchWorkerTest do use Microwaveprop.DataCase alias Microwaveprop.Workers.Era5FetchWorker 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