defmodule Microwaveprop.Weather.Era5CdsJobTest do use Microwaveprop.DataCase, async: true alias Microwaveprop.Weather.Era5CdsJob describe "changeset/2" do @valid_attrs %{ year: 2014, month: 3, tile_lat: 32, tile_lon: -98, single_job_id: "abc-single", pressure_job_id: "abc-pressure", submitted_at: ~U[2026-04-13 21:00:00Z] } test "accepts a complete attrs map" do changeset = Era5CdsJob.changeset(%Era5CdsJob{}, @valid_attrs) assert changeset.valid? end test "requires year, month, tile coordinates, and both CDS job IDs" do changeset = Era5CdsJob.changeset(%Era5CdsJob{}, %{}) refute changeset.valid? required = ~w(year month tile_lat tile_lon single_job_id pressure_job_id submitted_at)a errors = Keyword.keys(changeset.errors) for field <- required do assert field in errors, "expected #{inspect(field)} to be required" end end test "last_polled_at and poll_count default to nil/0 and can be updated" do {:ok, row} = %Era5CdsJob{} |> Era5CdsJob.changeset(@valid_attrs) |> Repo.insert() assert row.poll_count == 0 assert is_nil(row.last_polled_at) now = DateTime.truncate(DateTime.utc_now(), :second) {:ok, bumped} = row |> Era5CdsJob.changeset(%{last_polled_at: now, poll_count: 1}) |> Repo.update() assert bumped.poll_count == 1 assert bumped.last_polled_at == now end end describe "uniqueness" do test "collapses duplicate (year, month, tile_lat, tile_lon) inserts" do attrs = %{ year: 2014, month: 3, tile_lat: 32, tile_lon: -98, single_job_id: "first-single", pressure_job_id: "first-pressure", submitted_at: ~U[2026-04-13 21:00:00Z] } {:ok, _} = %Era5CdsJob{} |> Era5CdsJob.changeset(attrs) |> Repo.insert() {:error, changeset} = %Era5CdsJob{} |> Era5CdsJob.changeset(%{attrs | single_job_id: "second-single", pressure_job_id: "second-pressure"}) |> Repo.insert() refute changeset.valid? assert {:tile_month, _} = List.keyfind(changeset.errors, :tile_month, 0) || {:tile_month, nil} end end end