prop/test/support/data_case.ex
Graham McIntire 09e65462f5
Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 13m28s
fix: wire HrdpsClient into Req.Test plug pipeline
HrdpsClient already calls Req.get/head with req_options() that
merge :hrdps_req_options from config. Add plug: {Req.Test, HrdpsClient}
to test config so Req.Test.stub intercepts HTTP calls — same pattern
as HrrrClient. Also add hrdps_cycle_available_fn default so the
HTTP probe doesn't fire in tests.

Replace the env-based Application.put_env stubs in DataCase with
Req.Test.stub(HrdpsClient, 404).
2026-08-04 17:56:20 -05:00

84 lines
2.2 KiB
Elixir

defmodule Microwaveprop.DataCase do
@moduledoc """
Test case template for tests that need database access.
"""
use ExUnit.CaseTemplate
alias Ecto.Adapters.SQL.Sandbox
alias Microwaveprop.Propagation.ScoreCache
alias Microwaveprop.Weather.GridCache
using do
quote do
import Ecto
import Ecto.Changeset
import Ecto.Query
import Microwaveprop.DataCase
alias Microwaveprop.Repo
end
end
setup tags do
_owner = Microwaveprop.DataCase.setup_sandbox(tags)
Microwaveprop.DataCase.reset_test_state()
Req.Test.set_req_test_from_context(tags)
:ok
end
@doc """
Reset state that persists across tests within a single ExUnit run.
The sandbox isolates the database, but ETS-backed caches
(GridCache, ScoreCache) and on-disk score files survive across
test boundaries because the cache GenServers and filesystem live
in the app's BEAM process tree, not per-test. A single reset
here avoids per-file setup duplication.
"""
@spec reset_test_state :: :ok
def reset_test_state do
dir = Application.get_env(:microwaveprop, :propagation_scores_dir)
if is_binary(dir), do: File.rm_rf(dir)
GridCache.clear()
ScoreCache.clear()
Req.Test.stub(Microwaveprop.Weather.NexradClient, fn conn ->
Plug.Conn.send_resp(conn, 404, "not found")
end)
Req.Test.stub(Microwaveprop.Weather.HrrrClient, fn conn ->
Plug.Conn.send_resp(conn, 404, "not found")
end)
Req.Test.stub(Microwaveprop.Weather.NarrClient, fn conn ->
Plug.Conn.send_resp(conn, 404, "not found")
end)
Req.Test.stub(Microwaveprop.Weather.HrdpsClient, fn conn ->
Plug.Conn.send_resp(conn, 404, "not found")
end)
:ok
end
@spec setup_sandbox(map()) :: pid()
def setup_sandbox(tags) do
shared? = not tags[:async]
pid = Sandbox.start_owner!(Microwaveprop.Repo, shared: shared?)
on_exit(fn -> Sandbox.stop_owner(pid) end)
pid
end
@spec errors_on(Ecto.Changeset.t()) :: map()
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end