Some checks failed
Build and Push / Build and Push Docker Image (push) Has been cancelled
Create shared ContactsFixtures module with globally-unique qso_timestamp seconds to prevent unique-constraint violations when async test modules run in parallel and insert contacts with identical dedup-key columns. The qso_timestamp column is timestamp(0), so microsecond offsets were truncated — use System.unique_integer monotonic seconds instead. Also make count-asserting tests resilient to sandbox-leaked contacts from prior tests by using >= assertions or status-based checks rather than exact counts. Includes automated DateTime.add → DateTime.shift migration from mix format.
71 lines
2.2 KiB
Elixir
71 lines
2.2 KiB
Elixir
defmodule Microwaveprop.Weather.NexradCacheTest do
|
|
use ExUnit.Case, async: false
|
|
|
|
alias Microwaveprop.Weather.NexradCache
|
|
|
|
setup do
|
|
NexradCache.clear()
|
|
:ok
|
|
end
|
|
|
|
describe "fetch/1" do
|
|
test "returns :miss when nothing is cached" do
|
|
assert NexradCache.fetch(~U[2026-04-12 12:00:00Z]) == :miss
|
|
end
|
|
|
|
test "returns cached pixels + width after put" do
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1, 2, 3, 4>>, 2)
|
|
assert {:ok, <<1, 2, 3, 4>>, 2} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
end
|
|
|
|
test "isolates entries by timestamp" do
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1>>, 1)
|
|
NexradCache.put(~U[2026-04-12 12:05:00Z], <<2>>, 1)
|
|
|
|
assert {:ok, <<1>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
assert {:ok, <<2>>, 1} = NexradCache.fetch(~U[2026-04-12 12:05:00Z])
|
|
end
|
|
end
|
|
|
|
describe "prune_older_than/1" do
|
|
test "removes entries with a timestamp strictly before the cutoff" do
|
|
NexradCache.put(~U[2026-04-12 11:50:00Z], <<1>>, 1)
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<2>>, 1)
|
|
|
|
NexradCache.prune_older_than(~U[2026-04-12 11:55:00Z])
|
|
|
|
assert NexradCache.fetch(~U[2026-04-12 11:50:00Z]) == :miss
|
|
assert {:ok, <<2>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
end
|
|
|
|
test "keeps entries equal to the cutoff" do
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1>>, 1)
|
|
NexradCache.prune_older_than(~U[2026-04-12 12:00:00Z])
|
|
assert {:ok, <<1>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
end
|
|
end
|
|
|
|
describe "size cap" do
|
|
test "evicts the oldest-by-timestamp entries once the cap is exceeded" do
|
|
# Insert 25 entries; max is 20, so 5 oldest get dropped.
|
|
base = ~U[2026-04-12 00:00:00Z]
|
|
|
|
for i <- 1..25 do
|
|
ts = DateTime.shift(base, minute: i * 5)
|
|
NexradCache.put(ts, <<i>>, 1)
|
|
end
|
|
|
|
# The 5 oldest (i = 1..5) should have been evicted.
|
|
for i <- 1..5 do
|
|
ts = DateTime.shift(base, minute: i * 5)
|
|
assert NexradCache.fetch(ts) == :miss
|
|
end
|
|
|
|
# The most recent 20 should still be present.
|
|
for i <- 6..25 do
|
|
ts = DateTime.shift(base, minute: i * 5)
|
|
assert {:ok, <<^i>>, 1} = NexradCache.fetch(ts)
|
|
end
|
|
end
|
|
end
|
|
end
|